GEOS  3.14.0dev
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/OrdinateSet.h>
26 #include <geos/io/WKBConstants.h>
27 #include <iosfwd>
28 #include <cstdint>
29 #include <cstddef>
30 
31 // Forward declarations
32 namespace geos {
33 namespace geom {
34 
35 class CoordinateSequence;
36 class CompoundCurve;
37 class CurvePolygon;
38 class Geometry;
39 class GeometryCollection;
40 class Point;
41 class LineString;
42 class LinearRing;
43 class Polygon;
44 class MultiPoint;
45 class MultiLineString;
46 class MultiPolygon;
47 class PrecisionModel;
48 class SimpleCurve;
49 
50 } // namespace geom
51 } // namespace geos
52 
53 namespace geos {
54 namespace io {
55 
78 class GEOS_DLL WKBWriter {
79 
80 public:
81  /*
82  * \brief
83  * Initializes writer with target coordinate dimension, endianness
84  * flag and SRID value.
85  *
86  * @param dims Supported values are 2, 3 or 4. Note that 4 indicates
87  * up to 4 dimensions will be written but (e.g.) 2D WKB is still produced
88  * for 2D geometries. Default since GEOS 3.12 is 4.
89  * @param bo output byte order - default to native machine byte order.
90  * Legal values include 0 (big endian/xdr) and 1 (little endian/ndr).
91  * @param incudeSRID true if SRID should be included in WKB (an
92  * extension).
93  */
94  WKBWriter(
95  uint8_t dims = 4,
96  int bo = getMachineByteOrder(),
97  bool includeSRID = false,
98  int flv = WKBConstants::wkbExtended);
99 
100  /*
101  * \brief
102  * Destructor.
103  */
104  ~WKBWriter() = default;
105 
106  /*
107  * \brief
108  * Returns the output dimension used by the
109  * <code>WKBWriter</code>.
110  */
111  uint8_t
112  getOutputDimension() const
113  {
114  return defaultOutputDimension;
115  }
116 
117  /*
118  * Sets the output dimension used by the <code>WKBWriter</code>.
119  *
120  * @param newOutputDimension Supported values are 2, 3 or 4.
121  * Note that 4 indicates up to 4 dimensions will be written but
122  * (e.g.) 2D WKB is still produced for 2D geometries.
123  */
124  void setOutputDimension(uint8_t newOutputDimension);
125 
126  /*
127  * \brief
128  * Returns the byte order used by the
129  * <code>WKBWriter</code>.
130  */
131  int
132  getByteOrder() const
133  {
134  return byteOrder;
135  }
136 
137  /*
138  * Sets the byte order used by the
139  * <code>WKBWriter</code>.
140  */
141  void setByteOrder(int newByteOrder);
142 
143  /*
144  * \brief
145  * Returns whether SRID values are output by the
146  * <code>WKBWriter</code>.
147  */
148  bool
149  getIncludeSRID() const
150  {
151  return includeSRID;
152  }
153 
154  /*
155  * Sets whether SRID values should be output by the
156  * <code>WKBWriter</code>.
157  */
158  void
159  setIncludeSRID(bool newIncludeSRID)
160  {
161  includeSRID = newIncludeSRID;
162  }
163 
164  /*
165  * \brief
166  * Returns the WKB flavor the writer will emit.
167  */
168  int
169  getFlavor() const
170  {
171  return flavor;
172  }
173 
174  /*
175  * \brief
176  * Set the WKB flavor the writer will emit.
177  */
178  void setFlavor(int newFlavor);
179 
187  void write(const geom::Geometry& g, std::ostream& os);
188  // throws IOException, ParseException
189 
197  void writeHEX(const geom::Geometry& g, std::ostream& os);
198  // throws IOException, ParseException
199 
200  static int getWkbType(const geom::Geometry&);
201 
202 private:
203 
204  // 2, 3, or 4
205  uint8_t defaultOutputDimension;
206  OrdinateSet outputOrdinates;
207 
208  // WKBConstants::wkbwkbXDR | WKBConstants::wkbNDR
209  int byteOrder;
210  // WKBConstants::wkbIso | WKBConstants::wkbExtended
211  int flavor;
212 
213  bool includeSRID;
214 
215  std::ostream* outStream;
216 
217  unsigned char buf[8];
218 
219  void writePoint(const geom::Point& p);
220  void writePointEmpty(const geom::Point& p);
221  // throws IOException
222 
223  void writeSimpleCurve(const geom::SimpleCurve& ls);
224  // throws IOException
225 
226  void writeCompoundCurve(const geom::CompoundCurve& curve);
227 
228  void writePolygon(const geom::Polygon& p);
229  // throws IOException
230 
231  void writeCurvePolygon(const geom::CurvePolygon& p);
232 
233  void writeGeometryCollection(const geom::GeometryCollection& gc);
234  // throws IOException, ParseException
235 
236  void writeCoordinateSequence(const geom::CoordinateSequence& cs, bool sized);
237  // throws IOException
238 
239  void writeCoordinate(const geom::CoordinateSequence& cs, std::size_t idx);
240  // throws IOException
241 
242  void writeGeometryType(int geometryType, int SRID);
243  // throws IOException
244 
245  void writeSRID(int SRID);
246  // throws IOException
247 
248  void writeByteOrder();
249  // throws IOException
250 
251  void writeInt(int intValue);
252  // throws IOException
253 
254  OrdinateSet getOutputOrdinates(OrdinateSet ordinates);
255 
256 };
257 
258 } // namespace io
259 } // namespace geos
260 
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:56
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:51
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:197
Definition: Point.h:61
Represents a linear polygon, which may include holes.
Definition: Polygon.h:61
Utility class to manipulate a set of flags indicating whether X, Y, Z, or M dimensions are present....
Definition: OrdinateSet.h:29
Writes a Geometry into Well-Known Binary format.
Definition: WKBWriter.h:78
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: Angle.h:25