UNPKG

5.77 kBJavaScriptView Raw
1'use strict'
2
3/* const FIGURE = {
4 INTERIOR_RING: 0x00,
5 STROKE: 0x01,
6 EXTERIOR_RING: 0x02
7};
8
9const FIGURE_V2 = {
10 POINT: 0x00,
11 LINE: 0x01,
12 ARC: 0x02,
13 COMPOSITE_CURVE: 0x03
14};
15
16const SHAPE = {
17 POINT: 0x01,
18 LINESTRING: 0x02,
19 POLYGON: 0x03,
20 MULTIPOINT: 0x04,
21 MULTILINESTRING: 0x05,
22 MULTIPOLYGON: 0x06,
23 GEOMETRY_COLLECTION: 0x07
24};
25
26const SHAPE_V2 = {
27 POINT: 0x01,
28 LINESTRING: 0x02,
29 POLYGON: 0x03,
30 MULTIPOINT: 0x04,
31 MULTILINESTRING: 0x05,
32 MULTIPOLYGON: 0x06,
33 GEOMETRY_COLLECTION: 0x07,
34 CIRCULAR_STRING: 0x08,
35 COMPOUND_CURVE: 0x09,
36 CURVE_POLYGON: 0x0A,
37 FULL_GLOBE: 0x0B
38};
39
40const SEGMENT = {
41 LINE: 0x00,
42 ARC: 0x01,
43 FIRST_LINE: 0x02,
44 FIRST_ARC: 0x03
45}; */
46
47class Point {
48 constructor () {
49 this.x = 0
50 this.y = 0
51 this.z = null
52 this.m = null
53 }
54}
55
56const parsePoints = (buffer, count) => {
57 // s2.1.5 + s2.1.6
58
59 const points = []
60 if (count < 1) {
61 return points
62 }
63
64 for (let i = 1; i <= count; i++) {
65 const point = new Point()
66 points.push(point)
67 point.x = buffer.readDoubleLE(buffer.position)
68 point.y = buffer.readDoubleLE(buffer.position + 8)
69 buffer.position += 16
70 }
71
72 return points
73}
74
75const parseZ = (buffer, points) => {
76 // s2.1.1 + s.2.1.2
77
78 if (points < 1) {
79 return
80 }
81
82 points.forEach(point => {
83 point.z = buffer.readDoubleLE(buffer.position)
84 buffer.position += 8
85 })
86}
87
88const parseM = (buffer, points) => {
89 // s2.1.1 + s.2.1.2
90
91 if (points < 1) {
92 return
93 }
94
95 points.forEach(point => {
96 point.m = buffer.readDoubleLE(buffer.position)
97 buffer.position += 8
98 })
99}
100
101const parseFigures = (buffer, count, properties) => {
102 // s2.1.3
103
104 const figures = []
105 if (count < 1) {
106 return figures
107 }
108
109 if (properties.P) {
110 figures.push({
111 attribute: 0x01,
112 pointOffset: 0
113 })
114 } else if (properties.L) {
115 figures.push({
116 attribute: 0x01,
117 pointOffset: 0
118 })
119 } else {
120 for (let i = 1; i <= count; i++) {
121 figures.push({
122 attribute: buffer.readUInt8(buffer.position),
123 pointOffset: buffer.readInt32LE(buffer.position + 1)
124 })
125
126 buffer.position += 5
127 }
128 }
129
130 return figures
131}
132
133const parseShapes = (buffer, count, properties) => {
134 // s2.1.4
135
136 const shapes = []
137 if (count < 1) {
138 return shapes
139 }
140
141 if (properties.P) {
142 shapes.push({
143 parentOffset: -1,
144 figureOffset: 0,
145 type: 0x01
146 })
147 } else if (properties.L) {
148 shapes.push({
149 parentOffset: -1,
150 figureOffset: 0,
151 type: 0x02
152 })
153 } else {
154 for (let i = 1; i <= count; i++) {
155 shapes.push({
156 parentOffset: buffer.readInt32LE(buffer.position),
157 figureOffset: buffer.readInt32LE(buffer.position + 4),
158 type: buffer.readUInt8(buffer.position + 8)
159 })
160
161 buffer.position += 9
162 }
163 }
164
165 return shapes
166}
167
168const parseSegments = (buffer, count) => {
169 // s2.1.7
170
171 const segments = []
172 if (count < 1) {
173 return segments
174 }
175
176 for (let i = 1; i <= count; i++) {
177 segments.push({ type: buffer.readUInt8(buffer.position) })
178
179 buffer.position++
180 }
181
182 return segments
183}
184
185const parseGeography = buffer => {
186 // s2.1.1 + s.2.1.2
187
188 const srid = buffer.readInt32LE(0)
189 if (srid === -1) {
190 return null
191 }
192
193 const value = {
194 srid,
195 version: buffer.readUInt8(4)
196 }
197
198 const flags = buffer.readUInt8(5)
199 buffer.position = 6
200
201 // console.log("srid", srid)
202 // console.log("version", version)
203
204 const properties = {
205 Z: (flags & (1 << 0)) > 0,
206 M: (flags & (1 << 1)) > 0,
207 V: (flags & (1 << 2)) > 0,
208 P: (flags & (1 << 3)) > 0,
209 L: (flags & (1 << 4)) > 0
210 }
211
212 if (value.version === 2) {
213 properties.H = (flags & (1 << 3)) > 0
214 }
215
216 // console.log("properties", properties);
217
218 let numberOfPoints
219 if (properties.P) {
220 numberOfPoints = 1
221 } else if (properties.L) {
222 numberOfPoints = 2
223 } else {
224 numberOfPoints = buffer.readUInt32LE(buffer.position)
225 buffer.position += 4
226 }
227
228 // console.log("numberOfPoints", numberOfPoints)
229
230 value.points = parsePoints(buffer, numberOfPoints)
231
232 if (properties.Z) {
233 parseZ(buffer, value.points)
234 }
235
236 if (properties.M) {
237 parseM(buffer, value.points)
238 }
239
240 // console.log("points", points)
241
242 let numberOfFigures
243 if (properties.P) {
244 numberOfFigures = 1
245 } else if (properties.L) {
246 numberOfFigures = 1
247 } else {
248 numberOfFigures = buffer.readUInt32LE(buffer.position)
249 buffer.position += 4
250 }
251
252 // console.log("numberOfFigures", numberOfFigures)
253
254 value.figures = parseFigures(buffer, numberOfFigures, properties)
255
256 // console.log("figures", figures)
257
258 let numberOfShapes
259 if (properties.P) {
260 numberOfShapes = 1
261 } else if (properties.L) {
262 numberOfShapes = 1
263 } else {
264 numberOfShapes = buffer.readUInt32LE(buffer.position)
265 buffer.position += 4
266 }
267
268 // console.log("numberOfShapes", numberOfShapes)
269
270 value.shapes = parseShapes(buffer, numberOfShapes, properties)
271
272 // console.log( "shapes", shapes)
273
274 if (value.version === 2) {
275 const numberOfSegments = buffer.readUInt32LE(buffer.position)
276 buffer.position += 4
277
278 // console.log("numberOfSegments", numberOfSegments)
279
280 value.segments = parseSegments(buffer, numberOfSegments)
281
282 // console.log("segments", segments)
283 } else {
284 value.segments = []
285 }
286
287 return value
288}
289
290module.exports.PARSERS = {
291 geography (buffer) {
292 return parseGeography(buffer)
293 },
294
295 geometry (buffer) {
296 return parseGeography(buffer)
297 }
298}