max-mapper / geojson-js-utils

JavaScript helper functions for manipulating GeoJSON
BSD 2-Clause "Simplified" License
402 stars 58 forks source link

pointInPolygon does not work if polygon contains pole #28

Open pwiecz opened 8 years ago

pwiecz commented 8 years ago

I don't know if the project is developed any more, but anyway: If polygon contains pole pointInPolygon does not work. 1) founding bounding box does not work either 2) even if we ignore bounding box check it still does not work (always returns false)

I think that polygons crossing 180th meridian may also not work.

zeke commented 8 years ago

This is a great library, but @maxogden is a busy :cat2: so you might not see a fix here any time soon.

I would take a look at http://turfjs.org/static/docs/module-turf_inside.html -- turf is an awesome library, and is actively maintained my @mapbox folk. Here's a sample of turf.inside in use: https://github.com/zeke/hoods/blob/577719e273fef69685fdd8e056d80926aa50dc4e/index.js#L16-L18

pwiecz commented 8 years ago

Thanks, but unfortunately the turf.inside also does not seem to work for polygons crossing 180th meridian (and I'm pretty sure that it does not handle polygons containing pole as well). E.g. in the sandbox you've entered you can try using polygon [[179, -1],[-179, -1],[-179, 1],[179, 1],[179, -1]], and for inclusion of points [179.5, 0.5], [-179.5, -0.5] I think they both should be considered to be inside the polygon. Looking at pretty simple implementation (https://github.com/Turfjs/turf-inside/blob/master/index.js#L94 ) you can see that it only works if spherical polygons are very closely approximated by polygons in cartesian projection. I'm not very eager to implement my own library for that, but probably I'll have to. I'll submit an issue to turf-inside as well.