wkt-parse-and-geojson 1.0.0 → 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/README.md CHANGED
@@ -74,19 +74,23 @@ npm run typecheck
74
74
 
75
75
  ### Node.js
76
76
 
77
+ ```bash
78
+ npm install wkt-parse-and-geojson
79
+ ```
80
+
77
81
  ```javascript
78
82
  // CommonJS
79
- const { parse, build, wktToFeature } = require('./dist/index.cjs.js');
83
+ const { parse, build, wktToFeature } = require('wkt-parse-and-geojson');
80
84
 
81
85
  // ES Module
82
- import { parse, build, wktToFeature } from './dist/index.esm.js';
86
+ import { parse, build, wktToFeature } from 'wkt-parse-and-geojson';
83
87
  ```
84
88
 
85
89
  ### 浏览器 (script 标签)
86
90
 
87
91
  ```html
88
92
  <!-- UMD 方式:通过 script 标签直接引入,全局变量 WKTGeoJSON -->
89
- <script src="./dist/index.umd.js"></script>
93
+ <script src="https://unpkg.com/wkt-parse-and-geojson/dist/index.umd.js"></script>
90
94
  <script>
91
95
  const geom = WKTGeoJSON.parse('POINT (116.39 39.91)');
92
96
  console.log(geom);
@@ -102,7 +106,7 @@ import { parse, build, wktToFeature } from './dist/index.esm.js';
102
106
 
103
107
  ```html
104
108
  <script type="module">
105
- import { parse, build } from '../dist/index.esm.js';
109
+ import { parse, build } from 'wkt-parse-and-geojson';
106
110
 
107
111
  const geom = parse('POINT (116.39 39.91)');
108
112
  console.log(geom);
@@ -246,7 +250,7 @@ build({ type: 'GeometryCollection', geometries: [] })
246
250
  将 WKT 字符串转换为 GeoJSON Geometry 对象(`parse` 的语义化别名)。
247
251
 
248
252
  ```javascript
249
- import { wktToGeoJSON } from './dist/index.esm.js';
253
+ import { wktToGeoJSON } from 'wkt-parse-and-geojson';
250
254
 
251
255
  const geom = wktToGeoJSON('POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))');
252
256
  // → { type: 'Polygon', coordinates: [[[0,0],[1,0],[1,1],[0,1],[0,0]]] }
@@ -266,7 +270,7 @@ const geom = wktToGeoJSON('POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))');
266
270
  **返回:** `Feature`
267
271
 
268
272
  ```javascript
269
- import { wktToFeature } from './dist/index.esm.js';
273
+ import { wktToFeature } from 'wkt-parse-and-geojson';
270
274
 
271
275
  // 带属性
272
276
  wktToFeature('POINT (116.39 39.91)', { name: '北京', pop: 21540000 })
@@ -298,7 +302,7 @@ wktToFeature('POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))')
298
302
  **返回:** `FeatureCollection`
299
303
 
300
304
  ```javascript
301
- import { wktToFeatureCollection } from './dist/index.esm.js';
305
+ import { wktToFeatureCollection } from 'wkt-parse-and-geojson';
302
306
 
303
307
  const fc = wktToFeatureCollection(
304
308
  ['POINT (116.39 39.91)', 'POINT (121.47 31.23)', 'POINT (113.26 23.13)'],
@@ -324,7 +328,7 @@ const fc2 = wktToFeatureCollection(['POINT (0 0)', 'LINESTRING (0 0, 1 1)']);
324
328
  将 GeoJSON Geometry 对象转换为 WKT 字符串(`build` 的语义化别名)。
325
329
 
326
330
  ```javascript
327
- import { geojsonToWkt } from './dist/index.esm.js';
331
+ import { geojsonToWkt } from 'wkt-parse-and-geojson';
328
332
 
329
333
  geojsonToWkt({ type: 'Point', coordinates: [116.39, 39.91] })
330
334
  // → 'POINT (116.39 39.91)'
@@ -339,7 +343,7 @@ geojsonToWkt({ type: 'Point', coordinates: [116.39, 39.91] })
339
343
  **抛出:** 若 `Feature.geometry` 为 `null`,则抛出 `Error`
340
344
 
341
345
  ```javascript
342
- import { featureToWkt } from './dist/index.esm.js';
346
+ import { featureToWkt } from 'wkt-parse-and-geojson';
343
347
 
344
348
  featureToWkt({
345
349
  type: 'Feature',
@@ -358,7 +362,7 @@ featureToWkt({
358
362
  **返回:** `Array<string | null>`
359
363
 
360
364
  ```javascript
361
- import { featureCollectionToWkt } from './dist/index.esm.js';
365
+ import { featureCollectionToWkt } from 'wkt-parse-and-geojson';
362
366
 
363
367
  featureCollectionToWkt({
364
368
  type: 'FeatureCollection',
@@ -578,7 +582,7 @@ parse('POINT (0 0) garbage')
578
582
  ### WKT → GeoJSON Feature → 回写 WKT
579
583
 
580
584
  ```javascript
581
- import { wktToFeature, featureToWkt } from './dist/index.esm.js';
585
+ import { wktToFeature, featureToWkt } from 'wkt-parse-and-geojson';
582
586
 
583
587
  const wkt = 'POLYGON ((116 39, 117 39, 117 40, 116 40, 116 39))';
584
588
 
@@ -597,7 +601,7 @@ console.log(outputWkt);
597
601
  ### 批量城市点构建 FeatureCollection
598
602
 
599
603
  ```javascript
600
- import { wktToFeatureCollection } from './dist/index.esm.js';
604
+ import { wktToFeatureCollection } from 'wkt-parse-and-geojson';
601
605
 
602
606
  const cities = [
603
607
  { wkt: 'POINT (116.39 39.91)', props: { name: '北京', code: 'BJ' } },
@@ -617,7 +621,7 @@ console.log(JSON.stringify(fc, null, 2));
617
621
  ### 使用工厂方法组合复杂几何
618
622
 
619
623
  ```javascript
620
- import { createPoint, createLineString, createPolygon, createGeometryCollection, build } from './dist/index.esm.js';
624
+ import { createPoint, createLineString, createPolygon, createGeometryCollection, build } from 'wkt-parse-and-geojson';
621
625
 
622
626
  const collection = createGeometryCollection([
623
627
  createPoint(0, 0),
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
- const rings = this.parseCoordinateListList();
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.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
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
- if (this.peek().type === 'COMMA') {
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.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
221
+ while (!this.isDone()) {
209
222
  lines.push(this.parseCoordinatesList());
210
- if (this.peek().type === 'COMMA') {
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
- const polys = this.parseCoordinateListListList();
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.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
290
+ while (!this.isDone()) {
274
291
  coords.push(this.parseCoordinates());
275
- if (this.peek().type === 'COMMA') {
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
- const rings = this.parseCoordinateListList();
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.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
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
- if (this.peek().type === 'COMMA') {
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.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
219
+ while (!this.isDone()) {
207
220
  lines.push(this.parseCoordinatesList());
208
- if (this.peek().type === 'COMMA') {
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
- const polys = this.parseCoordinateListListList();
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.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
288
+ while (!this.isDone()) {
272
289
  coords.push(this.parseCoordinates());
273
- if (this.peek().type === 'COMMA') {
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
- const rings = this.parseCoordinateListList();
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.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
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
- if (this.peek().type === 'COMMA') {
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.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
225
+ while (!this.isDone()) {
213
226
  lines.push(this.parseCoordinatesList());
214
- if (this.peek().type === 'COMMA') {
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
- const polys = this.parseCoordinateListListList();
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.peek().type !== 'RPAREN' && this.peek().type !== 'EOF') {
294
+ while (!this.isDone()) {
278
295
  coords.push(this.parseCoordinates());
279
- if (this.peek().type === 'COMMA') {
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) {
@@ -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,20 +1,39 @@
1
1
  {
2
2
  "name": "wkt-parse-and-geojson",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
- "description": "Parse WKT and build GeoJSON, convert between WKT and GeoJSON",
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",
14
24
  "dev": "rollup -c -w",
15
25
  "typecheck": "tsc --noEmit"
16
26
  },
17
- "keywords": ["wkt", "geojson", "geometry", "parser", "gis"],
27
+ "keywords": [
28
+ "wkt",
29
+ "geojson",
30
+ "geometry",
31
+ "parser",
32
+ "builder",
33
+ "gis",
34
+ "ogc",
35
+ "spatial"
36
+ ],
18
37
  "author": "",
19
38
  "license": "MIT",
20
39
  "devDependencies": {
@@ -23,4 +42,4 @@
23
42
  "tslib": "^2.8.1",
24
43
  "typescript": "^5.8.3"
25
44
  }
26
- }
45
+ }