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

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon Jan 15 19:06:16 CET 2007


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.

Barry




More information about the R-sig-Geo mailing list