rautomap

Projection error with autoKrige function


I have a problem with autoKrig function and I try to make a reproducible example here:

library(automap)
library(raster)
library(dismo)

bio <- getData("worldclim", var="bio", res=10)

bio1 <- raster(bio, layer=1)
bio12 <- raster(bio, layer=12)
predictors <- stack(bio1, bio12)

bg <- randomPoints(bio1, 50)

data <- extract(predictors, bg)
data <- cbind(bg,data)
data <- data.frame(data)

coordinates(data)=~x+y
proj4string(data) = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
kg <- autoKrige(bio1~bio12, data, new_data=predictors)

This will result in:

Error in autoKrige(bio1 ~ bio12, data, new_data = predictors) : 
  Either input_data or new_data is in LongLat, please reproject.
   input_data:  +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
   new_data:    +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

I get the same error with my original data. I appreciate any help.


Solution

  • If you read the help file, it will tell you why it is throwing that error.

    autoKrige performs some checks on the coordinate systems of input_data and new_data. If one or both is NA, it is assigned the projection of the other. If they have different projections, an error is raised. If one or both has a non-projected system (i.e. latitude-longitude), an error is raised. This error is raised because 'gstat does use spherical distances when data are in geographical coordinates, however the usual variogram models are typically not non-negative definite on the sphere, and no appropriate models are available' (Edzer Pebesma on r-sig-geo).

    It looks like you need to project your data before calling autoKrige.