GEOS  3.13.0dev
WKTReader.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/WKTReader.java rev. 1.1 (JTS-1.7)
17  *
18  **********************************************************************/
19 
20 #pragma once
21 
22 #include <geos/export.h>
23 
24 #include <geos/geom/GeometryFactory.h>
25 #include <geos/geom/Geometry.h>
26 #include <geos/io/ParseException.h>
27 #include <geos/io/OrdinateSet.h>
28 
29 #include <string>
30 
31 // Forward declarations
32 namespace geos {
33 namespace io {
34 class StringTokenizer;
35 }
36 namespace geom {
37 class Coordinate;
38 class CoordinateSequence;
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 }
49 }
50 
51 
52 namespace geos {
53 namespace io {
54 
59 class GEOS_DLL WKTReader {
60 public:
61 
70  explicit WKTReader(const geom::GeometryFactory& gf)
71  : geometryFactory(&gf)
72  , precisionModel(gf.getPrecisionModel())
73  , fixStructure(false)
74  {};
75 
77  explicit WKTReader(const geom::GeometryFactory* gf)
78  : geometryFactory(gf)
79  , precisionModel(gf->getPrecisionModel())
80  , fixStructure(false)
81  {};
82 
88  : geometryFactory(geom::GeometryFactory::getDefaultInstance())
89  , precisionModel(geometryFactory->getPrecisionModel())
90  , fixStructure(false)
91  {};
92 
93  ~WKTReader() {};
94 
95  void
96  setFixStructure(bool doFixStructure) {
97  fixStructure = doFixStructure;
98  }
99 
101  template<typename T>
102  std::unique_ptr<T> read(const std::string& wkt) const {
103  auto g = read(wkt);
104  auto gt = dynamic_cast<const T*>(g.get());
105  if (!gt) {
106  // Can improve this message once there's a good way to get a string repr of T
107  throw io::ParseException("Unexpected WKT type");
108  }
109  return std::unique_ptr<T>(static_cast<T*>(g.release()));
110  }
111 
112  std::unique_ptr<geom::Geometry> read(const std::string& wellKnownText) const;
113 
114 protected:
115  std::unique_ptr<geom::CoordinateSequence> getCoordinates(io::StringTokenizer* tokenizer, OrdinateSet& ordinates) const;
116  static double getNextNumber(io::StringTokenizer* tokenizer);
117  static std::string getNextEmptyOrOpener(io::StringTokenizer* tokenizer, OrdinateSet& dim);
118  static std::string getNextCloserOrComma(io::StringTokenizer* tokenizer);
119  static std::string getNextCloser(io::StringTokenizer* tokenizer);
120  static std::string getNextWord(io::StringTokenizer* tokenizer);
121  std::unique_ptr<geom::Geometry> readGeometryTaggedText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
122  std::unique_ptr<geom::Point> readPointText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
123  std::unique_ptr<geom::LineString> readLineStringText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
124  std::unique_ptr<geom::LinearRing> readLinearRingText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
125  std::unique_ptr<geom::MultiPoint> readMultiPointText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
126  std::unique_ptr<geom::Polygon> readPolygonText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
127  std::unique_ptr<geom::MultiLineString> readMultiLineStringText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
128  std::unique_ptr<geom::MultiPolygon> readMultiPolygonText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
129  std::unique_ptr<geom::GeometryCollection> readGeometryCollectionText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
130 private:
131  const geom::GeometryFactory* geometryFactory;
132  const geom::PrecisionModel* precisionModel;
133  bool fixStructure;
134 
135  void getPreciseCoordinate(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags, geom::CoordinateXYZM&) const;
136 
137  static bool isNumberNext(io::StringTokenizer* tokenizer);
138  static bool isOpenerNext(io::StringTokenizer* tokenizer);
139 
140  static void readOrdinateFlags(const std::string & s, OrdinateSet& ordinateFlags);
141  static bool isTypeName(const std::string & type, const std::string & typeName);
142 };
143 
144 } // namespace io
145 } // namespace geos
146 
147 
148 
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:65
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:88
Utility class to manipulate a set of flags indicating whether X, Y, Z, or M dimensions are present....
Definition: OrdinateSet.h:29
Notifies a parsing error.
Definition: ParseException.h:33
WKT parser class; see also WKTWriter.
Definition: WKTReader.h:59
std::unique_ptr< T > read(const std::string &wkt) const
Parse a WKT string returning a Geometry.
Definition: WKTReader.h:102
WKTReader(const geom::GeometryFactory *gf)
Definition: WKTReader.h:77
WKTReader()
Initialize parser with default GeometryFactory.
Definition: WKTReader.h:87
WKTReader(const geom::GeometryFactory &gf)
Initialize parser with given GeometryFactory.
Definition: WKTReader.h:70
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25