geo-types-cz 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.
@@ -0,0 +1,231 @@
1
+ /**
2
+ * 扩展的GIS类型定义
3
+ * 包括样式、符号化、图层等前端GIS开发中常用的类型
4
+ */
5
+
6
+ import { Feature, FeatureCollection, FeatureType, Properties } from './feature';
7
+ import { Geometry } from './geometry';
8
+
9
+ // 颜色类型
10
+ export type Color = string; // CSS颜色值,如 '#ff0000', 'red', 'rgb(255,0,0)'
11
+
12
+ // 样式类型
13
+ export interface Style {
14
+ // 填充样式
15
+ fill?: {
16
+ color?: Color;
17
+ opacity?: number;
18
+ };
19
+
20
+ // 描边样式
21
+ stroke?: {
22
+ color?: Color;
23
+ width?: number;
24
+ opacity?: number;
25
+ dashArray?: number[];
26
+ lineCap?: 'butt' | 'round' | 'square';
27
+ lineJoin?: 'miter' | 'round' | 'bevel';
28
+ };
29
+
30
+ // 点样式
31
+ marker?: {
32
+ size?: number;
33
+ color?: Color;
34
+ opacity?: number;
35
+ symbol?: 'circle' | 'square' | 'triangle' | 'star' | 'cross' | 'diamond';
36
+ };
37
+
38
+ // 文本样式
39
+ text?: {
40
+ field?: string; // 属性字段名
41
+ font?: string;
42
+ size?: number;
43
+ color?: Color;
44
+ haloColor?: Color;
45
+ haloWidth?: number;
46
+ offset?: [number, number];
47
+ anchor?: 'start' | 'middle' | 'end';
48
+ baseline?: 'top' | 'middle' | 'bottom';
49
+ };
50
+ }
51
+
52
+ // 带样式的要素
53
+ export interface StyledFeature<G extends Geometry = Geometry, P extends Properties = Properties> extends Feature<G, P> {
54
+ style?: Style;
55
+ }
56
+
57
+ // 带样式的要素集合
58
+ export interface StyledFeatureCollection<G extends Geometry = Geometry, P extends Properties = Properties> {
59
+ type: FeatureType.FeatureCollection;
60
+ features: StyledFeature<G, P>[];
61
+ style?: Style; // 默认样式
62
+ bbox?: [number, number, number, number] | [number, number, number, number, number, number];
63
+ }
64
+
65
+ // 图层类型
66
+ export enum LayerType {
67
+ Vector = 'vector',
68
+ Raster = 'raster',
69
+ TileLayer = 'tile'
70
+ }
71
+
72
+ // 基础图层接口
73
+ export interface BaseLayer {
74
+ id: string;
75
+ name: string;
76
+ type: LayerType;
77
+ visible?: boolean;
78
+ opacity?: number;
79
+ minZoom?: number;
80
+ maxZoom?: number;
81
+ metadata?: { [key: string]: any };
82
+ }
83
+
84
+ // 矢量图层
85
+ export interface VectorLayer extends BaseLayer {
86
+ type: LayerType.Vector;
87
+ data: FeatureCollection | StyledFeatureCollection;
88
+ style?: Style;
89
+ }
90
+
91
+ // 栅格图层
92
+ export interface RasterLayer extends BaseLayer {
93
+ type: LayerType.Raster;
94
+ url: string;
95
+ bounds?: [number, number, number, number];
96
+ }
97
+
98
+ // 瓦片图层
99
+ export interface TileLayer extends BaseLayer {
100
+ type: LayerType.TileLayer;
101
+ url: string; // 瓦片URL模板,如 'https://tile.openstreetmap.org/{z}/{x}/{y}.png'
102
+ attribution?: string;
103
+ subdomains?: string[];
104
+ }
105
+
106
+ // 图层联合类型
107
+ export type Layer = VectorLayer | RasterLayer | TileLayer;
108
+
109
+ // 地图视图配置
110
+ export interface MapView {
111
+ center: [number, number]; // [longitude, latitude]
112
+ zoom: number;
113
+ bearing?: number; // 地图旋转角度
114
+ pitch?: number; // 地图倾斜角度
115
+ bounds?: [number, number, number, number]; // [west, south, east, north]
116
+ }
117
+ // 点聚类配置。
118
+ export interface ClusterConfig {
119
+ enabled: boolean
120
+ distance: number
121
+ maxZoom: number
122
+ minPoints?: number
123
+ style?: {
124
+ cluster?: Style
125
+ clusterText?: {
126
+ font?: string
127
+ size?: number
128
+ color?: string
129
+ haloColor?: string
130
+ haloWidth?: number
131
+ }
132
+ }
133
+ }
134
+
135
+ // 地图配置
136
+ export interface MapConfig {
137
+ container: string | any; // DOM Element or string selector
138
+ view: MapView;
139
+ layers?: Layer[];
140
+ controls?: {
141
+ zoom?: boolean;
142
+ attribution?: boolean;
143
+ scale?: boolean;
144
+ fullscreen?: boolean;
145
+ };
146
+ interactions?: {
147
+ dragPan?: boolean;
148
+ scrollZoom?: boolean;
149
+ doubleClickZoom?: boolean;
150
+ keyboard?: boolean;
151
+ };
152
+ }
153
+
154
+ // 空间查询类型
155
+ export enum SpatialQueryType {
156
+ Intersects = 'intersects',
157
+ Contains = 'contains',
158
+ Within = 'within',
159
+ Touches = 'touches',
160
+ Crosses = 'crosses',
161
+ Overlaps = 'overlaps'
162
+ }
163
+
164
+ // 空间查询接口
165
+ export interface SpatialQuery {
166
+ type: SpatialQueryType;
167
+ geometry: Geometry;
168
+ buffer?: number; // 缓冲区距离
169
+ }
170
+
171
+ // 属性查询接口
172
+ export interface AttributeQuery {
173
+ field: string;
174
+ operator: '=' | '!=' | '>' | '<' | '>=' | '<=' | 'like' | 'in' | 'not in';
175
+ value: any;
176
+ }
177
+
178
+ // 复合查询接口
179
+ export interface Query {
180
+ spatial?: SpatialQuery;
181
+ attributes?: AttributeQuery[];
182
+ logic?: 'and' | 'or';
183
+ }
184
+
185
+ // 类型守卫函数
186
+ export function isVectorLayer(layer: Layer): layer is VectorLayer {
187
+ return layer.type === LayerType.Vector;
188
+ }
189
+
190
+ export function isRasterLayer(layer: Layer): layer is RasterLayer {
191
+ return layer.type === LayerType.Raster;
192
+ }
193
+
194
+ export function isTileLayer(layer: Layer): layer is TileLayer {
195
+ return layer.type === LayerType.TileLayer;
196
+ }
197
+
198
+ // 工具函数
199
+ export function createVectorLayer(
200
+ id: string,
201
+ name: string,
202
+ data: FeatureCollection,
203
+ style?: Style
204
+ ): VectorLayer {
205
+ return {
206
+ id,
207
+ name,
208
+ type: LayerType.Vector,
209
+ data,
210
+ style,
211
+ visible: true,
212
+ opacity: 1
213
+ };
214
+ }
215
+
216
+ export function createTileLayer(
217
+ id: string,
218
+ name: string,
219
+ url: string,
220
+ attribution?: string
221
+ ): TileLayer {
222
+ return {
223
+ id,
224
+ name,
225
+ type: LayerType.TileLayer,
226
+ url,
227
+ attribution,
228
+ visible: true,
229
+ opacity: 1
230
+ };
231
+ }
package/src/feature.ts ADDED
@@ -0,0 +1,70 @@
1
+ /**
2
+ * GeoJSON要素类型定义
3
+ */
4
+
5
+ import { Geometry } from './geometry';
6
+
7
+ // 要素类型枚举
8
+ export enum FeatureType {
9
+ Feature = 'Feature',
10
+ FeatureCollection = 'FeatureCollection'
11
+ }
12
+
13
+ // 属性类型 - 可以是任意JSON值
14
+ export type Properties = { [key: string]: any } | null;
15
+
16
+ // 要素接口
17
+ export interface Feature<G extends Geometry = Geometry, P extends Properties = Properties> {
18
+ type: FeatureType.Feature;
19
+ geometry: G | null;
20
+ properties: P;
21
+ id?: string | number;
22
+ bbox?: [number, number, number, number] | [number, number, number, number, number, number];
23
+ }
24
+
25
+ // 要素集合接口
26
+ export interface FeatureCollection<G extends Geometry = Geometry, P extends Properties = Properties> {
27
+ type: FeatureType.FeatureCollection;
28
+ features: Feature<G, P>[];
29
+ bbox?: [number, number, number, number] | [number, number, number, number, number, number];
30
+ }
31
+
32
+ // GeoJSON对象联合类型
33
+ export type GeoJSONObject = Geometry | Feature | FeatureCollection;
34
+
35
+ // 类型守卫函数
36
+ export function isFeature(obj: GeoJSONObject): obj is Feature {
37
+ return obj.type === FeatureType.Feature;
38
+ }
39
+
40
+ export function isFeatureCollection(obj: GeoJSONObject): obj is FeatureCollection {
41
+ return obj.type === FeatureType.FeatureCollection;
42
+ }
43
+
44
+ // 工具函数
45
+ export function createFeature<G extends Geometry, P extends Properties>(
46
+ geometry: G | null,
47
+ properties: P,
48
+ id?: string | number
49
+ ): Feature<G, P> {
50
+ const feature: Feature<G, P> = {
51
+ type: FeatureType.Feature,
52
+ geometry,
53
+ properties
54
+ };
55
+
56
+ if (id !== undefined) {
57
+ feature.id = id;
58
+ }
59
+
60
+ return feature;
61
+ }
62
+
63
+ export function createFeatureCollection<G extends Geometry, P extends Properties>(
64
+ features: Feature<G, P>[]
65
+ ): FeatureCollection<G, P> {
66
+ return {
67
+ type: FeatureType.FeatureCollection,
68
+ features
69
+ };
70
+ }
@@ -0,0 +1,138 @@
1
+ /**
2
+ * GeoJSON几何类型定义
3
+ * 基于RFC 7946标准
4
+ */
5
+
6
+ import { isFeature } from "./feature";
7
+
8
+ // 基础位置类型
9
+ export type Position = [number, number] | [number, number, number];
10
+
11
+ // 几何类型枚举
12
+ export enum GeometryType {
13
+ Point = 'Point',
14
+ LineString = 'LineString',
15
+ Polygon = 'Polygon',
16
+ MultiPoint = 'MultiPoint',
17
+ MultiLineString = 'MultiLineString',
18
+ MultiPolygon = 'MultiPolygon',
19
+ GeometryCollection = 'GeometryCollection'
20
+ }
21
+
22
+ // 基础几何接口
23
+ export interface BaseGeometry {
24
+ type: GeometryType;
25
+ bbox?: [number, number, number, number] | [number, number, number, number, number, number];
26
+ }
27
+
28
+ // 点几何
29
+ export interface Point extends BaseGeometry {
30
+ type: GeometryType.Point;
31
+ coordinates: Position;
32
+ }
33
+
34
+ // 线几何
35
+ export interface LineString extends BaseGeometry {
36
+ type: GeometryType.LineString;
37
+ coordinates: Position[];
38
+ }
39
+
40
+ // 多边形几何
41
+ export interface Polygon extends BaseGeometry {
42
+ type: GeometryType.Polygon;
43
+ coordinates: Position[][];
44
+ }
45
+
46
+ // 多点几何
47
+ export interface MultiPoint extends BaseGeometry {
48
+ type: GeometryType.MultiPoint;
49
+ coordinates: Position[];
50
+ }
51
+
52
+ // 多线几何
53
+ export interface MultiLineString extends BaseGeometry {
54
+ type: GeometryType.MultiLineString;
55
+ coordinates: Position[][];
56
+ }
57
+
58
+ // 多多边形几何
59
+ export interface MultiPolygon extends BaseGeometry {
60
+ type: GeometryType.MultiPolygon;
61
+ coordinates: Position[][][];
62
+ }
63
+
64
+ // 几何集合
65
+ export interface GeometryCollection extends BaseGeometry {
66
+ type: GeometryType.GeometryCollection;
67
+ geometries: Geometry[];
68
+ }
69
+
70
+ // 联合几何类型
71
+ export type Geometry =
72
+ | Point
73
+ | LineString
74
+ | Polygon
75
+ | MultiPoint
76
+ | MultiLineString
77
+ | MultiPolygon
78
+ | GeometryCollection;
79
+
80
+ // 几何类型守卫函数
81
+ export function isPoint(geometry: Geometry): geometry is Point {
82
+ return geometry.type === GeometryType.Point;
83
+ }
84
+
85
+ export function isLineString(geometry: Geometry): geometry is LineString {
86
+ return geometry.type === GeometryType.LineString;
87
+ }
88
+
89
+ export function isPolygon(geometry: Geometry): geometry is Polygon {
90
+ return geometry.type === GeometryType.Polygon;
91
+ }
92
+
93
+ export function isMultiPoint(geometry: Geometry): geometry is MultiPoint {
94
+ return geometry.type === GeometryType.MultiPoint;
95
+ }
96
+
97
+ export function isMultiLineString(geometry: Geometry): geometry is MultiLineString {
98
+ return geometry.type === GeometryType.MultiLineString;
99
+ }
100
+
101
+ export function isMultiPolygon(geometry: Geometry): geometry is MultiPolygon {
102
+ return geometry.type === GeometryType.MultiPolygon;
103
+ }
104
+
105
+ export function isGeometryCollection(geometry: Geometry): geometry is GeometryCollection {
106
+ return geometry.type === GeometryType.GeometryCollection;
107
+ }
108
+
109
+
110
+ // 经纬度校验
111
+ export function validateCoordinates(coordinates: readonly [number, number]): boolean {
112
+ const [lon, lat] = coordinates
113
+ return lon >= -180 && lon <= 180 && lat >= -90 && lat <= 90
114
+ }
115
+
116
+ //集合体验证
117
+ export function validateGeometry(geometry: any): boolean {
118
+ if (!geometry || !geometry.type) return false
119
+
120
+ switch (geometry.type) {
121
+ case 'Point':
122
+ return isPoint(geometry) && validateCoordinates(geometry.coordinates as [number, number])
123
+ case 'LineString':
124
+ return isLineString(geometry) &&
125
+ geometry.coordinates.every((coord: any) => validateCoordinates(coord))
126
+ case 'Polygon':
127
+ return isPolygon(geometry) &&
128
+ geometry.coordinates.every((ring: any) =>
129
+ ring.every((coord: any) => validateCoordinates(coord))
130
+ )
131
+ default:
132
+ return false
133
+ }
134
+ }
135
+ // 验证要素的几何体
136
+ export function validateFeatureGeometry(feature: any): boolean {
137
+ return isFeature(feature) && validateGeometry(feature.geometry)
138
+ }
package/src/index.ts ADDED
@@ -0,0 +1,156 @@
1
+ /**
2
+ * geo-types-cz
3
+ * Complete TypeScript type definitions for GeoJSON and extended GIS types
4
+ *
5
+ * 这个包提供了完整的GeoJSON类型定义以及扩展的GIS类型,
6
+ * 适用于前端地理信息系统开发。
7
+ */
8
+
9
+ // 导出几何类型
10
+ export {
11
+ Position,
12
+ GeometryType,
13
+ BaseGeometry,
14
+ Point,
15
+ LineString,
16
+ Polygon,
17
+ MultiPoint,
18
+ MultiLineString,
19
+ MultiPolygon,
20
+ GeometryCollection,
21
+ Geometry,
22
+ isPoint,
23
+ isLineString,
24
+ isPolygon,
25
+ isMultiPoint,
26
+ isMultiLineString,
27
+ isMultiPolygon,
28
+ isGeometryCollection,
29
+ validateCoordinates,
30
+ validateFeatureGeometry
31
+ } from './geometry';
32
+
33
+ // 导出要素类型
34
+ export {
35
+ FeatureType,
36
+ Properties,
37
+ Feature,
38
+ FeatureCollection,
39
+ GeoJSONObject,
40
+ isFeature,
41
+ isFeatureCollection,
42
+ createFeature,
43
+ createFeatureCollection
44
+ } from './feature';
45
+
46
+ // 导出坐标参考系统类型
47
+ export {
48
+ CRSType,
49
+ BaseCRS,
50
+ NamedCRS,
51
+ LinkedCRS,
52
+ CRS,
53
+ CommonCRS,
54
+ isNamedCRS,
55
+ isLinkedCRS,
56
+ createNamedCRS,
57
+ createLinkedCRS
58
+ } from './crs';
59
+
60
+ // 导出边界框类型
61
+ export {
62
+ BBox2D,
63
+ BBox3D,
64
+ BBox,
65
+ BoundingBox,
66
+ is2DBBox,
67
+ is3DBBox,
68
+ createBBox2D,
69
+ createBBox3D,
70
+ bboxToObject,
71
+ objectToBBox,
72
+ unionBBox,
73
+ isPositionInBBox,
74
+ getBBoxCenter
75
+ } from './bbox';
76
+
77
+ // 导出扩展类型
78
+ export {
79
+ Color,
80
+ Style,
81
+ StyledFeature,
82
+ StyledFeatureCollection,
83
+ LayerType,
84
+ BaseLayer,
85
+ VectorLayer,
86
+ RasterLayer,
87
+ TileLayer,
88
+ Layer,
89
+ MapView,
90
+ MapConfig,
91
+ SpatialQueryType,
92
+ SpatialQuery,
93
+ AttributeQuery,
94
+ Query,
95
+ isVectorLayer,
96
+ isRasterLayer,
97
+ isTileLayer,
98
+ ClusterConfig
99
+ } from './extensions';
100
+
101
+ // 导出工具函数
102
+ export {
103
+ degreesToRadians,
104
+ radiansToDegrees,
105
+ calculateDistance,
106
+ calculateBearing,
107
+ calculateDestination,
108
+ calculateLineLength,
109
+ calculatePolygonArea,
110
+ calculateGeometryBBox,
111
+ calculateFeatureCollectionBBox,
112
+ simplifyLineString,
113
+ createBuffer
114
+ } from './utils';
115
+
116
+ // 版本信息
117
+ export const VERSION = '1.0.0';
118
+
119
+ // 默认导出
120
+ export default {
121
+ VERSION
122
+ };
123
+
124
+ /**
125
+ * 使用示例:
126
+ *
127
+ * ```typescript
128
+ * import {
129
+ * Point,
130
+ * Feature,
131
+ * FeatureCollection,
132
+ * createFeature,
133
+ * calculateDistance
134
+ * } from 'geo-types-cz';
135
+ *
136
+ * // 创建点几何
137
+ * const point: Point = {
138
+ * type: 'Point',
139
+ * coordinates: [116.3974, 39.9093] // 北京天安门
140
+ * };
141
+ *
142
+ * // 创建要素
143
+ * const feature = createFeature(point, {
144
+ * name: '天安门',
145
+ * city: '北京'
146
+ * });
147
+ *
148
+ * // 计算两点间距离
149
+ * const distance = calculateDistance(
150
+ * [116.3974, 39.9093], // 天安门
151
+ * [121.4737, 31.2304] // 上海外滩
152
+ * );
153
+ *
154
+ * console.log(`北京到上海的距离: ${Math.round(distance / 1000)}公里`);
155
+ * ```
156
+ */