[R-sig-Geo] Spatial overlay vs. extract and aggregation methods

Edzer Pebesma edzer.pebesma at uni-muenster.de
Tue Apr 28 14:47:58 CEST 2015



On 04/23/2015 07:04 PM, Juta Kawalerowicz wrote:
> Hi,
> 
> I have a spatial polygon data frame (spdf1) and try to aggregate
> information from it over polygons in a larger spatial polygon data
> frame(spdf2). I wanted to run some simple test to see that my code is sound
> (on nc.sids from maptools), but run into a problem which I don't
> understand. As far as I can see, there are 2 ways to do this:
> 
> 1) rasterise the smaller spdf1, extract and calculate over larger spdf2
> 
> library(maptools)
> library(raster)
> nc.sids <- readShapeSpatial(system.file("shapes/sids.shp",
> package="maptools")[1],
>                             IDvar="FIPSNO", proj4string=CRS("+proj=longlat
> +ellps=clrk66"))
> 
> #plot(nc.sids)
> r<-raster(ncol=180, nrow=180)
> extent(r)<-extent(nc.sids)
> rp<-rasterize(nc.sids, r, 'BIR74')
> plot(rp)
> plot(nc.sids, add=TRUE)
> 
> #this will take time on larger files...
> v <- extract(rp, nc.sids, weights=TRUE)
> res<-sapply(v, function(x) if (!is.null(x)) {sum(apply(x, 1, prod),
> na.rm=TRUE) / sum(x[,2])} else NA )
> cor(res, nc.sids$BIR74)
> #seems to be working ok.
> 
> 2) use over function from sp package
> 
> overlay<-over(nc.sids, nc.sids)
> #this should return the same indices of nc.sids which fall inside nc.sids -
> basically should be falling within itself for each polygon, right? But this
> is not what I get.

no, it uses gIntersects and picks the first intersecting polygon, which
might well be a neighbour. (Actually, over(nc.sids, geometry(nc.sids))
should return indexes and this call the corresponding attribute entries;
this is a bug, now fixed in rgeos on r-forge).

https://stat.ethz.ch/pipermail/r-sig-geo/2015-April/022636.html

has some pointers to the weighted aggregation problem of non-matching
polygons; sp 1.1-0 has the rgeos implementation mentioned
there, as option with sp::aggregate; see the last example of

example(aggregate).

> 
> Any hints would be greatly appreciated.
> 
> Cheers,
> Juta
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> 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,
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
Journal of Statistical Software:   http://www.jstatsoft.org/
Computers & Geosciences:   http://elsevier.com/locate/cageo/
Spatial Statistics Society http://www.spatialstatistics.info

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20150428/b6880801/attachment.bin>


More information about the R-sig-Geo mailing list