[R-sig-Geo] Count of points inside multiple polygons

Roger Bivand Roger.Bivand at nhh.no
Mon Jan 15 19:25:58 CET 2007


On Mon, 15 Jan 2007, Barry Rowlingson wrote:

> Miha Staut wrote:
> > Dear all,
> > 
> > I was wandering if there is a way to count the number of points inside
> > multiple polygons within R.
> 
> > The required approaches what is implented in the splancs function pip.
> > Just for all the polygons in the same operation.
> This is what programming languages are for!
> 
>   Write a loop over your polygons, then call pip for each polygon and 
> all your points. Then pip tells you which points are in that polygon. 
> Repeat your loop, tracking which points you found in which polygon in a 
> matrix or vector. If you want to speed things up at the end and you know 
> that your polygons dont overlap, you can exclude the points found to be 
> in polygon N from tests with polygon >N.
> 
>   Quite what this loop looks like depends on how your polygons are 
> stored. If they are just a list of 2-column matrices, then you are 
> looping over a list. If they are areas in a shapefile, then you need to 
> extract the ring from the shapefile and do pip with that.
> 
>   There are probably better ways to do this with sp or spatstat anyway, 
> that work with the whole set of polygons at once.

"Better" is still loops, but you get to avoid doing them yourself - look 
at the overlay methods in the sp package, overlaying SpatialPolygons on 
SpatialPoints should return a vector with a Polygons number for each 
point.

?"overlay-methods" and ?overlay should help.

Roger

> 
> Barry
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
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