/src/geos/include/geos/io/WKTReader.h
Line | Count | Source |
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 CircularString; |
44 | | class CompoundCurve; |
45 | | class Polygon; |
46 | | class CurvePolygon; |
47 | | class MultiPoint; |
48 | | class MultiLineString; |
49 | | class MultiCurve; |
50 | | class MultiPolygon; |
51 | | class MultiSurface; |
52 | | class PrecisionModel; |
53 | | } |
54 | | } |
55 | | |
56 | | |
57 | | namespace geos { |
58 | | namespace io { |
59 | | |
60 | | /** |
61 | | * \class WKTReader |
62 | | * \brief WKT parser class; see also WKTWriter. |
63 | | */ |
64 | | class GEOS_DLL WKTReader { |
65 | | public: |
66 | | |
67 | | /** |
68 | | * \brief Initialize parser with given GeometryFactory. |
69 | | * |
70 | | * Note that all Geometry objects created by the |
71 | | * parser will contain a pointer to the given factory |
72 | | * so be sure you'll keep the factory alive for the |
73 | | * whole WKTReader and created Geometry life. |
74 | | */ |
75 | | explicit WKTReader(const geom::GeometryFactory& gf) |
76 | | : geometryFactory(&gf) |
77 | | , precisionModel(gf.getPrecisionModel()) |
78 | | , fixStructure(false) |
79 | 0 | {}; |
80 | | |
81 | | /** @deprecated in 3.4.0 */ |
82 | | explicit WKTReader(const geom::GeometryFactory* gf) |
83 | 7.94k | : geometryFactory(gf) |
84 | 7.94k | , precisionModel(gf->getPrecisionModel()) |
85 | 7.94k | , fixStructure(false) |
86 | 7.94k | {}; |
87 | | |
88 | | /** |
89 | | * \brief Initialize parser with default GeometryFactory. |
90 | | * |
91 | | */ |
92 | | WKTReader() |
93 | | : geometryFactory(geom::GeometryFactory::getDefaultInstance()) |
94 | | , precisionModel(geometryFactory->getPrecisionModel()) |
95 | | , fixStructure(false) |
96 | 0 | {}; |
97 | | |
98 | 7.94k | ~WKTReader() {}; |
99 | | |
100 | | void |
101 | 0 | setFixStructure(bool doFixStructure) { |
102 | 0 | fixStructure = doFixStructure; |
103 | 0 | } |
104 | | |
105 | | /// Parse a WKT string returning a Geometry |
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 | | std::unique_ptr<geom::CoordinateSequence> readCoordinates(const std::string& wellKnownText) const; |
119 | | |
120 | | protected: |
121 | | std::unique_ptr<geom::CoordinateSequence> getCoordinates(io::StringTokenizer* tokenizer, OrdinateSet& ordinates) const; |
122 | | static double getNextNumber(io::StringTokenizer* tokenizer); |
123 | | static std::string getNextEmptyOrOpener(io::StringTokenizer* tokenizer, OrdinateSet& dim); |
124 | | static std::string getNextCloserOrComma(io::StringTokenizer* tokenizer); |
125 | | static std::string getNextCloser(io::StringTokenizer* tokenizer); |
126 | | static std::string getNextWord(io::StringTokenizer* tokenizer); |
127 | | std::unique_ptr<geom::Geometry> readGeometryTaggedText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags, const geom::GeometryTypeId* emptyType = nullptr) const; |
128 | | |
129 | | std::unique_ptr<geom::Point> readPointText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
130 | | std::unique_ptr<geom::LineString> readLineStringText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
131 | | std::unique_ptr<geom::LinearRing> readLinearRingText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
132 | | std::unique_ptr<geom::MultiPoint> readMultiPointText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
133 | | std::unique_ptr<geom::Polygon> readPolygonText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
134 | | std::unique_ptr<geom::MultiLineString> readMultiLineStringText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
135 | | std::unique_ptr<geom::MultiPolygon> readMultiPolygonText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
136 | | std::unique_ptr<geom::GeometryCollection> readGeometryCollectionText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
137 | | std::unique_ptr<geom::CircularString> readCircularStringText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
138 | | std::unique_ptr<geom::CompoundCurve> readCompoundCurveText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
139 | | std::unique_ptr<geom::CurvePolygon> readCurvePolygonText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
140 | | std::unique_ptr<geom::MultiCurve> readMultiCurveText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
141 | | std::unique_ptr<geom::MultiSurface> readMultiSurfaceText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
142 | | |
143 | | /// Read the contents of a LINEARRING, LINESTRING, CIRCULARSTRING, or COMPOUNDCURVE |
144 | | std::unique_ptr<geom::Curve> readCurveText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
145 | | |
146 | | /// Read the contents of a POLYGON or a CURVEPOLYGON |
147 | | std::unique_ptr<geom::Geometry> readSurfaceText(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags) const; |
148 | | private: |
149 | | const geom::GeometryFactory* geometryFactory; |
150 | | const geom::PrecisionModel* precisionModel; |
151 | | bool fixStructure; |
152 | | |
153 | | void getPreciseCoordinate(io::StringTokenizer* tokenizer, OrdinateSet& ordinateFlags, geom::CoordinateXYZM&) const; |
154 | | |
155 | | static bool isNumberNext(io::StringTokenizer* tokenizer); |
156 | | static bool isOpenerNext(io::StringTokenizer* tokenizer); |
157 | | |
158 | | static void readOrdinateFlags(const std::string & s, OrdinateSet& ordinateFlags); |
159 | | static bool isTypeName(const std::string & type, const std::string & typeName); |
160 | | }; |
161 | | |
162 | | } // namespace io |
163 | | } // namespace geos |
164 | | |
165 | | |
166 | | |