NA handling when some points are not in polygons
Opened this issue · 2 comments
I came across the same problem as mentionned in this post. Looking at the new version of the function, it appears that NA are deleted in statement z <- pts[!is.na(sp::over(pts, sp::geometry(polys))), ]
but aren't in the next statement z@data <- data.frame(sp::over(pts, polys))
. As a consequence data.frame(sp::over(pts, polys))
and z@data
don't have the same number of rows, triggering an error.
In my case NA values comes from the fact that some points do not fall in any of the polygons tested. This a not a bug since I'm testing a whole bunch of points to find which ones fall in the polygons.
I changed the two statement adding a NA removal line that seems to do the trick, but maybe you did that for a reason ?
z <- x[!is.na(sp::over(x, sp::geometry(y))), ]
zover <- data.frame(sp::over(x, y))
z@data <- data.frame(zover[!is.na(zover[[poly.id]]), ])
The point.in.poly function has been completely rewritten to account for potential multiple overlapping geometries. I added an argument "duplicate" that controls the behavior of the function. When multiple geometries are encountered the default behavior is to duplicate features representing each intersection. That is to say, if three polygons are intersected then there will be three associated point features with associated attributes. However, if duplicate=FALSE then new columns, containing the polygon IDs, are created for each intersection. Hopefully these changes will meet your needs. If not, please let me know so I can address any issues that may arise.
My problem wasn't coming from overlapping geometries but from the fact that one over
command was returning only the points that were matched with polygons, and the second over
command was returning the entire points dataframe. As a consequence merging between the two triggered an error.
I'll try with the newest version and let you know if the error still occurs !