[R-sig-Geo] loop through polygons for point in polygon function

Tom Gottfried tom.gottfried at wzw.tum.de
Thu Mar 17 13:38:12 CET 2011


Hi David,

Am 17.03.2011 00:32, schrieb david depew:
> Dear list, I'd like to loop through all polygons in a shapefile, and count
> the # of points of interest in each polygon.

in case these "points of interest" are something that can be represented as SpatialPoints, you could 
use over() or maybe aggregate() to produce counts of them corresponding to each polygon in your 
SpatialPolygonsDataFrame. This prevents you from looping through the slots of a complex object.

regards,
Tom

> I see previous postings suggest
> a for loop should work, but I'm not able to extract the individual polygons.
> I post the following portion since I can get to this point in my
> script....but the extraction step fails (most likely due to my inability to
> specify what part of the SpatialPolygonsDataFrame to extract.....
>
> library(sp)
> library(maptools)
> library(splancs)
> library(rgdal)
> library(spdep)
> nc.sids<- readShapePoly(system.file("etc/shapes/sids.shp",
> package="spdep")[1],
>    ID="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
> rn<- sapply(slot(nc.sids, "polygons"), function(x) slot(x, "ID"))
>
> for (i in 1:length(rn)) {
> temp.poly= ---- effectively, rn[i], i.e. a polygon with ID=="i". I can then
> assign data from the result of the pip function, but I'm having difficulty
> extracting each polygon sequentially to do this.
>
> Any help is much appreciated!
>
> Thanks!
>

-- 
Technische Universität München
Department für Pflanzenwissenschaften
Lehrstuhl für Grünlandlehre
Alte Akademie 12
85350 Freising / Germany
Phone: ++49 (0)8161 715324
Fax:   ++49 (0)8161 713243
email: tom.gottfried at wzw.tum.de
http://www.wzw.tum.de/gruenland



More information about the R-sig-Geo mailing list