[R-sig-Geo] points-in-polygons calculation
Roger Bivand
Roger.Bivand at nhh.no
Tue Feb 12 18:57:54 CET 2008
On Tue, 12 Feb 2008, Anne Ghisla wrote:
> Hi all,
>
> during a spatial analysis I need to compute a point-in-polygon
> calculation, and I chose findPolys() in PBSmapping package.
>
> For the moment the script is built as follows:
> - import the polygon shapefile with rgdal
> - import the point shapefile with PBSmapping
> - nested for cycles that
> - isolate each polygon (or region) through a superkey on the
> attributes in data slot. The selected element is a homerange. It's then
> converted in a PolySet, for next step
> - finds which points fall inside the homerange with findPolys()
> - picks up a defined value among the attributes of points and computes
> mean of all values of the selected points, then writes it in a new
> column of the attribute table
Well, if you showed the code used for doing this, it might be easier to
advise. Are you using the maptools function SpatialPolygons2PolySet()? You
can then use the PID and SID values as you would otherwise in PBSmapping.
But it does strike me that it might be simpler just to read the points
with rgdal, and use the overlay() method in sp to find which points belong
to which polygons, all in one go. Using the output membership vector, you
could simply run by() or aggregate on as(pts, "data.frame") to get your
summaries per home range. The only reason for caution might be if the home
range polygons overlap, in which case judicious use of lapply() on the
"polygons" slot of the SpatialPolygons* might be needed, because some
points might fall into multiple polygons.
The IDs of the Polygons in the SpatialPolygons* object SP are in:
sapply(slot(SP, "polygons"), function(x) slot(x, "ID"))
Hope this helps,
Roger
>
> Can the for cycle be based on a unique key like PolygonsID? Among the
> methods for sp classes I didn't find it. I found functions like
> getPolygonsIDSlot but they have not to be used directly.
>
> Any suggestions?
>
> thank you in advance
>
> Anne Ghisla
> Università degli Studi dell'Insubria, Varese
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list