-
Notifications
You must be signed in to change notification settings - Fork 347
/
WKBWriter.cpp
311 lines (252 loc) · 7.55 KB
/
WKBWriter.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2005-2006 Refractions Research Inc.
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************
*
* Last port: io/WKBWriter.java rev. 1.1 (JTS-1.7)
*
**********************************************************************/
#include <geos/io/WKBWriter.h>
#include <geos/io/WKBReader.h>
#include <geos/io/WKBConstants.h>
#include <geos/io/ByteOrderValues.h>
#include <geos/util/IllegalArgumentException.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/Point.h>
#include <geos/geom/LinearRing.h>
#include <geos/geom/LineString.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/MultiPoint.h>
#include <geos/geom/MultiLineString.h>
#include <geos/geom/MultiPolygon.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/PrecisionModel.h>
#include <ostream>
#include <sstream>
#include <cassert>
#undef DEBUG_WKB_WRITER
using namespace std;
using namespace geos::geom;
namespace geos {
namespace io { // geos.io
WKBWriter::WKBWriter(int dims, int bo, bool srid):
defaultOutputDimension(dims), byteOrder(bo), includeSRID(srid), outStream(nullptr)
{
if(dims < 2 || dims > 3) {
throw util::IllegalArgumentException("WKB output dimension must be 2 or 3");
}
outputDimension = defaultOutputDimension;
}
/* public */
void
WKBWriter::setOutputDimension(int dims)
{
if(dims < 2 || dims > 3) {
throw util::IllegalArgumentException("WKB output dimension must be 2 or 3");
}
defaultOutputDimension = dims;
}
void
WKBWriter::writeHEX(const Geometry& g, ostream& os)
{
// setup input/output stream
stringstream stream;
// write the geometry in wkb format
this->write(g, stream);
// convert to HEX
WKBReader::printHEX(stream, os);
}
void
WKBWriter::write(const Geometry& g, ostream& os)
{
outputDimension = defaultOutputDimension;
if(outputDimension > g.getCoordinateDimension()) {
outputDimension = g.getCoordinateDimension();
}
outStream = &os;
if(const Point* x = dynamic_cast<const Point*>(&g)) {
return writePoint(*x);
}
if(const LineString* x = dynamic_cast<const LineString*>(&g)) {
return writeLineString(*x);
}
if(const Polygon* x = dynamic_cast<const Polygon*>(&g)) {
return writePolygon(*x);
}
if(const MultiPoint* x = dynamic_cast<const MultiPoint*>(&g)) {
return writeGeometryCollection(*x, WKBConstants::wkbMultiPoint);
}
if(const MultiLineString* x = dynamic_cast<const MultiLineString*>(&g)) {
return writeGeometryCollection(*x, WKBConstants::wkbMultiLineString);
}
if(const MultiPolygon* x = dynamic_cast<const MultiPolygon*>(&g)) {
return writeGeometryCollection(*x, WKBConstants::wkbMultiPolygon);
}
if(const GeometryCollection* x =
dynamic_cast<const GeometryCollection*>(&g)) {
return writeGeometryCollection(*x, WKBConstants::wkbGeometryCollection);
}
assert(0); // Unknown Geometry type
}
void
WKBWriter::writePoint(const Point& g)
{
if(g.isEmpty()) throw
util::IllegalArgumentException("Empty Points cannot be represented in WKB");
writeByteOrder();
writeGeometryType(WKBConstants::wkbPoint, g.getSRID());
writeSRID(g.getSRID());
const CoordinateSequence* cs = g.getCoordinatesRO();
assert(cs);
writeCoordinateSequence(*cs, false);
}
void
WKBWriter::writeLineString(const LineString& g)
{
writeByteOrder();
writeGeometryType(WKBConstants::wkbLineString, g.getSRID());
writeSRID(g.getSRID());
const CoordinateSequence* cs = g.getCoordinatesRO();
assert(cs);
writeCoordinateSequence(*cs, true);
}
void
WKBWriter::writePolygon(const Polygon& g)
{
writeByteOrder();
writeGeometryType(WKBConstants::wkbPolygon, g.getSRID());
writeSRID(g.getSRID());
if(g.isEmpty()) {
writeInt(0);
return;
}
std::size_t nholes = g.getNumInteriorRing();
writeInt(static_cast<int>(nholes + 1));
const LineString* ls = g.getExteriorRing();
assert(ls);
const CoordinateSequence* cs = ls->getCoordinatesRO();
assert(cs);
writeCoordinateSequence(*cs, true);
for(std::size_t i = 0; i < nholes; i++) {
ls = g.getInteriorRingN(i);
assert(ls);
cs = ls->getCoordinatesRO();
assert(cs);
writeCoordinateSequence(*cs, true);
}
}
void
WKBWriter::writeGeometryCollection(const GeometryCollection& g,
int wkbtype)
{
writeByteOrder();
writeGeometryType(wkbtype, g.getSRID());
writeSRID(g.getSRID());
auto ngeoms = g.getNumGeometries();
writeInt(static_cast<int>(ngeoms));
auto orig_includeSRID = includeSRID;
includeSRID = false;
assert(outStream);
for(std::size_t i = 0; i < ngeoms; i++) {
const Geometry* elem = g.getGeometryN(i);
assert(elem);
write(*elem, *outStream);
}
includeSRID = orig_includeSRID;
}
void
WKBWriter::writeByteOrder()
{
if(byteOrder == ByteOrderValues::ENDIAN_LITTLE) {
buf[0] = WKBConstants::wkbNDR;
}
else {
buf[0] = WKBConstants::wkbXDR;
}
assert(outStream);
outStream->write(reinterpret_cast<char*>(buf), 1);
}
/* public */
void
WKBWriter::setByteOrder(int bo)
{
if(bo != ByteOrderValues::ENDIAN_LITTLE &&
bo != ByteOrderValues::ENDIAN_BIG) {
std::ostringstream os;
os << "WKB output dimension must be LITTLE ("
<< ByteOrderValues::ENDIAN_LITTLE
<< ") or BIG (" << ByteOrderValues::ENDIAN_BIG << ")";
throw util::IllegalArgumentException(os.str());
}
byteOrder = bo;
}
void
WKBWriter::writeGeometryType(int typeId, int SRID)
{
int flag3D = (outputDimension == 3) ? 0x80000000 : 0;
int typeInt = typeId | flag3D;
if(includeSRID && SRID != 0) {
typeInt = typeInt | 0x20000000;
}
writeInt(typeInt);
}
void
WKBWriter::writeSRID(int SRID)
{
if(includeSRID && SRID != 0) {
writeInt(SRID);
}
}
void
WKBWriter::writeInt(int val)
{
ByteOrderValues::putInt(val, buf, byteOrder);
outStream->write(reinterpret_cast<char*>(buf), 4);
//outStream->write(reinterpret_cast<char *>(&val), 4);
}
void
WKBWriter::writeCoordinateSequence(const CoordinateSequence& cs,
bool sized)
{
std::size_t size = cs.getSize();
bool is3d = false;
if(outputDimension > 2) {
is3d = true;
}
if(sized) {
writeInt(static_cast<int>(size));
}
for(std::size_t i = 0; i < size; i++) {
writeCoordinate(cs, i, is3d);
}
}
void
WKBWriter::writeCoordinate(const CoordinateSequence& cs, size_t idx,
bool is3d)
{
#if DEBUG_WKB_WRITER
cout << "writeCoordinate: X:" << cs.getX(idx) << " Y:" << cs.getY(idx) << endl;
#endif
assert(outStream);
ByteOrderValues::putDouble(cs.getX(idx), buf, byteOrder);
outStream->write(reinterpret_cast<char*>(buf), 8);
ByteOrderValues::putDouble(cs.getY(idx), buf, byteOrder);
outStream->write(reinterpret_cast<char*>(buf), 8);
if(is3d) {
ByteOrderValues::putDouble(
cs.getOrdinate(idx, CoordinateSequence::Z),
buf, byteOrder);
outStream->write(reinterpret_cast<char*>(buf), 8);
}
}
} // namespace geos.io
} // namespace geos