Contour plots generated by d3-contour overlay shapes (Multipolygons) of increasing value. To obtain the shape that is between two values a < b, we need to substract the shape B = contour(b) from the shape A = contour(a).
-
by definition, all polygons from B are included in 1 (or several) polygons of A, and there is no intersection (otherwise we would have value x simultaneously equal to a and to b)
-
each outer ring from B (where x > b) must be transformed into a hole in its encompassing polygon(s) of A
-
holes from B must be added as polygons to A.
The substraction algorithm for this case is then much simpler than a generic Multipolygon intersection/substraction:
For each polygon in multipolygon B, take a point from its outer ring. Then find the polygon of A that contains this point, and add that ring as a hole (which implies a reversal of its coordinates). Plus, add holes of B as polygons to A.
But I'm not sure this covers all cases, that is, what happens when a hole of A is inside a hole of B?
A simpler method works OK if one wants to extract 1 band (http://bl.ocks.org/Fil/94d4df7520ffc8434c40fe9b82ebd536), but it is useless if you need to extract several contiguous bands, as it does not give an exact boundary (there are gaps and overlaps, cf http://bl.ocks.org/Fil/3f866e2e90c3e019bfe3fd9e0d43fe14).
Forked from mbostock's block: Contour Plot
forked from Fil's block: Extract a band from a Contour Plot [UNLISTED]