[R-sig-Geo] Crop / clip a SpatialPixels* object

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sat Jun 22 16:28:37 CEST 2013


The way you specified the SpatialPixelsDataFrame preserves x and y as
attributes, which is good for this purpose; when e.g. built with

gridded(meuse.grid) = ~x+y

they are removed as attributes, and meuse.grid$x no longer gives the x
coordinate (we might correct this at some stage).

As an alternative to subset you can use subsetting by [, as in:

plot(m[m$x > 180000 & m$y > 332000,])

Alternatively, if you have some spatial object x denoting a clipping
region, e.g. created by

x = as(m[m$x > 180000 & m$y > 332000,], "SpatialPolygons")

you can use

m[x,]

to do the clipping. This latter statement is short for

m[!is.na(over(x,geometry(m))),]

On 06/22/2013 04:06 PM, Lionel Hertzog wrote:
> In this example it is pretty straightforward using the subset function:
> 
> m1<-subset(m,y>331000 & x>180000)
> bbox(m1)
> 
> See this link for another way to do this using rgeos:
> http://stackoverflow.com/questions/13982773/crop-for-spatialpolygonsdataframe
> 
> 
> Cheers,
> Lionel
> 
> 
> On 22/06/2013 15:24, Mark Payne wrote:
>> Hi,
>>
>> This should be simple, but I simply can't find an "elegant" solution.
>>
>> I have a SpatialPixelsDataFrame that I would like to clip (crop) to a
>> smaller object. For example,
>>
>>> data(meuse.grid)
>>> m = SpatialPixelsDataFrame(points = meuse.grid[c("x", "y")], data =
>>> meuse.grid)
>>>
>>> bbox(m)
>>       min    max
>> x 178440 181560
>> y 329600 333760
>> I would like to constrain m, to all y > 331000 and x >180000. What is
>> the most efficient way to do this (and have the other functionality
>> e.g bbox and plotting) reflect this reduced data set.
>>
>> Mark
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> 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
83 33081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de



More information about the R-sig-Geo mailing list