baddstats / polyclip

R package polyclip: a port of the Clipper library for polygon geometry
19 stars 6 forks source link

Expose PointInPolygon and other helpers #5

Open thomasp85 opened 6 years ago

thomasp85 commented 6 years ago

While my own interest is mainly in PointInPolygon, there are other helpers which would be nice to have access to from R, such as Area, Poly2ContainsPoly1, and IntersectPoint

baddstats commented 6 years ago

This has been put on the to-do list..

thomasp85 commented 6 years ago

Perfect - thanks.

Do you have any idea of a time-frame (not being pushy - just out of interest)

baddstats commented 6 years ago

PointInPolygon is now exposed (through new function pointinpolygon)

baddstats commented 6 years ago

Uh-oh,..., Poly2ContainsPoly1 seems to be incorrect. The code tests whether every vertex of Poly1 lies inside Poly2 but this doesn't deal with cases where an edge of Poly1 crosses outside Poly2. I will investigate whether this has been fixed in later versions of Clipper, and update.

mpadge commented 5 years ago

To avoid opening a new issue, may I request another extension of PointInPolygon, via vectorization of polygons to return a matrix of [npoints, npolygons] (or the other way around)?

I also concur with @thomasp85 that it would be helpful to expose other clipper fns, with my choice being to use area in an extension of the main ClipperLib::Clipper::Execute routine to calculate overlap areas? I suspect a great many people using the main polyclip routine would do so because they are interested in areas of overlap, and since the Clipper::Area function directly accepts the Paths object from the solution to the clip, it would be much more efficient if that could be extended within the C++ code, like this:

# set up layer1, layer2
areas <- polyclip_areas (layer1, layer2)
head (areas)
# area1   area2
# 1 456   123
# 2 ...

where area1 is just the area of the "subject polygon" (in AJ's terminology); area2 comes straight from the clipper solution in each instance; and the units can be directly re-scaled with your eps value, because clipper::Area returns a <double>.