[R-sig-Geo] Finding out in which polygon a point lies

Rolf Turner r.turner at auckland.ac.nz
Fri Dec 23 05:32:25 CET 2011


The spatstat package provides tools  which will easily allow you
to check which polygons contain points.

Turn your polygons into "owin" objects, make a *list* (say "L")
of these objects, and then

     sapply(L,function(W,x,y){any(inside.owin(x,y,W))},x=x,y=y)

will give TRUE for polygons containing points and FALSE otherwise.

     cheers,

         Rolf Turner

On 21/12/11 04:43, Dieter Vanderelst wrote:
> Dear List,
>
> I've read in a shapefile using the readShapePoly {maptools} function.
> This shapefile represents Angola and has different polygons for the
> different regions of the country. I also have a set of coordinates
> (lat/long). Now want to do two things:
>
> 1 - I want to plot the points on the map. Given that both my points
> and the shapefile use the same coordinate reference system
> (latitude/longitude) and theWGS84 datum, I think I can just use the
> function plot(lon,lat). Please correct me I if I'm wrong here.
>
> 2 - The second problem is a bit harder. I want to color the polygons
> which contain one or more points. How can I check in which polygon
> each of the points lie. I found a partial explanation online on how to
> do this with the sp package (see
> https://stat.ethz.ch/pipermail/r-help/2009-February/187435.html).
> However, I haven't been able to reconstruct the method suggested in
> this post.
>
> Any thoughts on how to select polygons based on coordinates using
> maptools would be most welcome.



More information about the R-sig-Geo mailing list