Question

I'm using d3.js for a non contiguous cartogram where countries are scaled depending on some data. I'm scaling from the centroid of each country/path in the svg generated from a GeoJson file, but this doesn't look good for countries described as a multipolygon, like the USA or Canada.

I would like to find the centroid of each polygon forming a multipolygon, so I can rescale without translating them. For example, when using the centroid of the USA, Alaska moves towards the right (in a mercator projection) while the rest moves up. Ideally, Alaska would be scaled from its own centroid, so it would stay in the same position.

Is it possible to iterate through each polygon in a multipolygon feature and get its centroid?

Thanks!

Was it helpful?

Solution

Finally I decided to split multipolygons in polygons by iterating over geometry.coordinates as @LarsKotthoff mentioned. With these polygons I created a new array of features with no Multipolygon types in it. Paths are added to the svg from this array, resulting in several polygons having the same id and name, i.e., several polygons for Canada with the same country abbreviation.

Thus you can keep using d3's path.centroid() to get the centroid of each polygon.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top