@duoquote/polytini 0.1.0 → 0.1.1
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/LICENSE +21 -21
- package/README.md +120 -120
- package/dist/types/cli.d.cts +2 -2
- package/dist/types/deckgl.d.cts +72 -60
- package/dist/types/flexible.d.cts +92 -80
- package/dist/types/geojson.jsr.d.cts +111 -0
- package/dist/types/google.d.cts +48 -48
- package/dist/types/google.jsr.d.cts +72 -60
- package/dist/types/index.d.cts +109 -87
- package/dist/types/leaflet-auto.d.cts +4 -4
- package/dist/types/leaflet-auto.jsr.d.cts +2 -2
- package/dist/types/leaflet.d.cts +39 -39
- package/dist/types/leaflet.jsr.d.cts +15 -13
- package/dist/types/mapbox.d.cts +50 -50
- package/dist/types/mapbox.jsr.d.cts +5 -0
- package/dist/types/maplibre.d.cts +54 -54
- package/dist/types/maplibre.jsr.d.cts +92 -0
- package/dist/types/openlayers.d.cts +23 -23
- package/dist/types/openlayers.jsr.d.cts +42 -0
- package/dist/types/presets.d.cts +70 -66
- package/dist/types/turf.d.cts +33 -33
- package/dist/types/turf.jsr.d.cts +46 -0
- package/dist/types/wasm.d.cts +42 -33
- package/package.json +200 -200
- package/src/browser.js +15 -15
- package/src/bun.js +4 -4
- package/src/cli-lib.js +160 -160
- package/src/cli.js +7 -7
- package/src/core.js +268 -268
- package/src/deckgl.js +176 -176
- package/src/deno.js +12 -12
- package/src/error.js +15 -15
- package/src/fast.js +179 -179
- package/src/flexible.js +283 -283
- package/src/google.js +70 -70
- package/src/leaflet-auto.js +11 -11
- package/src/leaflet.js +44 -44
- package/src/mapbox.js +10 -10
- package/src/maplibre.js +57 -57
- package/src/node.js +17 -17
- package/src/openlayers.js +172 -172
- package/src/presets.js +242 -242
- package/src/text.js +111 -111
- package/src/turf.js +80 -80
- package/src/wasm-bytes.js +2 -2
- package/src/wasm.js +235 -235
- package/src/web.js +9 -9
- package/types/cli.d.ts +2 -2
- package/types/deckgl.d.ts +72 -60
- package/types/flexible.d.ts +92 -80
- package/types/geojson.jsr.d.ts +111 -0
- package/types/google.d.ts +48 -48
- package/types/google.jsr.d.ts +72 -60
- package/types/index.d.ts +109 -87
- package/types/leaflet-auto.d.ts +4 -4
- package/types/leaflet-auto.jsr.d.ts +2 -2
- package/types/leaflet.d.ts +39 -39
- package/types/leaflet.jsr.d.ts +15 -13
- package/types/mapbox.d.ts +50 -50
- package/types/mapbox.jsr.d.ts +5 -0
- package/types/maplibre.d.ts +54 -54
- package/types/maplibre.jsr.d.ts +92 -0
- package/types/openlayers.d.ts +23 -23
- package/types/openlayers.jsr.d.ts +42 -0
- package/types/presets.d.ts +70 -66
- package/types/turf.d.ts +33 -33
- package/types/turf.jsr.d.ts +46 -0
- package/types/wasm.d.ts +42 -33
package/src/text.js
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
// Byte-buffer paths through TextEncoder/TextDecoder, for runtimes without
|
|
2
|
-
// Node's Buffer where those are fast: Deno. Measured on Deno 2.5 with the
|
|
3
|
-
// benchmark fixtures (see "Platform notes" in the README):
|
|
4
|
-
//
|
|
5
|
-
// - Decode: encodeInto copies the string into a reused Uint8Array, then the
|
|
6
|
-
// loop reads bytes instead of calling charCodeAt. 20 to 35% faster from 100
|
|
7
|
-
// points up. Any non-ASCII character makes `written` differ from the
|
|
8
|
-
// length, and those strings go to the exact slow path, so strict mode stays
|
|
9
|
-
// exact. Under 64 characters the copy costs more than it saves.
|
|
10
|
-
// - Encode: bytes into a reused buffer, then one TextDecoder call. 30 to 60%
|
|
11
|
-
// faster from 1,000 points up, but the call has a fixed cost that made
|
|
12
|
-
// lines of a few hundred points slower than the core's string building, so
|
|
13
|
-
// it starts at 512 points.
|
|
14
|
-
import { opts, st, LIMIT, slowDecode, slowEncode, decodeLoop, flip, encode as coreEncode, encodeFlat as coreEncodeFlat } from './core.js';
|
|
15
|
-
|
|
16
|
-
const te = new TextEncoder(), td = new TextDecoder('latin1');
|
|
17
|
-
// Reused up to 1 MB, like the Buffer paths.
|
|
18
|
-
let kept = new Uint8Array(1 << 14);
|
|
19
|
-
const scratch = (n) => (n <= kept.length ? kept : n <= 1 << 20 ? (kept = new Uint8Array(n)) : new Uint8Array(n));
|
|
20
|
-
|
|
21
|
-
function big(str, f, sw, flat) {
|
|
22
|
-
const len = str.length, b = scratch(len + 8);
|
|
23
|
-
if (te.encodeInto(str, b).written != len) return null;
|
|
24
|
-
// '?' padding: a truncated string stops inside the buffer, past len.
|
|
25
|
-
b[len] = b[len + 1] = 63;
|
|
26
|
-
let t = 0;
|
|
27
|
-
for (let k = 0; k < len; k++) t += (b[k] - 95) >>> 31;
|
|
28
|
-
const out = flat ? new Float64Array(t & ~1) : Array(t >> 1), a = +sw;
|
|
29
|
-
let i = 0, n = 0, lat = 0, lng = 0, bad = 0, c, v, s;
|
|
30
|
-
while (i < len) {
|
|
31
|
-
c = b[i++] - 63;
|
|
32
|
-
bad |= c;
|
|
33
|
-
v = c & 31;
|
|
34
|
-
for (s = 5; c >= 32; s += 5) {
|
|
35
|
-
c = b[i++] - 63;
|
|
36
|
-
bad |= c | (25 - s);
|
|
37
|
-
v |= (c & 31) << s;
|
|
38
|
-
}
|
|
39
|
-
lat += (v >>> 1) ^ -(v & 1);
|
|
40
|
-
c = b[i++] - 63;
|
|
41
|
-
bad |= c;
|
|
42
|
-
v = c & 31;
|
|
43
|
-
for (s = 5; c >= 32; s += 5) {
|
|
44
|
-
c = b[i++] - 63;
|
|
45
|
-
bad |= c | (25 - s);
|
|
46
|
-
v |= (c & 31) << s;
|
|
47
|
-
}
|
|
48
|
-
lng += (v >>> 1) ^ -(v & 1);
|
|
49
|
-
if (flat) {
|
|
50
|
-
out[n + a] = lat / f;
|
|
51
|
-
out[n + 1 - a] = lng / f;
|
|
52
|
-
n += 2;
|
|
53
|
-
} else out[n++] = [lat / f, lng / f];
|
|
54
|
-
}
|
|
55
|
-
return bad & ~63 || i != len ? null : out;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const decodeWith = (str, o, flat) => {
|
|
59
|
-
const f = opts(o);
|
|
60
|
-
if (st.dims != 2 || typeof str != 'string') return slowDecode(str, f, flat);
|
|
61
|
-
const out = (str.length < 64 ? decodeLoop : big)(str, f, st.swap, flat);
|
|
62
|
-
return out ? (st.swap && !flat ? flip(out) : out) : slowDecode(str, f, flat);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
function encodeLoop(src, f, a, flat) {
|
|
66
|
-
const n = flat ? src.length >> 1 : src.length, b = scratch(n * 14 + 8);
|
|
67
|
-
let i = 0, k = 0, la = 0, lb = 0, x, y, p, q, z, pt;
|
|
68
|
-
try {
|
|
69
|
-
for (; i < n; i++) {
|
|
70
|
-
if (flat) {
|
|
71
|
-
x = src[i * 2 + a];
|
|
72
|
-
y = src[i * 2 + 1 - a];
|
|
73
|
-
} else {
|
|
74
|
-
pt = src[i];
|
|
75
|
-
x = pt[a];
|
|
76
|
-
y = pt[1 - a];
|
|
77
|
-
}
|
|
78
|
-
if (typeof x != 'number' || typeof y != 'number' || !((x *= f) < LIMIT && x > -LIMIT && (y *= f) < LIMIT && y > -LIMIT)) return null;
|
|
79
|
-
// Half away from zero, the same way as core.js.
|
|
80
|
-
p = x < 0 ? -((0.5 - x) | 0) : (x + 0.5) | 0;
|
|
81
|
-
q = y < 0 ? -((0.5 - y) | 0) : (y + 0.5) | 0;
|
|
82
|
-
if (x < 0.5 && x > -0.5) p = 0;
|
|
83
|
-
if (y < 0.5 && y > -0.5) q = 0;
|
|
84
|
-
z = p - la;
|
|
85
|
-
z = (z << 1) ^ (z >> 31);
|
|
86
|
-
for (; z >= 32; z >>>= 5) b[k++] = (z & 31) + 95;
|
|
87
|
-
b[k++] = z + 63;
|
|
88
|
-
z = q - lb;
|
|
89
|
-
z = (z << 1) ^ (z >> 31);
|
|
90
|
-
for (; z >= 32; z >>>= 5) b[k++] = (z & 31) + 95;
|
|
91
|
-
b[k++] = z + 63;
|
|
92
|
-
la = p;
|
|
93
|
-
lb = q;
|
|
94
|
-
}
|
|
95
|
-
} catch {
|
|
96
|
-
return null;
|
|
97
|
-
}
|
|
98
|
-
return td.decode(b.subarray(0, k));
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const encodeWith = (src, o, flat, core) => {
|
|
102
|
-
// Short lines: the core's string building is faster than a TextDecoder call.
|
|
103
|
-
if (!(src?.length >= (flat ? 1024 : 512))) return core(src, o);
|
|
104
|
-
const f = opts(o);
|
|
105
|
-
return (st.dims == 2 && f <= 1e6 && (flat ? !(src.length % 2) : Array.isArray(src)) ? encodeLoop(src, f, +st.swap, flat) : null) ?? slowEncode(src, f, flat);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
export const decode = (str, o) => decodeWith(str, o, 0);
|
|
109
|
-
export const decodeFlat = (str, o) => decodeWith(str, o, 1);
|
|
110
|
-
export const encode = (src, o) => encodeWith(src, o, 0, coreEncode);
|
|
111
|
-
export const encodeFlat = (src, o) => encodeWith(src, o, 1, coreEncodeFlat);
|
|
1
|
+
// Byte-buffer paths through TextEncoder/TextDecoder, for runtimes without
|
|
2
|
+
// Node's Buffer where those are fast: Deno. Measured on Deno 2.5 with the
|
|
3
|
+
// benchmark fixtures (see "Platform notes" in the README):
|
|
4
|
+
//
|
|
5
|
+
// - Decode: encodeInto copies the string into a reused Uint8Array, then the
|
|
6
|
+
// loop reads bytes instead of calling charCodeAt. 20 to 35% faster from 100
|
|
7
|
+
// points up. Any non-ASCII character makes `written` differ from the
|
|
8
|
+
// length, and those strings go to the exact slow path, so strict mode stays
|
|
9
|
+
// exact. Under 64 characters the copy costs more than it saves.
|
|
10
|
+
// - Encode: bytes into a reused buffer, then one TextDecoder call. 30 to 60%
|
|
11
|
+
// faster from 1,000 points up, but the call has a fixed cost that made
|
|
12
|
+
// lines of a few hundred points slower than the core's string building, so
|
|
13
|
+
// it starts at 512 points.
|
|
14
|
+
import { opts, st, LIMIT, slowDecode, slowEncode, decodeLoop, flip, encode as coreEncode, encodeFlat as coreEncodeFlat } from './core.js';
|
|
15
|
+
|
|
16
|
+
const te = new TextEncoder(), td = new TextDecoder('latin1');
|
|
17
|
+
// Reused up to 1 MB, like the Buffer paths.
|
|
18
|
+
let kept = new Uint8Array(1 << 14);
|
|
19
|
+
const scratch = (n) => (n <= kept.length ? kept : n <= 1 << 20 ? (kept = new Uint8Array(n)) : new Uint8Array(n));
|
|
20
|
+
|
|
21
|
+
function big(str, f, sw, flat) {
|
|
22
|
+
const len = str.length, b = scratch(len + 8);
|
|
23
|
+
if (te.encodeInto(str, b).written != len) return null;
|
|
24
|
+
// '?' padding: a truncated string stops inside the buffer, past len.
|
|
25
|
+
b[len] = b[len + 1] = 63;
|
|
26
|
+
let t = 0;
|
|
27
|
+
for (let k = 0; k < len; k++) t += (b[k] - 95) >>> 31;
|
|
28
|
+
const out = flat ? new Float64Array(t & ~1) : Array(t >> 1), a = +sw;
|
|
29
|
+
let i = 0, n = 0, lat = 0, lng = 0, bad = 0, c, v, s;
|
|
30
|
+
while (i < len) {
|
|
31
|
+
c = b[i++] - 63;
|
|
32
|
+
bad |= c;
|
|
33
|
+
v = c & 31;
|
|
34
|
+
for (s = 5; c >= 32; s += 5) {
|
|
35
|
+
c = b[i++] - 63;
|
|
36
|
+
bad |= c | (25 - s);
|
|
37
|
+
v |= (c & 31) << s;
|
|
38
|
+
}
|
|
39
|
+
lat += (v >>> 1) ^ -(v & 1);
|
|
40
|
+
c = b[i++] - 63;
|
|
41
|
+
bad |= c;
|
|
42
|
+
v = c & 31;
|
|
43
|
+
for (s = 5; c >= 32; s += 5) {
|
|
44
|
+
c = b[i++] - 63;
|
|
45
|
+
bad |= c | (25 - s);
|
|
46
|
+
v |= (c & 31) << s;
|
|
47
|
+
}
|
|
48
|
+
lng += (v >>> 1) ^ -(v & 1);
|
|
49
|
+
if (flat) {
|
|
50
|
+
out[n + a] = lat / f;
|
|
51
|
+
out[n + 1 - a] = lng / f;
|
|
52
|
+
n += 2;
|
|
53
|
+
} else out[n++] = [lat / f, lng / f];
|
|
54
|
+
}
|
|
55
|
+
return bad & ~63 || i != len ? null : out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const decodeWith = (str, o, flat) => {
|
|
59
|
+
const f = opts(o);
|
|
60
|
+
if (st.dims != 2 || typeof str != 'string') return slowDecode(str, f, flat);
|
|
61
|
+
const out = (str.length < 64 ? decodeLoop : big)(str, f, st.swap, flat);
|
|
62
|
+
return out ? (st.swap && !flat ? flip(out) : out) : slowDecode(str, f, flat);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
function encodeLoop(src, f, a, flat) {
|
|
66
|
+
const n = flat ? src.length >> 1 : src.length, b = scratch(n * 14 + 8);
|
|
67
|
+
let i = 0, k = 0, la = 0, lb = 0, x, y, p, q, z, pt;
|
|
68
|
+
try {
|
|
69
|
+
for (; i < n; i++) {
|
|
70
|
+
if (flat) {
|
|
71
|
+
x = src[i * 2 + a];
|
|
72
|
+
y = src[i * 2 + 1 - a];
|
|
73
|
+
} else {
|
|
74
|
+
pt = src[i];
|
|
75
|
+
x = pt[a];
|
|
76
|
+
y = pt[1 - a];
|
|
77
|
+
}
|
|
78
|
+
if (typeof x != 'number' || typeof y != 'number' || !((x *= f) < LIMIT && x > -LIMIT && (y *= f) < LIMIT && y > -LIMIT)) return null;
|
|
79
|
+
// Half away from zero, the same way as core.js.
|
|
80
|
+
p = x < 0 ? -((0.5 - x) | 0) : (x + 0.5) | 0;
|
|
81
|
+
q = y < 0 ? -((0.5 - y) | 0) : (y + 0.5) | 0;
|
|
82
|
+
if (x < 0.5 && x > -0.5) p = 0;
|
|
83
|
+
if (y < 0.5 && y > -0.5) q = 0;
|
|
84
|
+
z = p - la;
|
|
85
|
+
z = (z << 1) ^ (z >> 31);
|
|
86
|
+
for (; z >= 32; z >>>= 5) b[k++] = (z & 31) + 95;
|
|
87
|
+
b[k++] = z + 63;
|
|
88
|
+
z = q - lb;
|
|
89
|
+
z = (z << 1) ^ (z >> 31);
|
|
90
|
+
for (; z >= 32; z >>>= 5) b[k++] = (z & 31) + 95;
|
|
91
|
+
b[k++] = z + 63;
|
|
92
|
+
la = p;
|
|
93
|
+
lb = q;
|
|
94
|
+
}
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
return td.decode(b.subarray(0, k));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const encodeWith = (src, o, flat, core) => {
|
|
102
|
+
// Short lines: the core's string building is faster than a TextDecoder call.
|
|
103
|
+
if (!(src?.length >= (flat ? 1024 : 512))) return core(src, o);
|
|
104
|
+
const f = opts(o);
|
|
105
|
+
return (st.dims == 2 && f <= 1e6 && (flat ? !(src.length % 2) : Array.isArray(src)) ? encodeLoop(src, f, +st.swap, flat) : null) ?? slowEncode(src, f, flat);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export const decode = (str, o) => decodeWith(str, o, 0);
|
|
109
|
+
export const decodeFlat = (str, o) => decodeWith(str, o, 1);
|
|
110
|
+
export const encode = (src, o) => encodeWith(src, o, 0, coreEncode);
|
|
111
|
+
export const encodeFlat = (src, o) => encodeWith(src, o, 1, coreEncodeFlat);
|
package/src/turf.js
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
// Turf.js interop: decode straight to GeoJSON Features in Turf's shape, and
|
|
2
|
-
// encode whatever Turf hands back. No Turf code is imported; the types come
|
|
3
|
-
// from @turf/helpers and geojson.
|
|
4
|
-
//
|
|
5
|
-
// import { toFeature, fromGeoJSON } from '@duoquote/polytini/turf';
|
|
6
|
-
// const km = turf.length(toFeature(route.geometry, { precision: 6 }));
|
|
7
|
-
// const [simplified] = fromGeoJSON(turf.simplify(toFeature(str), { tolerance: 0.001 }));
|
|
8
|
-
/* @ts-self-types="../types/turf.d.ts" */
|
|
9
|
-
import { decode, encode } from './web.js';
|
|
10
|
-
import { fail } from './error.js';
|
|
11
|
-
|
|
12
|
-
const lngLat = (o) => ({ ...(typeof o == 'number' ? { precision: o } : o), order: 'lnglat' });
|
|
13
|
-
|
|
14
|
-
// [west, south, east, north] over every position of nested arrays.
|
|
15
|
-
function bbox(c) {
|
|
16
|
-
const b = [1 / 0, 1 / 0, -1 / 0, -1 / 0];
|
|
17
|
-
const walk = (a) => {
|
|
18
|
-
if (typeof a[0] == 'number') {
|
|
19
|
-
b[0] = Math.min(b[0], a[0]);
|
|
20
|
-
b[1] = Math.min(b[1], a[1]);
|
|
21
|
-
b[2] = Math.max(b[2], a[0]);
|
|
22
|
-
b[3] = Math.max(b[3], a[1]);
|
|
23
|
-
} else a.forEach(walk);
|
|
24
|
-
};
|
|
25
|
-
walk(c);
|
|
26
|
-
return b;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Turf's feature() shape: type, id, bbox, properties, geometry.
|
|
30
|
-
function feature(geometry, o, i) {
|
|
31
|
-
const f = { type: 'Feature' }, p = o?.properties;
|
|
32
|
-
if (o?.id != null) f.id = Array.isArray(o.id) ? o.id[i] : o.id;
|
|
33
|
-
if (o?.bbox && geometry.coordinates.length) f.bbox = bbox(geometry.coordinates);
|
|
34
|
-
f.properties = (Array.isArray(p) ? p[i] : p) ?? {};
|
|
35
|
-
f.geometry = geometry;
|
|
36
|
-
return f;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** A Feature<LineString> from one encoded line. Options: polytini options plus properties, id and bbox. */
|
|
40
|
-
export const toFeature = (str, o, i) => feature({ type: 'LineString', coordinates: decode(str, lngLat(o)) }, o, i);
|
|
41
|
-
|
|
42
|
-
/** A FeatureCollection<LineString>, one Feature per string. properties and id may be arrays, one per line. */
|
|
43
|
-
export const toFeatureCollection = (strs, o) => ({ type: 'FeatureCollection', features: strs.map((s, i) => toFeature(s, o, i)) });
|
|
44
|
-
|
|
45
|
-
/** A Feature<Polygon> from encoded rings: the outer ring first, then holes, like Google's encoded polygons. */
|
|
46
|
-
export const toPolygonFeature = (rings, o) => feature({ type: 'Polygon', coordinates: rings.map((s) => decode(s, lngLat(o))) }, o, 0);
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Encodes every line in any GeoJSON object: a LineString, each part of a
|
|
50
|
-
* MultiLineString, each ring of a Polygon or MultiPolygon, recursing into
|
|
51
|
-
* Features, FeatureCollections and GeometryCollections. Points are skipped.
|
|
52
|
-
* Always returns an array of strings, in document order.
|
|
53
|
-
*/
|
|
54
|
-
export function fromGeoJSON(g, o) {
|
|
55
|
-
const out = [], ll = lngLat(o);
|
|
56
|
-
const walk = (g) => {
|
|
57
|
-
switch (g?.type) {
|
|
58
|
-
case 'Feature':
|
|
59
|
-
return g.geometry && walk(g.geometry);
|
|
60
|
-
case 'FeatureCollection':
|
|
61
|
-
return g.features.forEach(walk);
|
|
62
|
-
case 'GeometryCollection':
|
|
63
|
-
return g.geometries.forEach(walk);
|
|
64
|
-
case 'LineString':
|
|
65
|
-
return out.push(encode(g.coordinates, ll));
|
|
66
|
-
case 'MultiLineString':
|
|
67
|
-
case 'Polygon':
|
|
68
|
-
return g.coordinates.forEach((c) => out.push(encode(c, ll)));
|
|
69
|
-
case 'MultiPolygon':
|
|
70
|
-
return g.coordinates.forEach((p) => p.forEach((c) => out.push(encode(c, ll))));
|
|
71
|
-
case 'Point':
|
|
72
|
-
case 'MultiPoint':
|
|
73
|
-
return;
|
|
74
|
-
default:
|
|
75
|
-
fail('input', 'Expected GeoJSON');
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
walk(g);
|
|
79
|
-
return out;
|
|
80
|
-
}
|
|
1
|
+
// Turf.js interop: decode straight to GeoJSON Features in Turf's shape, and
|
|
2
|
+
// encode whatever Turf hands back. No Turf code is imported; the types come
|
|
3
|
+
// from @turf/helpers and geojson.
|
|
4
|
+
//
|
|
5
|
+
// import { toFeature, fromGeoJSON } from '@duoquote/polytini/turf';
|
|
6
|
+
// const km = turf.length(toFeature(route.geometry, { precision: 6 }));
|
|
7
|
+
// const [simplified] = fromGeoJSON(turf.simplify(toFeature(str), { tolerance: 0.001 }));
|
|
8
|
+
/* @ts-self-types="../types/turf.jsr.d.ts" */
|
|
9
|
+
import { decode, encode } from './web.js';
|
|
10
|
+
import { fail } from './error.js';
|
|
11
|
+
|
|
12
|
+
const lngLat = (o) => ({ ...(typeof o == 'number' ? { precision: o } : o), order: 'lnglat' });
|
|
13
|
+
|
|
14
|
+
// [west, south, east, north] over every position of nested arrays.
|
|
15
|
+
function bbox(c) {
|
|
16
|
+
const b = [1 / 0, 1 / 0, -1 / 0, -1 / 0];
|
|
17
|
+
const walk = (a) => {
|
|
18
|
+
if (typeof a[0] == 'number') {
|
|
19
|
+
b[0] = Math.min(b[0], a[0]);
|
|
20
|
+
b[1] = Math.min(b[1], a[1]);
|
|
21
|
+
b[2] = Math.max(b[2], a[0]);
|
|
22
|
+
b[3] = Math.max(b[3], a[1]);
|
|
23
|
+
} else a.forEach(walk);
|
|
24
|
+
};
|
|
25
|
+
walk(c);
|
|
26
|
+
return b;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Turf's feature() shape: type, id, bbox, properties, geometry.
|
|
30
|
+
function feature(geometry, o, i) {
|
|
31
|
+
const f = { type: 'Feature' }, p = o?.properties;
|
|
32
|
+
if (o?.id != null) f.id = Array.isArray(o.id) ? o.id[i] : o.id;
|
|
33
|
+
if (o?.bbox && geometry.coordinates.length) f.bbox = bbox(geometry.coordinates);
|
|
34
|
+
f.properties = (Array.isArray(p) ? p[i] : p) ?? {};
|
|
35
|
+
f.geometry = geometry;
|
|
36
|
+
return f;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A Feature<LineString> from one encoded line. Options: polytini options plus properties, id and bbox. */
|
|
40
|
+
export const toFeature = (str, o, i) => feature({ type: 'LineString', coordinates: decode(str, lngLat(o)) }, o, i);
|
|
41
|
+
|
|
42
|
+
/** A FeatureCollection<LineString>, one Feature per string. properties and id may be arrays, one per line. */
|
|
43
|
+
export const toFeatureCollection = (strs, o) => ({ type: 'FeatureCollection', features: strs.map((s, i) => toFeature(s, o, i)) });
|
|
44
|
+
|
|
45
|
+
/** A Feature<Polygon> from encoded rings: the outer ring first, then holes, like Google's encoded polygons. */
|
|
46
|
+
export const toPolygonFeature = (rings, o) => feature({ type: 'Polygon', coordinates: rings.map((s) => decode(s, lngLat(o))) }, o, 0);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Encodes every line in any GeoJSON object: a LineString, each part of a
|
|
50
|
+
* MultiLineString, each ring of a Polygon or MultiPolygon, recursing into
|
|
51
|
+
* Features, FeatureCollections and GeometryCollections. Points are skipped.
|
|
52
|
+
* Always returns an array of strings, in document order.
|
|
53
|
+
*/
|
|
54
|
+
export function fromGeoJSON(g, o) {
|
|
55
|
+
const out = [], ll = lngLat(o);
|
|
56
|
+
const walk = (g) => {
|
|
57
|
+
switch (g?.type) {
|
|
58
|
+
case 'Feature':
|
|
59
|
+
return g.geometry && walk(g.geometry);
|
|
60
|
+
case 'FeatureCollection':
|
|
61
|
+
return g.features.forEach(walk);
|
|
62
|
+
case 'GeometryCollection':
|
|
63
|
+
return g.geometries.forEach(walk);
|
|
64
|
+
case 'LineString':
|
|
65
|
+
return out.push(encode(g.coordinates, ll));
|
|
66
|
+
case 'MultiLineString':
|
|
67
|
+
case 'Polygon':
|
|
68
|
+
return g.coordinates.forEach((c) => out.push(encode(c, ll)));
|
|
69
|
+
case 'MultiPolygon':
|
|
70
|
+
return g.coordinates.forEach((p) => p.forEach((c) => out.push(encode(c, ll))));
|
|
71
|
+
case 'Point':
|
|
72
|
+
case 'MultiPoint':
|
|
73
|
+
return;
|
|
74
|
+
default:
|
|
75
|
+
fail('input', 'Expected GeoJSON');
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
walk(g);
|
|
79
|
+
return out;
|
|
80
|
+
}
|
package/src/wasm-bytes.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// Generated by scripts/build-wasm.js from wasm/polytini.wat. Do not edit.
|
|
2
|
-
export default 'AGFzbQEAAAABLAVgBX9/f3x/AX9gB39/f39/fH8Bf2ABfAF/YAR/f3x/AX9gBn9/f3x/fwF/AwgHAAABAgMDBAUDAQABBzgFBm1lbW9yeQIABmRlY29kZQABC2RlY29kZV9tYW55AAIGZW5jb2RlAAULZW5jb2RlX21hbnkABgqIBgflAQEJfyAAIAFqIQUgAiEGAkADQCAAIAVPDQFBACENA0BBACEIQQAhCQNAIAAgBU8gCUEZS3IEQEF/DwsgAC0AAEE/ayEHIAwgB3IhDCAAQQFqIQAgCCAHQR9xIAl0ciEIIAlBBWohCSAHQSBODQALIAhBAXZBACAIQQFxa3MhCCANBEAgCyAIaiELBSAKIAhqIQoLIA1BAWohDSANQQJJDQALIAYgBEEDdGogCrcgA6M5AwAgBkEIaiAEQQN0ayALtyADozkDACAGQRBqIQYMAAsLIAxBQHEEQEF/DwsgBiACa0EDdgsOACAAIAEgAiADIAQQAAt6AQR/IAMhCiAEQQA2AgACQANAIAcgAk8NASABIAdBAnRqKAIAIQggACAIIAogBSAGEAAhCSAJQQBIBEBBfyAHaw8LIAogCUEDdGohCiAAIAhqIQAgB0EBaiEHIAQgB0ECdGogCiADa0EEdjYCAAwACwsgCiADa0EDdgtXAgF/AXwgAJlEAAAAAAAAwEFjRQRAQYCAgIB4DwsgAKohASAAIAG3oSECIAJEAAAAAAAA4D9mBEAgAUEBag8LIAJEAAAAAAAA4L9lBEAgAUEBaw8LIAELtAEBB38gACABQQN0aiEEIAMhBQJAA0AgACAETw0BIAArAwAgAqIQAyEGIAZBgICAgHhGBEBBfw8LIAoEQCAGIAlrIQcgBiEJBSAGIAhrIQcgBiEICyAKQQFzIQogB0EBdCAHQR91cyEHAkADQCAHQSBJDQEgBSAHQR9xQd8AajoAACAFQQFqIQUgB0EFdiEHDAALCyAFIAdBP2o6AAAgBUEBaiEFIABBCGohAAwACwsgBSADawsMACAAIAEgAiADEAQLegEFfyAEIQoCQANAIAYgAk8NASABIAZBAnRqKAIAIQcgASAGQQJ0aigCBCEIIAAgB0EEdGogCCAHa0EBdCADIAoQBCEJIAlBAEgEQEF/IAZrDwsgCiAJaiEKIAUgBkECdGogCiAEazYCACAGQQFqIQYMAAsLIAogBGsL';
|
|
1
|
+
// Generated by scripts/build-wasm.js from wasm/polytini.wat. Do not edit.
|
|
2
|
+
export default 'AGFzbQEAAAABLAVgBX9/f3x/AX9gB39/f39/fH8Bf2ABfAF/YAR/f3x/AX9gBn9/f3x/fwF/AwgHAAABAgMDBAUDAQABBzgFBm1lbW9yeQIABmRlY29kZQABC2RlY29kZV9tYW55AAIGZW5jb2RlAAULZW5jb2RlX21hbnkABgqIBgflAQEJfyAAIAFqIQUgAiEGAkADQCAAIAVPDQFBACENA0BBACEIQQAhCQNAIAAgBU8gCUEZS3IEQEF/DwsgAC0AAEE/ayEHIAwgB3IhDCAAQQFqIQAgCCAHQR9xIAl0ciEIIAlBBWohCSAHQSBODQALIAhBAXZBACAIQQFxa3MhCCANBEAgCyAIaiELBSAKIAhqIQoLIA1BAWohDSANQQJJDQALIAYgBEEDdGogCrcgA6M5AwAgBkEIaiAEQQN0ayALtyADozkDACAGQRBqIQYMAAsLIAxBQHEEQEF/DwsgBiACa0EDdgsOACAAIAEgAiADIAQQAAt6AQR/IAMhCiAEQQA2AgACQANAIAcgAk8NASABIAdBAnRqKAIAIQggACAIIAogBSAGEAAhCSAJQQBIBEBBfyAHaw8LIAogCUEDdGohCiAAIAhqIQAgB0EBaiEHIAQgB0ECdGogCiADa0EEdjYCAAwACwsgCiADa0EDdgtXAgF/AXwgAJlEAAAAAAAAwEFjRQRAQYCAgIB4DwsgAKohASAAIAG3oSECIAJEAAAAAAAA4D9mBEAgAUEBag8LIAJEAAAAAAAA4L9lBEAgAUEBaw8LIAELtAEBB38gACABQQN0aiEEIAMhBQJAA0AgACAETw0BIAArAwAgAqIQAyEGIAZBgICAgHhGBEBBfw8LIAoEQCAGIAlrIQcgBiEJBSAGIAhrIQcgBiEICyAKQQFzIQogB0EBdCAHQR91cyEHAkADQCAHQSBJDQEgBSAHQR9xQd8AajoAACAFQQFqIQUgB0EFdiEHDAALCyAFIAdBP2o6AAAgBUEBaiEFIABBCGohAAwACwsgBSADawsMACAAIAEgAiADEAQLegEFfyAEIQoCQANAIAYgAk8NASABIAZBAnRqKAIAIQcgASAGQQJ0aigCBCEIIAAgB0EEdGogCCAHa0EBdCADIAoQBCEJIAlBAEgEQEF/IAZrDwsgCiAJaiEKIAUgBkECdGogCiAEazYCACAGQQFqIQYMAAsLIAogBGsL';
|