GEOS  3.13.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 Geometry;
37 class GeometryCollection;
38 class Point;
39 class LineString;
40 class LinearRing;
41 class Polygon;
42 class MultiPoint;
43 class MultiLineString;
44 class MultiPolygon;
45 class PrecisionModel;
46 
47 } // namespace geom
48 } // namespace geos
49 
50 namespace geos {
51 namespace io {
52 
75 class GEOS_DLL WKBWriter {
76 
77 public:
78  /*
79  * \brief
80  * Initializes writer with target coordinate dimension, endianness
81  * flag and SRID value.
82  *
83  * @param dims Supported values are 2, 3 or 4. Note that 4 indicates
84  * up to 4 dimensions will be written but (e.g.) 2D WKB is still produced
85  * for 2D geometries. Default since GEOS 3.12 is 4.
86  * @param bo output byte order - default to native machine byte order.
87  * Legal values include 0 (big endian/xdr) and 1 (little endian/ndr).
88  * @param incudeSRID true if SRID should be included in WKB (an
89  * extension).
90  */
91  WKBWriter(
92  uint8_t dims = 4,
93  int bo = getMachineByteOrder(),
94  bool includeSRID = false,
95  int flv = WKBConstants::wkbExtended);
96 
97  /*
98  * \brief
99  * Destructor.
100  */
101  ~WKBWriter() = default;
102 
103  /*
104  * \brief
105  * Returns the output dimension used by the
106  * <code>WKBWriter</code>.
107  */
108  uint8_t
109  getOutputDimension() const
110  {
111  return defaultOutputDimension;
112  }
113 
114  /*
115  * Sets the output dimension used by the <code>WKBWriter</code>.
116  *
117  * @param newOutputDimension Supported values are 2, 3 or 4.
118  * Note that 4 indicates up to 4 dimensions will be written but
119  * (e.g.) 2D WKB is still produced for 2D geometries.
120  */
121  void setOutputDimension(uint8_t newOutputDimension);
122 
123  /*
124  * \brief
125  * Returns the byte order used by the
126  * <code>WKBWriter</code>.
127  */
128  int
129  getByteOrder() const
130  {
131  return byteOrder;
132  }
133 
134  /*
135  * Sets the byte order used by the
136  * <code>WKBWriter</code>.
137  */
138  void setByteOrder(int newByteOrder);
139 
140  /*
141  * \brief
142  * Returns whether SRID values are output by the
143  * <code>WKBWriter</code>.
144  */
145  bool
146  getIncludeSRID() const
147  {
148  return includeSRID;
149  }
150 
151  /*
152  * Sets whether SRID values should be output by the
153  * <code>WKBWriter</code>.
154  */
155  void
156  setIncludeSRID(bool newIncludeSRID)
157  {
158  includeSRID = newIncludeSRID;
159  }
160 
161  /*
162  * \brief
163  * Returns the WKB flavor the writer will emit.
164  */
165  int
166  getFlavor() const
167  {
168  return flavor;
169  }
170 
171  /*
172  * \brief
173  * Set the WKB flavor the writer will emit.
174  */
175  void setFlavor(int newFlavor);
176 
184  void write(const geom::Geometry& g, std::ostream& os);
185  // throws IOException, ParseException
186 
194  void writeHEX(const geom::Geometry& g, std::ostream& os);
195  // throws IOException, ParseException
196 
197 private:
198 
199  // 2, 3, or 4
200  uint8_t defaultOutputDimension;
201  OrdinateSet outputOrdinates;
202 
203  // WKBConstants::wkbwkbXDR | WKBConstants::wkbNDR
204  int byteOrder;
205  // WKBConstants::wkbIso | WKBConstants::wkbExtended
206  int flavor;
207 
208  bool includeSRID;
209 
210  std::ostream* outStream;
211 
212  unsigned char buf[8];
213 
214  void writePoint(const geom::Point& p);
215  void writePointEmpty(const geom::Point& p);
216  // throws IOException
217 
218  void writeLineString(const geom::LineString& ls);
219  // throws IOException
220 
221  void writePolygon(const geom::Polygon& p);
222  // throws IOException
223 
224  void writeGeometryCollection(const geom::GeometryCollection& c, int wkbtype);
225  // throws IOException, ParseException
226 
227  void writeCoordinateSequence(const geom::CoordinateSequence& cs, bool sized);
228  // throws IOException
229 
230  void writeCoordinate(const geom::CoordinateSequence& cs, std::size_t idx);
231  // throws IOException
232 
233  void writeGeometryType(int geometryType, int SRID);
234  // throws IOException
235 
236  void writeSRID(int SRID);
237  // throws IOException
238 
239  void writeByteOrder();
240  // throws IOException
241 
242  void writeInt(int intValue);
243  // throws IOException
244 
245  OrdinateSet getOutputOrdinates(OrdinateSet ordinates);
246 
247 };
248 
249 } // namespace io
250 } // namespace geos
251 
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:186
Definition: LineString.h:65
Definition: Point.h:61
Represents a linear polygon, which may include holes.
Definition: Polygon.h:60
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:75
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