[R-sig-Geo] Determining in which polygon points lie

Roger Bivand Roger.Bivand at nhh.no
Fri Jun 14 18:06:51 CEST 2013


On Fri, 14 Jun 2013, Dieter Vanderelst wrote:

> Dear List,
>
> I'm reading in a shapefile of a country with different subregions in the 
> shapefile (the shapefile was downloaded from the GADM database of Global 
> Administrative Areas). I also have a number of points. Therefore, my code 
> looks like this (using the maptools package):
>
> country<-readShapePoly(file)
> points<-readShapePoints(file)
>
> Now, I want to know in which of the N regions of the country each of the 
> spatial points lies? Is there an easy way to do this? I know there is a 
> function points.in.poly in sp. But this functions seems incompatible with the 
> objects created by the maptools functions. Also, this function requires 
> looping over the regions in a set.

This is a FAQ. See:

library(sp)
vignette("over")
?over

Example:

library(maptools)
nc1 <- readShapePoly(system.file("shapes/sids.shp",
  package="maptools")[1],
  proj4string=CRS("+proj=longlat +ellps=clrk66"))
dim(nc1)
set.seed(1)
pts <- spsample(nc1, n=200, type="random")
str(pts %over% as(nc1, "SpatialPolygons"))

Hope this clarifies

Roger

>
> Regards,
> Dieter
>
> ---
> Behavioural, Acoustic and Sensory Ecology Lab
> University Bristol, UK
>
> http://bitsofbats.weebly.com/
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
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