wkt-parse-and-geojson 1.0.1 → 1.0.2
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/dist/index.cjs.js +29 -29
- package/dist/index.esm.js +29 -29
- package/dist/index.umd.js +29 -29
- package/dist/wkt-parser.d.ts +2 -2
- package/package.json +17 -7
package/dist/index.cjs.js
CHANGED
|
@@ -94,6 +94,15 @@ class WKTParser {
|
|
|
94
94
|
}
|
|
95
95
|
return this.advance();
|
|
96
96
|
}
|
|
97
|
+
skipComma() {
|
|
98
|
+
if (this.peek().type === 'COMMA') {
|
|
99
|
+
this.advance();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
isDone() {
|
|
103
|
+
const t = this.peek();
|
|
104
|
+
return t.type === 'RPAREN' || t.type === 'EOF';
|
|
105
|
+
}
|
|
97
106
|
parseGeometry() {
|
|
98
107
|
const token = this.peek();
|
|
99
108
|
if (token.type !== 'WORD') {
|
|
@@ -166,7 +175,13 @@ class WKTParser {
|
|
|
166
175
|
if (this.isEmptyGeometry()) {
|
|
167
176
|
return { type: 'Polygon', coordinates: [] };
|
|
168
177
|
}
|
|
169
|
-
|
|
178
|
+
this.consume('LPAREN');
|
|
179
|
+
const rings = [];
|
|
180
|
+
while (!this.isDone()) {
|
|
181
|
+
rings.push(this.parseCoordinatesList());
|
|
182
|
+
this.skipComma();
|
|
183
|
+
}
|
|
184
|
+
this.consume('RPAREN');
|
|
170
185
|
return { type: 'Polygon', coordinates: rings };
|
|
171
186
|
}
|
|
172
187
|
// ── MULTIPOINT ───────────────────────────────────────────────────
|
|
@@ -178,7 +193,7 @@ class WKTParser {
|
|
|
178
193
|
}
|
|
179
194
|
this.advance(); // consume outer (
|
|
180
195
|
const coords = [];
|
|
181
|
-
while (this.
|
|
196
|
+
while (!this.isDone()) {
|
|
182
197
|
if (this.peek().type === 'LPAREN') {
|
|
183
198
|
// 标准写法: MULTIPOINT ((x y), (x y))
|
|
184
199
|
this.advance(); // consume (
|
|
@@ -189,9 +204,7 @@ class WKTParser {
|
|
|
189
204
|
// 非标准写法: MULTIPOINT (x y, x y)
|
|
190
205
|
coords.push(this.parseCoordinates());
|
|
191
206
|
}
|
|
192
|
-
|
|
193
|
-
this.advance();
|
|
194
|
-
}
|
|
207
|
+
this.skipComma();
|
|
195
208
|
}
|
|
196
209
|
this.consume('RPAREN');
|
|
197
210
|
return { type: 'MultiPoint', coordinates: coords };
|
|
@@ -205,11 +218,9 @@ class WKTParser {
|
|
|
205
218
|
}
|
|
206
219
|
this.advance(); // consume outer (
|
|
207
220
|
const lines = [];
|
|
208
|
-
while (this.
|
|
221
|
+
while (!this.isDone()) {
|
|
209
222
|
lines.push(this.parseCoordinatesList());
|
|
210
|
-
|
|
211
|
-
this.advance();
|
|
212
|
-
}
|
|
223
|
+
this.skipComma();
|
|
213
224
|
}
|
|
214
225
|
this.consume('RPAREN');
|
|
215
226
|
return { type: 'MultiLineString', coordinates: lines };
|
|
@@ -221,7 +232,13 @@ class WKTParser {
|
|
|
221
232
|
if (this.isEmptyGeometry()) {
|
|
222
233
|
return { type: 'MultiPolygon', coordinates: [] };
|
|
223
234
|
}
|
|
224
|
-
|
|
235
|
+
this.consume('LPAREN');
|
|
236
|
+
const polys = [];
|
|
237
|
+
while (!this.isDone()) {
|
|
238
|
+
polys.push(this.parseCoordinateListList());
|
|
239
|
+
this.skipComma();
|
|
240
|
+
}
|
|
241
|
+
this.consume('RPAREN');
|
|
225
242
|
return { type: 'MultiPolygon', coordinates: polys };
|
|
226
243
|
}
|
|
227
244
|
// ── GEOMETRYCOLLECTION ───────────────────────────────────────────
|
|
@@ -270,11 +287,9 @@ class WKTParser {
|
|
|
270
287
|
parseCoordinatesList() {
|
|
271
288
|
this.consume('LPAREN');
|
|
272
289
|
const coords = [];
|
|
273
|
-
while (this.
|
|
290
|
+
while (!this.isDone()) {
|
|
274
291
|
coords.push(this.parseCoordinates());
|
|
275
|
-
|
|
276
|
-
this.advance();
|
|
277
|
-
}
|
|
292
|
+
this.skipComma();
|
|
278
293
|
}
|
|
279
294
|
this.consume('RPAREN');
|
|
280
295
|
return coords;
|
|
@@ -294,21 +309,6 @@ class WKTParser {
|
|
|
294
309
|
this.consume('RPAREN');
|
|
295
310
|
return lists;
|
|
296
311
|
}
|
|
297
|
-
/** 解析多边形列表(MultiPolygon 级别):( ((...)), ((...)) ) */
|
|
298
|
-
parseCoordinateListListList() {
|
|
299
|
-
if (this.peek().type !== 'LPAREN')
|
|
300
|
-
return [];
|
|
301
|
-
this.advance(); // consume outer (
|
|
302
|
-
const lists = [];
|
|
303
|
-
while (this.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
|
|
304
|
-
lists.push(this.parseCoordinateListList());
|
|
305
|
-
if (this.peek().type === 'COMMA') {
|
|
306
|
-
this.advance();
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
this.consume('RPAREN');
|
|
310
|
-
return lists;
|
|
311
|
-
}
|
|
312
312
|
}
|
|
313
313
|
/** 将 WKT 字符串解析为 GeoJSON Geometry 对象 */
|
|
314
314
|
function parse(wkt) {
|
package/dist/index.esm.js
CHANGED
|
@@ -92,6 +92,15 @@ class WKTParser {
|
|
|
92
92
|
}
|
|
93
93
|
return this.advance();
|
|
94
94
|
}
|
|
95
|
+
skipComma() {
|
|
96
|
+
if (this.peek().type === 'COMMA') {
|
|
97
|
+
this.advance();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
isDone() {
|
|
101
|
+
const t = this.peek();
|
|
102
|
+
return t.type === 'RPAREN' || t.type === 'EOF';
|
|
103
|
+
}
|
|
95
104
|
parseGeometry() {
|
|
96
105
|
const token = this.peek();
|
|
97
106
|
if (token.type !== 'WORD') {
|
|
@@ -164,7 +173,13 @@ class WKTParser {
|
|
|
164
173
|
if (this.isEmptyGeometry()) {
|
|
165
174
|
return { type: 'Polygon', coordinates: [] };
|
|
166
175
|
}
|
|
167
|
-
|
|
176
|
+
this.consume('LPAREN');
|
|
177
|
+
const rings = [];
|
|
178
|
+
while (!this.isDone()) {
|
|
179
|
+
rings.push(this.parseCoordinatesList());
|
|
180
|
+
this.skipComma();
|
|
181
|
+
}
|
|
182
|
+
this.consume('RPAREN');
|
|
168
183
|
return { type: 'Polygon', coordinates: rings };
|
|
169
184
|
}
|
|
170
185
|
// ── MULTIPOINT ───────────────────────────────────────────────────
|
|
@@ -176,7 +191,7 @@ class WKTParser {
|
|
|
176
191
|
}
|
|
177
192
|
this.advance(); // consume outer (
|
|
178
193
|
const coords = [];
|
|
179
|
-
while (this.
|
|
194
|
+
while (!this.isDone()) {
|
|
180
195
|
if (this.peek().type === 'LPAREN') {
|
|
181
196
|
// 标准写法: MULTIPOINT ((x y), (x y))
|
|
182
197
|
this.advance(); // consume (
|
|
@@ -187,9 +202,7 @@ class WKTParser {
|
|
|
187
202
|
// 非标准写法: MULTIPOINT (x y, x y)
|
|
188
203
|
coords.push(this.parseCoordinates());
|
|
189
204
|
}
|
|
190
|
-
|
|
191
|
-
this.advance();
|
|
192
|
-
}
|
|
205
|
+
this.skipComma();
|
|
193
206
|
}
|
|
194
207
|
this.consume('RPAREN');
|
|
195
208
|
return { type: 'MultiPoint', coordinates: coords };
|
|
@@ -203,11 +216,9 @@ class WKTParser {
|
|
|
203
216
|
}
|
|
204
217
|
this.advance(); // consume outer (
|
|
205
218
|
const lines = [];
|
|
206
|
-
while (this.
|
|
219
|
+
while (!this.isDone()) {
|
|
207
220
|
lines.push(this.parseCoordinatesList());
|
|
208
|
-
|
|
209
|
-
this.advance();
|
|
210
|
-
}
|
|
221
|
+
this.skipComma();
|
|
211
222
|
}
|
|
212
223
|
this.consume('RPAREN');
|
|
213
224
|
return { type: 'MultiLineString', coordinates: lines };
|
|
@@ -219,7 +230,13 @@ class WKTParser {
|
|
|
219
230
|
if (this.isEmptyGeometry()) {
|
|
220
231
|
return { type: 'MultiPolygon', coordinates: [] };
|
|
221
232
|
}
|
|
222
|
-
|
|
233
|
+
this.consume('LPAREN');
|
|
234
|
+
const polys = [];
|
|
235
|
+
while (!this.isDone()) {
|
|
236
|
+
polys.push(this.parseCoordinateListList());
|
|
237
|
+
this.skipComma();
|
|
238
|
+
}
|
|
239
|
+
this.consume('RPAREN');
|
|
223
240
|
return { type: 'MultiPolygon', coordinates: polys };
|
|
224
241
|
}
|
|
225
242
|
// ── GEOMETRYCOLLECTION ───────────────────────────────────────────
|
|
@@ -268,11 +285,9 @@ class WKTParser {
|
|
|
268
285
|
parseCoordinatesList() {
|
|
269
286
|
this.consume('LPAREN');
|
|
270
287
|
const coords = [];
|
|
271
|
-
while (this.
|
|
288
|
+
while (!this.isDone()) {
|
|
272
289
|
coords.push(this.parseCoordinates());
|
|
273
|
-
|
|
274
|
-
this.advance();
|
|
275
|
-
}
|
|
290
|
+
this.skipComma();
|
|
276
291
|
}
|
|
277
292
|
this.consume('RPAREN');
|
|
278
293
|
return coords;
|
|
@@ -292,21 +307,6 @@ class WKTParser {
|
|
|
292
307
|
this.consume('RPAREN');
|
|
293
308
|
return lists;
|
|
294
309
|
}
|
|
295
|
-
/** 解析多边形列表(MultiPolygon 级别):( ((...)), ((...)) ) */
|
|
296
|
-
parseCoordinateListListList() {
|
|
297
|
-
if (this.peek().type !== 'LPAREN')
|
|
298
|
-
return [];
|
|
299
|
-
this.advance(); // consume outer (
|
|
300
|
-
const lists = [];
|
|
301
|
-
while (this.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
|
|
302
|
-
lists.push(this.parseCoordinateListList());
|
|
303
|
-
if (this.peek().type === 'COMMA') {
|
|
304
|
-
this.advance();
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
this.consume('RPAREN');
|
|
308
|
-
return lists;
|
|
309
|
-
}
|
|
310
310
|
}
|
|
311
311
|
/** 将 WKT 字符串解析为 GeoJSON Geometry 对象 */
|
|
312
312
|
function parse(wkt) {
|
package/dist/index.umd.js
CHANGED
|
@@ -98,6 +98,15 @@
|
|
|
98
98
|
}
|
|
99
99
|
return this.advance();
|
|
100
100
|
}
|
|
101
|
+
skipComma() {
|
|
102
|
+
if (this.peek().type === 'COMMA') {
|
|
103
|
+
this.advance();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
isDone() {
|
|
107
|
+
const t = this.peek();
|
|
108
|
+
return t.type === 'RPAREN' || t.type === 'EOF';
|
|
109
|
+
}
|
|
101
110
|
parseGeometry() {
|
|
102
111
|
const token = this.peek();
|
|
103
112
|
if (token.type !== 'WORD') {
|
|
@@ -170,7 +179,13 @@
|
|
|
170
179
|
if (this.isEmptyGeometry()) {
|
|
171
180
|
return { type: 'Polygon', coordinates: [] };
|
|
172
181
|
}
|
|
173
|
-
|
|
182
|
+
this.consume('LPAREN');
|
|
183
|
+
const rings = [];
|
|
184
|
+
while (!this.isDone()) {
|
|
185
|
+
rings.push(this.parseCoordinatesList());
|
|
186
|
+
this.skipComma();
|
|
187
|
+
}
|
|
188
|
+
this.consume('RPAREN');
|
|
174
189
|
return { type: 'Polygon', coordinates: rings };
|
|
175
190
|
}
|
|
176
191
|
// ── MULTIPOINT ───────────────────────────────────────────────────
|
|
@@ -182,7 +197,7 @@
|
|
|
182
197
|
}
|
|
183
198
|
this.advance(); // consume outer (
|
|
184
199
|
const coords = [];
|
|
185
|
-
while (this.
|
|
200
|
+
while (!this.isDone()) {
|
|
186
201
|
if (this.peek().type === 'LPAREN') {
|
|
187
202
|
// 标准写法: MULTIPOINT ((x y), (x y))
|
|
188
203
|
this.advance(); // consume (
|
|
@@ -193,9 +208,7 @@
|
|
|
193
208
|
// 非标准写法: MULTIPOINT (x y, x y)
|
|
194
209
|
coords.push(this.parseCoordinates());
|
|
195
210
|
}
|
|
196
|
-
|
|
197
|
-
this.advance();
|
|
198
|
-
}
|
|
211
|
+
this.skipComma();
|
|
199
212
|
}
|
|
200
213
|
this.consume('RPAREN');
|
|
201
214
|
return { type: 'MultiPoint', coordinates: coords };
|
|
@@ -209,11 +222,9 @@
|
|
|
209
222
|
}
|
|
210
223
|
this.advance(); // consume outer (
|
|
211
224
|
const lines = [];
|
|
212
|
-
while (this.
|
|
225
|
+
while (!this.isDone()) {
|
|
213
226
|
lines.push(this.parseCoordinatesList());
|
|
214
|
-
|
|
215
|
-
this.advance();
|
|
216
|
-
}
|
|
227
|
+
this.skipComma();
|
|
217
228
|
}
|
|
218
229
|
this.consume('RPAREN');
|
|
219
230
|
return { type: 'MultiLineString', coordinates: lines };
|
|
@@ -225,7 +236,13 @@
|
|
|
225
236
|
if (this.isEmptyGeometry()) {
|
|
226
237
|
return { type: 'MultiPolygon', coordinates: [] };
|
|
227
238
|
}
|
|
228
|
-
|
|
239
|
+
this.consume('LPAREN');
|
|
240
|
+
const polys = [];
|
|
241
|
+
while (!this.isDone()) {
|
|
242
|
+
polys.push(this.parseCoordinateListList());
|
|
243
|
+
this.skipComma();
|
|
244
|
+
}
|
|
245
|
+
this.consume('RPAREN');
|
|
229
246
|
return { type: 'MultiPolygon', coordinates: polys };
|
|
230
247
|
}
|
|
231
248
|
// ── GEOMETRYCOLLECTION ───────────────────────────────────────────
|
|
@@ -274,11 +291,9 @@
|
|
|
274
291
|
parseCoordinatesList() {
|
|
275
292
|
this.consume('LPAREN');
|
|
276
293
|
const coords = [];
|
|
277
|
-
while (this.
|
|
294
|
+
while (!this.isDone()) {
|
|
278
295
|
coords.push(this.parseCoordinates());
|
|
279
|
-
|
|
280
|
-
this.advance();
|
|
281
|
-
}
|
|
296
|
+
this.skipComma();
|
|
282
297
|
}
|
|
283
298
|
this.consume('RPAREN');
|
|
284
299
|
return coords;
|
|
@@ -298,21 +313,6 @@
|
|
|
298
313
|
this.consume('RPAREN');
|
|
299
314
|
return lists;
|
|
300
315
|
}
|
|
301
|
-
/** 解析多边形列表(MultiPolygon 级别):( ((...)), ((...)) ) */
|
|
302
|
-
parseCoordinateListListList() {
|
|
303
|
-
if (this.peek().type !== 'LPAREN')
|
|
304
|
-
return [];
|
|
305
|
-
this.advance(); // consume outer (
|
|
306
|
-
const lists = [];
|
|
307
|
-
while (this.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
|
|
308
|
-
lists.push(this.parseCoordinateListList());
|
|
309
|
-
if (this.peek().type === 'COMMA') {
|
|
310
|
-
this.advance();
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
this.consume('RPAREN');
|
|
314
|
-
return lists;
|
|
315
|
-
}
|
|
316
316
|
}
|
|
317
317
|
/** 将 WKT 字符串解析为 GeoJSON Geometry 对象 */
|
|
318
318
|
function parse(wkt) {
|
package/dist/wkt-parser.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export declare class WKTParser {
|
|
|
7
7
|
private advance;
|
|
8
8
|
/** 消费当前 token 并返回,若类型不匹配则抛出错误 */
|
|
9
9
|
private consume;
|
|
10
|
+
private skipComma;
|
|
11
|
+
private isDone;
|
|
10
12
|
private parseGeometry;
|
|
11
13
|
private skipDimensionKeyword;
|
|
12
14
|
private isEmptyGeometry;
|
|
@@ -26,8 +28,6 @@ export declare class WKTParser {
|
|
|
26
28
|
private parseCoordinatesList;
|
|
27
29
|
/** 解析环列表(Polygon 级别):( (...), (...) ) */
|
|
28
30
|
private parseCoordinateListList;
|
|
29
|
-
/** 解析多边形列表(MultiPolygon 级别):( ((...)), ((...)) ) */
|
|
30
|
-
private parseCoordinateListListList;
|
|
31
31
|
}
|
|
32
32
|
/** 将 WKT 字符串解析为 GeoJSON Geometry 对象 */
|
|
33
33
|
export declare function parse(wkt: string): Geometry;
|
package/package.json
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wkt-parse-and-geojson",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "Zero-dependency WKT parser/builder and WKT↔GeoJSON converter",
|
|
6
6
|
"main": "dist/index.cjs.js",
|
|
7
7
|
"module": "dist/index.esm.js",
|
|
8
|
+
"browser": "dist/index.umd.js",
|
|
8
9
|
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.esm.js",
|
|
13
|
+
"require": "./dist/index.cjs.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
9
17
|
"files": [
|
|
10
|
-
"dist"
|
|
18
|
+
"dist",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
11
21
|
],
|
|
12
22
|
"scripts": {
|
|
13
23
|
"build": "rollup -c",
|
|
@@ -19,7 +29,10 @@
|
|
|
19
29
|
"geojson",
|
|
20
30
|
"geometry",
|
|
21
31
|
"parser",
|
|
22
|
-
"
|
|
32
|
+
"builder",
|
|
33
|
+
"gis",
|
|
34
|
+
"ogc",
|
|
35
|
+
"spatial"
|
|
23
36
|
],
|
|
24
37
|
"author": "",
|
|
25
38
|
"license": "MIT",
|
|
@@ -28,8 +41,5 @@
|
|
|
28
41
|
"rollup": "^4.40.0",
|
|
29
42
|
"tslib": "^2.8.1",
|
|
30
43
|
"typescript": "^5.8.3"
|
|
31
|
-
},
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"wkt-parse-and-geojson": "^1.0.0"
|
|
34
44
|
}
|
|
35
45
|
}
|