[GRASS-stats] Overlapping polygons in SpatialPolygonsDataFrame
Rainer M Krug
r.m.krug at gmail.com
Wed Aug 6 05:24:38 EDT 2008
Hi
I have a SpatialPolygonsDataFrame with polygons which are overlapping
(spdf in the example below), which is an imported shape file.
I would like to get the maximum of ALL the polygons at given
locations, defined as a SpatialPixelsDataFrame (grd in the example
below).
How can I achieve this (or apply a function to the values at a given
location, e.g. calculate the mean or minimum)?
I know about
overlay(spdf, grd, fn=)
but as far as I understand it, the function is applied to the values
of different attribute columns, and I would need a function which is
applied to the values of the same attribute column but from different
polygons (at locations where the polygons are overlapping)
Below an example which illustrate my problem.
Thanks a lot,
Rainer
library(sp)
p1 <- cbind( c(90, 50, 50, 90, 90), c(10, 50, 70, 70, 10) )
p2 <- cbind( c(10, 10, 70, 10), c(90, 10, 70, 90) )
sp1 <- Polygons(list(Polygon(p1)),"p1")
sp2 <- Polygons(list(Polygon(p2)),"p2")
sp <- SpatialPolygons( list(sp1,sp2) )
spdf <- SpatialPolygonsDataFrame(sp, data.frame(f=c(10, 20),
row.names=c("p1","p2")))
grd <- expand.grid(xc = 1:100, yc = 1:100)
grd <- data.frame(grd, z1 = 1:100, z2 = 100:1)
coordinates(grd) = ~xc + yc
gridded(grd) <- TRUE
image(grd)
plot(spdf, add=TRUE, col=spdf[[1]])
--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)
Centre of Excellence for Invasion Biology
Faculty of Science
Natural Sciences Building
Private Bag X1
University of Stellenbosch
Matieland 7602
South Africa
More information about the grass-stats
mailing list