/src/geos/src/io/WKBWriter.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2005-2006 Refractions Research Inc. |
7 | | * |
8 | | * This is free software; you can redistribute and/or modify it under |
9 | | * the terms of the GNU Lesser General Public Licence as published |
10 | | * by the Free Software Foundation. |
11 | | * See the COPYING file for more information. |
12 | | * |
13 | | ********************************************************************** |
14 | | * |
15 | | * Last port: io/WKBWriter.java rev. 1.1 (JTS-1.7) |
16 | | * |
17 | | **********************************************************************/ |
18 | | |
19 | | #include <geos/io/WKBWriter.h> |
20 | | #include <geos/io/WKBReader.h> |
21 | | #include <geos/io/ByteOrderValues.h> |
22 | | #include <geos/io/CheckOrdinatesFilter.h> |
23 | | #include <geos/util/IllegalArgumentException.h> |
24 | | #include <geos/geom/Coordinate.h> |
25 | | #include <geos/geom/CompoundCurve.h> |
26 | | #include <geos/geom/CurvePolygon.h> |
27 | | #include <geos/geom/Point.h> |
28 | | #include <geos/geom/LinearRing.h> |
29 | | #include <geos/geom/LineString.h> |
30 | | #include <geos/geom/Polygon.h> |
31 | | #include <geos/geom/MultiPoint.h> |
32 | | #include <geos/geom/MultiLineString.h> |
33 | | #include <geos/geom/MultiPolygon.h> |
34 | | #include <geos/geom/CoordinateSequence.h> |
35 | | #include <geos/geom/PrecisionModel.h> |
36 | | |
37 | | #include <ostream> |
38 | | #include <sstream> |
39 | | #include <cassert> |
40 | | |
41 | | #include "geos/util.h" |
42 | | |
43 | | #undef DEBUG_WKB_WRITER |
44 | | |
45 | | #if DEBUG_WKB_READER |
46 | | #include <iostream> |
47 | | #endif |
48 | | |
49 | | |
50 | | using namespace geos::geom; |
51 | | |
52 | | namespace geos { |
53 | | namespace io { // geos.io |
54 | | |
55 | | WKBWriter::WKBWriter(uint8_t dims, int bo, bool srid, int flv) |
56 | 18.3k | : defaultOutputDimension(dims) |
57 | 18.3k | , outputOrdinates(getOutputOrdinates(OrdinateSet::createXYZM())) |
58 | 18.3k | , byteOrder(bo) |
59 | 18.3k | , flavor(flv) |
60 | 18.3k | , includeSRID(srid) |
61 | 18.3k | , outStream(nullptr) |
62 | 18.3k | { |
63 | 18.3k | if(dims < 2 || dims > 4) { |
64 | 0 | throw util::IllegalArgumentException("WKB output dimension must be 2, 3, or 4"); |
65 | 0 | } |
66 | 18.3k | } |
67 | | |
68 | | |
69 | | /* public */ |
70 | | void |
71 | | WKBWriter::setOutputDimension(uint8_t dims) |
72 | 0 | { |
73 | 0 | if(dims < 2 || dims > 4) { |
74 | 0 | throw util::IllegalArgumentException("WKB output dimension must be 2, 3, or 4"); |
75 | 0 | } |
76 | 0 | defaultOutputDimension = dims; |
77 | 0 | } |
78 | | |
79 | | |
80 | | /* public */ |
81 | | void |
82 | | WKBWriter::setFlavor(int newFlavor) |
83 | 0 | { |
84 | 0 | if (newFlavor != WKBConstants::wkbIso && |
85 | 0 | newFlavor != WKBConstants::wkbExtended) { |
86 | 0 | throw util::IllegalArgumentException("Invalid WKB output flavour"); |
87 | 0 | } |
88 | 0 | flavor = newFlavor; |
89 | 0 | } |
90 | | |
91 | | |
92 | | void |
93 | | WKBWriter::writeHEX(const Geometry& g, std::ostream& os) |
94 | 0 | { |
95 | | // setup input/output stream |
96 | 0 | std::stringstream stream; |
97 | | |
98 | | // write the geometry in wkb format |
99 | 0 | this->write(g, stream); |
100 | | |
101 | | // convert to HEX |
102 | 0 | WKBReader::printHEX(stream, os); |
103 | 0 | } |
104 | | |
105 | | void |
106 | | WKBWriter::write(const Geometry& g, std::ostream& os) |
107 | 6.46M | { |
108 | 6.46M | OrdinateSet inputOrdinates = OrdinateSet::createXY(); |
109 | 6.46M | inputOrdinates.setM(g.hasM()); |
110 | 6.46M | inputOrdinates.setZ(g.hasZ()); |
111 | 6.46M | outputOrdinates = getOutputOrdinates(inputOrdinates); |
112 | | |
113 | 6.46M | outStream = &os; |
114 | | |
115 | 6.46M | switch(g.getGeometryTypeId()) { |
116 | 5.74M | case GEOS_POINT: writePoint(static_cast<const Point&>(g)); break; |
117 | 7.87k | case GEOS_LINESTRING: |
118 | 8.13k | case GEOS_LINEARRING: |
119 | 11.5k | case GEOS_CIRCULARSTRING: writeSimpleCurve(static_cast<const SimpleCurve&>(g)); break; |
120 | 919 | case GEOS_COMPOUNDCURVE: writeCompoundCurve(static_cast<const CompoundCurve&>(g)); break; |
121 | 670k | case GEOS_POLYGON: writePolygon(static_cast<const Polygon&>(g)); break; |
122 | 1.59k | case GEOS_CURVEPOLYGON: writeCurvePolygon(static_cast<const CurvePolygon&>(g)); break; |
123 | 31.7k | case GEOS_MULTIPOINT: |
124 | 32.1k | case GEOS_MULTILINESTRING: |
125 | 33.4k | case GEOS_MULTIPOLYGON: |
126 | 34.3k | case GEOS_MULTICURVE: |
127 | 34.5k | case GEOS_MULTISURFACE: |
128 | 37.4k | case GEOS_GEOMETRYCOLLECTION: writeGeometryCollection(static_cast<const GeometryCollection&>(g)); break; |
129 | 6.46M | } |
130 | 6.46M | } |
131 | | |
132 | | void |
133 | | WKBWriter::writePointEmpty(const Point& g) |
134 | 1.13k | { |
135 | 1.13k | writeByteOrder(); |
136 | 1.13k | writeGeometryType(WKBConstants::wkbPoint, g.getSRID()); |
137 | 1.13k | writeSRID(g.getSRID()); |
138 | | |
139 | 1.13k | Coordinate c(DoubleNotANumber, DoubleNotANumber, DoubleNotANumber); |
140 | 1.13k | CoordinateSequence cas(std::size_t(1), std::size_t(g.getCoordinateDimension())); |
141 | 1.13k | cas.setAt(c, 0); |
142 | | |
143 | 1.13k | writeCoordinateSequence(cas, false); |
144 | 1.13k | } |
145 | | |
146 | | void |
147 | | WKBWriter::writePoint(const Point& g) |
148 | 5.74M | { |
149 | 5.74M | if (g.isEmpty()) { |
150 | 1.13k | return writePointEmpty(g); |
151 | 1.13k | } |
152 | | |
153 | 5.74M | writeByteOrder(); |
154 | | |
155 | 5.74M | writeGeometryType(WKBConstants::wkbPoint, g.getSRID()); |
156 | 5.74M | writeSRID(g.getSRID()); |
157 | | |
158 | 5.74M | const CoordinateSequence* cs = g.getCoordinatesRO(); |
159 | 5.74M | assert(cs); |
160 | 5.74M | writeCoordinateSequence(*cs, false); |
161 | 5.74M | } |
162 | | |
163 | | void |
164 | | WKBWriter::writeSimpleCurve(const SimpleCurve& g) |
165 | 13.9k | { |
166 | 13.9k | writeByteOrder(); |
167 | | |
168 | 13.9k | writeGeometryType(getWkbType(g), g.getSRID()); |
169 | 13.9k | writeSRID(g.getSRID()); |
170 | | |
171 | 13.9k | const CoordinateSequence* cs = g.getCoordinatesRO(); |
172 | 13.9k | assert(cs); |
173 | 13.9k | writeCoordinateSequence(*cs, true); |
174 | 13.9k | } |
175 | | |
176 | | void |
177 | | WKBWriter::writeCompoundCurve(const CompoundCurve& g) |
178 | 919 | { |
179 | 919 | writeByteOrder(); |
180 | | |
181 | 919 | writeGeometryType(getWkbType(g), g.getSRID()); |
182 | 919 | writeSRID(g.getSRID()); |
183 | | |
184 | 919 | writeInt(static_cast<int>(g.getNumCurves())); |
185 | | |
186 | 919 | auto orig_includeSRID = includeSRID; |
187 | 919 | includeSRID = false; |
188 | | |
189 | 919 | try { |
190 | 3.36k | for (std::size_t i = 0; i < g.getNumCurves(); i++) { |
191 | 2.44k | const SimpleCurve& section = *g.getCurveN(i); |
192 | 2.44k | writeSimpleCurve(section); |
193 | 2.44k | } |
194 | 919 | } catch (...) { |
195 | 0 | includeSRID = orig_includeSRID; |
196 | 0 | throw; |
197 | 0 | } |
198 | | |
199 | 919 | includeSRID = orig_includeSRID; |
200 | 919 | } |
201 | | |
202 | | void |
203 | | WKBWriter::writePolygon(const Polygon& g) |
204 | 670k | { |
205 | 670k | writeByteOrder(); |
206 | | |
207 | 670k | writeGeometryType(getWkbType(g), g.getSRID()); |
208 | 670k | writeSRID(g.getSRID()); |
209 | | |
210 | 670k | if (g.isEmpty()) { |
211 | 1.66k | writeInt(0); |
212 | 1.66k | return; |
213 | 1.66k | } |
214 | | |
215 | 668k | std::size_t nholes = g.getNumInteriorRing(); |
216 | 668k | writeInt(static_cast<int>(nholes + 1)); |
217 | | |
218 | 668k | const LineString* ls = g.getExteriorRing(); |
219 | 668k | assert(ls); |
220 | | |
221 | 668k | const CoordinateSequence* cs = ls->getCoordinatesRO(); |
222 | 668k | assert(cs); |
223 | | |
224 | 668k | writeCoordinateSequence(*cs, true); |
225 | 921k | for(std::size_t i = 0; i < nholes; i++) { |
226 | 252k | ls = g.getInteriorRingN(i); |
227 | 252k | assert(ls); |
228 | | |
229 | 252k | cs = ls->getCoordinatesRO(); |
230 | 252k | assert(cs); |
231 | | |
232 | 252k | writeCoordinateSequence(*cs, true); |
233 | 252k | } |
234 | 668k | } |
235 | | |
236 | | void |
237 | | WKBWriter::writeCurvePolygon(const CurvePolygon& g) |
238 | 1.59k | { |
239 | | // Why not combine this with writePolygon? |
240 | | // A CurvePolygon differs from a Polygon in that its rings |
241 | | // can one of three different types. Therefore, the type |
242 | | // information is written for each ring, unlike a Polygon. |
243 | | |
244 | 1.59k | writeByteOrder(); |
245 | | |
246 | 1.59k | writeGeometryType(getWkbType(g), g.getSRID()); |
247 | | |
248 | 1.59k | writeSRID(g.getSRID()); |
249 | | |
250 | 1.59k | if (g.isEmpty()) { |
251 | 96 | writeInt(0); |
252 | 96 | return; |
253 | 96 | } |
254 | | |
255 | 1.49k | std::size_t nholes = g.getNumInteriorRing(); |
256 | 1.49k | writeInt(static_cast<int>(nholes + 1)); |
257 | | |
258 | 1.49k | const Curve* ring = g.getExteriorRing(); |
259 | 1.49k | write(*ring, *outStream); |
260 | | |
261 | 3.13k | for(std::size_t i = 0; i < nholes; i++) { |
262 | 1.64k | ring = g.getInteriorRingN(i); |
263 | 1.64k | write(*ring, *outStream); |
264 | 1.64k | } |
265 | 1.49k | } |
266 | | |
267 | | void |
268 | | WKBWriter::writeGeometryCollection(const GeometryCollection& g) |
269 | 37.4k | { |
270 | 37.4k | writeByteOrder(); |
271 | | |
272 | 37.4k | writeGeometryType(getWkbType(g), g.getSRID()); |
273 | 37.4k | writeSRID(g.getSRID()); |
274 | | |
275 | 37.4k | auto ngeoms = g.getNumGeometries(); |
276 | 37.4k | writeInt(static_cast<int>(ngeoms)); |
277 | 37.4k | auto orig_includeSRID = includeSRID; |
278 | 37.4k | includeSRID = false; |
279 | | |
280 | 37.4k | assert(outStream); |
281 | | |
282 | 37.4k | try { |
283 | 6.48M | for(std::size_t i = 0; i < ngeoms; i++) { |
284 | 6.44M | const Geometry* elem = g.getGeometryN(i); |
285 | 6.44M | assert(elem); |
286 | | |
287 | 6.44M | write(*elem, *outStream); |
288 | 6.44M | } |
289 | 37.4k | } catch (...) { |
290 | 0 | includeSRID = orig_includeSRID; |
291 | 0 | throw; |
292 | 0 | } |
293 | | |
294 | 37.4k | includeSRID = orig_includeSRID; |
295 | 37.4k | } |
296 | | |
297 | | void |
298 | | WKBWriter::writeByteOrder() |
299 | 6.47M | { |
300 | 6.47M | if(byteOrder == ByteOrderValues::ENDIAN_LITTLE) { |
301 | 6.47M | buf[0] = WKBConstants::wkbNDR; |
302 | 6.47M | } |
303 | 0 | else { |
304 | 0 | buf[0] = WKBConstants::wkbXDR; |
305 | 0 | } |
306 | | |
307 | 6.47M | assert(outStream); |
308 | 6.47M | outStream->write(reinterpret_cast<char*>(buf), 1); |
309 | 6.47M | } |
310 | | |
311 | | /* public */ |
312 | | void |
313 | | WKBWriter::setByteOrder(int bo) |
314 | 0 | { |
315 | 0 | if(bo != ByteOrderValues::ENDIAN_LITTLE && |
316 | 0 | bo != ByteOrderValues::ENDIAN_BIG) { |
317 | 0 | std::ostringstream os; |
318 | 0 | os << "WKB output dimension must be LITTLE (" |
319 | 0 | << ByteOrderValues::ENDIAN_LITTLE |
320 | 0 | << ") or BIG (" << ByteOrderValues::ENDIAN_BIG << ")"; |
321 | 0 | throw util::IllegalArgumentException(os.str()); |
322 | 0 | } |
323 | | |
324 | 0 | byteOrder = bo; |
325 | 0 | } |
326 | | |
327 | | void |
328 | | WKBWriter::writeGeometryType(int typeId, int SRID) |
329 | 6.47M | { |
330 | 6.47M | if (flavor == WKBConstants::wkbExtended) { |
331 | 6.47M | int dimFlag = 0; |
332 | 6.47M | if (outputOrdinates.hasZ()) { |
333 | 0 | dimFlag |= static_cast<int>(0x80000000); |
334 | 0 | } |
335 | 6.47M | if (outputOrdinates.hasM()) { |
336 | 0 | dimFlag |= static_cast<int>(0x40000000); |
337 | 0 | } |
338 | | |
339 | 6.47M | typeId |= dimFlag; |
340 | 6.47M | if(includeSRID && SRID != 0) { |
341 | 0 | typeId |= 0x20000000; |
342 | 0 | } |
343 | 6.47M | } |
344 | 0 | else if (flavor == WKBConstants::wkbIso) { |
345 | 0 | if (outputOrdinates.hasZ()) { |
346 | 0 | typeId += 1000; |
347 | 0 | } |
348 | 0 | if (outputOrdinates.hasM()) { |
349 | 0 | typeId += 2000; |
350 | 0 | } |
351 | 0 | } |
352 | 0 | else { |
353 | 0 | throw util::IllegalArgumentException("Unknown WKB flavor"); |
354 | 0 | } |
355 | 6.47M | writeInt(typeId); |
356 | 6.47M | } |
357 | | |
358 | | void |
359 | | WKBWriter::writeSRID(int SRID) |
360 | 6.47M | { |
361 | | // Only write the SRID in if |
362 | | // it is requested and |
363 | | // it is non zero and |
364 | | // the format is extended (ISO doesn't support SRID embedding) |
365 | 6.47M | if (includeSRID && |
366 | 0 | SRID != 0 && |
367 | 0 | flavor == WKBConstants::wkbExtended) |
368 | 0 | { |
369 | 0 | writeInt(SRID); |
370 | 0 | } |
371 | 6.47M | } |
372 | | |
373 | | void |
374 | | WKBWriter::writeInt(int val) |
375 | 8.11M | { |
376 | 8.11M | ByteOrderValues::putInt(val, buf, byteOrder); |
377 | 8.11M | outStream->write(reinterpret_cast<char*>(buf), 4); |
378 | | //outStream->write(reinterpret_cast<char *>(&val), 4); |
379 | 8.11M | } |
380 | | |
381 | | void |
382 | | WKBWriter::writeCoordinateSequence(const CoordinateSequence& cs, |
383 | | bool sized) |
384 | 6.68M | { |
385 | 6.68M | std::size_t size = cs.getSize(); |
386 | | |
387 | 6.68M | if(sized) { |
388 | 935k | writeInt(static_cast<int>(size)); |
389 | 935k | } |
390 | 20.8M | for(std::size_t i = 0; i < size; i++) { |
391 | 14.1M | writeCoordinate(cs, i); |
392 | 14.1M | } |
393 | 6.68M | } |
394 | | |
395 | | void |
396 | | WKBWriter::writeCoordinate(const CoordinateSequence& cs, std::size_t idx) |
397 | 14.1M | { |
398 | | #if DEBUG_WKB_WRITER |
399 | | std::cout << "writeCoordinate: X:" << cs.getX(idx) << " Y:" << cs.getY(idx) << std::endl; |
400 | | #endif |
401 | 14.1M | assert(outStream); |
402 | | |
403 | 14.1M | CoordinateXYZM coord(DoubleNotANumber, DoubleNotANumber, DoubleNotANumber, DoubleNotANumber); |
404 | 14.1M | cs.getAt(idx, coord); |
405 | | |
406 | 14.1M | ByteOrderValues::putDouble(coord.x, buf, byteOrder); |
407 | 14.1M | outStream->write(reinterpret_cast<char*>(buf), 8); |
408 | 14.1M | ByteOrderValues::putDouble(coord.y, buf, byteOrder); |
409 | 14.1M | outStream->write(reinterpret_cast<char*>(buf), 8); |
410 | 14.1M | if(outputOrdinates.hasZ()) { |
411 | 0 | ByteOrderValues::putDouble(coord.z, buf, byteOrder); |
412 | 0 | outStream->write(reinterpret_cast<char*>(buf), 8); |
413 | 0 | } |
414 | 14.1M | if(outputOrdinates.hasM()) { |
415 | 0 | ByteOrderValues::putDouble(coord.m, buf, byteOrder); |
416 | 0 | outStream->write(reinterpret_cast<char*>(buf), 8); |
417 | 0 | } |
418 | 14.1M | } |
419 | | |
420 | | OrdinateSet |
421 | | WKBWriter::getOutputOrdinates(OrdinateSet ordinates) |
422 | 6.48M | { |
423 | | // drop specified ordinates to meet defaultOutputDimension |
424 | 6.48M | OrdinateSet newOrdinates = ordinates; |
425 | 6.53M | while (newOrdinates.size() > defaultOutputDimension) { |
426 | 53.7k | if (newOrdinates.hasM()) { |
427 | 28.3k | newOrdinates.setM(false); |
428 | 28.3k | } else if (newOrdinates.hasZ()) { |
429 | 25.3k | newOrdinates.setZ(false); |
430 | 25.3k | } |
431 | 53.7k | } |
432 | | |
433 | 6.48M | return newOrdinates; |
434 | 6.48M | } |
435 | | |
436 | | int |
437 | 724k | WKBWriter::getWkbType(const Geometry& g) { |
438 | 724k | switch(g.getGeometryTypeId()) { |
439 | 0 | case GEOS_POINT: return WKBConstants::wkbPoint; |
440 | 10.3k | case GEOS_LINESTRING: |
441 | 10.5k | case GEOS_LINEARRING: return WKBConstants::wkbLineString; |
442 | 3.39k | case GEOS_CIRCULARSTRING: return WKBConstants::wkbCircularString; |
443 | 919 | case GEOS_COMPOUNDCURVE: return WKBConstants::wkbCompoundCurve; |
444 | 670k | case GEOS_POLYGON: return WKBConstants::wkbPolygon; |
445 | 1.59k | case GEOS_CURVEPOLYGON: return WKBConstants::wkbCurvePolygon; |
446 | 31.7k | case GEOS_MULTIPOINT: return WKBConstants::wkbMultiPoint; |
447 | 392 | case GEOS_MULTILINESTRING: return WKBConstants::wkbMultiLineString; |
448 | 878 | case GEOS_MULTICURVE: return WKBConstants::wkbMultiCurve; |
449 | 1.35k | case GEOS_MULTIPOLYGON: return WKBConstants::wkbMultiPolygon; |
450 | 224 | case GEOS_MULTISURFACE: return WKBConstants::wkbMultiSurface; |
451 | 2.90k | case GEOS_GEOMETRYCOLLECTION: return WKBConstants::wkbGeometryCollection; |
452 | 724k | } |
453 | | |
454 | | // Avoid -Wreturn-type warning |
455 | 0 | throw util::IllegalArgumentException("Invalid geometry type."); |
456 | 724k | } |
457 | | |
458 | | |
459 | | } // namespace geos.io |
460 | | } // namespace geos |