score:17

If you interpolate the path using D3’s naïve string interpolator (d3.interpolateString), then the number of coordinates in the starting path and the number of the coordinates in the ending path must match exactly, including in the same order. But this is almost never the case due to clipping, cutting and resampling. Shape interpolation is possible (using multiple strategies), but it’s a hard problem to solve in the general case. See this explanation (part of the Path Transitions) for why naïve interpolation is insufficient.

Instead of interpolating the path, you want to interpolate the projection. Interpolating the projection does not require an exact correspondence between coordinates and therefore avoids interpolation artifacts. See these examples for a demonstration:

As shown in the first example, here is an implementation you can use:

``````function interpolatedProjection(a, b) {
var projection = d3.geo.projection(raw).scale(1),
center = projection.center,
translate = projection.translate,
α;

function raw(λ, φ) {
var pa = a([λ *= 180 / Math.PI, φ *= 180 / Math.PI]), pb = b([λ, φ]);
return [(1 - α) * pa + α * pb, (α - 1) * pa - α * pb];
}

projection.alpha = function(_) {
if (!arguments.length) return α;
α = +_;
var ca = a.center(), cb = b.center(),
ta = a.translate(), tb = b.translate();
center([(1 - α) * ca + α * cb, (1 - α) * ca + α * cb]);
translate([(1 - α) * ta + α * tb, (1 - α) * ta + α * tb]);
return projection;
};

delete projection.scale;
delete projection.translate;
delete projection.center;
return projection.alpha(0);
}
``````

Create the interpolated projection using two projections `a` and `b`, and then set the interpolated alpha to a value between 0 (for `a`) and 1 (for `b`).