Question

I have two object :

pts=readOGR(dsn="overpass-turbo/shp/",layer="amenity")
poly=readOGR(dsn="overpass-turbo/shp/",layer="polygons")

Like

points in polygon

I would have, as a result, a spatial data frame with all points in the polgygon... I'have find something in this post but it doesn't work as I want!

a<-over(pts, poly)

but a in not a Sp data frame !!

I know !! I always have troubles with over fonction ...

Was it helpful?

Solution

I have find something more :-)

pts_in<-pts[!is.na(over(pts,poly)),]

enter image description here

It keep only points in the polygon (source : http://cran.r-project.org)

OTHER TIPS

Not entirely sure what you exactly want but I assume poly is your spatial data frame of choice. I think you have to do the following if you want to count the number of points in your polygon:

pts_in=over(SpatialPolygons(poly@polygons,SpatialPoints(pts),
                returnlist=TRUE)
poly$npoints<-unlist(lapply(pts_in,length)

If on the other hand you want to assign the corresponding polygon to each point, you do the following:

pts$nrow=over(SpatialPoints(pts),SpatialPolygons(poly@polygons),
          returnlist=TRUE)

You can also check this reference for some useful hints.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top