function taggedBorders(geometries, tag = (a, b) => a !== b) {
const x = [];
for (const d of geometries) {
for (const i of d.type === "MultiPolygon"
? d.arcs.flat(2)
: d.type === "Polygon"
? d.arcs.flat(1)
: [])
x[i] = d;
}
const borders = new Map();
for (let i = 0; i < x.length; i++) {
const t = x[-i - 1] ? tag(x[i], x[-i - 1]) : null;
if (t != null && t !== false) {
borders.set(t, borders.get(t) || []);
borders.get(t).push(i);
}
}
return {
type: "GeometryCollection",
geometries: Array.from(borders, ([tag, arcs]) => {
const lines = arcs.map((i) => [i]);
return lines.length === 1
? {
type: "LineString",
arcs: lines[0],
properties: { tag }
}
: {
type: "MultiLineString",
arcs: lines,
properties: { tag }
};
})
};
}