@vim3d/geojson 0.2.0 → 0.3.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 +37 -7
- package/esm/fetch.js +25 -0
- package/esm/index.js +4 -37
- package/esm/resample.js +135 -0
- package/esm/scene.js +49 -0
- package/esm/simplify.js +50 -0
- package/esm/types.js +1 -0
- package/fetch.d.ts +14 -0
- package/fetch.js +28 -0
- package/index.d.ts +8 -3
- package/index.js +13 -39
- package/package.json +10 -8
- package/resample.d.ts +26 -0
- package/resample.js +141 -0
- package/scene.d.ts +19 -0
- package/scene.js +52 -0
- package/simplify.d.ts +11 -0
- package/simplify.js +54 -0
- package/types.d.ts +42 -0
- package/types.js +2 -0
package/README.md
CHANGED
|
@@ -1,14 +1,44 @@
|
|
|
1
1
|
# @vim3d/geojson
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
neighborhood boundaries stay light enough to render.
|
|
5
|
-
|
|
3
|
+
Fetch GeoJSON when a scene loads and resample it so heavy city and
|
|
4
|
+
neighborhood boundaries stay light enough to render. The data is never bundled
|
|
5
|
+
or committed: the host fetches it at load time and decides whether to keep it.
|
|
6
6
|
|
|
7
7
|
```ts
|
|
8
|
-
import
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { loadGeoJSON, toSceneCurves } from '@vim3d/geojson';
|
|
9
10
|
|
|
10
|
-
const
|
|
11
|
+
const neighborhoods = await loadGeoJSON('https://example.com/neighborhoods.geojson', {
|
|
12
|
+
maxPositions: 5000,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const origin = { lat: 37.7749, lon: -122.4194, elevation: 0 };
|
|
16
|
+
const material = new THREE.LineBasicMaterial();
|
|
17
|
+
for (const curve of neighborhoods ? toSceneCurves(neighborhoods, origin) : []) {
|
|
18
|
+
const points = curve.geometry.vertices!.map(([x, y, z]) => new THREE.Vector3(x, y, z));
|
|
19
|
+
const geometry = new THREE.BufferGeometry().setFromPoints(points);
|
|
20
|
+
scene.add(curve.closed ? new THREE.LineLoop(geometry, material) : new THREE.Line(geometry, material));
|
|
21
|
+
}
|
|
11
22
|
```
|
|
12
23
|
|
|
13
|
-
|
|
14
|
-
|
|
24
|
+
In a browser the URL must send CORS headers for your origin, or be proxied
|
|
25
|
+
through it.
|
|
26
|
+
|
|
27
|
+
A public neighborhoods file of about 1.7 MB (41 polygons, 42,667 positions)
|
|
28
|
+
comes out of `maxPositions: 5000` with all 41 polygons kept.
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
- `loadGeoJSON(url, options)`: fetches with `createFetch()` from
|
|
33
|
+
[@constructive-io/fetch](https://www.npmjs.com/package/@constructive-io/fetch)
|
|
34
|
+
(pass `fetch` to use another), checks the response is GeoJSON, then
|
|
35
|
+
resamples it. Takes `signal` and `headers` too.
|
|
36
|
+
- `resampleGeoJSON(input, { tolerance, maxPositions })`: Douglas-Peucker
|
|
37
|
+
simplification of every line and ring, with no dependencies. `tolerance` is
|
|
38
|
+
in coordinate units (degrees for WGS84). `maxPositions` finds the smallest
|
|
39
|
+
tolerance that fits the budget. Points are kept; polygons, holes and
|
|
40
|
+
features that collapse below four positions are dropped.
|
|
41
|
+
- `toSceneCurves(input, origin, { elevation })`: lines and polygon rings as
|
|
42
|
+
`curve` geometries in the Vim3D `world` frame around `origin` (x east, y up,
|
|
43
|
+
-z north), with each feature's `properties` and `id`.
|
|
44
|
+
- `simplifyLine`, `simplifyRing`, `countPositions`, `bbox`, `eachPosition`.
|
package/esm/fetch.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createFetch } from '@constructive-io/fetch';
|
|
2
|
+
import { resampleGeoJSON } from './resample.js';
|
|
3
|
+
const TYPES = new Set([
|
|
4
|
+
'FeatureCollection', 'Feature', 'Point', 'MultiPoint', 'LineString', 'MultiLineString',
|
|
5
|
+
'Polygon', 'MultiPolygon', 'GeometryCollection',
|
|
6
|
+
]);
|
|
7
|
+
/**
|
|
8
|
+
* Fetches GeoJSON when a scene loads and resamples it. Nothing is cached or
|
|
9
|
+
* stored; the host decides whether to keep the result. Returns null when the
|
|
10
|
+
* whole input collapses at the chosen tolerance.
|
|
11
|
+
*/
|
|
12
|
+
export async function loadGeoJSON(url, options = {}) {
|
|
13
|
+
const fetch = options.fetch ?? createFetch();
|
|
14
|
+
const response = await fetch(url, {
|
|
15
|
+
signal: options.signal,
|
|
16
|
+
headers: { accept: 'application/geo+json, application/json', ...options.headers },
|
|
17
|
+
});
|
|
18
|
+
if (!response.ok)
|
|
19
|
+
throw new Error(`GeoJSON request failed: ${response.status} ${response.statusText} (${url})`);
|
|
20
|
+
const data = await response.json();
|
|
21
|
+
if (!data || typeof data !== 'object' || !TYPES.has(data.type ?? '')) {
|
|
22
|
+
throw new Error(`Not GeoJSON: ${url}`);
|
|
23
|
+
}
|
|
24
|
+
return resampleGeoJSON(data, options);
|
|
25
|
+
}
|
package/esm/index.js
CHANGED
|
@@ -1,38 +1,5 @@
|
|
|
1
1
|
export const name = '@vim3d/geojson';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
let t = lengthSq === 0 ? 0 : ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / lengthSq;
|
|
7
|
-
t = Math.max(0, Math.min(1, t));
|
|
8
|
-
const x = a[0] + t * dx - p[0];
|
|
9
|
-
const y = a[1] + t * dy - p[1];
|
|
10
|
-
return x * x + y * y;
|
|
11
|
-
}
|
|
12
|
-
/** Douglas-Peucker simplification; keeps the first and last positions. */
|
|
13
|
-
export function simplifyLine(points, tolerance) {
|
|
14
|
-
if (points.length <= 2 || !(tolerance > 0))
|
|
15
|
-
return points.slice();
|
|
16
|
-
const toleranceSq = tolerance * tolerance;
|
|
17
|
-
const keep = new Uint8Array(points.length);
|
|
18
|
-
keep[0] = 1;
|
|
19
|
-
keep[points.length - 1] = 1;
|
|
20
|
-
const stack = [[0, points.length - 1]];
|
|
21
|
-
while (stack.length) {
|
|
22
|
-
const [first, last] = stack.pop();
|
|
23
|
-
let maxSq = 0;
|
|
24
|
-
let index = -1;
|
|
25
|
-
for (let i = first + 1; i < last; i++) {
|
|
26
|
-
const d = perpendicularDistanceSq(points[i], points[first], points[last]);
|
|
27
|
-
if (d > maxSq) {
|
|
28
|
-
maxSq = d;
|
|
29
|
-
index = i;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (index !== -1 && maxSq > toleranceSq) {
|
|
33
|
-
keep[index] = 1;
|
|
34
|
-
stack.push([first, index], [index, last]);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return points.filter((_, i) => keep[i] === 1);
|
|
38
|
-
}
|
|
2
|
+
export { loadGeoJSON } from './fetch.js';
|
|
3
|
+
export { bbox, countPositions, eachPosition, resampleGeoJSON } from './resample.js';
|
|
4
|
+
export { toSceneCurves } from './scene.js';
|
|
5
|
+
export { simplifyLine, simplifyRing } from './simplify.js';
|
package/esm/resample.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { simplifyLine, simplifyRing } from './simplify.js';
|
|
2
|
+
function walkGeometry(geometry, visit) {
|
|
3
|
+
if (!geometry)
|
|
4
|
+
return;
|
|
5
|
+
switch (geometry.type) {
|
|
6
|
+
case 'Point':
|
|
7
|
+
visit(geometry.coordinates);
|
|
8
|
+
break;
|
|
9
|
+
case 'MultiPoint':
|
|
10
|
+
case 'LineString':
|
|
11
|
+
geometry.coordinates.forEach(visit);
|
|
12
|
+
break;
|
|
13
|
+
case 'MultiLineString':
|
|
14
|
+
case 'Polygon':
|
|
15
|
+
geometry.coordinates.forEach(line => line.forEach(visit));
|
|
16
|
+
break;
|
|
17
|
+
case 'MultiPolygon':
|
|
18
|
+
geometry.coordinates.forEach(polygon => polygon.forEach(ring => ring.forEach(visit)));
|
|
19
|
+
break;
|
|
20
|
+
case 'GeometryCollection':
|
|
21
|
+
geometry.geometries.forEach(child => walkGeometry(child, visit));
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Calls `visit` for every position in a geometry, feature or collection. */
|
|
26
|
+
export function eachPosition(input, visit) {
|
|
27
|
+
if (input.type === 'FeatureCollection')
|
|
28
|
+
input.features.forEach(feature => walkGeometry(feature.geometry, visit));
|
|
29
|
+
else if (input.type === 'Feature')
|
|
30
|
+
walkGeometry(input.geometry, visit);
|
|
31
|
+
else
|
|
32
|
+
walkGeometry(input, visit);
|
|
33
|
+
}
|
|
34
|
+
export function countPositions(input) {
|
|
35
|
+
let count = 0;
|
|
36
|
+
eachPosition(input, () => count++);
|
|
37
|
+
return count;
|
|
38
|
+
}
|
|
39
|
+
/** [minLon, minLat, maxLon, maxLat], or null when there are no positions. */
|
|
40
|
+
export function bbox(input) {
|
|
41
|
+
let box = null;
|
|
42
|
+
eachPosition(input, ([x, y]) => {
|
|
43
|
+
if (!box)
|
|
44
|
+
box = [x, y, x, y];
|
|
45
|
+
else {
|
|
46
|
+
box[0] = Math.min(box[0], x);
|
|
47
|
+
box[1] = Math.min(box[1], y);
|
|
48
|
+
box[2] = Math.max(box[2], x);
|
|
49
|
+
box[3] = Math.max(box[3], y);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return box;
|
|
53
|
+
}
|
|
54
|
+
function simplifyPolygon(rings, tolerance) {
|
|
55
|
+
const outer = simplifyRing(rings[0] ?? [], tolerance);
|
|
56
|
+
if (!outer)
|
|
57
|
+
return null;
|
|
58
|
+
const holes = rings.slice(1).map(ring => simplifyRing(ring, tolerance)).filter((ring) => !!ring);
|
|
59
|
+
return [outer, ...holes];
|
|
60
|
+
}
|
|
61
|
+
function simplifyGeometry(geometry, tolerance) {
|
|
62
|
+
if (!geometry)
|
|
63
|
+
return null;
|
|
64
|
+
switch (geometry.type) {
|
|
65
|
+
case 'Point':
|
|
66
|
+
case 'MultiPoint':
|
|
67
|
+
return geometry;
|
|
68
|
+
case 'LineString':
|
|
69
|
+
return { type: 'LineString', coordinates: simplifyLine(geometry.coordinates, tolerance) };
|
|
70
|
+
case 'MultiLineString':
|
|
71
|
+
return { type: 'MultiLineString', coordinates: geometry.coordinates.map(line => simplifyLine(line, tolerance)) };
|
|
72
|
+
case 'Polygon': {
|
|
73
|
+
const coordinates = simplifyPolygon(geometry.coordinates, tolerance);
|
|
74
|
+
return coordinates && { type: 'Polygon', coordinates };
|
|
75
|
+
}
|
|
76
|
+
case 'MultiPolygon': {
|
|
77
|
+
const coordinates = geometry.coordinates.map(polygon => simplifyPolygon(polygon, tolerance))
|
|
78
|
+
.filter((polygon) => !!polygon);
|
|
79
|
+
return coordinates.length ? { type: 'MultiPolygon', coordinates } : null;
|
|
80
|
+
}
|
|
81
|
+
case 'GeometryCollection': {
|
|
82
|
+
const geometries = geometry.geometries.map(child => simplifyGeometry(child, tolerance))
|
|
83
|
+
.filter((child) => !!child);
|
|
84
|
+
return geometries.length ? { type: 'GeometryCollection', geometries } : null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function simplifyFeature(feature, tolerance) {
|
|
89
|
+
if (!feature.geometry)
|
|
90
|
+
return feature;
|
|
91
|
+
const geometry = simplifyGeometry(feature.geometry, tolerance);
|
|
92
|
+
return geometry && { ...feature, geometry };
|
|
93
|
+
}
|
|
94
|
+
function simplifyAll(input, tolerance) {
|
|
95
|
+
if (input.type === 'FeatureCollection') {
|
|
96
|
+
const features = input.features.map(feature => simplifyFeature(feature, tolerance))
|
|
97
|
+
.filter((feature) => !!feature);
|
|
98
|
+
return { ...input, features };
|
|
99
|
+
}
|
|
100
|
+
if (input.type === 'Feature')
|
|
101
|
+
return simplifyFeature(input, tolerance);
|
|
102
|
+
return simplifyGeometry(input, tolerance);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Simplifies every line and ring with Douglas-Peucker. Points are kept as is.
|
|
106
|
+
* Polygons and holes that collapse below four positions are dropped, and so
|
|
107
|
+
* are features whose geometry disappears; a lone geometry that collapses
|
|
108
|
+
* returns null.
|
|
109
|
+
*/
|
|
110
|
+
export function resampleGeoJSON(input, options = {}) {
|
|
111
|
+
const minimum = Math.max(0, options.tolerance ?? 0);
|
|
112
|
+
const { maxPositions } = options;
|
|
113
|
+
const first = simplifyAll(input, minimum);
|
|
114
|
+
if (maxPositions === undefined || !first || countPositions(first) <= maxPositions)
|
|
115
|
+
return first;
|
|
116
|
+
const box = bbox(input);
|
|
117
|
+
if (!box)
|
|
118
|
+
return first;
|
|
119
|
+
let lo = minimum;
|
|
120
|
+
let hi = Math.max(Math.hypot(box[2] - box[0], box[3] - box[1]), minimum);
|
|
121
|
+
let best = simplifyAll(input, hi);
|
|
122
|
+
for (let i = 0; i < 40 && hi - lo > hi * 1e-6; i++) {
|
|
123
|
+
const mid = (lo + hi) / 2;
|
|
124
|
+
const candidate = simplifyAll(input, mid);
|
|
125
|
+
if (!candidate)
|
|
126
|
+
hi = mid;
|
|
127
|
+
else if (countPositions(candidate) <= maxPositions) {
|
|
128
|
+
hi = mid;
|
|
129
|
+
best = candidate;
|
|
130
|
+
}
|
|
131
|
+
else
|
|
132
|
+
lo = mid;
|
|
133
|
+
}
|
|
134
|
+
return best;
|
|
135
|
+
}
|
package/esm/scene.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { geodeticToScene } from '@vim3d/core';
|
|
2
|
+
function lines(geometry, emit) {
|
|
3
|
+
if (!geometry)
|
|
4
|
+
return;
|
|
5
|
+
switch (geometry.type) {
|
|
6
|
+
case 'LineString':
|
|
7
|
+
emit(geometry.coordinates, false);
|
|
8
|
+
break;
|
|
9
|
+
case 'MultiLineString':
|
|
10
|
+
geometry.coordinates.forEach(line => emit(line, false));
|
|
11
|
+
break;
|
|
12
|
+
case 'Polygon':
|
|
13
|
+
geometry.coordinates.forEach(ring => emit(ring, true));
|
|
14
|
+
break;
|
|
15
|
+
case 'MultiPolygon':
|
|
16
|
+
geometry.coordinates.forEach(polygon => polygon.forEach(ring => emit(ring, true)));
|
|
17
|
+
break;
|
|
18
|
+
case 'GeometryCollection':
|
|
19
|
+
geometry.geometries.forEach(child => lines(child, emit));
|
|
20
|
+
break;
|
|
21
|
+
default: break;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Turns every line and polygon ring into a scene-space curve around `origin`,
|
|
26
|
+
* using the same local frame as Vim3D `world` (x east, y up, -z north).
|
|
27
|
+
* Positions without a finite longitude and latitude are skipped.
|
|
28
|
+
*/
|
|
29
|
+
export function toSceneCurves(input, origin, options = {}) {
|
|
30
|
+
const curves = [];
|
|
31
|
+
const elevation = options.elevation ?? origin.elevation;
|
|
32
|
+
const add = (geometry, properties, id) => lines(geometry, (line, closed) => {
|
|
33
|
+
const vertices = line.filter(([lon, lat]) => Number.isFinite(lon) && Number.isFinite(lat))
|
|
34
|
+
.map(([lon, lat, height]) => geodeticToScene(origin, lat, lon, Number.isFinite(height) ? height : elevation));
|
|
35
|
+
if (vertices.length < 2)
|
|
36
|
+
return;
|
|
37
|
+
const [first, last] = [vertices[0], vertices[vertices.length - 1]];
|
|
38
|
+
if (closed && first.some((v, i) => v !== last[i]))
|
|
39
|
+
vertices.push(first);
|
|
40
|
+
curves.push({ geometry: { primitive: 'curve', args: [], vertices }, properties, id, closed });
|
|
41
|
+
});
|
|
42
|
+
if (input.type === 'FeatureCollection')
|
|
43
|
+
input.features.forEach(feature => add(feature.geometry, feature.properties, feature.id));
|
|
44
|
+
else if (input.type === 'Feature')
|
|
45
|
+
add(input.geometry, input.properties, input.id);
|
|
46
|
+
else
|
|
47
|
+
add(input, null);
|
|
48
|
+
return curves;
|
|
49
|
+
}
|
package/esm/simplify.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
function segmentDistanceSq(p, a, b) {
|
|
2
|
+
const dx = b[0] - a[0];
|
|
3
|
+
const dy = b[1] - a[1];
|
|
4
|
+
const lengthSq = dx * dx + dy * dy;
|
|
5
|
+
let t = lengthSq === 0 ? 0 : ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / lengthSq;
|
|
6
|
+
t = Math.max(0, Math.min(1, t));
|
|
7
|
+
const x = a[0] + t * dx - p[0];
|
|
8
|
+
const y = a[1] + t * dy - p[1];
|
|
9
|
+
return x * x + y * y;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Douglas-Peucker simplification in coordinate units (degrees for WGS84 GeoJSON).
|
|
13
|
+
* Keeps the first and last positions and the elevation of every kept position.
|
|
14
|
+
*/
|
|
15
|
+
export function simplifyLine(points, tolerance) {
|
|
16
|
+
if (points.length <= 2 || !(tolerance > 0))
|
|
17
|
+
return points.slice();
|
|
18
|
+
const toleranceSq = tolerance * tolerance;
|
|
19
|
+
const keep = new Uint8Array(points.length);
|
|
20
|
+
keep[0] = 1;
|
|
21
|
+
keep[points.length - 1] = 1;
|
|
22
|
+
const stack = [[0, points.length - 1]];
|
|
23
|
+
while (stack.length) {
|
|
24
|
+
const [first, last] = stack.pop();
|
|
25
|
+
let maxSq = 0;
|
|
26
|
+
let index = -1;
|
|
27
|
+
for (let i = first + 1; i < last; i++) {
|
|
28
|
+
const d = segmentDistanceSq(points[i], points[first], points[last]);
|
|
29
|
+
if (d > maxSq) {
|
|
30
|
+
maxSq = d;
|
|
31
|
+
index = i;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (index !== -1 && maxSq > toleranceSq) {
|
|
35
|
+
keep[index] = 1;
|
|
36
|
+
stack.push([first, index], [index, last]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return points.filter((_, i) => keep[i] === 1);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Simplifies a closed linear ring. Returns null when the ring collapses below
|
|
43
|
+
* the four positions GeoJSON requires.
|
|
44
|
+
*/
|
|
45
|
+
export function simplifyRing(ring, tolerance) {
|
|
46
|
+
if (ring.length < 4)
|
|
47
|
+
return null;
|
|
48
|
+
const simplified = simplifyLine(ring, tolerance);
|
|
49
|
+
return simplified.length < 4 ? null : simplified;
|
|
50
|
+
}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/fetch.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ResampleOptions } from './resample.js';
|
|
2
|
+
import type { GeoJSON } from './types.js';
|
|
3
|
+
export interface LoadGeoJSONOptions extends ResampleOptions {
|
|
4
|
+
/** Defaults to `createFetch()` from @constructive-io/fetch. */
|
|
5
|
+
fetch?: typeof globalThis.fetch;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Fetches GeoJSON when a scene loads and resamples it. Nothing is cached or
|
|
11
|
+
* stored; the host decides whether to keep the result. Returns null when the
|
|
12
|
+
* whole input collapses at the chosen tolerance.
|
|
13
|
+
*/
|
|
14
|
+
export declare function loadGeoJSON<T extends GeoJSON = GeoJSON>(url: string | URL, options?: LoadGeoJSONOptions): Promise<T | null>;
|
package/fetch.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadGeoJSON = loadGeoJSON;
|
|
4
|
+
const fetch_1 = require("@constructive-io/fetch");
|
|
5
|
+
const resample_js_1 = require("./resample.js");
|
|
6
|
+
const TYPES = new Set([
|
|
7
|
+
'FeatureCollection', 'Feature', 'Point', 'MultiPoint', 'LineString', 'MultiLineString',
|
|
8
|
+
'Polygon', 'MultiPolygon', 'GeometryCollection',
|
|
9
|
+
]);
|
|
10
|
+
/**
|
|
11
|
+
* Fetches GeoJSON when a scene loads and resamples it. Nothing is cached or
|
|
12
|
+
* stored; the host decides whether to keep the result. Returns null when the
|
|
13
|
+
* whole input collapses at the chosen tolerance.
|
|
14
|
+
*/
|
|
15
|
+
async function loadGeoJSON(url, options = {}) {
|
|
16
|
+
const fetch = options.fetch ?? (0, fetch_1.createFetch)();
|
|
17
|
+
const response = await fetch(url, {
|
|
18
|
+
signal: options.signal,
|
|
19
|
+
headers: { accept: 'application/geo+json, application/json', ...options.headers },
|
|
20
|
+
});
|
|
21
|
+
if (!response.ok)
|
|
22
|
+
throw new Error(`GeoJSON request failed: ${response.status} ${response.statusText} (${url})`);
|
|
23
|
+
const data = await response.json();
|
|
24
|
+
if (!data || typeof data !== 'object' || !TYPES.has(data.type ?? '')) {
|
|
25
|
+
throw new Error(`Not GeoJSON: ${url}`);
|
|
26
|
+
}
|
|
27
|
+
return (0, resample_js_1.resampleGeoJSON)(data, options);
|
|
28
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export declare const name = "@vim3d/geojson";
|
|
2
|
-
export type
|
|
3
|
-
|
|
4
|
-
export
|
|
2
|
+
export type { LoadGeoJSONOptions } from './fetch.js';
|
|
3
|
+
export { loadGeoJSON } from './fetch.js';
|
|
4
|
+
export type { ResampleOptions } from './resample.js';
|
|
5
|
+
export { bbox, countPositions, eachPosition, resampleGeoJSON } from './resample.js';
|
|
6
|
+
export type { SceneCurve, SceneCurveOptions } from './scene.js';
|
|
7
|
+
export { toSceneCurves } from './scene.js';
|
|
8
|
+
export { simplifyLine, simplifyRing } from './simplify.js';
|
|
9
|
+
export type * from './types.js';
|
package/index.js
CHANGED
|
@@ -1,42 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.name = void 0;
|
|
4
|
-
exports.simplifyLine = simplifyLine;
|
|
3
|
+
exports.simplifyRing = exports.simplifyLine = exports.toSceneCurves = exports.resampleGeoJSON = exports.eachPosition = exports.countPositions = exports.bbox = exports.loadGeoJSON = exports.name = void 0;
|
|
5
4
|
exports.name = '@vim3d/geojson';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
function
|
|
18
|
-
if (points.length <= 2 || !(tolerance > 0))
|
|
19
|
-
return points.slice();
|
|
20
|
-
const toleranceSq = tolerance * tolerance;
|
|
21
|
-
const keep = new Uint8Array(points.length);
|
|
22
|
-
keep[0] = 1;
|
|
23
|
-
keep[points.length - 1] = 1;
|
|
24
|
-
const stack = [[0, points.length - 1]];
|
|
25
|
-
while (stack.length) {
|
|
26
|
-
const [first, last] = stack.pop();
|
|
27
|
-
let maxSq = 0;
|
|
28
|
-
let index = -1;
|
|
29
|
-
for (let i = first + 1; i < last; i++) {
|
|
30
|
-
const d = perpendicularDistanceSq(points[i], points[first], points[last]);
|
|
31
|
-
if (d > maxSq) {
|
|
32
|
-
maxSq = d;
|
|
33
|
-
index = i;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (index !== -1 && maxSq > toleranceSq) {
|
|
37
|
-
keep[index] = 1;
|
|
38
|
-
stack.push([first, index], [index, last]);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return points.filter((_, i) => keep[i] === 1);
|
|
42
|
-
}
|
|
5
|
+
var fetch_js_1 = require("./fetch.js");
|
|
6
|
+
Object.defineProperty(exports, "loadGeoJSON", { enumerable: true, get: function () { return fetch_js_1.loadGeoJSON; } });
|
|
7
|
+
var resample_js_1 = require("./resample.js");
|
|
8
|
+
Object.defineProperty(exports, "bbox", { enumerable: true, get: function () { return resample_js_1.bbox; } });
|
|
9
|
+
Object.defineProperty(exports, "countPositions", { enumerable: true, get: function () { return resample_js_1.countPositions; } });
|
|
10
|
+
Object.defineProperty(exports, "eachPosition", { enumerable: true, get: function () { return resample_js_1.eachPosition; } });
|
|
11
|
+
Object.defineProperty(exports, "resampleGeoJSON", { enumerable: true, get: function () { return resample_js_1.resampleGeoJSON; } });
|
|
12
|
+
var scene_js_1 = require("./scene.js");
|
|
13
|
+
Object.defineProperty(exports, "toSceneCurves", { enumerable: true, get: function () { return scene_js_1.toSceneCurves; } });
|
|
14
|
+
var simplify_js_1 = require("./simplify.js");
|
|
15
|
+
Object.defineProperty(exports, "simplifyLine", { enumerable: true, get: function () { return simplify_js_1.simplifyLine; } });
|
|
16
|
+
Object.defineProperty(exports, "simplifyRing", { enumerable: true, get: function () { return simplify_js_1.simplifyRing; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vim3d/geojson",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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",
|
|
@@ -20,17 +20,19 @@
|
|
|
20
20
|
"bugs": {
|
|
21
21
|
"url": "https://github.com/pyramation/vim3d/issues"
|
|
22
22
|
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@constructive-io/fetch": "^1.2.2",
|
|
25
|
+
"@vim3d/core": "^0.6.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"makage": "^0.8.0"
|
|
29
|
+
},
|
|
23
30
|
"scripts": {
|
|
24
31
|
"clean": "makage clean",
|
|
25
|
-
"prepack": "npm run build",
|
|
26
32
|
"build": "makage build && cp scripts/esm-package.json dist/esm/package.json",
|
|
27
33
|
"build:dev": "makage build --dev && cp scripts/esm-package.json dist/esm/package.json",
|
|
28
34
|
"lint": "eslint . --fix",
|
|
29
35
|
"test": "jest",
|
|
30
36
|
"test:watch": "jest --watch"
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
"makage": "^0.8.0"
|
|
34
|
-
},
|
|
35
|
-
"gitHead": "b641c4ae556dd92d80d858cfe02d7e47f5af89bb"
|
|
36
|
-
}
|
|
37
|
+
}
|
|
38
|
+
}
|
package/resample.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { GeoJSON, Position } from './types.js';
|
|
2
|
+
export interface ResampleOptions {
|
|
3
|
+
/** Douglas-Peucker tolerance in coordinate units (degrees for WGS84). */
|
|
4
|
+
tolerance?: number;
|
|
5
|
+
/**
|
|
6
|
+
* Target number of positions kept. The smallest tolerance that fits is found
|
|
7
|
+
* by bisection; `tolerance`, when given, is the minimum used. Points are
|
|
8
|
+
* never dropped, so when they alone exceed the target the result is the
|
|
9
|
+
* smallest reachable, which can still be over it.
|
|
10
|
+
*/
|
|
11
|
+
maxPositions?: number;
|
|
12
|
+
}
|
|
13
|
+
type Collect = (position: Position) => void;
|
|
14
|
+
/** Calls `visit` for every position in a geometry, feature or collection. */
|
|
15
|
+
export declare function eachPosition(input: GeoJSON, visit: Collect): void;
|
|
16
|
+
export declare function countPositions(input: GeoJSON): number;
|
|
17
|
+
/** [minLon, minLat, maxLon, maxLat], or null when there are no positions. */
|
|
18
|
+
export declare function bbox(input: GeoJSON): [number, number, number, number] | null;
|
|
19
|
+
/**
|
|
20
|
+
* Simplifies every line and ring with Douglas-Peucker. Points are kept as is.
|
|
21
|
+
* Polygons and holes that collapse below four positions are dropped, and so
|
|
22
|
+
* are features whose geometry disappears; a lone geometry that collapses
|
|
23
|
+
* returns null.
|
|
24
|
+
*/
|
|
25
|
+
export declare function resampleGeoJSON<T extends GeoJSON>(input: T, options?: ResampleOptions): T | null;
|
|
26
|
+
export {};
|
package/resample.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.eachPosition = eachPosition;
|
|
4
|
+
exports.countPositions = countPositions;
|
|
5
|
+
exports.bbox = bbox;
|
|
6
|
+
exports.resampleGeoJSON = resampleGeoJSON;
|
|
7
|
+
const simplify_js_1 = require("./simplify.js");
|
|
8
|
+
function walkGeometry(geometry, visit) {
|
|
9
|
+
if (!geometry)
|
|
10
|
+
return;
|
|
11
|
+
switch (geometry.type) {
|
|
12
|
+
case 'Point':
|
|
13
|
+
visit(geometry.coordinates);
|
|
14
|
+
break;
|
|
15
|
+
case 'MultiPoint':
|
|
16
|
+
case 'LineString':
|
|
17
|
+
geometry.coordinates.forEach(visit);
|
|
18
|
+
break;
|
|
19
|
+
case 'MultiLineString':
|
|
20
|
+
case 'Polygon':
|
|
21
|
+
geometry.coordinates.forEach(line => line.forEach(visit));
|
|
22
|
+
break;
|
|
23
|
+
case 'MultiPolygon':
|
|
24
|
+
geometry.coordinates.forEach(polygon => polygon.forEach(ring => ring.forEach(visit)));
|
|
25
|
+
break;
|
|
26
|
+
case 'GeometryCollection':
|
|
27
|
+
geometry.geometries.forEach(child => walkGeometry(child, visit));
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Calls `visit` for every position in a geometry, feature or collection. */
|
|
32
|
+
function eachPosition(input, visit) {
|
|
33
|
+
if (input.type === 'FeatureCollection')
|
|
34
|
+
input.features.forEach(feature => walkGeometry(feature.geometry, visit));
|
|
35
|
+
else if (input.type === 'Feature')
|
|
36
|
+
walkGeometry(input.geometry, visit);
|
|
37
|
+
else
|
|
38
|
+
walkGeometry(input, visit);
|
|
39
|
+
}
|
|
40
|
+
function countPositions(input) {
|
|
41
|
+
let count = 0;
|
|
42
|
+
eachPosition(input, () => count++);
|
|
43
|
+
return count;
|
|
44
|
+
}
|
|
45
|
+
/** [minLon, minLat, maxLon, maxLat], or null when there are no positions. */
|
|
46
|
+
function bbox(input) {
|
|
47
|
+
let box = null;
|
|
48
|
+
eachPosition(input, ([x, y]) => {
|
|
49
|
+
if (!box)
|
|
50
|
+
box = [x, y, x, y];
|
|
51
|
+
else {
|
|
52
|
+
box[0] = Math.min(box[0], x);
|
|
53
|
+
box[1] = Math.min(box[1], y);
|
|
54
|
+
box[2] = Math.max(box[2], x);
|
|
55
|
+
box[3] = Math.max(box[3], y);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return box;
|
|
59
|
+
}
|
|
60
|
+
function simplifyPolygon(rings, tolerance) {
|
|
61
|
+
const outer = (0, simplify_js_1.simplifyRing)(rings[0] ?? [], tolerance);
|
|
62
|
+
if (!outer)
|
|
63
|
+
return null;
|
|
64
|
+
const holes = rings.slice(1).map(ring => (0, simplify_js_1.simplifyRing)(ring, tolerance)).filter((ring) => !!ring);
|
|
65
|
+
return [outer, ...holes];
|
|
66
|
+
}
|
|
67
|
+
function simplifyGeometry(geometry, tolerance) {
|
|
68
|
+
if (!geometry)
|
|
69
|
+
return null;
|
|
70
|
+
switch (geometry.type) {
|
|
71
|
+
case 'Point':
|
|
72
|
+
case 'MultiPoint':
|
|
73
|
+
return geometry;
|
|
74
|
+
case 'LineString':
|
|
75
|
+
return { type: 'LineString', coordinates: (0, simplify_js_1.simplifyLine)(geometry.coordinates, tolerance) };
|
|
76
|
+
case 'MultiLineString':
|
|
77
|
+
return { type: 'MultiLineString', coordinates: geometry.coordinates.map(line => (0, simplify_js_1.simplifyLine)(line, tolerance)) };
|
|
78
|
+
case 'Polygon': {
|
|
79
|
+
const coordinates = simplifyPolygon(geometry.coordinates, tolerance);
|
|
80
|
+
return coordinates && { type: 'Polygon', coordinates };
|
|
81
|
+
}
|
|
82
|
+
case 'MultiPolygon': {
|
|
83
|
+
const coordinates = geometry.coordinates.map(polygon => simplifyPolygon(polygon, tolerance))
|
|
84
|
+
.filter((polygon) => !!polygon);
|
|
85
|
+
return coordinates.length ? { type: 'MultiPolygon', coordinates } : null;
|
|
86
|
+
}
|
|
87
|
+
case 'GeometryCollection': {
|
|
88
|
+
const geometries = geometry.geometries.map(child => simplifyGeometry(child, tolerance))
|
|
89
|
+
.filter((child) => !!child);
|
|
90
|
+
return geometries.length ? { type: 'GeometryCollection', geometries } : null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function simplifyFeature(feature, tolerance) {
|
|
95
|
+
if (!feature.geometry)
|
|
96
|
+
return feature;
|
|
97
|
+
const geometry = simplifyGeometry(feature.geometry, tolerance);
|
|
98
|
+
return geometry && { ...feature, geometry };
|
|
99
|
+
}
|
|
100
|
+
function simplifyAll(input, tolerance) {
|
|
101
|
+
if (input.type === 'FeatureCollection') {
|
|
102
|
+
const features = input.features.map(feature => simplifyFeature(feature, tolerance))
|
|
103
|
+
.filter((feature) => !!feature);
|
|
104
|
+
return { ...input, features };
|
|
105
|
+
}
|
|
106
|
+
if (input.type === 'Feature')
|
|
107
|
+
return simplifyFeature(input, tolerance);
|
|
108
|
+
return simplifyGeometry(input, tolerance);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Simplifies every line and ring with Douglas-Peucker. Points are kept as is.
|
|
112
|
+
* Polygons and holes that collapse below four positions are dropped, and so
|
|
113
|
+
* are features whose geometry disappears; a lone geometry that collapses
|
|
114
|
+
* returns null.
|
|
115
|
+
*/
|
|
116
|
+
function resampleGeoJSON(input, options = {}) {
|
|
117
|
+
const minimum = Math.max(0, options.tolerance ?? 0);
|
|
118
|
+
const { maxPositions } = options;
|
|
119
|
+
const first = simplifyAll(input, minimum);
|
|
120
|
+
if (maxPositions === undefined || !first || countPositions(first) <= maxPositions)
|
|
121
|
+
return first;
|
|
122
|
+
const box = bbox(input);
|
|
123
|
+
if (!box)
|
|
124
|
+
return first;
|
|
125
|
+
let lo = minimum;
|
|
126
|
+
let hi = Math.max(Math.hypot(box[2] - box[0], box[3] - box[1]), minimum);
|
|
127
|
+
let best = simplifyAll(input, hi);
|
|
128
|
+
for (let i = 0; i < 40 && hi - lo > hi * 1e-6; i++) {
|
|
129
|
+
const mid = (lo + hi) / 2;
|
|
130
|
+
const candidate = simplifyAll(input, mid);
|
|
131
|
+
if (!candidate)
|
|
132
|
+
hi = mid;
|
|
133
|
+
else if (countPositions(candidate) <= maxPositions) {
|
|
134
|
+
hi = mid;
|
|
135
|
+
best = candidate;
|
|
136
|
+
}
|
|
137
|
+
else
|
|
138
|
+
lo = mid;
|
|
139
|
+
}
|
|
140
|
+
return best;
|
|
141
|
+
}
|
package/scene.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type GeometrySnapshot, type GeoOrigin } from '@vim3d/core';
|
|
2
|
+
import type { GeoJSON, Properties } from './types.js';
|
|
3
|
+
export interface SceneCurve {
|
|
4
|
+
/** A curve geometry @vim3d/threejs `createGeometry` draws as a line. */
|
|
5
|
+
readonly geometry: GeometrySnapshot;
|
|
6
|
+
readonly properties: Properties;
|
|
7
|
+
readonly id?: string | number;
|
|
8
|
+
readonly closed: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface SceneCurveOptions {
|
|
11
|
+
/** Elevation in metres for positions without one; defaults to the origin's. */
|
|
12
|
+
elevation?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Turns every line and polygon ring into a scene-space curve around `origin`,
|
|
16
|
+
* using the same local frame as Vim3D `world` (x east, y up, -z north).
|
|
17
|
+
* Positions without a finite longitude and latitude are skipped.
|
|
18
|
+
*/
|
|
19
|
+
export declare function toSceneCurves(input: GeoJSON, origin: GeoOrigin, options?: SceneCurveOptions): SceneCurve[];
|
package/scene.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toSceneCurves = toSceneCurves;
|
|
4
|
+
const core_1 = require("@vim3d/core");
|
|
5
|
+
function lines(geometry, emit) {
|
|
6
|
+
if (!geometry)
|
|
7
|
+
return;
|
|
8
|
+
switch (geometry.type) {
|
|
9
|
+
case 'LineString':
|
|
10
|
+
emit(geometry.coordinates, false);
|
|
11
|
+
break;
|
|
12
|
+
case 'MultiLineString':
|
|
13
|
+
geometry.coordinates.forEach(line => emit(line, false));
|
|
14
|
+
break;
|
|
15
|
+
case 'Polygon':
|
|
16
|
+
geometry.coordinates.forEach(ring => emit(ring, true));
|
|
17
|
+
break;
|
|
18
|
+
case 'MultiPolygon':
|
|
19
|
+
geometry.coordinates.forEach(polygon => polygon.forEach(ring => emit(ring, true)));
|
|
20
|
+
break;
|
|
21
|
+
case 'GeometryCollection':
|
|
22
|
+
geometry.geometries.forEach(child => lines(child, emit));
|
|
23
|
+
break;
|
|
24
|
+
default: break;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Turns every line and polygon ring into a scene-space curve around `origin`,
|
|
29
|
+
* using the same local frame as Vim3D `world` (x east, y up, -z north).
|
|
30
|
+
* Positions without a finite longitude and latitude are skipped.
|
|
31
|
+
*/
|
|
32
|
+
function toSceneCurves(input, origin, options = {}) {
|
|
33
|
+
const curves = [];
|
|
34
|
+
const elevation = options.elevation ?? origin.elevation;
|
|
35
|
+
const add = (geometry, properties, id) => lines(geometry, (line, closed) => {
|
|
36
|
+
const vertices = line.filter(([lon, lat]) => Number.isFinite(lon) && Number.isFinite(lat))
|
|
37
|
+
.map(([lon, lat, height]) => (0, core_1.geodeticToScene)(origin, lat, lon, Number.isFinite(height) ? height : elevation));
|
|
38
|
+
if (vertices.length < 2)
|
|
39
|
+
return;
|
|
40
|
+
const [first, last] = [vertices[0], vertices[vertices.length - 1]];
|
|
41
|
+
if (closed && first.some((v, i) => v !== last[i]))
|
|
42
|
+
vertices.push(first);
|
|
43
|
+
curves.push({ geometry: { primitive: 'curve', args: [], vertices }, properties, id, closed });
|
|
44
|
+
});
|
|
45
|
+
if (input.type === 'FeatureCollection')
|
|
46
|
+
input.features.forEach(feature => add(feature.geometry, feature.properties, feature.id));
|
|
47
|
+
else if (input.type === 'Feature')
|
|
48
|
+
add(input.geometry, input.properties, input.id);
|
|
49
|
+
else
|
|
50
|
+
add(input, null);
|
|
51
|
+
return curves;
|
|
52
|
+
}
|
package/simplify.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Position } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Douglas-Peucker simplification in coordinate units (degrees for WGS84 GeoJSON).
|
|
4
|
+
* Keeps the first and last positions and the elevation of every kept position.
|
|
5
|
+
*/
|
|
6
|
+
export declare function simplifyLine<T extends Position>(points: readonly T[], tolerance: number): T[];
|
|
7
|
+
/**
|
|
8
|
+
* Simplifies a closed linear ring. Returns null when the ring collapses below
|
|
9
|
+
* the four positions GeoJSON requires.
|
|
10
|
+
*/
|
|
11
|
+
export declare function simplifyRing<T extends Position>(ring: readonly T[], tolerance: number): T[] | null;
|
package/simplify.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.simplifyLine = simplifyLine;
|
|
4
|
+
exports.simplifyRing = simplifyRing;
|
|
5
|
+
function segmentDistanceSq(p, a, b) {
|
|
6
|
+
const dx = b[0] - a[0];
|
|
7
|
+
const dy = b[1] - a[1];
|
|
8
|
+
const lengthSq = dx * dx + dy * dy;
|
|
9
|
+
let t = lengthSq === 0 ? 0 : ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / lengthSq;
|
|
10
|
+
t = Math.max(0, Math.min(1, t));
|
|
11
|
+
const x = a[0] + t * dx - p[0];
|
|
12
|
+
const y = a[1] + t * dy - p[1];
|
|
13
|
+
return x * x + y * y;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Douglas-Peucker simplification in coordinate units (degrees for WGS84 GeoJSON).
|
|
17
|
+
* Keeps the first and last positions and the elevation of every kept position.
|
|
18
|
+
*/
|
|
19
|
+
function simplifyLine(points, tolerance) {
|
|
20
|
+
if (points.length <= 2 || !(tolerance > 0))
|
|
21
|
+
return points.slice();
|
|
22
|
+
const toleranceSq = tolerance * tolerance;
|
|
23
|
+
const keep = new Uint8Array(points.length);
|
|
24
|
+
keep[0] = 1;
|
|
25
|
+
keep[points.length - 1] = 1;
|
|
26
|
+
const stack = [[0, points.length - 1]];
|
|
27
|
+
while (stack.length) {
|
|
28
|
+
const [first, last] = stack.pop();
|
|
29
|
+
let maxSq = 0;
|
|
30
|
+
let index = -1;
|
|
31
|
+
for (let i = first + 1; i < last; i++) {
|
|
32
|
+
const d = segmentDistanceSq(points[i], points[first], points[last]);
|
|
33
|
+
if (d > maxSq) {
|
|
34
|
+
maxSq = d;
|
|
35
|
+
index = i;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (index !== -1 && maxSq > toleranceSq) {
|
|
39
|
+
keep[index] = 1;
|
|
40
|
+
stack.push([first, index], [index, last]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return points.filter((_, i) => keep[i] === 1);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Simplifies a closed linear ring. Returns null when the ring collapses below
|
|
47
|
+
* the four positions GeoJSON requires.
|
|
48
|
+
*/
|
|
49
|
+
function simplifyRing(ring, tolerance) {
|
|
50
|
+
if (ring.length < 4)
|
|
51
|
+
return null;
|
|
52
|
+
const simplified = simplifyLine(ring, tolerance);
|
|
53
|
+
return simplified.length < 4 ? null : simplified;
|
|
54
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type Position = [number, number] | [number, number, number];
|
|
2
|
+
export interface Point {
|
|
3
|
+
type: 'Point';
|
|
4
|
+
coordinates: Position;
|
|
5
|
+
}
|
|
6
|
+
export interface MultiPoint {
|
|
7
|
+
type: 'MultiPoint';
|
|
8
|
+
coordinates: Position[];
|
|
9
|
+
}
|
|
10
|
+
export interface LineString {
|
|
11
|
+
type: 'LineString';
|
|
12
|
+
coordinates: Position[];
|
|
13
|
+
}
|
|
14
|
+
export interface MultiLineString {
|
|
15
|
+
type: 'MultiLineString';
|
|
16
|
+
coordinates: Position[][];
|
|
17
|
+
}
|
|
18
|
+
export interface Polygon {
|
|
19
|
+
type: 'Polygon';
|
|
20
|
+
coordinates: Position[][];
|
|
21
|
+
}
|
|
22
|
+
export interface MultiPolygon {
|
|
23
|
+
type: 'MultiPolygon';
|
|
24
|
+
coordinates: Position[][][];
|
|
25
|
+
}
|
|
26
|
+
export interface GeometryCollection {
|
|
27
|
+
type: 'GeometryCollection';
|
|
28
|
+
geometries: Geometry[];
|
|
29
|
+
}
|
|
30
|
+
export type Geometry = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon | GeometryCollection;
|
|
31
|
+
export type Properties = Record<string, unknown> | null;
|
|
32
|
+
export interface Feature<G extends Geometry | null = Geometry | null> {
|
|
33
|
+
type: 'Feature';
|
|
34
|
+
id?: string | number;
|
|
35
|
+
geometry: G;
|
|
36
|
+
properties: Properties;
|
|
37
|
+
}
|
|
38
|
+
export interface FeatureCollection {
|
|
39
|
+
type: 'FeatureCollection';
|
|
40
|
+
features: Feature[];
|
|
41
|
+
}
|
|
42
|
+
export type GeoJSON = Geometry | Feature | FeatureCollection;
|
package/types.js
ADDED