Skip to content
Prev 6637 / 29559 Next

Strange behavior of spsample when sampling from Sgrid

Dear list,

I spotted the following behavior when using overlay to sample from a 
SpatialPixels or SpatialGrid. The following illustrates my point:

  library(sp)
  data(meuse.grid)
  gridded(meuse.grid) = ~x+y
  meuse2 = spsample(meuse.grid,
                    type = "regular",
                    cellsize = c(40, 40),
                    offset = c(0.5,0.5))
  spplot(meuse.grid, "dist",
                sp.layout = list("sp.points", meuse2[1:20,]))

It seems that when using a grid as input for spsample it samples from 
left bottom to right top instead of from left top to right bottom. When 
using the following command to use overlay to extract data from 'meuse' 
and plotting the result illustrates my problem:

  meuse.grid$dist_from_meuse2 = meuse.grid$dist[overlay(meuse.grid, meuse2)]
  spplot(meuse.grid, c("dist", "dist_from_meuse2"))

Transforming meuse2 to SpatialPixels, to SpatialGrid and back to 
SpatialPixels restores the order in meuse2 to 'top-left to bottom right'.

  gridded(meuse2) = TRUE
  fullgrid(meuse2) = TRUE
  fullgrid(meuse2) = FALSE

  val = meuse.grid$dist[overlay(meuse.grid, meuse2)]
  meuse.grid$dist_from_meuse2_pt2 = val[!is.na(val)]
  spplot(meuse.grid, c("dist", "dist_from_meuse2",
                                "dist_from_meuse2_pt2"))

But this is ofcourse not ideal. Is there a way to force spsample to 
always sample from top left to bottom right?

cheers,
Paul