-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgml.ts
More file actions
244 lines (216 loc) · 5.65 KB
/
Copy pathgml.ts
File metadata and controls
244 lines (216 loc) · 5.65 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { XMLParser, X2jOptions } from "fast-xml-parser";
import type { Data, DataRange, Feature, Geometry } from "../types";
import { f, convertToCoordinates, FetchOptions } from "./utils";
export async function fetchGMLData(
data: Data,
_range?: DataRange,
options?: FetchOptions,
): Promise<Feature[] | void> {
const xmlDataStr = data.url ? await (await f(data.url, options)).text() : data.value;
return handler(xmlDataStr);
}
function handler(xmlDataStr: string) {
const cleanXml = xmlDataStr.replace(/([a-zA-Z]+:)/g, "");
const gmlData = parseBuffer(cleanXml);
return transform(gmlData);
}
function parseBuffer(xmlDataStr: string) {
const options: Partial<X2jOptions> = {
ignoreAttributes: false,
attributeNamePrefix: "",
};
const parser = new XMLParser(options);
return parser.parse(xmlDataStr) as GMLFeatureCollection;
}
function transform(gmlFeatureCollection: GMLFeatureCollection): Feature[] {
const stations = gmlFeatureCollection.FeatureCollection.featureMember;
const result: Feature[] = [];
if (stations) {
for (const station of stations) {
const {
Shape: shape,
name: name,
WMO_ID: WMO_ID,
alternative_ID: alternative_ID,
elevation: elevation,
hash: hash,
fid: fid,
} = station["stations_mix-obs"];
const properties = {
name,
WMO_ID,
alternative_ID,
elevation,
hash,
fid,
};
const geometry = getGeometryOfItem(shape);
const feature: Feature = {
type: "feature",
id: fid,
properties,
geometry,
};
result.push(feature);
}
}
return result;
}
export function getGeometryOfItem(shape: Shape): Geometry {
let geometry: Geometry = {
type: "Point",
coordinates: [1],
};
if ("Point" in shape) {
const coordinates = getCoordinatesFromPoint(shape.Point);
if (coordinates) {
geometry = {
type: "Point",
coordinates: convertToCoordinates(coordinates),
};
}
} else if ("MultiPoint" in shape) {
geometry = {
type: "MultiPoint",
coordinates: shape.MultiPoint.pointMember.map(point => {
const coordinates = getCoordinatesFromPoint(point.Point);
return convertToCoordinates(coordinates);
}),
};
} else if ("LineString" in shape) {
const lineString = shape.LineString;
geometry = {
type: "LineString",
coordinates: lineString.posList.split(" ").map(point => convertToCoordinates(point)),
};
} else if ("MultiLineString" in shape) {
geometry = {
type: "MultiLineString",
coordinates: shape.MultiLineString.lineStringMember.map(lineString => {
const line = lineString.LineString;
return line.posList.split(" ").map(point => convertToCoordinates(point));
}),
};
} else if ("MultiPolygon" in shape) {
let interior: number[][][] = [[[1]]];
geometry = {
type: "MultiPolygon",
coordinates: shape.MultiPolygon.polygonMember.map(polygon => {
const exterior = polygon.Polygon.exterior.LinearRing.posList
.split(" ")
.map(point => convertToCoordinates(point));
if (polygon.Polygon.interior) {
interior = polygon.Polygon.interior.map(interiorRing => {
const ring = interiorRing.LinearRing;
return ring.posList.split(" ").map(point => convertToCoordinates(point));
});
}
return [exterior, ...interior];
}),
};
} else if ("Polygon" in shape) {
const polygon = shape.Polygon;
const exterior = polygon.exterior.LinearRing.posList
.split(" ")
.map(point => convertToCoordinates(point));
let interior: number[][][] = [[[]]];
if (polygon.interior) {
interior = polygon.interior.map(interiorRing => {
const ring = interiorRing.LinearRing;
return ring.posList.split(" ").map(point => convertToCoordinates(point));
});
}
geometry = {
type: "Polygon",
coordinates: [exterior, ...interior],
};
} else {
throw new Error("unsupported shape");
}
return geometry;
}
function getCoordinatesFromPoint(point: Coordinates) {
if (point.coordinates) {
return point.coordinates;
} else if (point.pos) {
return point.pos;
}
throw new Error("something went wrong");
}
type Xml = {
version: string;
encoding: string;
};
type Coordinates = {
pos?: string;
coordinates?: string;
srsName?: string;
};
type Point = {
Point: Coordinates;
};
type MultiPoint = {
MultiPoint: {
pointMember: Point[];
};
};
type LineString = {
LineString: Coordinates & {
posList: string;
};
};
type MultiLineString = {
MultiLineString: {
lineStringMember: LineString[];
};
};
type Polygon = {
Polygon: {
exterior: {
LinearRing: Coordinates & {
posList: string;
};
};
interior?: {
LinearRing: Coordinates & {
posList: string;
};
}[];
};
};
type MultiPolygon = {
MultiPolygon: {
polygonMember: Polygon[];
};
};
export type Shape = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon;
type StationsMixObs = {
"stations_mix-obs": {
Shape: Shape;
name?: string;
WMO_ID?: number;
alternative_ID?: string;
elevation?: string;
hash?: number;
fid: string;
};
};
type Box = {
Box: Coordinates;
};
type BoundedBy = {
boundedBy: Box;
};
type FeatureCollection1 = {
boundedBy: BoundedBy;
featureMember?: StationsMixObs[];
"xmlns:mm": string;
"xmlns:gml": string;
"xmlns:wfs": string;
"xmlns:xsi": string;
"xsi:schemaLocation": string;
};
type GMLFeatureCollection = {
"?xml": Xml;
FeatureCollection: FeatureCollection1;
};