@vim3d/geojson 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/esm/index.js +1 -0
- package/esm/resample.js +43 -19
- package/esm/topology.js +154 -0
- package/index.d.ts +1 -0
- package/index.js +3 -1
- package/package.json +1 -1
- package/resample.d.ts +7 -1
- package/resample.js +43 -19
- package/topology.d.ts +29 -0
- package/topology.js +159 -0
package/README.md
CHANGED
|
@@ -38,6 +38,13 @@ comes out of `maxPositions: 5000` with all 41 polygons kept.
|
|
|
38
38
|
in coordinate units (degrees for WGS84). `maxPositions` finds the smallest
|
|
39
39
|
tolerance that fits the budget. Points are kept; polygons, holes and
|
|
40
40
|
features that collapse below four positions are dropped.
|
|
41
|
+
- `resampleGeoJSON(input, { topology: true })` (also on `loadGeoJSON`): cuts
|
|
42
|
+
lines and rings into arcs where they meet and simplifies each arc once, so
|
|
43
|
+
a border two neighborhoods share stays identical in both. Only positions
|
|
44
|
+
that match exactly count as shared.
|
|
45
|
+
- `borders(input)`: every line and ring edge once, as a `MultiLineString`, so
|
|
46
|
+
shared borders draw once:
|
|
47
|
+
`toSceneCurves(borders(neighborhoods), origin)`.
|
|
41
48
|
- `toSceneCurves(input, origin, { elevation })`: lines and polygon rings as
|
|
42
49
|
`curve` geometries in the Vim3D `world` frame around `origin` (x east, y up,
|
|
43
50
|
-z north), with each feature's `properties` and `id`.
|
package/esm/index.js
CHANGED
|
@@ -3,3 +3,4 @@ export { loadGeoJSON } from './fetch.js';
|
|
|
3
3
|
export { bbox, countPositions, eachPosition, resampleGeoJSON } from './resample.js';
|
|
4
4
|
export { toSceneCurves } from './scene.js';
|
|
5
5
|
export { simplifyLine, simplifyRing } from './simplify.js';
|
|
6
|
+
export { borders } from './topology.js';
|
package/esm/resample.js
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
1
|
import { simplifyLine, simplifyRing } from './simplify.js';
|
|
2
|
+
import { buildTopology, simplifyTopology } from './topology.js';
|
|
3
|
+
function area(ring) {
|
|
4
|
+
const [x0, y0] = ring[0];
|
|
5
|
+
let sum = 0;
|
|
6
|
+
for (let i = 2; i < ring.length - 1; i++) {
|
|
7
|
+
sum += (ring[i - 1][0] - x0) * (ring[i][1] - y0) - (ring[i][0] - x0) * (ring[i - 1][1] - y0);
|
|
8
|
+
}
|
|
9
|
+
return sum;
|
|
10
|
+
}
|
|
11
|
+
function simplifier(tolerance, topology) {
|
|
12
|
+
if (!topology)
|
|
13
|
+
return { line: points => simplifyLine(points, tolerance), ring: ring => simplifyRing(ring, tolerance) };
|
|
14
|
+
const rebuild = simplifyTopology(topology, tolerance);
|
|
15
|
+
return {
|
|
16
|
+
line: points => rebuild(points) ?? points.slice(),
|
|
17
|
+
ring: ring => {
|
|
18
|
+
const out = rebuild(ring);
|
|
19
|
+
return out && out.length >= 4 && area(out) !== 0 ? out : null;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
2
23
|
function walkGeometry(geometry, visit) {
|
|
3
24
|
if (!geometry)
|
|
4
25
|
return;
|
|
@@ -51,14 +72,14 @@ export function bbox(input) {
|
|
|
51
72
|
});
|
|
52
73
|
return box;
|
|
53
74
|
}
|
|
54
|
-
function simplifyPolygon(rings,
|
|
55
|
-
const outer =
|
|
75
|
+
function simplifyPolygon(rings, s) {
|
|
76
|
+
const outer = s.ring(rings[0] ?? []);
|
|
56
77
|
if (!outer)
|
|
57
78
|
return null;
|
|
58
|
-
const holes = rings.slice(1).map(ring =>
|
|
79
|
+
const holes = rings.slice(1).map(ring => s.ring(ring)).filter((ring) => !!ring);
|
|
59
80
|
return [outer, ...holes];
|
|
60
81
|
}
|
|
61
|
-
function simplifyGeometry(geometry,
|
|
82
|
+
function simplifyGeometry(geometry, s) {
|
|
62
83
|
if (!geometry)
|
|
63
84
|
return null;
|
|
64
85
|
switch (geometry.type) {
|
|
@@ -66,51 +87,54 @@ function simplifyGeometry(geometry, tolerance) {
|
|
|
66
87
|
case 'MultiPoint':
|
|
67
88
|
return geometry;
|
|
68
89
|
case 'LineString':
|
|
69
|
-
return { type: 'LineString', coordinates:
|
|
90
|
+
return { type: 'LineString', coordinates: s.line(geometry.coordinates) };
|
|
70
91
|
case 'MultiLineString':
|
|
71
|
-
return { type: 'MultiLineString', coordinates: geometry.coordinates.map(line =>
|
|
92
|
+
return { type: 'MultiLineString', coordinates: geometry.coordinates.map(line => s.line(line)) };
|
|
72
93
|
case 'Polygon': {
|
|
73
|
-
const coordinates = simplifyPolygon(geometry.coordinates,
|
|
94
|
+
const coordinates = simplifyPolygon(geometry.coordinates, s);
|
|
74
95
|
return coordinates && { type: 'Polygon', coordinates };
|
|
75
96
|
}
|
|
76
97
|
case 'MultiPolygon': {
|
|
77
|
-
const coordinates = geometry.coordinates.map(polygon => simplifyPolygon(polygon,
|
|
98
|
+
const coordinates = geometry.coordinates.map(polygon => simplifyPolygon(polygon, s))
|
|
78
99
|
.filter((polygon) => !!polygon);
|
|
79
100
|
return coordinates.length ? { type: 'MultiPolygon', coordinates } : null;
|
|
80
101
|
}
|
|
81
102
|
case 'GeometryCollection': {
|
|
82
|
-
const geometries = geometry.geometries.map(child => simplifyGeometry(child,
|
|
103
|
+
const geometries = geometry.geometries.map(child => simplifyGeometry(child, s))
|
|
83
104
|
.filter((child) => !!child);
|
|
84
105
|
return geometries.length ? { type: 'GeometryCollection', geometries } : null;
|
|
85
106
|
}
|
|
86
107
|
}
|
|
87
108
|
}
|
|
88
|
-
function simplifyFeature(feature,
|
|
109
|
+
function simplifyFeature(feature, s) {
|
|
89
110
|
if (!feature.geometry)
|
|
90
111
|
return feature;
|
|
91
|
-
const geometry = simplifyGeometry(feature.geometry,
|
|
112
|
+
const geometry = simplifyGeometry(feature.geometry, s);
|
|
92
113
|
return geometry && { ...feature, geometry };
|
|
93
114
|
}
|
|
94
|
-
function simplifyAll(input,
|
|
115
|
+
function simplifyAll(input, s) {
|
|
95
116
|
if (input.type === 'FeatureCollection') {
|
|
96
|
-
const features = input.features.map(feature => simplifyFeature(feature,
|
|
117
|
+
const features = input.features.map(feature => simplifyFeature(feature, s))
|
|
97
118
|
.filter((feature) => !!feature);
|
|
98
119
|
return { ...input, features };
|
|
99
120
|
}
|
|
100
121
|
if (input.type === 'Feature')
|
|
101
|
-
return simplifyFeature(input,
|
|
102
|
-
return simplifyGeometry(input,
|
|
122
|
+
return simplifyFeature(input, s);
|
|
123
|
+
return simplifyGeometry(input, s);
|
|
103
124
|
}
|
|
104
125
|
/**
|
|
105
126
|
* Simplifies every line and ring with Douglas-Peucker. Points are kept as is.
|
|
106
127
|
* Polygons and holes that collapse below four positions are dropped, and so
|
|
107
128
|
* are features whose geometry disappears; a lone geometry that collapses
|
|
108
|
-
* returns null.
|
|
129
|
+
* returns null. With `topology`, shared borders are simplified once and stay
|
|
130
|
+
* identical in every polygon that uses them.
|
|
109
131
|
*/
|
|
110
132
|
export function resampleGeoJSON(input, options = {}) {
|
|
111
133
|
const minimum = Math.max(0, options.tolerance ?? 0);
|
|
112
134
|
const { maxPositions } = options;
|
|
113
|
-
const
|
|
135
|
+
const topology = options.topology ? buildTopology(input) : undefined;
|
|
136
|
+
const at = (tolerance) => simplifyAll(input, simplifier(tolerance, topology));
|
|
137
|
+
const first = at(minimum);
|
|
114
138
|
if (maxPositions === undefined || !first || countPositions(first) <= maxPositions)
|
|
115
139
|
return first;
|
|
116
140
|
const box = bbox(input);
|
|
@@ -118,10 +142,10 @@ export function resampleGeoJSON(input, options = {}) {
|
|
|
118
142
|
return first;
|
|
119
143
|
let lo = minimum;
|
|
120
144
|
let hi = Math.max(Math.hypot(box[2] - box[0], box[3] - box[1]), minimum);
|
|
121
|
-
let best =
|
|
145
|
+
let best = at(hi);
|
|
122
146
|
for (let i = 0; i < 40 && hi - lo > hi * 1e-6; i++) {
|
|
123
147
|
const mid = (lo + hi) / 2;
|
|
124
|
-
const candidate =
|
|
148
|
+
const candidate = at(mid);
|
|
125
149
|
if (!candidate)
|
|
126
150
|
hi = mid;
|
|
127
151
|
else if (countPositions(candidate) <= maxPositions) {
|
package/esm/topology.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { simplifyLine } from './simplify.js';
|
|
2
|
+
const keyOf = (p) => `${p[0]},${p[1]}`;
|
|
3
|
+
function eachLine(input, visit) {
|
|
4
|
+
const walk = (geometry) => {
|
|
5
|
+
if (!geometry)
|
|
6
|
+
return;
|
|
7
|
+
switch (geometry.type) {
|
|
8
|
+
case 'LineString':
|
|
9
|
+
visit(geometry.coordinates, false);
|
|
10
|
+
break;
|
|
11
|
+
case 'MultiLineString':
|
|
12
|
+
geometry.coordinates.forEach(line => visit(line, false));
|
|
13
|
+
break;
|
|
14
|
+
case 'Polygon':
|
|
15
|
+
geometry.coordinates.forEach(ring => visit(ring, true));
|
|
16
|
+
break;
|
|
17
|
+
case 'MultiPolygon':
|
|
18
|
+
geometry.coordinates.forEach(polygon => polygon.forEach(ring => visit(ring, true)));
|
|
19
|
+
break;
|
|
20
|
+
case 'GeometryCollection':
|
|
21
|
+
geometry.geometries.forEach(walk);
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
if (input.type === 'FeatureCollection')
|
|
26
|
+
input.features.forEach(feature => walk(feature.geometry));
|
|
27
|
+
else if (input.type === 'Feature')
|
|
28
|
+
walk(input.geometry);
|
|
29
|
+
else
|
|
30
|
+
walk(input);
|
|
31
|
+
}
|
|
32
|
+
/** Drops repeated positions and, for rings, the closing position. */
|
|
33
|
+
function dedupe(line, closed) {
|
|
34
|
+
const out = [];
|
|
35
|
+
for (const p of line)
|
|
36
|
+
if (!out.length || keyOf(out[out.length - 1]) !== keyOf(p))
|
|
37
|
+
out.push(p);
|
|
38
|
+
if (closed && out.length > 1 && keyOf(out[0]) === keyOf(out[out.length - 1]))
|
|
39
|
+
out.pop();
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
function lowest(points) {
|
|
43
|
+
let best = 0;
|
|
44
|
+
points.forEach((p, i) => {
|
|
45
|
+
const q = points[best];
|
|
46
|
+
if (p[0] < q[0] || (p[0] === q[0] && p[1] < q[1]))
|
|
47
|
+
best = i;
|
|
48
|
+
});
|
|
49
|
+
return best;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Finds where lines and rings meet (a position whose neighbors differ between
|
|
53
|
+
* two of its uses, or a line end) and cuts them there into shared arcs.
|
|
54
|
+
*/
|
|
55
|
+
export function buildTopology(input) {
|
|
56
|
+
const entries = [];
|
|
57
|
+
eachLine(input, (line, closed) => entries.push([line, dedupe(line, closed), closed]));
|
|
58
|
+
const neighbors = new Map();
|
|
59
|
+
const junctions = new Set();
|
|
60
|
+
const visit = (p, a, b) => {
|
|
61
|
+
const [ka, kb] = [keyOf(a), keyOf(b)];
|
|
62
|
+
const pair = ka < kb ? `${ka}|${kb}` : `${kb}|${ka}`;
|
|
63
|
+
const key = keyOf(p);
|
|
64
|
+
const seen = neighbors.get(key);
|
|
65
|
+
if (seen === undefined)
|
|
66
|
+
neighbors.set(key, pair);
|
|
67
|
+
else if (seen !== pair)
|
|
68
|
+
junctions.add(key);
|
|
69
|
+
};
|
|
70
|
+
for (const [, points, closed] of entries) {
|
|
71
|
+
const n = points.length;
|
|
72
|
+
if (closed) {
|
|
73
|
+
if (n >= 3)
|
|
74
|
+
points.forEach((p, i) => visit(p, points[(i + n - 1) % n], points[(i + 1) % n]));
|
|
75
|
+
}
|
|
76
|
+
else if (n) {
|
|
77
|
+
junctions.add(keyOf(points[0]));
|
|
78
|
+
junctions.add(keyOf(points[n - 1]));
|
|
79
|
+
for (let i = 1; i < n - 1; i++)
|
|
80
|
+
visit(points[i], points[i - 1], points[i + 1]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const arcs = [];
|
|
84
|
+
const index = new Map();
|
|
85
|
+
const add = (arc) => {
|
|
86
|
+
const keys = arc.map(keyOf);
|
|
87
|
+
const forward = keys.join(';');
|
|
88
|
+
const backward = keys.reverse().join(';');
|
|
89
|
+
const reversed = backward < forward;
|
|
90
|
+
const key = reversed ? backward : forward;
|
|
91
|
+
let i = index.get(key);
|
|
92
|
+
if (i === undefined) {
|
|
93
|
+
i = arcs.length;
|
|
94
|
+
index.set(key, i);
|
|
95
|
+
arcs.push(reversed ? arc.slice().reverse() : arc);
|
|
96
|
+
}
|
|
97
|
+
return { index: i, reversed };
|
|
98
|
+
};
|
|
99
|
+
const lines = new WeakMap();
|
|
100
|
+
for (const [line, points, closed] of entries) {
|
|
101
|
+
if (points.length < (closed ? 3 : 2))
|
|
102
|
+
continue;
|
|
103
|
+
let sequence = points;
|
|
104
|
+
if (closed) {
|
|
105
|
+
let start = points.findIndex(p => junctions.has(keyOf(p)));
|
|
106
|
+
if (start === -1)
|
|
107
|
+
start = lowest(points);
|
|
108
|
+
sequence = [...points.slice(start), ...points.slice(0, start), points[start]];
|
|
109
|
+
}
|
|
110
|
+
const refs = [];
|
|
111
|
+
let from = 0;
|
|
112
|
+
for (let i = 1; i < sequence.length; i++) {
|
|
113
|
+
if (i === sequence.length - 1 || junctions.has(keyOf(sequence[i]))) {
|
|
114
|
+
refs.push(add(sequence.slice(from, i + 1)));
|
|
115
|
+
from = i;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
lines.set(line, refs);
|
|
119
|
+
}
|
|
120
|
+
return { arcs, lines };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Returns a function that rebuilds a line or ring of the topology's input from
|
|
124
|
+
* its arcs, each simplified once, so shared borders stay identical. Lines the
|
|
125
|
+
* topology skipped (too short to draw) return undefined.
|
|
126
|
+
*/
|
|
127
|
+
export function simplifyTopology(topology, tolerance) {
|
|
128
|
+
const simplified = new Map();
|
|
129
|
+
const arc = ({ index, reversed }) => {
|
|
130
|
+
let points = simplified.get(index);
|
|
131
|
+
if (!points)
|
|
132
|
+
simplified.set(index, points = simplifyLine(topology.arcs[index], tolerance));
|
|
133
|
+
return reversed ? points.slice().reverse() : points;
|
|
134
|
+
};
|
|
135
|
+
return line => {
|
|
136
|
+
const refs = topology.lines.get(line);
|
|
137
|
+
if (!refs)
|
|
138
|
+
return undefined;
|
|
139
|
+
const out = [];
|
|
140
|
+
for (const ref of refs) {
|
|
141
|
+
const points = arc(ref);
|
|
142
|
+
for (let i = out.length ? 1 : 0; i < points.length; i++)
|
|
143
|
+
out.push(points[i]);
|
|
144
|
+
}
|
|
145
|
+
return out;
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Every line and ring edge once, as a MultiLineString: a border two polygons
|
|
150
|
+
* share comes out as one line instead of two.
|
|
151
|
+
*/
|
|
152
|
+
export function borders(input) {
|
|
153
|
+
return { type: 'MultiLineString', coordinates: buildTopology(input).arcs.map(arc => arc.slice()) };
|
|
154
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -6,4 +6,5 @@ export { bbox, countPositions, eachPosition, resampleGeoJSON } from './resample.
|
|
|
6
6
|
export type { SceneCurve, SceneCurveOptions } from './scene.js';
|
|
7
7
|
export { toSceneCurves } from './scene.js';
|
|
8
8
|
export { simplifyLine, simplifyRing } from './simplify.js';
|
|
9
|
+
export { borders } from './topology.js';
|
|
9
10
|
export type * from './types.js';
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.simplifyRing = exports.simplifyLine = exports.toSceneCurves = exports.resampleGeoJSON = exports.eachPosition = exports.countPositions = exports.bbox = exports.loadGeoJSON = exports.name = void 0;
|
|
3
|
+
exports.borders = exports.simplifyRing = exports.simplifyLine = exports.toSceneCurves = exports.resampleGeoJSON = exports.eachPosition = exports.countPositions = exports.bbox = exports.loadGeoJSON = exports.name = void 0;
|
|
4
4
|
exports.name = '@vim3d/geojson';
|
|
5
5
|
var fetch_js_1 = require("./fetch.js");
|
|
6
6
|
Object.defineProperty(exports, "loadGeoJSON", { enumerable: true, get: function () { return fetch_js_1.loadGeoJSON; } });
|
|
@@ -14,3 +14,5 @@ Object.defineProperty(exports, "toSceneCurves", { enumerable: true, get: functio
|
|
|
14
14
|
var simplify_js_1 = require("./simplify.js");
|
|
15
15
|
Object.defineProperty(exports, "simplifyLine", { enumerable: true, get: function () { return simplify_js_1.simplifyLine; } });
|
|
16
16
|
Object.defineProperty(exports, "simplifyRing", { enumerable: true, get: function () { return simplify_js_1.simplifyRing; } });
|
|
17
|
+
var topology_js_1 = require("./topology.js");
|
|
18
|
+
Object.defineProperty(exports, "borders", { enumerable: true, get: function () { return topology_js_1.borders; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vim3d/geojson",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Fetch GeoJSON at load time and resample it for Vim3D scenes.",
|
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/pyramation/vim3d/tree/master/js/packages/geojson",
|
package/resample.d.ts
CHANGED
|
@@ -9,6 +9,11 @@ export interface ResampleOptions {
|
|
|
9
9
|
* smallest reachable, which can still be over it.
|
|
10
10
|
*/
|
|
11
11
|
maxPositions?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Cut lines and rings into arcs where they meet and simplify each arc once,
|
|
14
|
+
* so borders that polygons share stay identical after resampling.
|
|
15
|
+
*/
|
|
16
|
+
topology?: boolean;
|
|
12
17
|
}
|
|
13
18
|
type Collect = (position: Position) => void;
|
|
14
19
|
/** Calls `visit` for every position in a geometry, feature or collection. */
|
|
@@ -20,7 +25,8 @@ export declare function bbox(input: GeoJSON): [number, number, number, number] |
|
|
|
20
25
|
* Simplifies every line and ring with Douglas-Peucker. Points are kept as is.
|
|
21
26
|
* Polygons and holes that collapse below four positions are dropped, and so
|
|
22
27
|
* are features whose geometry disappears; a lone geometry that collapses
|
|
23
|
-
* returns null.
|
|
28
|
+
* returns null. With `topology`, shared borders are simplified once and stay
|
|
29
|
+
* identical in every polygon that uses them.
|
|
24
30
|
*/
|
|
25
31
|
export declare function resampleGeoJSON<T extends GeoJSON>(input: T, options?: ResampleOptions): T | null;
|
|
26
32
|
export {};
|
package/resample.js
CHANGED
|
@@ -5,6 +5,27 @@ exports.countPositions = countPositions;
|
|
|
5
5
|
exports.bbox = bbox;
|
|
6
6
|
exports.resampleGeoJSON = resampleGeoJSON;
|
|
7
7
|
const simplify_js_1 = require("./simplify.js");
|
|
8
|
+
const topology_js_1 = require("./topology.js");
|
|
9
|
+
function area(ring) {
|
|
10
|
+
const [x0, y0] = ring[0];
|
|
11
|
+
let sum = 0;
|
|
12
|
+
for (let i = 2; i < ring.length - 1; i++) {
|
|
13
|
+
sum += (ring[i - 1][0] - x0) * (ring[i][1] - y0) - (ring[i][0] - x0) * (ring[i - 1][1] - y0);
|
|
14
|
+
}
|
|
15
|
+
return sum;
|
|
16
|
+
}
|
|
17
|
+
function simplifier(tolerance, topology) {
|
|
18
|
+
if (!topology)
|
|
19
|
+
return { line: points => (0, simplify_js_1.simplifyLine)(points, tolerance), ring: ring => (0, simplify_js_1.simplifyRing)(ring, tolerance) };
|
|
20
|
+
const rebuild = (0, topology_js_1.simplifyTopology)(topology, tolerance);
|
|
21
|
+
return {
|
|
22
|
+
line: points => rebuild(points) ?? points.slice(),
|
|
23
|
+
ring: ring => {
|
|
24
|
+
const out = rebuild(ring);
|
|
25
|
+
return out && out.length >= 4 && area(out) !== 0 ? out : null;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
8
29
|
function walkGeometry(geometry, visit) {
|
|
9
30
|
if (!geometry)
|
|
10
31
|
return;
|
|
@@ -57,14 +78,14 @@ function bbox(input) {
|
|
|
57
78
|
});
|
|
58
79
|
return box;
|
|
59
80
|
}
|
|
60
|
-
function simplifyPolygon(rings,
|
|
61
|
-
const outer =
|
|
81
|
+
function simplifyPolygon(rings, s) {
|
|
82
|
+
const outer = s.ring(rings[0] ?? []);
|
|
62
83
|
if (!outer)
|
|
63
84
|
return null;
|
|
64
|
-
const holes = rings.slice(1).map(ring =>
|
|
85
|
+
const holes = rings.slice(1).map(ring => s.ring(ring)).filter((ring) => !!ring);
|
|
65
86
|
return [outer, ...holes];
|
|
66
87
|
}
|
|
67
|
-
function simplifyGeometry(geometry,
|
|
88
|
+
function simplifyGeometry(geometry, s) {
|
|
68
89
|
if (!geometry)
|
|
69
90
|
return null;
|
|
70
91
|
switch (geometry.type) {
|
|
@@ -72,51 +93,54 @@ function simplifyGeometry(geometry, tolerance) {
|
|
|
72
93
|
case 'MultiPoint':
|
|
73
94
|
return geometry;
|
|
74
95
|
case 'LineString':
|
|
75
|
-
return { type: 'LineString', coordinates:
|
|
96
|
+
return { type: 'LineString', coordinates: s.line(geometry.coordinates) };
|
|
76
97
|
case 'MultiLineString':
|
|
77
|
-
return { type: 'MultiLineString', coordinates: geometry.coordinates.map(line =>
|
|
98
|
+
return { type: 'MultiLineString', coordinates: geometry.coordinates.map(line => s.line(line)) };
|
|
78
99
|
case 'Polygon': {
|
|
79
|
-
const coordinates = simplifyPolygon(geometry.coordinates,
|
|
100
|
+
const coordinates = simplifyPolygon(geometry.coordinates, s);
|
|
80
101
|
return coordinates && { type: 'Polygon', coordinates };
|
|
81
102
|
}
|
|
82
103
|
case 'MultiPolygon': {
|
|
83
|
-
const coordinates = geometry.coordinates.map(polygon => simplifyPolygon(polygon,
|
|
104
|
+
const coordinates = geometry.coordinates.map(polygon => simplifyPolygon(polygon, s))
|
|
84
105
|
.filter((polygon) => !!polygon);
|
|
85
106
|
return coordinates.length ? { type: 'MultiPolygon', coordinates } : null;
|
|
86
107
|
}
|
|
87
108
|
case 'GeometryCollection': {
|
|
88
|
-
const geometries = geometry.geometries.map(child => simplifyGeometry(child,
|
|
109
|
+
const geometries = geometry.geometries.map(child => simplifyGeometry(child, s))
|
|
89
110
|
.filter((child) => !!child);
|
|
90
111
|
return geometries.length ? { type: 'GeometryCollection', geometries } : null;
|
|
91
112
|
}
|
|
92
113
|
}
|
|
93
114
|
}
|
|
94
|
-
function simplifyFeature(feature,
|
|
115
|
+
function simplifyFeature(feature, s) {
|
|
95
116
|
if (!feature.geometry)
|
|
96
117
|
return feature;
|
|
97
|
-
const geometry = simplifyGeometry(feature.geometry,
|
|
118
|
+
const geometry = simplifyGeometry(feature.geometry, s);
|
|
98
119
|
return geometry && { ...feature, geometry };
|
|
99
120
|
}
|
|
100
|
-
function simplifyAll(input,
|
|
121
|
+
function simplifyAll(input, s) {
|
|
101
122
|
if (input.type === 'FeatureCollection') {
|
|
102
|
-
const features = input.features.map(feature => simplifyFeature(feature,
|
|
123
|
+
const features = input.features.map(feature => simplifyFeature(feature, s))
|
|
103
124
|
.filter((feature) => !!feature);
|
|
104
125
|
return { ...input, features };
|
|
105
126
|
}
|
|
106
127
|
if (input.type === 'Feature')
|
|
107
|
-
return simplifyFeature(input,
|
|
108
|
-
return simplifyGeometry(input,
|
|
128
|
+
return simplifyFeature(input, s);
|
|
129
|
+
return simplifyGeometry(input, s);
|
|
109
130
|
}
|
|
110
131
|
/**
|
|
111
132
|
* Simplifies every line and ring with Douglas-Peucker. Points are kept as is.
|
|
112
133
|
* Polygons and holes that collapse below four positions are dropped, and so
|
|
113
134
|
* are features whose geometry disappears; a lone geometry that collapses
|
|
114
|
-
* returns null.
|
|
135
|
+
* returns null. With `topology`, shared borders are simplified once and stay
|
|
136
|
+
* identical in every polygon that uses them.
|
|
115
137
|
*/
|
|
116
138
|
function resampleGeoJSON(input, options = {}) {
|
|
117
139
|
const minimum = Math.max(0, options.tolerance ?? 0);
|
|
118
140
|
const { maxPositions } = options;
|
|
119
|
-
const
|
|
141
|
+
const topology = options.topology ? (0, topology_js_1.buildTopology)(input) : undefined;
|
|
142
|
+
const at = (tolerance) => simplifyAll(input, simplifier(tolerance, topology));
|
|
143
|
+
const first = at(minimum);
|
|
120
144
|
if (maxPositions === undefined || !first || countPositions(first) <= maxPositions)
|
|
121
145
|
return first;
|
|
122
146
|
const box = bbox(input);
|
|
@@ -124,10 +148,10 @@ function resampleGeoJSON(input, options = {}) {
|
|
|
124
148
|
return first;
|
|
125
149
|
let lo = minimum;
|
|
126
150
|
let hi = Math.max(Math.hypot(box[2] - box[0], box[3] - box[1]), minimum);
|
|
127
|
-
let best =
|
|
151
|
+
let best = at(hi);
|
|
128
152
|
for (let i = 0; i < 40 && hi - lo > hi * 1e-6; i++) {
|
|
129
153
|
const mid = (lo + hi) / 2;
|
|
130
|
-
const candidate =
|
|
154
|
+
const candidate = at(mid);
|
|
131
155
|
if (!candidate)
|
|
132
156
|
hi = mid;
|
|
133
157
|
else if (countPositions(candidate) <= maxPositions) {
|
package/topology.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { GeoJSON, MultiLineString, Position } from './types.js';
|
|
2
|
+
export interface ArcRef {
|
|
3
|
+
index: number;
|
|
4
|
+
reversed: boolean;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Lines and rings cut into arcs at the positions where they meet, so a border
|
|
8
|
+
* two polygons share is one arc.
|
|
9
|
+
*/
|
|
10
|
+
export interface Topology {
|
|
11
|
+
arcs: Position[][];
|
|
12
|
+
lines: WeakMap<readonly Position[], ArcRef[]>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Finds where lines and rings meet (a position whose neighbors differ between
|
|
16
|
+
* two of its uses, or a line end) and cuts them there into shared arcs.
|
|
17
|
+
*/
|
|
18
|
+
export declare function buildTopology(input: GeoJSON): Topology;
|
|
19
|
+
/**
|
|
20
|
+
* Returns a function that rebuilds a line or ring of the topology's input from
|
|
21
|
+
* its arcs, each simplified once, so shared borders stay identical. Lines the
|
|
22
|
+
* topology skipped (too short to draw) return undefined.
|
|
23
|
+
*/
|
|
24
|
+
export declare function simplifyTopology(topology: Topology, tolerance: number): (line: readonly Position[]) => Position[] | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Every line and ring edge once, as a MultiLineString: a border two polygons
|
|
27
|
+
* share comes out as one line instead of two.
|
|
28
|
+
*/
|
|
29
|
+
export declare function borders(input: GeoJSON): MultiLineString;
|
package/topology.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildTopology = buildTopology;
|
|
4
|
+
exports.simplifyTopology = simplifyTopology;
|
|
5
|
+
exports.borders = borders;
|
|
6
|
+
const simplify_js_1 = require("./simplify.js");
|
|
7
|
+
const keyOf = (p) => `${p[0]},${p[1]}`;
|
|
8
|
+
function eachLine(input, visit) {
|
|
9
|
+
const walk = (geometry) => {
|
|
10
|
+
if (!geometry)
|
|
11
|
+
return;
|
|
12
|
+
switch (geometry.type) {
|
|
13
|
+
case 'LineString':
|
|
14
|
+
visit(geometry.coordinates, false);
|
|
15
|
+
break;
|
|
16
|
+
case 'MultiLineString':
|
|
17
|
+
geometry.coordinates.forEach(line => visit(line, false));
|
|
18
|
+
break;
|
|
19
|
+
case 'Polygon':
|
|
20
|
+
geometry.coordinates.forEach(ring => visit(ring, true));
|
|
21
|
+
break;
|
|
22
|
+
case 'MultiPolygon':
|
|
23
|
+
geometry.coordinates.forEach(polygon => polygon.forEach(ring => visit(ring, true)));
|
|
24
|
+
break;
|
|
25
|
+
case 'GeometryCollection':
|
|
26
|
+
geometry.geometries.forEach(walk);
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
if (input.type === 'FeatureCollection')
|
|
31
|
+
input.features.forEach(feature => walk(feature.geometry));
|
|
32
|
+
else if (input.type === 'Feature')
|
|
33
|
+
walk(input.geometry);
|
|
34
|
+
else
|
|
35
|
+
walk(input);
|
|
36
|
+
}
|
|
37
|
+
/** Drops repeated positions and, for rings, the closing position. */
|
|
38
|
+
function dedupe(line, closed) {
|
|
39
|
+
const out = [];
|
|
40
|
+
for (const p of line)
|
|
41
|
+
if (!out.length || keyOf(out[out.length - 1]) !== keyOf(p))
|
|
42
|
+
out.push(p);
|
|
43
|
+
if (closed && out.length > 1 && keyOf(out[0]) === keyOf(out[out.length - 1]))
|
|
44
|
+
out.pop();
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
function lowest(points) {
|
|
48
|
+
let best = 0;
|
|
49
|
+
points.forEach((p, i) => {
|
|
50
|
+
const q = points[best];
|
|
51
|
+
if (p[0] < q[0] || (p[0] === q[0] && p[1] < q[1]))
|
|
52
|
+
best = i;
|
|
53
|
+
});
|
|
54
|
+
return best;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Finds where lines and rings meet (a position whose neighbors differ between
|
|
58
|
+
* two of its uses, or a line end) and cuts them there into shared arcs.
|
|
59
|
+
*/
|
|
60
|
+
function buildTopology(input) {
|
|
61
|
+
const entries = [];
|
|
62
|
+
eachLine(input, (line, closed) => entries.push([line, dedupe(line, closed), closed]));
|
|
63
|
+
const neighbors = new Map();
|
|
64
|
+
const junctions = new Set();
|
|
65
|
+
const visit = (p, a, b) => {
|
|
66
|
+
const [ka, kb] = [keyOf(a), keyOf(b)];
|
|
67
|
+
const pair = ka < kb ? `${ka}|${kb}` : `${kb}|${ka}`;
|
|
68
|
+
const key = keyOf(p);
|
|
69
|
+
const seen = neighbors.get(key);
|
|
70
|
+
if (seen === undefined)
|
|
71
|
+
neighbors.set(key, pair);
|
|
72
|
+
else if (seen !== pair)
|
|
73
|
+
junctions.add(key);
|
|
74
|
+
};
|
|
75
|
+
for (const [, points, closed] of entries) {
|
|
76
|
+
const n = points.length;
|
|
77
|
+
if (closed) {
|
|
78
|
+
if (n >= 3)
|
|
79
|
+
points.forEach((p, i) => visit(p, points[(i + n - 1) % n], points[(i + 1) % n]));
|
|
80
|
+
}
|
|
81
|
+
else if (n) {
|
|
82
|
+
junctions.add(keyOf(points[0]));
|
|
83
|
+
junctions.add(keyOf(points[n - 1]));
|
|
84
|
+
for (let i = 1; i < n - 1; i++)
|
|
85
|
+
visit(points[i], points[i - 1], points[i + 1]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const arcs = [];
|
|
89
|
+
const index = new Map();
|
|
90
|
+
const add = (arc) => {
|
|
91
|
+
const keys = arc.map(keyOf);
|
|
92
|
+
const forward = keys.join(';');
|
|
93
|
+
const backward = keys.reverse().join(';');
|
|
94
|
+
const reversed = backward < forward;
|
|
95
|
+
const key = reversed ? backward : forward;
|
|
96
|
+
let i = index.get(key);
|
|
97
|
+
if (i === undefined) {
|
|
98
|
+
i = arcs.length;
|
|
99
|
+
index.set(key, i);
|
|
100
|
+
arcs.push(reversed ? arc.slice().reverse() : arc);
|
|
101
|
+
}
|
|
102
|
+
return { index: i, reversed };
|
|
103
|
+
};
|
|
104
|
+
const lines = new WeakMap();
|
|
105
|
+
for (const [line, points, closed] of entries) {
|
|
106
|
+
if (points.length < (closed ? 3 : 2))
|
|
107
|
+
continue;
|
|
108
|
+
let sequence = points;
|
|
109
|
+
if (closed) {
|
|
110
|
+
let start = points.findIndex(p => junctions.has(keyOf(p)));
|
|
111
|
+
if (start === -1)
|
|
112
|
+
start = lowest(points);
|
|
113
|
+
sequence = [...points.slice(start), ...points.slice(0, start), points[start]];
|
|
114
|
+
}
|
|
115
|
+
const refs = [];
|
|
116
|
+
let from = 0;
|
|
117
|
+
for (let i = 1; i < sequence.length; i++) {
|
|
118
|
+
if (i === sequence.length - 1 || junctions.has(keyOf(sequence[i]))) {
|
|
119
|
+
refs.push(add(sequence.slice(from, i + 1)));
|
|
120
|
+
from = i;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
lines.set(line, refs);
|
|
124
|
+
}
|
|
125
|
+
return { arcs, lines };
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Returns a function that rebuilds a line or ring of the topology's input from
|
|
129
|
+
* its arcs, each simplified once, so shared borders stay identical. Lines the
|
|
130
|
+
* topology skipped (too short to draw) return undefined.
|
|
131
|
+
*/
|
|
132
|
+
function simplifyTopology(topology, tolerance) {
|
|
133
|
+
const simplified = new Map();
|
|
134
|
+
const arc = ({ index, reversed }) => {
|
|
135
|
+
let points = simplified.get(index);
|
|
136
|
+
if (!points)
|
|
137
|
+
simplified.set(index, points = (0, simplify_js_1.simplifyLine)(topology.arcs[index], tolerance));
|
|
138
|
+
return reversed ? points.slice().reverse() : points;
|
|
139
|
+
};
|
|
140
|
+
return line => {
|
|
141
|
+
const refs = topology.lines.get(line);
|
|
142
|
+
if (!refs)
|
|
143
|
+
return undefined;
|
|
144
|
+
const out = [];
|
|
145
|
+
for (const ref of refs) {
|
|
146
|
+
const points = arc(ref);
|
|
147
|
+
for (let i = out.length ? 1 : 0; i < points.length; i++)
|
|
148
|
+
out.push(points[i]);
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Every line and ring edge once, as a MultiLineString: a border two polygons
|
|
155
|
+
* share comes out as one line instead of two.
|
|
156
|
+
*/
|
|
157
|
+
function borders(input) {
|
|
158
|
+
return { type: 'MultiLineString', coordinates: buildTopology(input).arcs.map(arc => arc.slice()) };
|
|
159
|
+
}
|