Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geoContains does not work with US County topography (https://d3js.org/us-10m.v1.json) #168

Closed
andersjr1984 opened this issue Jun 30, 2019 · 3 comments

Comments

@andersjr1984
Copy link

So, I have an issue and a "hacked" solution that worked for me.

I have several (100) cities that I would like to color the county they are in based on a score that they are given in an array.

I call geoContains with the following parameters: geoContains(county, points). When mapped, several counties now "contain" New York City (pretty much the "rust belt" and along the northern border).

I dug through the code posted here and noticed the following issue when comparing it to Json topography data:

geoContains line 37: This is where the coordinates of the polygon are passed to the polygonContains (object.coordinates);

polygonContains line 2: This takes the length of the polygon array that was passed in (object.coordinates). For all polygons in the above-referenced Json file, the length of this array is 1 and it contains another array that has all the coordinates to create the polygon. This is where the error occurs.

I've gotten around this by passing the correct coordinates array into the polygonContains function rather than the array that holds the array or going through the geoContains function. It works beautifully.

I wanted to point this out as an issue and also give people the cause and eventual solution to the problem. I'm just a newb programmer (less than 6 months programming, self taught, and one week working in d3), so I am not sure if I am doing this correctly. I spent a lot of time combing the internet trying to figure out what was wrong, so I'm hoping the above helps someone else out.

@Fil
Copy link
Member

Fil commented Jun 30, 2019

The coordinates in https://github.com/topojson/us-atlas are already projected to screen coordinates (x between 0 and 960, y between 0 and 600).

d3.geoContains expects WSG84 spherical coordinates (longitude, latitude), not projected coordinates.

Using polygonContains from https://github.com/d3/d3-polygon is a good solution, but as you've discovered it doesn't manage GeoJSON, only a flat list of the polygon’s vertices.

In GeoJSON, polygons are defined as the intersection of rings: if there are two rings, the second can represent a hole in the first (a lake inside a country).

I hope this clarifies the subject. Glad you found a solution that works for your case!

@Fil Fil closed this as completed Jun 30, 2019
@mbostock
Copy link
Member

We should implement path.contains. #109

@andersjr1984
Copy link
Author

Hey guys, thanks for the quick response and the explanation.

These are some excellent tools you have developed for the community and you guys provide great support.

Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants