Fil / d3-geo-voronoi

Voronoi / Delaunay tessellations on the sphere
ISC License
229 stars 24 forks source link

Geo Voronoi polygons out of triangles #11

Closed misha-panyushkin closed 6 years ago

misha-panyushkin commented 6 years ago

Hi, I'm trying to build Voronoi diagram for "infinite horizon" map. The points - are the cities with defined coordinates [lat, lon].

Input: geoJSON

{
  "features": [
    {
      "geometry": {
        "coordinates": [
          2.1699187,
          41.387917
        ],
        "type": "Point"
      },
      "properties": {},
      "type": "Feature"
    },
    {
      "geometry": {
        "coordinates": [
          7.205232,
          43.66049
        ],
        "type": "Point"
      },
      "properties": {},
      "type": "Feature"
    },
    {
      "geometry": {
        "coordinates": [
          12.4942486,
          41.8905198
        ],
        "type": "Point"
      },
      "properties": {},
      "type": "Feature"
    }
  ],
  "type": "FeatureCollection"
}

Links d3.geoVoronoi(geoJSON).links() image Triangles d3.geoVoronoi(geoJSON).triangles() image Polygons d3.geoVoronoi(geoJSON).polygons() image

Can't understand what I'm doing wrong. I will appreciate any help on these.

Fil commented 6 years ago

You're not doing anything wrong! The reason for the bad display is that D3 doesn't have the same definition of geojson as the others. (See the introduction in https://github.com/d3/d3-geo).

In d3-geo, all the lines follow the geodesic lines (great circles), and they don't need to be cut at the antimeridian (longitude=180°).

To convert your results to the standard geojson you can use the d3.geoProject utility from https://github.com/d3/d3-geo-projection and do this:

projected = d3.geoProject(
  d3.geoProject(
    polygons,
    d3
      .geoEquirectangular()
      .scale(180 / Math.PI)
      .translate([0, 0])
  ),
  d3.geoIdentity().reflectY(true)
)

After this operation you should be able to see the features correctly. https://gist.github.com/Fil/f67b0f6ccfa1f072c555775e331c307b

As you can see if you inspect the resulting structure, one of the polygons has been transformed to a multipolygon, which doesn't make sense from a topological point of view… and that is basically the explanation for this (very inconvenient) difference.

We should probably offer a simpler API for this "solution".

Fil commented 6 years ago

Another reference here: https://github.com/d3/d3-geo/issues/104

misha-panyushkin commented 6 years ago

@Fil many thanks! I've spent a lot of time finding solution. And now I understand the correct approach!

misha-panyushkin commented 6 years ago

image