GEOS  3.11.0
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 
28 #include <string>
29 
30 // Forward declarations
31 namespace geos {
32 namespace io {
33 class StringTokenizer;
34 }
35 namespace geom {
36 class Coordinate;
37 class CoordinateSequence;
38 class GeometryCollection;
39 class Point;
40 class LineString;
41 class LinearRing;
42 class Polygon;
43 class MultiPoint;
44 class MultiLineString;
45 class MultiPolygon;
46 class PrecisionModel;
47 }
48 }
49 
50 
51 namespace geos {
52 namespace io {
53 
58 class GEOS_DLL WKTReader {
59 public:
60 
69  explicit WKTReader(const geom::GeometryFactory& gf)
70  : geometryFactory(&gf)
71  , precisionModel(gf.getPrecisionModel())
72  , fixStructure(false)
73  {};
74 
76  explicit WKTReader(const geom::GeometryFactory* gf)
77  : geometryFactory(gf)
78  , precisionModel(gf->getPrecisionModel())
79  , fixStructure(false)
80  {};
81 
87  : geometryFactory(geom::GeometryFactory::getDefaultInstance())
88  , precisionModel(geometryFactory->getPrecisionModel())
89  , fixStructure(false)
90  {};
91 
92  ~WKTReader() {};
93 
94  void
95  setFixStructure(bool doFixStructure) {
96  fixStructure = doFixStructure;
97  }
98 
100  template<typename T>
101  std::unique_ptr<T> read(const std::string& wkt) const {
102  auto g = read(wkt);
103  auto gt = dynamic_cast<const T*>(g.get());
104  if (!gt) {
105  // Can improve this message once there's a good way to get a string repr of T
106  throw io::ParseException("Unexpected WKT type");
107  }
108  return std::unique_ptr<T>(static_cast<T*>(g.release()));
109  }
110 
111  std::unique_ptr<geom::Geometry> read(const std::string& wellKnownText) const;
112 
113 protected:
114  std::unique_ptr<geom::CoordinateSequence> getCoordinates(io::StringTokenizer* tokenizer) const;
115  static double getNextNumber(io::StringTokenizer* tokenizer);
116  static std::string getNextEmptyOrOpener(io::StringTokenizer* tokenizer, std::size_t& dim);
117  static std::string getNextCloserOrComma(io::StringTokenizer* tokenizer);
118  static std::string getNextCloser(io::StringTokenizer* tokenizer);
119  static std::string getNextWord(io::StringTokenizer* tokenizer);
120  std::unique_ptr<geom::Geometry> readGeometryTaggedText(io::StringTokenizer* tokenizer) const;
121  std::unique_ptr<geom::Point> readPointText(io::StringTokenizer* tokenizer) const;
122  std::unique_ptr<geom::LineString> readLineStringText(io::StringTokenizer* tokenizer) const;
123  std::unique_ptr<geom::LinearRing> readLinearRingText(io::StringTokenizer* tokenizer) const;
124  std::unique_ptr<geom::MultiPoint> readMultiPointText(io::StringTokenizer* tokenizer) const;
125  std::unique_ptr<geom::Polygon> readPolygonText(io::StringTokenizer* tokenizer) const;
126  std::unique_ptr<geom::MultiLineString> readMultiLineStringText(io::StringTokenizer* tokenizer) const;
127  std::unique_ptr<geom::MultiPolygon> readMultiPolygonText(io::StringTokenizer* tokenizer) const;
128  std::unique_ptr<geom::GeometryCollection> readGeometryCollectionText(io::StringTokenizer* tokenizer) const;
129 private:
130  const geom::GeometryFactory* geometryFactory;
131  const geom::PrecisionModel* precisionModel;
132  bool fixStructure;
133 
134  void getPreciseCoordinate(io::StringTokenizer* tokenizer, geom::Coordinate&, std::size_t& dim) const;
135 
136  static bool isNumberNext(io::StringTokenizer* tokenizer);
137 };
138 
139 } // namespace io
140 } // namespace geos
141 
142 
143 
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:58
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:66
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:90
Notifies a parsing error.
Definition: ParseException.h:33
WKT parser class; see also WKTWriter.
Definition: WKTReader.h:58
std::unique_ptr< T > read(const std::string &wkt) const
Parse a WKT string returning a Geometry.
Definition: WKTReader.h:101
WKTReader(const geom::GeometryFactory *gf)
Definition: WKTReader.h:76
WKTReader()
Initialize parser with default GeometryFactory.
Definition: WKTReader.h:86
WKTReader(const geom::GeometryFactory &gf)
Initialize parser with given GeometryFactory.
Definition: WKTReader.h:69
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25