@@ -67,6 +67,28 @@ setMethod("idname", "SoilProfileCollection",
67
67
return (object @ idcol )
68
68
)
69
69
70
+ # # horizon ID name
71
+ if (! isGeneric(" hzidname" ))
72
+ setGeneric ("hzidname ", function(object, ...) standardGeneric("hzidname"))
73
+
74
+ setMethod ("hzidname ", "SoilProfileCollection",
75
+ function (object )
76
+ return (object @ hzidcol )
77
+ )
78
+
79
+ # # get horizon IDs
80
+ if (! isGeneric(" hzID" ))
81
+ setGeneric ("hzID ", function(object, ...) standardGeneric("hzID"))
82
+
83
+ setMethod ("hzID ", "SoilProfileCollection",
84
+ function (object ) {
85
+ h <- horizons(object )
86
+ res <- h [[hzidname(object )]]
87
+ return (res )
88
+ }
89
+
90
+ )
91
+
70
92
71
93
# # distinct profile IDs
72
94
if (! isGeneric(" profile_id" ))
@@ -189,103 +211,6 @@ setMethod("horizonNames", "SoilProfileCollection",
189
211
# # overloads
190
212
# #
191
213
192
- # ## This will be greatly improved with new class structure
193
- # # concatentation
194
- # # # https://github.com/ncss-tech/aqp/issues/71
195
- # # TODO: concatenation of data with duplicated IDs in @site, but unique data in other @site fields, will result in corrupt SPC
196
- # # TODO: duplicates in @sp will cause errors
197
- # # TODO: duplicates are removed in all other slots... does this make sense?
198
- rbind.SoilProfileCollection <- function (... ) {
199
- # setup some defaults
200
- options(stringsAsFactors = FALSE )
201
-
202
- # parse dots
203
- objects <- list (... )
204
- names(objects ) <- NULL
205
-
206
- # short-circuits
207
- if (length(objects ) == 0 )
208
- return (NULL )
209
- if (length(objects ) == 1 )
210
- return (objects [1 ])
211
-
212
- # # TODO: normalize idname and horizonDepths
213
- # profile_id() <-
214
- # horizonDepths() <-
215
-
216
-
217
- # combine pieces
218
- # should have length of 1
219
- o.idname <- unique(lapply(objects , idname ))
220
- o.depth.units <- unique(lapply(objects , depth_units ))
221
- o.hz.depths <- unique(lapply(objects , horizonDepths ))
222
- o.m <- unique(lapply(objects , aqp :: metadata ))
223
- o.coords <- unique(lapply(objects , function (i ) ncol(coordinates(i ))))
224
- o.p4s <- unique(lapply(objects , proj4string ))
225
-
226
- # should have length > 1
227
- o.h <- lapply(objects , horizons )
228
- o.s <- lapply(objects , site )
229
- o.d <- lapply(objects , diagnostic_hz )
230
- o.sp <- lapply(objects , slot , ' sp' )
231
-
232
- # sanity checks:
233
- if (length(o.idname ) > 1 )
234
- stop(' inconsistent ID names' , call. = FALSE )
235
- if (length(o.depth.units ) > 1 )
236
- stop(' inconsistent depth units' , call. = FALSE )
237
- if (length(o.hz.depths ) > 1 )
238
- stop(' inconsistent depth columns' , call. = FALSE )
239
- if (length(o.m ) > 1 )
240
- stop(' inconsistent metadata' , call. = FALSE )
241
-
242
- # spatial data may be missing...
243
- if (length(o.coords ) > 1 )
244
- stop(' inconsistent spatial data' , call. = FALSE )
245
- if (length(o.p4s ) > 1 )
246
- stop(' inconsistent CRS' , call. = FALSE )
247
-
248
- # generate new SPC components
249
- # using plyr::rbind.fill seems to solve the problem on non-conformal DF
250
- # is it safe?
251
- # https://github.com/ncss-tech/aqp/issues/71
252
- o.h <- unique(do.call(' rbind.fill' , o.h )) # horizon data
253
- o.s <- unique(do.call(' rbind.fill' , o.s )) # site data
254
- o.d <- unique(do.call(' rbind.fill' , o.d )) # diagnostic data, leave as-is
255
-
256
- # # 2015-12-18: removed re-ordering, was creating corrupt SPC objects
257
- # # site and horizon data
258
-
259
- # spatial points require some more effort when spatial data are missing
260
- o.1.sp <- objects [[1 ]]@ sp
261
- if (ncol(coordinates(o.1.sp )) == 1 ) # missing spatial data
262
- o.sp <- o.1.sp # copy the first filler
263
-
264
- # # 2015-12-18: added call to specific function: "sp::rbind.SpatialPoints"
265
- # not missing spatial data
266
- else
267
- o.sp <- do.call(" rbind.SpatialPoints" , o.sp ) # rbind properly
268
-
269
- # make SPC and return
270
- res <- SoilProfileCollection(idcol = o.idname [[1 ]], depthcols = o.hz.depths [[1 ]], metadata = o.m [[1 ]], horizons = o.h , site = o.s , sp = o.sp , diagnostic = o.d )
271
-
272
- # # one more final check:
273
- # print(profile_id(res))
274
- # print( site(res)[[idname(res)]])
275
- # print(site(res))
276
-
277
- if (length(profile_id(res )) != length(site(res )[[idname(res )]]))
278
- stop(" SPC object corruption. This shouldn't happen and will be fixed in aqp 2.0" , call. = FALSE )
279
- if (! all.equal(profile_id(res ), site(res )[[idname(res )]]))
280
- stop(" SPC object corruption. This shouldn't happen and will be fixed in aqp 2.0" , call. = FALSE )
281
-
282
- return (res )
283
- }
284
-
285
-
286
- # # TODO: this doesn't work as expected ... fix in 2.0
287
- # # overload rbind
288
- # setMethod("rbind", "SoilProfileCollection", .rbind.SoilProfileCollection)
289
214
290
215
291
216
0 commit comments