@@ -143,7 +143,7 @@ cv_spatial_autocor <- function(
143143 }
144144 }
145145
146- # to be used for filtering blocks
146+ # to be used for filtering blocks only
147147 samp_point <- terra :: spatSample(
148148 r [[1 ]],
149149 size = 5e4 ,
@@ -178,10 +178,10 @@ cv_spatial_autocor <- function(
178178
179179 # make a dataframe from variograms data
180180 vario_data <- data.frame (layers = seq_len(nlayer ), range = 1 , sill = 1 )
181- for (v in seq_along(vario_list )){
181+ for (v in seq_along(vario_list )){
182182 vario_data $ layers [v ] <- if (missing(x )) names(r )[v ] else column [v ]
183- vario_data $ range [v ] <- vario_list [[v ]]$ var_model [2 ,3 ]
184- vario_data $ sill [v ] <- vario_list [[v ]]$ var_model [2 ,2 ]
183+ vario_data $ range [v ] <- vario_list [[v ]]$ var_model [2 , 3 ]
184+ vario_data $ sill [v ] <- vario_list [[v ]]$ var_model [2 , 2 ]
185185 }
186186
187187 # order them for plotting
@@ -191,7 +191,7 @@ cv_spatial_autocor <- function(
191191
192192 size <- the_range <- stats :: median(vario_data $ range )
193193
194- if (sf :: st_is_longlat(x_obj )){
194+ if (sf :: st_is_longlat(x_obj )){
195195 vario_data $ range <- vario_data $ range * 1000
196196 the_range <- the_range * 1000
197197 size <- the_range / deg_to_metre
@@ -268,6 +268,67 @@ summary.cv_spatial_autocor <- function(object, ...){
268268}
269269
270270
271+ # auto-fit variogram models
272+ .fit_variogram <- function (
273+ i ,
274+ xx = NULL ,
275+ rr = NULL ,
276+ column = NULL ,
277+ num_sample = 1e4 ,
278+ progress = FALSE ,
279+ pb = NULL
280+ ){
281+ if (is.null(xx )){
282+ points <- sf :: st_as_sf(
283+ terra :: spatSample(
284+ x = stats :: setNames(rr [[i ]], " target" ),
285+ size = num_sample ,
286+ method = " random" ,
287+ as.points = TRUE ,
288+ na.rm = TRUE
289+ )
290+ )
291+ } else {
292+ points <- xx [column [i ]] # [i] in case there are more column
293+ names(points ) <- c(" target" , " geometry" )
294+ }
295+ # NOTE: apparently the gstat package returns different units for range with sp and sf objects!
296+ # So, need to keep this a SpatialPointDataFrame for now and have to fake using it!!!
297+ if (FALSE ) {
298+ sp :: SpatialPoints
299+ }
300+ fit_vario <- automap :: autofitVariogram(
301+ formula = target ~ 1 ,
302+ input_data = .as_sp(points )
303+ )
304+ if (progress ) utils :: setTxtProgressBar(pb , i )
305+
306+ return (fit_vario )
307+ }
308+
309+
310+ # Annoying step to import sp so there'll be no CRAN errors
311+ # Sp is only require because automap produces different output with latlong sf objects
312+ # Don't use sf::as_Spatial because this somehow depends on sp and requires sp dependency anyway
313+ .as_sp <- function (x ) {
314+ out <- if (sf :: st_is_longlat(x )) {
315+ coords <- sf :: st_coordinates(x )
316+ attrs <- sf :: st_drop_geometry(x )
317+ crs_info <- sf :: st_crs(x )$ proj4string
318+
319+ sp :: SpatialPointsDataFrame(
320+ coords = coords ,
321+ data = attrs ,
322+ proj4string = sp :: CRS(crs_info )
323+ )
324+ } else {
325+ x
326+ }
327+
328+ return (out )
329+ }
330+
331+
271332
272333# make a bar plot for cv_spatial_autocor
273334.make_bar_plot <- function (vario_data , the_range , ptnum ){
@@ -306,34 +367,3 @@ summary.cv_spatial_autocor <- function(object, ...){
306367 return (p )
307368}
308369
309-
310- # auto-fit variogram models
311- .fit_variogram <- function (
312- i ,
313- xx = NULL ,
314- rr = NULL ,
315- column = NULL ,
316- num_sample = 1e4 ,
317- progress = FALSE ,
318- pb = NULL
319- ){
320- if (is.null(xx )){
321- points <- terra :: spatSample(
322- x = rr [[i ]],
323- size = num_sample ,
324- method = " random" ,
325- as.points = TRUE ,
326- na.rm = TRUE
327- )
328- points <- sf :: as_Spatial(sf :: st_as_sf(points ))
329- names(points ) <- " target"
330- } else {
331- points <- xx [column [i ]] # [i] in case there are more column
332- points <- sf :: as_Spatial(points )
333- names(points ) <- " target"
334- }
335- fit_vario <- automap :: autofitVariogram(target ~ 1 , points )
336- if (progress ) utils :: setTxtProgressBar(pb , i )
337-
338- return (fit_vario )
339- }
0 commit comments