[R-sig-Geo] overlay SpatialPolygons and SpatialGridDataFrame, overlay

Edzer Pebesma e.pebesma at gmail.com
Thu Dec 24 16:59:39 CET 2009

Dear Patrick,

Patrick Giraudoux wrote:
> I am trying to clip a SpatialGridDataframe using a 
> SpatialPolygonsObject (actually a simple rectangle, e.g. a region), 
> the ultimate aim being to extract the corresponding area to a new 
> SpatialGridDataFrame corresponding to the region. I was supposing that 
> I can start doing it using the overlay function. Here we are:
> region<-matrix(c(73.5,18.1,73.5,54,135,54,135,18.1,73.5,18.1),ncol=2,byrow=T) 
> region<-SpatialPolygons(list(Polygons(list(Polygon(region)),ID=1)))
> idx<-overlay(WorldTemp,region)
> WorldTemp[!is.na(idx),]
> I get:
> Error in WorldTemp[!is.na(idx), ] :
>  (subscript) logical subscript too long
> The SpatialGridDataframe has  259200 pixels. Am I too greedy and is 
> there really a limitation in length?
do first a

fullgrid(WorldTemp) = FALSE

and then do the overlay. If you want to select individual pixels, this 
needs be done on SpatialPixelsDataFrame objects; if you select the way 
you did it, on a SpatialGridDataFrame object, it will select grid rows, 
as in WorldTemp[rowNumbers,colNumbers] -- no wonder that the pixel 
indexes (cell index numbers) vector was too long.

 From a design perspective, this might sound complex, but both selection 
mechanisms are convenient at times, and they match the way the data are 
stored. If your selection is as simple as you sketch above, you might 
prefer to select straight away on grid rows and columns of a 
SpatialGridDataFrame object.
> A second step would be to compute the upper left coordinates, etc... 
> to get the new SpatialGridDataFrame But this is another story...
R does this for you automatically, by


but note that the bbox values for *Grid* and *Pixel* objects are grid 
and cell corners, not cell centers.
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster 
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251 
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de 
http://www.52north.org/geostatistics      e.pebesma at wwu.de

More information about the R-sig-Geo mailing list