[R-sig-Geo] Finding polygons corresponding to points

Karl Ove Hufthammer karl at huftis.org
Mon Nov 9 12:48:41 CET 2009


Dear list members

I have a collection of polygons (a SpatialPolygonsDataFrame object, but 
I can easily convert it to different formats), and a collection of 
coordinate points. Each point corresponds to (at most) one polygon.

For each point, I need to find the corresponding polygon. You can think 
of the points as positions of for example earthquakes, and the polygons 
as state or country borders. For each earthquake, I need to calculate 
the state/country where the earthquake occured.

There are a number of functions to check if a point is inside a given 
polygon, e.g., point.in.polygon, and I *could* write a double loop that 
for each point checks each polygon. But as I have very many points (tens 
of thousands) and hundreds of polygons, this may be quite slow.

Is there an easier and more efficient way of / existing function for 
doing this? As each point is only inside (at most) one polygon, and the 
number of points inside each polygon varies a great deal (for example, 
most earthquakes occur in California), there clearly is much room for 
optimisation.

-- 
Karl Ove Hufthammer



More information about the R-sig-Geo mailing list