[R-sig-Geo] Strange behavior of spsample when sampling from Sgrid

Paul Hiemstra p.hiemstra at geo.uu.nl
Wed Oct 14 17:58:23 CEST 2009


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

-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul



More information about the R-sig-Geo mailing list