GEOS  3.14.0dev
WKBReader.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/WKBReader.java rev. 1.1 (JTS-1.7)
17  *
18  **********************************************************************/
19 
20 #pragma once
21 
22 #include <geos/export.h>
23 #include <geos/geom/Geometry.h>
24 #include <geos/geom/GeometryTypeName.h>
25 #include <geos/io/ByteOrderDataInStream.h> // for composition
26 
27 #include <iosfwd> // ostream, istream
28 #include <memory>
29 // #include <vector>
30 #include <array>
31 
32 #ifdef _MSC_VER
33 #pragma warning(push)
34 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
35 #endif
36 
37 // Forward declarations
38 namespace geos {
39 namespace geom {
40 
41 class GeometryFactory;
42 class Coordinate;
43 class CircularString;
44 class CompoundCurve;
45 class CurvePolygon;
46 class Geometry;
47 enum GeometryTypeId : int;
48 class GeometryCollection;
49 class Point;
50 class LineString;
51 class LinearRing;
52 class Polygon;
53 class MultiCurve;
54 class MultiPoint;
55 class MultiLineString;
56 class MultiPolygon;
57 class MultiSurface;
58 class PrecisionModel;
59 class CoordinateSequence;
60 class SimpleCurve;
61 
62 } // namespace geom
63 } // namespace geos
64 
65 
66 namespace geos {
67 namespace io {
68 
85 class GEOS_DLL WKBReader {
86 
87 public:
88 
90 
93 
94  void setFixStructure(bool doFixStructure);
95 
104  std::unique_ptr<geom::Geometry> read(std::istream& is);
105 
115  std::unique_ptr<geom::Geometry> read(const unsigned char* buf, size_t size);
116 
125  std::unique_ptr<geom::Geometry> readHEX(std::istream& is);
126 
133  static std::ostream& printHEX(std::istream& is, std::ostream& os);
134 
135 private:
136 
137  const geom::GeometryFactory& factory;
138 
139  // for now support the WKB standard only - may be generalized later
140  unsigned int inputDimension;
141  bool hasZ;
142  bool hasM;
143  bool fixStructure;
144 
146 
147  std::array<double, 4> ordValues;
148 
149  std::unique_ptr<geom::Geometry> readGeometry();
150 
151  std::unique_ptr<geom::Point> readPoint();
152 
153  std::unique_ptr<geom::LineString> readLineString();
154 
155  std::unique_ptr<geom::LinearRing> readLinearRing();
156 
157  std::unique_ptr<geom::CircularString> readCircularString();
158 
159  std::unique_ptr<geom::CompoundCurve> readCompoundCurve();
160 
161  std::unique_ptr<geom::Polygon> readPolygon();
162 
163  std::unique_ptr<geom::CurvePolygon> readCurvePolygon();
164 
165  std::unique_ptr<geom::MultiPoint> readMultiPoint();
166 
167  std::unique_ptr<geom::MultiLineString> readMultiLineString();
168 
169  std::unique_ptr<geom::MultiCurve> readMultiCurve();
170 
171  std::unique_ptr<geom::MultiPolygon> readMultiPolygon();
172 
173  std::unique_ptr<geom::MultiSurface> readMultiSurface();
174 
175  std::unique_ptr<geom::GeometryCollection> readGeometryCollection();
176 
177  std::unique_ptr<geom::CoordinateSequence> readCoordinateSequence(unsigned int); // throws IOException
178 
179  void minMemSize(geom::GeometryTypeId geomType, uint64_t size) const;
180 
181  void readCoordinate(); // throws IOException
182 
183  template<typename T>
184  std::unique_ptr<T> readChild()
185  {
186  auto g = readGeometry();
187  if (dynamic_cast<const T*>(g.get())) {
188  return std::unique_ptr<T>(static_cast<T*>(g.release()));
189  }
190  throw io::ParseException(std::string("Expected ") + geom::GeometryTypeName<T>::name + " but got " + g->getGeometryType());
191  }
192 
193  // Declare type as noncopyable
194  WKBReader(const WKBReader& other) = delete;
195  WKBReader& operator=(const WKBReader& rhs) = delete;
196 };
197 
198 } // namespace io
199 } // namespace geos
200 
201 #ifdef _MSC_VER
202 #pragma warning(pop)
203 #endif
204 
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:70
Allows reading an stream of primitive datatypes from an underlying istream, with the representation b...
Definition: ByteOrderDataInStream.h:40
Notifies a parsing error.
Definition: ParseException.h:33
Reads a Geometry from Well-Known Binary format.
Definition: WKBReader.h:85
std::unique_ptr< geom::Geometry > read(const unsigned char *buf, size_t size)
Reads a Geometry from a buffer.
std::unique_ptr< geom::Geometry > readHEX(std::istream &is)
Reads a Geometry from an istream in hex format.
WKBReader()
Initialize parser with default GeometryFactory.
static std::ostream & printHEX(std::istream &is, std::ostream &os)
Print WKB in HEX form to out stream.
std::unique_ptr< geom::Geometry > read(std::istream &is)
Reads a Geometry from an istream.
GeometryTypeId
Geometry types.
Definition: Geometry.h:74
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25