GEOS  3.10.3
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 #ifndef GEOS_IO_WKTREADER_H
21 #define GEOS_IO_WKTREADER_H
22 
23 #include <geos/export.h>
24 
25 #include <geos/geom/GeometryFactory.h>
26 #include <geos/geom/CoordinateSequence.h>
27 #include <geos/geom/Geometry.h>
28 #include <geos/io/ParseException.h>
29 #include <string>
30 
31 // Forward declarations
32 namespace geos {
33 namespace io {
34 class StringTokenizer;
35 }
36 namespace geom {
37 
38 class Coordinate;
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  //WKTReader();
62 
71  explicit WKTReader(const geom::GeometryFactory& gf);
72 
74  explicit WKTReader(const geom::GeometryFactory* gf);
75 
81 
82  ~WKTReader();
83 
85  template<typename T>
86  std::unique_ptr<T> read(const std::string& wkt) const {
87  auto g = read(wkt);
88  auto gt = dynamic_cast<const T*>(g.get());
89  if (!gt) {
90  // Can improve this message once there's a good way to get a string repr of T
91  throw io::ParseException("Unexpected WKT type");
92  }
93  return std::unique_ptr<T>(static_cast<T*>(g.release()));
94  }
95 
96  std::unique_ptr<geom::Geometry> read(const std::string& wellKnownText) const;
97 
98 // Geometry* read(Reader& reader); //Not implemented yet
99 
100 protected:
101  std::unique_ptr<geom::CoordinateSequence> getCoordinates(io::StringTokenizer* tokenizer) const;
102  static double getNextNumber(io::StringTokenizer* tokenizer);
103  static std::string getNextEmptyOrOpener(io::StringTokenizer* tokenizer, std::size_t& dim);
104  static std::string getNextCloserOrComma(io::StringTokenizer* tokenizer);
105  static std::string getNextCloser(io::StringTokenizer* tokenizer);
106  static std::string getNextWord(io::StringTokenizer* tokenizer);
107  std::unique_ptr<geom::Geometry> readGeometryTaggedText(io::StringTokenizer* tokenizer) const;
108  std::unique_ptr<geom::Point> readPointText(io::StringTokenizer* tokenizer) const;
109  std::unique_ptr<geom::LineString> readLineStringText(io::StringTokenizer* tokenizer) const;
110  std::unique_ptr<geom::LinearRing> readLinearRingText(io::StringTokenizer* tokenizer) const;
111  std::unique_ptr<geom::MultiPoint> readMultiPointText(io::StringTokenizer* tokenizer) const;
112  std::unique_ptr<geom::Polygon> readPolygonText(io::StringTokenizer* tokenizer) const;
113  std::unique_ptr<geom::MultiLineString> readMultiLineStringText(io::StringTokenizer* tokenizer) const;
114  std::unique_ptr<geom::MultiPolygon> readMultiPolygonText(io::StringTokenizer* tokenizer) const;
115  std::unique_ptr<geom::GeometryCollection> readGeometryCollectionText(io::StringTokenizer* tokenizer) const;
116 private:
117  const geom::GeometryFactory* geometryFactory;
118  const geom::PrecisionModel* precisionModel;
119 
120  void getPreciseCoordinate(io::StringTokenizer* tokenizer, geom::Coordinate&, std::size_t& dim) const;
121 
122  static bool isNumberNext(io::StringTokenizer* tokenizer);
123 };
124 
125 } // namespace io
126 } // namespace geos
127 
128 #ifdef GEOS_INLINE
129 # include <geos/io/WKTReader.inl>
130 #endif
131 
132 #endif // #ifndef GEOS_IO_WKTREADER_H
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:68
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:87
Notifies a parsing error.
Definition: ParseException.h:34
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:86
WKTReader(const geom::GeometryFactory *gf)
WKTReader()
Initialize parser with default GeometryFactory.
WKTReader(const geom::GeometryFactory &gf)
Initialize parser with given GeometryFactory.
Basic namespace for all GEOS functionalities.
Definition: Angle.h:26