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

Paul Hiemstra p.hiemstra at geo.uu.nl
Wed Oct 14 18:01:19 CEST 2009


...and in addition:

 > sessionInfo()
R version 2.9.2 (2009-08-24)
i486-pc-linux-gnu

locale:
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] sp_0.9-44

loaded via a namespace (and not attached):
[1] grid_2.9.2      lattice_0.17-20 tools_2.9.2


Paul Hiemstra wrote:
> 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