[R] maptools: Test if point is in polygon

Roger Bivand Roger.Bivand at nhh.no
Fri Feb 6 14:54:56 CET 2009


Renaud Lancelot <renaud.lancelot <at> gmail.com> writes:

> 
> See function pip (point in polygon) in package splancs.
> 
> Renaud
> 
> 2009/2/5 Rainer M Krug <r.m.krug <at> gmail.com>
> 
> > On Thu, Feb 5, 2009 at 3:21 PM, Aleksandr Andreev
> > <aleksandr.andreev <at> gmail.com> wrote:
> > > In R's maptools package, is there a built-in function to test if a
> > > given point is "inside" a given polygon on the map?  The map was
> > > loaded from an ESRI Shapefile. The point's latitude and longitude are
> > > known.

The initial question was not put clearly. The pip function in splancs will 
work for a two-column matrix of points, but not for the object in question, 
if it was read in from a shapefile using readShapeSpatial(). In that case, 
the object is a SpatialPolygonsDataFrame as defined in the sp package, and 
the overlay() methods in sp should be the place to look - see 
?"overlay-methods". Typically, one would define the query point(s) as 
a SpatialPoints object, and overlay it on the input object. The output is 
a vector of indices, showing which Polygons object the points lie in, or NA 
if they do not.

Roger Bivand

PS. These kinds of questions typically get more focussed response on the 
R-sig-geo list; further, RSiteSearch() with 'point polygon' does show many 
useful avenues.

> >
> > If everything else fails, convert the shape file into a raster and
> > "drill down" at the location of the points.
> >
> > Rainer
> >




More information about the R-help mailing list