| 1 |
import {object} from "./feature"; |
| 2 |
import stitchArcs from "./stitchArcs"; |
| 3 |
|
| 4 |
export default function(topology) { |
| 5 |
return object(topology, meshArcs.apply(this, arguments)); |
| 6 |
} |
| 7 |
|
| 8 |
export function meshArcs(topology, o, filter) { |
| 9 |
var arcs = []; |
| 10 |
|
| 11 |
function arc(i) { |
| 12 |
var j = i < 0 ? ~i : i; |
| 13 |
(geomsByArc[j] || (geomsByArc[j] = [])).push({i: i, g: geom}); |
| 14 |
} |
| 15 |
|
| 16 |
function line(arcs) { |
| 17 |
arcs.forEach(arc); |
| 18 |
} |
| 19 |
|
| 20 |
function polygon(arcs) { |
| 21 |
arcs.forEach(line); |
| 22 |
} |
| 23 |
|
| 24 |
function geometry(o) { |
| 25 |
if (o.type === "GeometryCollection") o.geometries.forEach(geometry); |
| 26 |
else if (o.type in geometryType) geom = o, geometryType[o.type](o.arcs); |
| 27 |
} |
| 28 |
|
| 29 |
if (arguments.length > 1) { |
| 30 |
var geomsByArc = [], |
| 31 |
geom; |
| 32 |
|
| 33 |
var geometryType = { |
| 34 |
LineString: line, |
| 35 |
MultiLineString: polygon, |
| 36 |
Polygon: polygon, |
| 37 |
MultiPolygon: function(arcs) { arcs.forEach(polygon); } |
| 38 |
}; |
| 39 |
|
| 40 |
geometry(o); |
| 41 |
|
| 42 |
geomsByArc.forEach(arguments.length < 3 |
| 43 |
? function(geoms) { arcs.push(geoms[0].i); } |
| 44 |
: function(geoms) { if (filter(geoms[0].g, geoms[geoms.length - 1].g)) arcs.push(geoms[0].i); }); |
| 45 |
} else { |
| 46 |
for (var i = 0, n = topology.arcs.length; i < n; ++i) arcs.push(i); |
| 47 |
} |
| 48 |
|
| 49 |
return {type: "MultiLineString", arcs: stitchArcs(topology, arcs)}; |
| 50 |
} |