GEOS  3.13.0dev
WKTWriter.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2011 Sandro Santilli <strk@kbt.io>
7  * Copyright (C) 2005-2006 Refractions Research Inc.
8  * Copyright (C) 2001-2002 Vivid Solutions Inc.
9  *
10  * This is free software; you can redistribute and/or modify it under
11  * the terms of the GNU Lesser General Public Licence as published
12  * by the Free Software Foundation.
13  * See the COPYING file for more information.
14  *
15  **********************************************************************
16  *
17  * Last port: io/WKTWriter.java rev. 1.34 (JTS-1.7)
18  *
19  **********************************************************************/
20 
21 #pragma once
22 
23 #include <geos/export.h>
24 #include <geos/io/OrdinateSet.h>
25 
26 #include <string>
27 #include <cctype>
28 #include <cstdint>
29 
30 #ifdef _MSC_VER
31 #pragma warning(push)
32 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
33 #endif
34 
35 // Forward declarations
36 namespace geos {
37 namespace geom {
38 class Coordinate;
39 class CoordinateXY;
40 class CoordinateXYZM;
41 class CoordinateSequence;
42 class Geometry;
43 class GeometryCollection;
44 class Point;
45 class LineString;
46 class LinearRing;
47 class Polygon;
48 class MultiPoint;
49 class MultiLineString;
50 class MultiPolygon;
51 class PrecisionModel;
52 }
53 namespace io {
54 class Writer;
55 }
56 }
57 
58 
59 namespace geos {
60 namespace io {
61 
83 class GEOS_DLL WKTWriter {
84 public:
85  WKTWriter();
86  ~WKTWriter() = default;
87 
88  //string(count, ch) can be used for this
89  //static string stringOfChar(char ch, int count);
90 
92  std::string write(const geom::Geometry* geometry);
93 
94  std::string write(const geom::Geometry& geometry);
95 
96  // Send Geometry's WKT to the given Writer
97  void write(const geom::Geometry* geometry, Writer* writer);
98 
99  std::string writeFormatted(const geom::Geometry* geometry);
100 
101  void writeFormatted(const geom::Geometry* geometry, Writer* writer);
102 
110  static std::string toLineString(const geom::CoordinateSequence& seq);
111 
120  static std::string toLineString(const geom::Coordinate& p0, const geom::Coordinate& p1);
121 
129  static std::string toPoint(const geom::Coordinate& p0);
130  static std::string toPoint(const geom::CoordinateXY& p0);
131 
139  void setRoundingPrecision(int p0);
140 
147  void setTrim(bool p0);
148 
156  void setRemoveEmptyDimensions(bool remove)
157  {
158  removeEmptyDimensions = remove;
159  }
160 
171  void
172  setOld3D(bool useOld3D)
173  {
174  old3D = useOld3D;
175  }
176 
177  /*
178  * \brief
179  * Returns the output dimension used by the
180  * <code>WKTWriter</code>.
181  */
182  int
183  getOutputDimension() const
184  {
185  return defaultOutputDimension;
186  }
187 
188  /*
189  * Sets the output dimension used by the <code>WKTWriter</code>.
190  *
191  * @param newOutputDimension Supported values are 2, 3 or 4.
192  * Default since GEOS 3.12 is 4.
193  * Note that 3 indicates up to 3 dimensions will be
194  * written but 2D WKT is still produced for 2D geometries.
195  */
196  void setOutputDimension(uint8_t newOutputDimension);
197 
198  static std::string writeNumber(double d, bool trim, uint32_t precision);
199  static int writeTrimmedNumber(double d, uint32_t precision, char* buf);
200 
201 protected:
202 
203  int decimalPlaces;
204 
205  void appendGeometryTaggedText(
206  const geom::Geometry& geometry,
207  OrdinateSet outputOrdinates,
208  int level,
209  Writer& writer) const;
210 
211  void appendPointTaggedText(
212  const geom::Point& point,
213  OrdinateSet outputOrdinates,
214  int level, Writer& writer) const;
215 
216  void appendLineStringTaggedText(
217  const geom::LineString& lineString,
218  OrdinateSet outputOrdinates,
219  int level, Writer& writer) const;
220 
221  void appendLinearRingTaggedText(
222  const geom::LinearRing& lineString,
223  OrdinateSet outputOrdinates,
224  int level, Writer& writer) const;
225 
226  void appendPolygonTaggedText(
227  const geom::Polygon& polygon,
228  OrdinateSet outputOrdinates,
229  int level, Writer& writer) const;
230 
231  void appendMultiPointTaggedText(
232  const geom::MultiPoint& multipoint,
233  OrdinateSet outputOrdinates,
234  int level, Writer& writer) const;
235 
236  void appendMultiLineStringTaggedText(
237  const geom::MultiLineString& multiLineString,
238  OrdinateSet outputOrdinates,
239  int level, Writer& writer) const;
240 
241  void appendMultiPolygonTaggedText(
242  const geom::MultiPolygon& multiPolygon,
243  OrdinateSet outputOrdinates,
244  int level, Writer& writer) const;
245 
246  void appendGeometryCollectionTaggedText(
247  const geom::GeometryCollection& geometryCollection,
248  OrdinateSet outputOrdinates,
249  int level, Writer& writer) const;
250 
251  void appendOrdinateText(OrdinateSet outputOrdinates,
252  Writer& writer) const;
253 
254  void appendSequenceText(const geom::CoordinateSequence& seq,
255  OrdinateSet outputOrdinates,
256  int level,
257  bool doIntent,
258  Writer& writer) const;
259 
260  void appendCoordinate(const geom::CoordinateXYZM& coordinate,
261  OrdinateSet outputOrdinates,
262  Writer& writer) const;
263 
264  std::string writeNumber(double d) const;
265 
266  void appendLineStringText(
267  const geom::LineString& lineString,
268  OrdinateSet outputOrdinates,
269  int level, bool doIndent, Writer& writer) const;
270 
271  void appendPolygonText(
272  const geom::Polygon& polygon,
273  OrdinateSet outputOrdinates,
274  int level, bool indentFirst, Writer& writer) const;
275 
276  void appendMultiPointText(
277  const geom::MultiPoint& multiPoint,
278  OrdinateSet outputOrdinates,
279  int level, Writer& writer) const;
280 
281  void appendMultiLineStringText(
282  const geom::MultiLineString& multiLineString,
283  OrdinateSet outputOrdinates,
284  int level, bool indentFirst, Writer& writer) const;
285 
286  void appendMultiPolygonText(
287  const geom::MultiPolygon& multiPolygon,
288  OrdinateSet outputOrdinates,
289  int level, Writer& writer) const;
290 
291  void appendGeometryCollectionText(
292  const geom::GeometryCollection& geometryCollection,
293  OrdinateSet outputOrdinates,
294  int level, Writer& writer) const;
295 
296 private:
297 
298  enum {
299  INDENT = 2
300  };
301 
302 // static const int INDENT = 2;
303 
304  bool isFormatted;
305 
306  int roundingPrecision;
307 
308  bool trim;
309 
310  bool removeEmptyDimensions = false;
311 
312  int level;
313 
314  static constexpr int coordsPerLine = 10;
315 
316  uint8_t defaultOutputDimension;
317  bool old3D;
318 
319  void writeFormatted(
320  const geom::Geometry* geometry,
321  bool isFormatted, Writer* writer);
322 
323  void indent(int level, Writer* writer) const;
324 };
325 
326 } // namespace geos::io
327 } // namespace geos
328 
329 #ifdef _MSC_VER
330 #pragma warning(pop)
331 #endif
332 
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:56
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:216
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:51
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
Definition: LineString.h:65
Models an OGC SFS LinearRing. A LinearRing is a LineString which is both closed and simple.
Definition: LinearRing.h:54
Models a collection of LineStrings.
Definition: MultiLineString.h:49
Definition: MultiPoint.h:50
Definition: MultiPolygon.h:58
Definition: Point.h:61
Represents a linear polygon, which may include holes.
Definition: Polygon.h:60
Outputs the textual representation of a Geometry. See also WKTReader.
Definition: WKTWriter.h:83
static std::string toPoint(const geom::Coordinate &p0)
static std::string toLineString(const geom::Coordinate &p0, const geom::Coordinate &p1)
void setOld3D(bool useOld3D)
Definition: WKTWriter.h:172
void setTrim(bool p0)
void setRoundingPrecision(int p0)
std::string write(const geom::Geometry *geometry)
Returns WKT string for the given Geometry.
void setRemoveEmptyDimensions(bool remove)
setRemoveEmptyDimensions
Definition: WKTWriter.h:156
static std::string toLineString(const geom::CoordinateSequence &seq)
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25