giser-geometry-parse 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/LICENSE +21 -0
- package/README.md +752 -0
- package/dist/default-export.d.ts +29 -0
- package/dist/geojson-builder.d.ts +92 -0
- package/dist/geojson-to-wkt.d.ts +32 -0
- package/dist/index.cjs.js +959 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.esm.js +932 -0
- package/dist/index.umd.js +965 -0
- package/dist/types.d.ts +51 -0
- package/dist/validate.d.ts +29 -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 +44 -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,29 @@
|
|
|
1
|
+
import { Geometry } from './types';
|
|
2
|
+
export interface ValidationResult {
|
|
3
|
+
valid: boolean;
|
|
4
|
+
error?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 校验 WKT 字符串格式是否合法
|
|
8
|
+
*/
|
|
9
|
+
export declare function validateWKT(wkt: string): ValidationResult;
|
|
10
|
+
/**
|
|
11
|
+
* 校验 GeoJSON Geometry 对象是否合法
|
|
12
|
+
*/
|
|
13
|
+
export declare function validateGeoJSON(geojson: unknown): ValidationResult;
|
|
14
|
+
/**
|
|
15
|
+
* 尝试从可能不规范的 WKT 中恢复出有效结果
|
|
16
|
+
* 主要处理尾部多余字符的情况
|
|
17
|
+
*/
|
|
18
|
+
export declare function tryFixWKT(wkt: string): {
|
|
19
|
+
fixed: string;
|
|
20
|
+
changed: boolean;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* 深度克隆 GeoJSON 对象(用于避免意外修改原对象)
|
|
24
|
+
*/
|
|
25
|
+
export declare function cloneGeometry<G extends Geometry>(geometry: G): G;
|
|
26
|
+
/**
|
|
27
|
+
* 判断两个几何对象是否相等(坐标对比)
|
|
28
|
+
*/
|
|
29
|
+
export declare function geometryEquals(a: Geometry, b: Geometry): boolean;
|
|
@@ -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 skipComma;
|
|
11
|
+
private isDone;
|
|
12
|
+
private parseGeometry;
|
|
13
|
+
private skipDimensionKeyword;
|
|
14
|
+
private isEmptyGeometry;
|
|
15
|
+
private parsePoint;
|
|
16
|
+
private parseLineString;
|
|
17
|
+
private parsePolygon;
|
|
18
|
+
private parseMultiPoint;
|
|
19
|
+
private parseMultiLineString;
|
|
20
|
+
private parseMultiPolygon;
|
|
21
|
+
private parseGeometryCollection;
|
|
22
|
+
/**
|
|
23
|
+
* 读取一个坐标点(自动检测维度:X Y 或 X Y Z)
|
|
24
|
+
* 读完 X、Y 后,若下一个 token 仍是 NUMBER,则继续读 Z
|
|
25
|
+
*/
|
|
26
|
+
private parseCoordinates;
|
|
27
|
+
/** 解析带括号的坐标序列:( x y, x y, ... ) */
|
|
28
|
+
private parseCoordinatesList;
|
|
29
|
+
/** 解析环列表(Polygon 级别):( (...), (...) ) */
|
|
30
|
+
private parseCoordinateListList;
|
|
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,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "giser-geometry-parse",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Zero-dependency WKT parser/builder and WKT↔GeoJSON converter",
|
|
6
|
+
"main": "dist/index.cjs.js",
|
|
7
|
+
"module": "dist/index.esm.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.esm.js",
|
|
12
|
+
"require": "./dist/index.cjs.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "rollup -c",
|
|
23
|
+
"dev": "rollup -c -w",
|
|
24
|
+
"typecheck": "tsc --noEmit"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"wkt",
|
|
28
|
+
"geojson",
|
|
29
|
+
"geometry",
|
|
30
|
+
"parser",
|
|
31
|
+
"builder",
|
|
32
|
+
"gis",
|
|
33
|
+
"ogc",
|
|
34
|
+
"spatial"
|
|
35
|
+
],
|
|
36
|
+
"author": "",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
40
|
+
"rollup": "^4.40.0",
|
|
41
|
+
"tslib": "^2.8.1",
|
|
42
|
+
"typescript": "^5.8.3"
|
|
43
|
+
}
|
|
44
|
+
}
|