geojson_union_wasm 0.1.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/LICENSE.txt +7 -0
- package/README.md +21 -0
- package/geojson_union_wasm.d.ts +13 -0
- package/geojson_union_wasm.js +5 -0
- package/geojson_union_wasm_bg.js +170 -0
- package/geojson_union_wasm_bg.wasm +0 -0
- package/package.json +22 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2025 https://github.com/lart2150
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# GeoJSON Union WASM
|
|
2
|
+
|
|
3
|
+
This simple WASM wrapper for geo::algorithm::unary_union to desolve or create the union of one or more GeoJSON strings. I created this because I was trying to combine very large GeoJSON files that took @turf/union longer then I wanted to find the union of. This library for my usecase is approximtely 18 times faster than @turf/union^7.3.1 when creating the uinion of [cb_2018_us_county_500k](https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html).
|
|
4
|
+
|
|
5
|
+
## Build
|
|
6
|
+
`wasm-pack build --target bundler`
|
|
7
|
+
|
|
8
|
+
The built code will be in the pkg folder
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
### Basic dissolve
|
|
13
|
+
```
|
|
14
|
+
const unionString = geojson_union([
|
|
15
|
+
`{"features":[
|
|
16
|
+
{"geometry":{"coordinates":[[[4,0],[4,4],[8,4],[8,0],[4,0]]],"type":"Polygon"},"properties":{},"type":"Feature"},
|
|
17
|
+
{"geometry":{"coordinates":[[[0,0],[0,4],[4,4],[4,0],[0,0]]],"type":"Polygon"},"properties":{},"type":"Feature"}],
|
|
18
|
+
"type":"FeatureCollection"}`
|
|
19
|
+
]);
|
|
20
|
+
console.log('Union result:', JSON.parse(unionString));
|
|
21
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Takes an array of GeoJSON strings, dissolves (unions) all Polygon and MultiPolygon returning a GeoJSON string.
|
|
6
|
+
* const unionString = geojson_union([
|
|
7
|
+
* `{"features":[
|
|
8
|
+
* {"geometry":{"coordinates":[[[4,0],[4,4],[8,4],[8,0],[4,0]]],"type":"Polygon"},"properties":{},"type":"Feature"},
|
|
9
|
+
* {"geometry":{"coordinates":[[[0,0],[0,4],[4,4],[4,0],[0,0]]],"type":"Polygon"},"properties":{},"type":"Feature"}],
|
|
10
|
+
* "type":"FeatureCollection"}`
|
|
11
|
+
* ]);
|
|
12
|
+
*/
|
|
13
|
+
export function geojson_union(input: Array<any>): string;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
let wasm;
|
|
2
|
+
export function __wbg_set_wasm(val) {
|
|
3
|
+
wasm = val;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
let cachedDataViewMemory0 = null;
|
|
7
|
+
function getDataViewMemory0() {
|
|
8
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
9
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
10
|
+
}
|
|
11
|
+
return cachedDataViewMemory0;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getStringFromWasm0(ptr, len) {
|
|
15
|
+
ptr = ptr >>> 0;
|
|
16
|
+
return decodeText(ptr, len);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let cachedUint8ArrayMemory0 = null;
|
|
20
|
+
function getUint8ArrayMemory0() {
|
|
21
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
22
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
23
|
+
}
|
|
24
|
+
return cachedUint8ArrayMemory0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isLikeNone(x) {
|
|
28
|
+
return x === undefined || x === null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
32
|
+
if (realloc === undefined) {
|
|
33
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
34
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
35
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
36
|
+
WASM_VECTOR_LEN = buf.length;
|
|
37
|
+
return ptr;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let len = arg.length;
|
|
41
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
42
|
+
|
|
43
|
+
const mem = getUint8ArrayMemory0();
|
|
44
|
+
|
|
45
|
+
let offset = 0;
|
|
46
|
+
|
|
47
|
+
for (; offset < len; offset++) {
|
|
48
|
+
const code = arg.charCodeAt(offset);
|
|
49
|
+
if (code > 0x7F) break;
|
|
50
|
+
mem[ptr + offset] = code;
|
|
51
|
+
}
|
|
52
|
+
if (offset !== len) {
|
|
53
|
+
if (offset !== 0) {
|
|
54
|
+
arg = arg.slice(offset);
|
|
55
|
+
}
|
|
56
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
57
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
58
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
59
|
+
|
|
60
|
+
offset += ret.written;
|
|
61
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
WASM_VECTOR_LEN = offset;
|
|
65
|
+
return ptr;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function takeFromExternrefTable0(idx) {
|
|
69
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
70
|
+
wasm.__externref_table_dealloc(idx);
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
75
|
+
cachedTextDecoder.decode();
|
|
76
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
77
|
+
let numBytesDecoded = 0;
|
|
78
|
+
function decodeText(ptr, len) {
|
|
79
|
+
numBytesDecoded += len;
|
|
80
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
81
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
82
|
+
cachedTextDecoder.decode();
|
|
83
|
+
numBytesDecoded = len;
|
|
84
|
+
}
|
|
85
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const cachedTextEncoder = new TextEncoder();
|
|
89
|
+
|
|
90
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
91
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
92
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
93
|
+
view.set(buf);
|
|
94
|
+
return {
|
|
95
|
+
read: arg.length,
|
|
96
|
+
written: buf.length
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let WASM_VECTOR_LEN = 0;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Takes an array of GeoJSON strings, dissolves (unions) all Polygon and MultiPolygon returning a GeoJSON string.
|
|
105
|
+
* const unionString = geojson_union([
|
|
106
|
+
* `{"features":[
|
|
107
|
+
* {"geometry":{"coordinates":[[[4,0],[4,4],[8,4],[8,0],[4,0]]],"type":"Polygon"},"properties":{},"type":"Feature"},
|
|
108
|
+
* {"geometry":{"coordinates":[[[0,0],[0,4],[4,4],[4,0],[0,0]]],"type":"Polygon"},"properties":{},"type":"Feature"}],
|
|
109
|
+
* "type":"FeatureCollection"}`
|
|
110
|
+
* ]);
|
|
111
|
+
* @param {Array<any>} input
|
|
112
|
+
* @returns {string}
|
|
113
|
+
*/
|
|
114
|
+
export function geojson_union(input) {
|
|
115
|
+
let deferred2_0;
|
|
116
|
+
let deferred2_1;
|
|
117
|
+
try {
|
|
118
|
+
const ret = wasm.geojson_union(input);
|
|
119
|
+
var ptr1 = ret[0];
|
|
120
|
+
var len1 = ret[1];
|
|
121
|
+
if (ret[3]) {
|
|
122
|
+
ptr1 = 0; len1 = 0;
|
|
123
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
124
|
+
}
|
|
125
|
+
deferred2_0 = ptr1;
|
|
126
|
+
deferred2_1 = len1;
|
|
127
|
+
return getStringFromWasm0(ptr1, len1);
|
|
128
|
+
} finally {
|
|
129
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function __wbg___wbindgen_string_get_a2a31e16edf96e42(arg0, arg1) {
|
|
134
|
+
const obj = arg1;
|
|
135
|
+
const ret = typeof(obj) === 'string' ? obj : undefined;
|
|
136
|
+
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
137
|
+
var len1 = WASM_VECTOR_LEN;
|
|
138
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
139
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export function __wbg___wbindgen_throw_dd24417ed36fc46e(arg0, arg1) {
|
|
143
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
export function __wbg_get_6b7bd52aca3f9671(arg0, arg1) {
|
|
147
|
+
const ret = arg0[arg1 >>> 0];
|
|
148
|
+
return ret;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export function __wbg_length_d45040a40c570362(arg0) {
|
|
152
|
+
const ret = arg0.length;
|
|
153
|
+
return ret;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export function __wbindgen_cast_2241b6af4c4b2941(arg0, arg1) {
|
|
157
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
158
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
159
|
+
return ret;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export function __wbindgen_init_externref_table() {
|
|
163
|
+
const table = wasm.__wbindgen_externrefs;
|
|
164
|
+
const offset = table.grow(4);
|
|
165
|
+
table.set(0, undefined);
|
|
166
|
+
table.set(offset + 0, undefined);
|
|
167
|
+
table.set(offset + 1, null);
|
|
168
|
+
table.set(offset + 2, true);
|
|
169
|
+
table.set(offset + 3, false);
|
|
170
|
+
};
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "geojson_union_wasm",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/lart2150/geojson_union_wasm"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"geojson_union_wasm_bg.wasm",
|
|
12
|
+
"geojson_union_wasm.js",
|
|
13
|
+
"geojson_union_wasm_bg.js",
|
|
14
|
+
"geojson_union_wasm.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"main": "geojson_union_wasm.js",
|
|
17
|
+
"types": "geojson_union_wasm.d.ts",
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./geojson_union_wasm.js",
|
|
20
|
+
"./snippets/*"
|
|
21
|
+
]
|
|
22
|
+
}
|