d3 / d3-geo

Geographic projections, spherical shapes and spherical trigonometry.

Home Page:https://d3js.org/d3-geo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

geoContains function doesn't seem to handle MultiPolygons

secondl1ght opened this issue · comments

I am using the geoContains function to determine if a point is inside a polygon. It works fine for simple polygons, but it does not seem to work for multi polys. Is this expected behavior? Thanks

It should work. Please make sure the polygon has the expected winding order. https://observablehq.com/@fil/rewind
I'm going to close the issue, but if you think this is a bug, please share the data which triggers it, and I'll reopen.

Thanks for the quick response @Fil, I have tested to make sure the polygon is in the expected winding order by using this GeoJSON linter at https://geojsonlint.com/. It will notify you if you need to rewind the data and also the polygon is displaying correctly on my map and I am not seeing this behavior you described:

Alas when it happens, it’s painful: a shape that represents a tiny speck of land becomes inflated to represent the whole globe minus that tiny speck of land, the map fills with a uniform color, the local projection explodes.

Here is an example of one of the GeoJSON files that does not seem to be working correctly for me: https://gist.github.com/secondl1ght/a902b36dac2379fead3452af49a77973

The same code I wrote is working for other polygons so your theory does make sense that it would be certain GeoJSONs that I am using causing issues. I thought I had it narrowed down to a multipoly issue because those seemed to be the problem ones from when I looked. Maybe you can see if something is wrong with this Dublin file I shared?

Thanks for the help!

Wow ok thanks! I guess I should have tested that the polygon drew correctly using D3. I am trying to find your package to use but I don't see it on NPM when I search for @fil/rewind, do you mind sharing a link to it please?

For now it's just a function in a notebook. https://observablehq.com/@fil/rewind

More discussion at #138

Thanks, I had a look and it's not super clear to me which part of the code I need to copy over into my app. There is a lot of code there, do I need all of it to complete the rewind that I require?

Consider fixing the geojson outside your application. If that's not possible, then you should copy 3 cells: geoRewindFeature and its dependencies geoRewindStream and geoProjectSimple. Also make sure you agree with the license (which I had not set yet, now ISC).

Thanks, is there also a way to check which polygons need to be rewound? I don't want to rewind ones that are already following the required spec.

I tried fixing the geojsons outside the application using https://geojsonlint.com/ to check for the 'right-hand rule'. But for some reason the problem GeoJSONs are not being picked up with errors on the lint website but they are still not working with D3.