d3 / d3-geo-projection

Extended geographic projections for d3-geo.
https://observablehq.com/collection/@d3/d3-geo-projection
Other
1.09k stars 203 forks source link

geostitch --segment? #75

Open mbostock opened 7 years ago

mbostock commented 7 years ago

Some GIS (GISes?) assume linear interpolation, even in spherical coordinate systems such as WGS84 / EPSG:4326. This is bad because it is rotation-variant. In contrast, D3 / TopoJSON assume great arcs (spherical interpolation) between points on lines and rings in spherical coordinates.

It would be useful if TopoJSON had a built-in way to fix this assumption as part of the conversion process, introducing interstitial points. Ideally this would be adaptive and measure the deviation between the great arc and the Cartesian line, but here is a simple approach that could then be simplified to achieve the same result:

// Takes a sparse line string that assumes Cartesian interpolation in spherical
// coordinates and inserts interstitial points for greater accuracy when
// rendering with D3, which assumes spherical interpolation.
function resample(coordinates) {
  var i = 0,
      j = -1,
      n = coordinates.length,
      source = coordinates.slice(),
      p0, x0, y0,
      p1 = coordinates[0], x1 = p1[0], y1 = p1[1],
      dx, dy, d2,
      m2 = 10; // squared minimum angular distance
  while (++i < n) {
    p0 = p1, x0 = x1, y0 = y1;
    p1 = source[i], x1 = p1[0], y1 = p1[1];
    dx = x1 - x0, dy = y1 - y0, d2 = dx * dx + dy * dy;
    coordinates[++j] = p0;
    if (d2 > m2) for (var k = 1, m = Math.ceil(Math.sqrt(d2 / m2)); k < m; ++k) {
      coordinates[++j] = [x0 + dx * k / m, y0 + dy * k / m];
    }
  }
  coordinates[++j] = p1;
  coordinates.length = j + 1;
}

I’m not sure whether we want to assume that GeoJSON / Shapefile input assumes linear interpolation in spherical coordinates, or if we only want to enable this selectively using a command-line argument. Probably the latter is the safer course of action, although I suspect many users are not aware of this issue.

Moved from topojson/topojson#110.

Fil commented 6 years ago

This can be useful in the browser, for instance to display nextzen's vector tiles https://beta.observablehq.com/@fil/wgs84-resampling