GEOS 3.14.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
32namespace geos {
33namespace io {
34class StringTokenizer;
35}
36namespace geom {
37class Coordinate;
38class CoordinateSequence;
39class GeometryCollection;
40class Point;
41class LineString;
42class LinearRing;
43class CircularString;
44class CompoundCurve;
45class Polygon;
46class CurvePolygon;
47class MultiPoint;
48class MultiLineString;
49class MultiCurve;
50class MultiPolygon;
51class MultiSurface;
52class PrecisionModel;
53}
54}
55
56
57namespace geos {
58namespace io {
59
64class GEOS_DLL WKTReader {
65public:
66
75 explicit WKTReader(const geom::GeometryFactory& gf)
76 : geometryFactory(&gf)
77 , precisionModel(gf.getPrecisionModel())
78 , fixStructure(false)
79 {};
80
82 explicit WKTReader(const geom::GeometryFactory* gf)
83 : geometryFactory(gf)
84 , precisionModel(gf->getPrecisionModel())
85 , fixStructure(false)
86 {};
87
93 : geometryFactory(geom::GeometryFactory::getDefaultInstance())
94 , precisionModel(geometryFactory->getPrecisionModel())
95 , fixStructure(false)
96 {};
97
98 ~WKTReader() {};
99
100 void
101 setFixStructure(bool doFixStructure) {
102 fixStructure = doFixStructure;
103 }
104
106 template<typename T>
107 std::unique_ptr<T> read(const std::string& wkt) const {
108 auto g = read(wkt);
109 auto gt = dynamic_cast<const T*>(g.get());
110 if (!gt) {
111 // Can improve this message once there's a good way to get a string repr of T
112 throw io::ParseException("Unexpected WKT type");
113 }
114 return std::unique_ptr<T>(static_cast<T*>(g.release()));
115 }
116
117 std::unique_ptr<geom::Geometry> read(const std::string& wellKnownText) const;
118
119protected:
120 std::unique_ptr<geom::CoordinateSequence> getCoordinates(io::StringTokenizer* tokenizer, OrdinateSet& ordinates) const;
121 static double getNextNumber(io::StringTokenizer* tokenizer);
122 static std::string getNextEmptyOrOpener(io::StringTokenizer* tokenizer, OrdinateSet& dim);
123 static std::string getNextCloserOrComma(io::StringTokenizer* tokenizer);
124 static std::string getNextCloser(io::StringTokenizer* tokenizer);
125 static std::string getNextWord(io::StringTokenizer* tokenizer);
126 std::unique_ptr<geom::Geometry> readGeometryTaggedText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags, const geom::GeometryTypeId* emptyType = nullptr) const;
127
128 std::unique_ptr<geom::Point> readPointText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
129 std::unique_ptr<geom::LineString> readLineStringText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
130 std::unique_ptr<geom::LinearRing> readLinearRingText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
131 std::unique_ptr<geom::MultiPoint> readMultiPointText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
132 std::unique_ptr<geom::Polygon> readPolygonText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
133 std::unique_ptr<geom::MultiLineString> readMultiLineStringText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
134 std::unique_ptr<geom::MultiPolygon> readMultiPolygonText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
135 std::unique_ptr<geom::GeometryCollection> readGeometryCollectionText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
136 std::unique_ptr<geom::CircularString> readCircularStringText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
137 std::unique_ptr<geom::CompoundCurve> readCompoundCurveText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
138 std::unique_ptr<geom::CurvePolygon> readCurvePolygonText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
139 std::unique_ptr<geom::MultiCurve> readMultiCurveText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
140 std::unique_ptr<geom::MultiSurface> readMultiSurfaceText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
141
143 std::unique_ptr<geom::Curve> readCurveText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
144
146 std::unique_ptr<geom::Geometry> readSurfaceText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const;
147private:
148 const geom::GeometryFactory* geometryFactory;
149 const geom::PrecisionModel* precisionModel;
150 bool fixStructure;
151
152 void getPreciseCoordinate(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags, geom::CoordinateXYZM&) const;
153
154 static bool isNumberNext(io::StringTokenizer* tokenizer);
155 static bool isOpenerNext(io::StringTokenizer* tokenizer);
156
157 static void readOrdinateFlags(const std::string & s, OrdinateSet& ordinateFlags);
158 static bool isTypeName(const std::string & type, const std::string & typeName);
159};
160
161} // namespace io
162} // namespace geos
163
164
165
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:70
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:64
std::unique_ptr< geom::Geometry > readSurfaceText(io::StringTokenizer *tokenizer, OrdinateSet &ordinateFlags) const
Read the contents of a POLYGON or a CURVEPOLYGON.
std::unique_ptr< T > read(const std::string &wkt) const
Parse a WKT string returning a Geometry.
Definition WKTReader.h:107
std::unique_ptr< geom::Curve > readCurveText(io::StringTokenizer *tokenizer, OrdinateSet &ordinateFlags) const
Read the contents of a LINEARRING, LINESTRING, CIRCULARSTRING, or COMPOUNDCURVE.
WKTReader(const geom::GeometryFactory *gf)
Definition WKTReader.h:82
WKTReader()
Initialize parser with default GeometryFactory.
Definition WKTReader.h:92
WKTReader(const geom::GeometryFactory &gf)
Initialize parser with given GeometryFactory.
Definition WKTReader.h:75
GeometryTypeId
Geometry types.
Definition Geometry.h:74
Basic namespace for all GEOS functionalities.
Definition geos.h:39