GEOS 3.11.1
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
31namespace geos {
32namespace io {
33class StringTokenizer;
34}
35namespace geom {
36class Coordinate;
37class CoordinateSequence;
38class GeometryCollection;
39class Point;
40class LineString;
41class LinearRing;
42class Polygon;
43class MultiPoint;
44class MultiLineString;
45class MultiPolygon;
46class PrecisionModel;
47}
48}
49
50
51namespace geos {
52namespace io {
53
58class GEOS_DLL WKTReader {
59public:
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
113protected:
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;
129private:
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: geos.h:39