[R-sig-Geo] subregion of a SpatialGridDataFrame

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed Feb 6 09:42:46 CET 2008


Ingo Holz wrote:
> Hi,
>
>  this sounds like a stupid question, however:
>
>  I have a SpatialGridDataFrame (SGDF) and want to cut a rectangular 
> subregion from this SGDF to a new sub-region-SGDF.
>
>  Is it possible to define a new bounding-box with two points and cut the 
> subregion out of the SGDF?
>
> Ingo
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>   
 > data(meuse.grid)
 > coordinates(meuse.grid)=~x+y
 > meuse.grid=as(meuse.grid, "SpatialGridDataFrame")
Error in as(meuse.grid, "SpatialGridDataFrame") :
  no method or default for coercing "SpatialPointsDataFrame" to 
"SpatialGridDataFrame"
# this surprised me, but the following does work:
 > meuse.grid=as(meuse.grid, "SpatialPixelsDataFrame")
 > meuse.grid=as(meuse.grid, "SpatialGridDataFrame")
 > image(meuse.grid[40:60,20:50,1])

# meaning the first index selects rows (top to bottom), and the second 
columns; the third attribute

 > bbox(meuse.grid[40:60,20:50,1])
     min    max
x 179200 180440
y 331360 332200
 > x1 = bbox(meuse.grid[40:60,20:50,1])[1,1]
 > x2 = bbox(meuse.grid[40:60,20:50,1])[1,2]
 > y1 = bbox(meuse.grid[40:60,20:50,1])[2,1]
 > y2 = bbox(meuse.grid[40:60,20:50,1])[2,2]
 > p = Polygon(cbind(c(x1,x2,x2,x1,x1),c(y1,y1,y2,y2,y1)))  
 > sps = SpatialPolygons(list(Polygons(list(p),"bbox")))
 > meuse.grid=as(meuse.grid,"SpatialPixelsDataFrame")
 > image(meuse.grid[!is.na(overlay(meuse.grid,sps)),1])

# this second form needs the grid represented as pixels, as the overlay 
considers each grid cell, and retuns a vector the length of the number 
of pixels. The image should be identical to the former.
--
Edzer




More information about the R-sig-Geo mailing list