1 | 'use strict'
|
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 | class Point {
|
48 | constructor () {
|
49 | this.x = 0
|
50 | this.y = 0
|
51 | this.z = null
|
52 | this.m = null
|
53 | }
|
54 | }
|
55 |
|
56 | const parsePoints = (buffer, count) => {
|
57 |
|
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 |
|
75 | const parseZ = (buffer, points) => {
|
76 |
|
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 |
|
88 | const parseM = (buffer, points) => {
|
89 |
|
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 |
|
101 | const parseFigures = (buffer, count, properties) => {
|
102 |
|
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 |
|
133 | const parseShapes = (buffer, count, properties) => {
|
134 |
|
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 |
|
168 | const parseSegments = (buffer, count) => {
|
169 |
|
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 |
|
185 | const parseGeography = buffer => {
|
186 |
|
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 |
|
202 |
|
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 |
|
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 |
|
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 |
|
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 |
|
253 |
|
254 | value.figures = parseFigures(buffer, numberOfFigures, properties)
|
255 |
|
256 |
|
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 |
|
269 |
|
270 | value.shapes = parseShapes(buffer, numberOfShapes, properties)
|
271 |
|
272 |
|
273 |
|
274 | if (value.version === 2) {
|
275 | const numberOfSegments = buffer.readUInt32LE(buffer.position)
|
276 | buffer.position += 4
|
277 |
|
278 |
|
279 |
|
280 | value.segments = parseSegments(buffer, numberOfSegments)
|
281 |
|
282 |
|
283 | } else {
|
284 | value.segments = []
|
285 | }
|
286 |
|
287 | return value
|
288 | }
|
289 |
|
290 | module.exports.PARSERS = {
|
291 | geography (buffer) {
|
292 | return parseGeography(buffer)
|
293 | },
|
294 |
|
295 | geometry (buffer) {
|
296 | return parseGeography(buffer)
|
297 | }
|
298 | }
|