MLavoie
MLavoie

Reputation: 9836

overlay points over polygons in R

I need to overlay 40,000 points over 100,000 polygons. The polygons were created using:

polygon <- gBuffer(pc, width=500, byid=TRUE)

The overlay was created using:

test <- over(pts, polygon)

So as a result, I have a data frame with the attributes of the points falling in each polygon (mean). My question is is it possible to cbind() the gps coordinates (centroid) of the polygon to my data frame?

I have looked on google and stackoverflow but I did not find anything on that subject.The solution is probably easy but I just can't see it :)

Any help would be appreciated!

thank you Martin

Edit: So what if I want to select polygons that have (one or more) points in them. I used

test <- over(polygons, pts, returnList = TRUE)

How can I get the coordinates of the polygon bind to the list of data frame (each corresponding to a polygon). I need the info. as a list because I need to calculate distance driven. thanks M.

Upvotes: 3

Views: 2507

Answers (1)

Edzer Pebesma
Edzer Pebesma

Reputation: 4121

In your example, test is not a data.frame but a vector with the polygon index for each point in pts. Try

pts$polygon_id = over(pts, polygon)

this promotes pts from a SpatialPoints object to a SpatialPointsDataFrame.

Upvotes: 4

Related Questions