[R-sig-Geo] subregion of a SpatialGridDataFrame
Paul Hiemstra
p.hiemstra at geo.uu.nl
Wed Feb 6 14:11:39 CET 2008
Hi,
Loading the grid and telling R that it is a grid can also be done by:
library(gstat)
data(meuse.grid)
gridded(meuse.grid) =~x+y # Now it is a SPixelDF
# The former is shorthand for:
# coordinates(meuse.grid) =~x+y
# gridded(meuse.grid) = TRUE
fullgrid(meuse.grid) = TRUE # Make it a SGridDF
cheers,
Paul
Edzer Pebesma wrote:
> 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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone: +31302535773
Fax: +31302531145
http://intamap.geo.uu.nl/~paul
More information about the R-sig-Geo
mailing list