GEOS 3.11.1
WKBWriter.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2005-2006 Refractions Research Inc.
7 * Copyright (C) 2001-2002 Vivid Solutions Inc.
8 *
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
13 *
14 **********************************************************************
15 *
16 * Last port: io/WKBWriter.java rev. 1.1 (JTS-1.7)
17 *
18 **********************************************************************/
19
20#pragma once
21
22#include <geos/export.h>
23
24#include <geos/util/Machine.h> // for getMachineByteOrder
25#include <geos/io/WKBConstants.h>
26#include <iosfwd>
27#include <cstdint>
28#include <cstddef>
29
30// Forward declarations
31namespace geos {
32namespace geom {
33
34class CoordinateSequence;
35class Geometry;
36class GeometryCollection;
37class Point;
38class LineString;
39class LinearRing;
40class Polygon;
41class MultiPoint;
42class MultiLineString;
43class MultiPolygon;
44class PrecisionModel;
45
46} // namespace geom
47} // namespace geos
48
49namespace geos {
50namespace io {
51
74class GEOS_DLL WKBWriter {
75
76public:
77 /*
78 * \brief
79 * Initializes writer with target coordinate dimension, endianness
80 * flag and SRID value.
81 *
82 * @param dims Supported values are 2 or 3. Note that 3 indicates
83 * up to 3 dimensions will be written but 2D WKB is still produced for 2D geometries.
84 * @param bo output byte order - default to native machine byte order.
85 * Legal values include 0 (big endian/xdr) and 1 (little endian/ndr).
86 * @param incudeSRID true if SRID should be included in WKB (an
87 * extension).
88 */
90 uint8_t dims = 2,
91 int bo = getMachineByteOrder(),
92 bool includeSRID = false,
93 int flv = WKBConstants::wkbExtended);
94
95 /*
96 * \brief
97 * Destructor.
98 */
99 ~WKBWriter() = default;
100
101 /*
102 * \brief
103 * Returns the output dimension used by the
104 * <code>WKBWriter</code>.
105 */
106 uint8_t
107 getOutputDimension() const
108 {
109 return defaultOutputDimension;
110 }
111
112 /*
113 * Sets the output dimension used by the <code>WKBWriter</code>.
114 *
115 * @param newOutputDimension Supported values are 2 or 3.
116 * Note that 3 indicates up to 3 dimensions will be written but
117 * 2D WKB is still produced for 2D geometries.
118 */
119 void setOutputDimension(uint8_t newOutputDimension);
120
121 /*
122 * \brief
123 * Returns the byte order used by the
124 * <code>WKBWriter</code>.
125 */
126 int
127 getByteOrder() const
128 {
129 return byteOrder;
130 }
131
132 /*
133 * Sets the byte order used by the
134 * <code>WKBWriter</code>.
135 */
136 void setByteOrder(int newByteOrder);
137
138 /*
139 * \brief
140 * Returns whether SRID values are output by the
141 * <code>WKBWriter</code>.
142 */
143 bool
144 getIncludeSRID() const
145 {
146 return includeSRID;
147 }
148
149 /*
150 * Sets whether SRID values should be output by the
151 * <code>WKBWriter</code>.
152 */
153 void
154 setIncludeSRID(bool newIncludeSRID)
155 {
156 includeSRID = newIncludeSRID;
157 }
158
159 /*
160 * \brief
161 * Returns the WKB flavor the writer will emit.
162 */
163 int
164 getFlavor() const
165 {
166 return flavor;
167 }
168
169 /*
170 * \brief
171 * Set the WKB flavor the writer will emit.
172 */
173 void setFlavor(int newFlavor);
174
182 void write(const geom::Geometry& g, std::ostream& os);
183 // throws IOException, ParseException
184
192 void writeHEX(const geom::Geometry& g, std::ostream& os);
193 // throws IOException, ParseException
194
195private:
196
197 // 2 or 3
198 uint8_t defaultOutputDimension;
199 uint8_t outputDimension;
200
201 // WKBConstants::wkbwkbXDR | WKBConstants::wkbNDR
202 int byteOrder;
203 // WKBConstants::wkbIso | WKBConstants::wkbExtended
204 int flavor;
205
206 bool includeSRID;
207
208 std::ostream* outStream;
209
210 unsigned char buf[8];
211
212 void writePoint(const geom::Point& p);
213 void writePointEmpty(const geom::Point& p);
214 // throws IOException
215
216 void writeLineString(const geom::LineString& ls);
217 // throws IOException
218
219 void writePolygon(const geom::Polygon& p);
220 // throws IOException
221
222 void writeGeometryCollection(const geom::GeometryCollection& c, int wkbtype);
223 // throws IOException, ParseException
224
225 void writeCoordinateSequence(const geom::CoordinateSequence& cs, bool sized);
226 // throws IOException
227
228 void writeCoordinate(const geom::CoordinateSequence& cs, std::size_t idx, bool is3d);
229 // throws IOException
230
231 void writeGeometryType(int geometryType, int SRID);
232 // throws IOException
233
234 void writeSRID(int SRID);
235 // throws IOException
236
237 void writeByteOrder();
238 // throws IOException
239
240 void writeInt(int intValue);
241 // throws IOException
242
243};
244
245} // namespace io
246} // namespace geos
247
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:44
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:52
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
Definition: LineString.h:66
Definition: Point.h:63
Represents a linear polygon, which may include holes.
Definition: Polygon.h:61
Writes a Geometry into Well-Known Binary format.
Definition: WKBWriter.h:74
void writeHEX(const geom::Geometry &g, std::ostream &os)
Write a Geometry to an ostream in binary hex format.
void write(const geom::Geometry &g, std::ostream &os)
Write a Geometry to an ostream.
Basic namespace for all GEOS functionalities.
Definition: geos.h:39