wkt-parse-and-geojson 1.0.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 +656 -0
- package/dist/geojson-builder.d.ts +92 -0
- package/dist/geojson-to-wkt.d.ts +32 -0
- package/dist/index.cjs.js +673 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.esm.js +654 -0
- package/dist/index.umd.js +679 -0
- package/dist/types.d.ts +51 -0
- package/dist/wkt-builder.d.ts +17 -0
- package/dist/wkt-parser.d.ts +33 -0
- package/dist/wkt-to-geojson.d.ts +35 -0
- package/package.json +26 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export type Position = [number, number] | [number, number, number];
|
|
2
|
+
export interface Point {
|
|
3
|
+
type: 'Point';
|
|
4
|
+
coordinates: Position;
|
|
5
|
+
}
|
|
6
|
+
export interface LineString {
|
|
7
|
+
type: 'LineString';
|
|
8
|
+
coordinates: Position[];
|
|
9
|
+
}
|
|
10
|
+
export interface Polygon {
|
|
11
|
+
type: 'Polygon';
|
|
12
|
+
coordinates: Position[][];
|
|
13
|
+
}
|
|
14
|
+
export interface MultiPoint {
|
|
15
|
+
type: 'MultiPoint';
|
|
16
|
+
coordinates: Position[];
|
|
17
|
+
}
|
|
18
|
+
export interface MultiLineString {
|
|
19
|
+
type: 'MultiLineString';
|
|
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 | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon | GeometryCollection;
|
|
31
|
+
/** GeoJSON Feature,包含一个 Geometry 和任意属性 */
|
|
32
|
+
export interface Feature<G extends Geometry = Geometry> {
|
|
33
|
+
type: 'Feature';
|
|
34
|
+
geometry: G | null;
|
|
35
|
+
properties: Record<string, unknown> | null;
|
|
36
|
+
id?: string | number;
|
|
37
|
+
}
|
|
38
|
+
/** GeoJSON FeatureCollection,包含多个 Feature */
|
|
39
|
+
export interface FeatureCollection {
|
|
40
|
+
type: 'FeatureCollection';
|
|
41
|
+
features: Feature[];
|
|
42
|
+
}
|
|
43
|
+
/** 所有 GeoJSON 对象的联合类型 */
|
|
44
|
+
export type GeoJSONObject = Geometry | Feature | FeatureCollection;
|
|
45
|
+
/**
|
|
46
|
+
* @deprecated 请使用 `GeoJSONObject`(包含 Geometry、Feature、FeatureCollection)
|
|
47
|
+
* 或直接使用 `Geometry` 类型(仅几何体)。
|
|
48
|
+
* 此别名保留用于向后兼容。
|
|
49
|
+
*/
|
|
50
|
+
export type GeoJSON = GeoJSONObject;
|
|
51
|
+
export type WKTType = 'POINT' | 'LINESTRING' | 'POLYGON' | 'MULTIPOINT' | 'MULTILINESTRING' | 'MULTIPOLYGON' | 'GEOMETRYCOLLECTION';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Geometry } from './types';
|
|
2
|
+
export declare class WKTBuilder {
|
|
3
|
+
build(geometry: Geometry): string;
|
|
4
|
+
private buildPoint;
|
|
5
|
+
private buildLineString;
|
|
6
|
+
private buildPolygon;
|
|
7
|
+
/**
|
|
8
|
+
* 按 OGC/ISO WKT 标准,MULTIPOINT 每个点用括号包裹:
|
|
9
|
+
* MULTIPOINT ((0 0), (1 1), (2 2))
|
|
10
|
+
*/
|
|
11
|
+
private buildMultiPoint;
|
|
12
|
+
private buildMultiLineString;
|
|
13
|
+
private buildMultiPolygon;
|
|
14
|
+
private buildGeometryCollection;
|
|
15
|
+
}
|
|
16
|
+
/** 将 GeoJSON Geometry 对象转换为 WKT 字符串 */
|
|
17
|
+
export declare function build(geometry: Geometry): string;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Geometry } from './types';
|
|
2
|
+
export declare class WKTParser {
|
|
3
|
+
private tokens;
|
|
4
|
+
private pos;
|
|
5
|
+
parse(wkt: string): Geometry;
|
|
6
|
+
private peek;
|
|
7
|
+
private advance;
|
|
8
|
+
/** 消费当前 token 并返回,若类型不匹配则抛出错误 */
|
|
9
|
+
private consume;
|
|
10
|
+
private parseGeometry;
|
|
11
|
+
private skipDimensionKeyword;
|
|
12
|
+
private isEmptyGeometry;
|
|
13
|
+
private parsePoint;
|
|
14
|
+
private parseLineString;
|
|
15
|
+
private parsePolygon;
|
|
16
|
+
private parseMultiPoint;
|
|
17
|
+
private parseMultiLineString;
|
|
18
|
+
private parseMultiPolygon;
|
|
19
|
+
private parseGeometryCollection;
|
|
20
|
+
/**
|
|
21
|
+
* 读取一个坐标点(自动检测维度:X Y 或 X Y Z)
|
|
22
|
+
* 读完 X、Y 后,若下一个 token 仍是 NUMBER,则继续读 Z
|
|
23
|
+
*/
|
|
24
|
+
private parseCoordinates;
|
|
25
|
+
/** 解析带括号的坐标序列:( x y, x y, ... ) */
|
|
26
|
+
private parseCoordinatesList;
|
|
27
|
+
/** 解析环列表(Polygon 级别):( (...), (...) ) */
|
|
28
|
+
private parseCoordinateListList;
|
|
29
|
+
/** 解析多边形列表(MultiPolygon 级别):( ((...)), ((...)) ) */
|
|
30
|
+
private parseCoordinateListListList;
|
|
31
|
+
}
|
|
32
|
+
/** 将 WKT 字符串解析为 GeoJSON Geometry 对象 */
|
|
33
|
+
export declare function parse(wkt: string): Geometry;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Geometry, Feature, FeatureCollection } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 将 WKT 字符串转换为 GeoJSON Geometry 对象。
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* wktToGeoJSON('POINT (30.5 40.5)')
|
|
7
|
+
* // → { type: 'Point', coordinates: [30.5, 40.5] }
|
|
8
|
+
*
|
|
9
|
+
* wktToGeoJSON('POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))')
|
|
10
|
+
* // → { type: 'Polygon', coordinates: [[[0,0],[1,0],[1,1],[0,1],[0,0]]] }
|
|
11
|
+
*/
|
|
12
|
+
export declare function wktToGeoJSON(wkt: string): Geometry;
|
|
13
|
+
/**
|
|
14
|
+
* 将 WKT 字符串转换为 GeoJSON Feature 对象。
|
|
15
|
+
*
|
|
16
|
+
* @param wkt WKT 字符串
|
|
17
|
+
* @param properties 可选的 Feature 属性对象
|
|
18
|
+
* @param id 可选的 Feature ID
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* wktToFeature('POINT (30.5 40.5)', { name: '北京' })
|
|
22
|
+
* // → { type: 'Feature', geometry: { type: 'Point', ... }, properties: { name: '北京' } }
|
|
23
|
+
*/
|
|
24
|
+
export declare function wktToFeature(wkt: string, properties?: Record<string, unknown> | null, id?: string | number): Feature;
|
|
25
|
+
/**
|
|
26
|
+
* 将多个 WKT 字符串批量转换为 GeoJSON FeatureCollection。
|
|
27
|
+
*
|
|
28
|
+
* @param wkts WKT 字符串数组
|
|
29
|
+
* @param properties 可选,每个 Feature 的属性数组(长度应与 wkts 一致)
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* wktToFeatureCollection(['POINT (0 0)', 'POINT (1 1)'])
|
|
33
|
+
* // → { type: 'FeatureCollection', features: [...] }
|
|
34
|
+
*/
|
|
35
|
+
export declare function wktToFeatureCollection(wkts: string[], properties?: Array<Record<string, unknown> | null>): FeatureCollection;
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wkt-parse-and-geojson",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Parse WKT and build GeoJSON, convert between WKT and GeoJSON",
|
|
6
|
+
"main": "dist/index.cjs.js",
|
|
7
|
+
"module": "dist/index.esm.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "rollup -c",
|
|
14
|
+
"dev": "rollup -c -w",
|
|
15
|
+
"typecheck": "tsc --noEmit"
|
|
16
|
+
},
|
|
17
|
+
"keywords": ["wkt", "geojson", "geometry", "parser", "gis"],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
22
|
+
"rollup": "^4.40.0",
|
|
23
|
+
"tslib": "^2.8.1",
|
|
24
|
+
"typescript": "^5.8.3"
|
|
25
|
+
}
|
|
26
|
+
}
|