-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeojson.helper.ts
227 lines (210 loc) · 5.9 KB
/
geojson.helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import {
BBox,
Feature,
FeatureCollection,
GeoJsonGeometryTypes,
GeoJsonProperties,
Geometry,
GeometryCollection,
Id,
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
Position,
} from "./geojson";
export function isNumber(num: any): boolean {
return !isNaN(num) && num !== null && !Array.isArray(num);
}
export function featureCollection<G extends Geometry, P = GeoJsonProperties>(
features: Array<Feature<G, P>>,
options: { bbox?: BBox; id?: Id } = {}
): FeatureCollection<G, P> {
const fc: any = { type: "FeatureCollection" };
if (options.id) {
fc.id = options.id;
}
if (options.bbox) {
fc.bbox = options.bbox;
}
fc.features = features;
return fc;
}
export function feature<G extends Geometry, P = GeoJsonProperties>(
geometry: G | null,
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): Feature<G, P> {
const feat: any = { type: "Feature" };
if (options.id === 0 || options.id) {
feat.id = options.id;
}
if (options.bbox) {
feat.bbox = options.bbox;
}
feat.properties = properties || {};
feat.geometry = geometry;
return feat;
}
export function geometryCollection<P = GeoJsonProperties>(
geometries: Array<
Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon
>,
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): Feature<GeometryCollection, P> {
const geometry: GeometryCollection = {
type: "GeometryCollection",
geometries,
};
return feature(geometry, properties, options);
}
export function geometry(
type: Omit<GeoJsonGeometryTypes, "GeometryCollection">,
coordinates: Position | Position[] | Position[][] | Position[][][],
_options: Record<string, never> = {}
): Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon {
switch (type) {
case "Point":
return point(coordinates as Position).geometry;
case "LineString":
return lineString(coordinates as Position[]).geometry;
case "Polygon":
return polygon(coordinates as Position[][]).geometry;
case "MultiPoint":
return multiPoint(coordinates as Position[]).geometry;
case "MultiLineString":
return multiLineString(coordinates as Position[][]).geometry;
case "MultiPolygon":
return multiPolygon(coordinates as Position[][][]).geometry;
default:
throw new Error(type + " 无效");
}
}
export function point<P = GeoJsonProperties>(
coordinates: Position,
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): Feature<Point, P> {
if (!coordinates) {
throw new Error("坐标不能加载");
}
if (!Array.isArray(coordinates)) {
throw new Error("坐标不是数组");
}
if (coordinates.length < 2) {
throw new Error("坐标至少有 2 个数字");
}
if (!isNumber(coordinates[0]) || !isNumber(coordinates[1])) {
throw new Error("坐标必须是数字");
}
const geometry: Point = {
type: "Point",
coordinates,
};
return feature(geometry, properties, options);
}
export function points<P = GeoJsonProperties>(
coordinates: Position[],
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): FeatureCollection<Point, P> {
return featureCollection(
coordinates.map((coords) => point(coords, properties)),
options
);
}
export function multiPoint<P = GeoJsonProperties>(
coordinates: Position[],
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): Feature<MultiPoint, P> {
const geometry: MultiPoint = {
type: "MultiPoint",
coordinates,
};
return feature(geometry, properties, options);
}
export function lineString<P = GeoJsonProperties>(
coordinates: Position[],
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): Feature<LineString, P> {
if (coordinates.length < 2) {
throw new Error("坐标必须是 2 个或以上点的数组");
}
const geometry: LineString = {
type: "LineString",
coordinates,
};
return feature(geometry, properties, options);
}
export function lineStrings<P = GeoJsonProperties>(
coordinates: Position[][],
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): FeatureCollection<LineString, P> {
return featureCollection(
coordinates.map((coords) => lineString(coords, properties)),
options
);
}
export function multiLineString<P = GeoJsonProperties>(
coordinates: Position[][],
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): Feature<MultiLineString, P> {
const geometry: MultiLineString = {
type: "MultiLineString",
coordinates,
};
return feature(geometry, properties, options);
}
export function polygon<P = GeoJsonProperties>(
coordinates: Position[][],
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): Feature<Polygon, P> {
for (const ring of coordinates) {
if (ring.length < 4) {
throw new Error("每一个多边形至少要有 4 个点");
}
if (ring[ring.length - 1].length !== ring[0].length) {
throw new Error("第一个点和最后一个点不相同");
}
for (let j = 0; j < ring[ring.length - 1].length; j++) {
// 检查第一个位置和最后一个位置经纬度是否相同
if (ring[ring.length - 1][j] !== ring[0][j]) {
throw new Error("第一个点和最后一个点不相同");
}
}
}
const geometry: Polygon = {
type: "Polygon",
coordinates,
};
return feature(geometry, properties, options);
}
export function polygons<P = GeoJsonProperties>(
coordinates: Position[][][],
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): FeatureCollection<Polygon, P> {
return featureCollection(
coordinates.map((coords) => polygon(coords, properties)),
options
);
}
export function multiPolygon<P = GeoJsonProperties>(
coordinates: Position[][][],
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
): Feature<MultiPolygon, P> {
const geometry: MultiPolygon = {
type: "MultiPolygon",
coordinates,
};
return feature(geometry, properties, options);
}