GEOS  3.12.0
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 protected:
199 
200  int decimalPlaces;
201 
202  void appendGeometryTaggedText(
203  const geom::Geometry& geometry,
204  OrdinateSet outputOrdinates,
205  int level,
206  Writer& writer) const;
207 
208  void appendPointTaggedText(
209  const geom::Point& point,
210  OrdinateSet outputOrdinates,
211  int level, Writer& writer) const;
212 
213  void appendLineStringTaggedText(
214  const geom::LineString& lineString,
215  OrdinateSet outputOrdinates,
216  int level, Writer& writer) const;
217 
218  void appendLinearRingTaggedText(
219  const geom::LinearRing& lineString,
220  OrdinateSet outputOrdinates,
221  int level, Writer& writer) const;
222 
223  void appendPolygonTaggedText(
224  const geom::Polygon& polygon,
225  OrdinateSet outputOrdinates,
226  int level, Writer& writer) const;
227 
228  void appendMultiPointTaggedText(
229  const geom::MultiPoint& multipoint,
230  OrdinateSet outputOrdinates,
231  int level, Writer& writer) const;
232 
233  void appendMultiLineStringTaggedText(
234  const geom::MultiLineString& multiLineString,
235  OrdinateSet outputOrdinates,
236  int level, Writer& writer) const;
237 
238  void appendMultiPolygonTaggedText(
239  const geom::MultiPolygon& multiPolygon,
240  OrdinateSet outputOrdinates,
241  int level, Writer& writer) const;
242 
243  void appendGeometryCollectionTaggedText(
244  const geom::GeometryCollection& geometryCollection,
245  OrdinateSet outputOrdinates,
246  int level, Writer& writer) const;
247 
248  void appendOrdinateText(OrdinateSet outputOrdinates,
249  Writer& writer) const;
250 
251  void appendSequenceText(const geom::CoordinateSequence& seq,
252  OrdinateSet outputOrdinates,
253  int level,
254  bool doIntent,
255  Writer& writer) const;
256 
257  void appendCoordinate(const geom::CoordinateXYZM& coordinate,
258  OrdinateSet outputOrdinates,
259  Writer& writer) const;
260 
261  std::string writeNumber(double d) const;
262 
263  void appendLineStringText(
264  const geom::LineString& lineString,
265  OrdinateSet outputOrdinates,
266  int level, bool doIndent, Writer& writer) const;
267 
268  void appendPolygonText(
269  const geom::Polygon& polygon,
270  OrdinateSet outputOrdinates,
271  int level, bool indentFirst, Writer& writer) const;
272 
273  void appendMultiPointText(
274  const geom::MultiPoint& multiPoint,
275  OrdinateSet outputOrdinates,
276  int level, Writer& writer) const;
277 
278  void appendMultiLineStringText(
279  const geom::MultiLineString& multiLineString,
280  OrdinateSet outputOrdinates,
281  int level, bool indentFirst, Writer& writer) const;
282 
283  void appendMultiPolygonText(
284  const geom::MultiPolygon& multiPolygon,
285  OrdinateSet outputOrdinates,
286  int level, Writer& writer) const;
287 
288  void appendGeometryCollectionText(
289  const geom::GeometryCollection& geometryCollection,
290  OrdinateSet outputOrdinates,
291  int level, Writer& writer) const;
292 
293 private:
294 
295  enum {
296  INDENT = 2
297  };
298 
299 // static const int INDENT = 2;
300 
301  bool isFormatted;
302 
303  int roundingPrecision;
304 
305  bool trim;
306 
307  bool removeEmptyDimensions = false;
308 
309  int level;
310 
311  static constexpr int coordsPerLine = 10;
312 
313  uint8_t defaultOutputDimension;
314  bool old3D;
315 
316  void writeFormatted(
317  const geom::Geometry* geometry,
318  bool isFormatted, Writer* writer);
319 
320  void indent(int level, Writer* writer) const;
321 };
322 
323 } // namespace geos::io
324 } // namespace geos
325 
326 #ifdef _MSC_VER
327 #pragma warning(pop)
328 #endif
329 
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:216
Definition: MultiPolygon.h:58
void setRemoveEmptyDimensions(bool remove)
setRemoveEmptyDimensions
Definition: WKTWriter.h:156
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
Definition: LineString.h:65
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
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:51
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25
Definition: MultiPoint.h:50
Models an OGC SFS LinearRing. A LinearRing is a LineString which is both closed and simple...
Definition: LinearRing.h:54
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:56
void setOld3D(bool useOld3D)
Definition: WKTWriter.h:172
Definition: Point.h:61
Models a collection of LineStrings.
Definition: MultiLineString.h:49