WKTWriter.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/geometry/WKTWriter.cpp
22 
23  \brief A class that serializes a geometry to the WKT format.
24 */
25 
26 // TerraLib
27 #include "../core/translator/Translator.h"
28 #include "Exception.h"
29 #include "GeometryCollection.h"
30 #include "LinearRing.h"
31 #include "MultiLineString.h"
32 #include "MultiPoint.h"
33 #include "MultiPolygon.h"
34 #include "Point.h"
35 #include "Polygon.h"
36 #include "PolyhedralSurface.h"
37 #include "WKTWriter.h"
38 
39 // STL
40 #include <cassert>
41 #include <cstring>
42 #include <sstream>
43 
45  : m_ostream(o),
46  m_tagged(true)
47 {
48 }
49 
51 
53 {
54  geom->accept(*this);
55 }
56 
58 {
59  geom.accept(*this);
60 }
61 
62 void te::gm::WKTWriter::write(const Geometry* geom, std::ostream& o)
63 {
64  WKTWriter w(o);
65  w.write(geom);
66 }
67 
68 void te::gm::WKTWriter::write(const Geometry& geom, std::ostream& o)
69 {
70  WKTWriter w(o);
71  w.write(geom);
72 }
73 
75 {
76  if(m_tagged)
77  {
78  switch(visited.getGeomTypeId())
79  {
81  m_ostream << "geometrycollection";
82  break;
83 
85  m_ostream << "geometrycollection z";
86  break;
87 
89  m_ostream << "geometrycollection m";
90  break;
91 
93  m_ostream << "geometrycollection zm";
94  break;
95 
96  default:
97  break;
98  }
99  }
100 
101  m_ostream << "(";
102 
103  std::size_t size = visited.getNumGeometries();
104 
105  for(std::size_t i = 0; i < size; ++i)
106  {
107  if(i != 0)
108  m_ostream << ",";
109 
110  write(visited.getGeometryN(i));
111  }
112 
113  m_ostream << ")";
114 }
115 
116 void te::gm::WKTWriter::visit(const LinearRing& /*visited*/)
117 {
118 }
119 
121 {
122  if(m_tagged)
123  {
124  switch(visited.getGeomTypeId())
125  {
126  case LineStringType:
127  m_ostream << "linestring";
128  break;
129 
130  case LineStringZType:
131  m_ostream << "linestring z";
132  break;
133 
134  case LineStringMType:
135  m_ostream << "linestring m";
136  break;
137 
138  case LineStringZMType:
139  m_ostream << "linestring zm";
140  break;
141 
142  default:
143  break;
144  }
145  }
146 
147  m_ostream << "(";
148 
149  const std::size_t nPts = visited.size();
150 
151  if(visited.getZ() == nullptr && visited.getM() == nullptr)
152  {
153  for(std::size_t i = 0; i < nPts; ++i)
154  {
155  if(i != 0)
156  m_ostream << ",";
157 
158  m_ostream << visited.getCoordinates()[i].x << " " << visited.getCoordinates()[i].y;
159  }
160  }
161  else if(visited.getM() == nullptr)
162  {
163  for(std::size_t i = 0; i < nPts; ++i)
164  {
165  if(i != 0)
166  m_ostream << ",";
167 
168  m_ostream << visited.getCoordinates()[i].x << " " << visited.getCoordinates()[i].y << " " << visited.getZ()[i];
169  }
170  }
171  else if(visited.getZ() == nullptr)
172  {
173  for(std::size_t i = 0; i < nPts; ++i)
174  {
175  if(i != 0)
176  m_ostream << ",";
177 
178  m_ostream << visited.getCoordinates()[i].x << " " << visited.getCoordinates()[i].y << " " << visited.getM()[i];
179  }
180  }
181  else
182  {
183  for(std::size_t i = 0; i < nPts; ++i)
184  {
185  if(i != 0)
186  m_ostream << ",";
187 
188  m_ostream << visited.getCoordinates()[i].x << " " << visited.getCoordinates()[i].y << " " << visited.getZ()[i] << " " << visited.getM()[i];
189  }
190  }
191 
192  m_ostream << ")";
193 }
194 
196 {
197  if(m_tagged)
198  {
199  switch(visited.getGeomTypeId())
200  {
201  case MultiLineStringType:
202  m_ostream << "multilinestring";
203  break;
204 
206  m_ostream << "multilinestring z";
207  break;
208 
210  m_ostream << "multilinestring m";
211  break;
212 
214  m_ostream << "multilinestring zm";
215  break;
216 
217  default:
218  break;
219  }
220  }
221 
222  m_tagged = false;
223 
224  visit(static_cast<const GeometryCollection&>(visited));
225 }
226 
228 {
229  if(m_tagged)
230  {
231  switch(visited.getGeomTypeId())
232  {
233  case MultiPointType:
234  m_ostream << "multipoint";
235  break;
236 
237  case MultiPointZType:
238  m_ostream << "multipoint z";
239  break;
240 
241  case MultiPointMType:
242  m_ostream << "multipoint m";
243  break;
244 
245  case MultiPointZMType:
246  m_ostream << "multipoint zm";
247  break;
248 
249  default:
250  break;
251  }
252  }
253 
254  m_tagged = false;
255 
256  visit(static_cast<const GeometryCollection&>(visited));
257 }
258 
260 {
261  if(m_tagged)
262  {
263  switch(visited.getGeomTypeId())
264  {
265  case MultiPolygonType:
266  m_ostream << "multipolygon";
267  break;
268 
269  case MultiPolygonZType:
270  m_ostream << "multipolygon z";
271  break;
272 
273  case MultiPolygonMType:
274  m_ostream << "multipolygon m";
275  break;
276 
277  case MultiPolygonZMType:
278  m_ostream << "multipolygon zm";
279  break;
280 
281  default:
282  break;
283  }
284  }
285 
286  m_tagged = false;
287 
288  visit(static_cast<const GeometryCollection&>(visited));
289 }
290 
291 void te::gm::WKTWriter::visit(const Point& visited)
292 {
293  if(m_tagged)
294  switch (visited.getGeomTypeId())
295  {
296  case PointType:
297  m_ostream << "point";
298  m_ostream << "(" << visited.getX() << " " << visited.getY() << ")";
299  break;
300 
301  case PointZType:
302  m_ostream << "point z";
303  m_ostream << "(" << visited.getX() << " " << visited.getY() << " " << visited.getZ() << ")";
304  break;
305 
306  case PointMType:
307  m_ostream << "point m";
308  m_ostream << "(" << visited.getX() << " " << visited.getY() << " " << visited.getM() << ")";
309  break;
310 
311  case PointZMType:
312  m_ostream << "point zm";
313  m_ostream << "(" << visited.getX() << " " << visited.getY() << " " << visited.getZ() << " " << visited.getM() << ")";
314  break;
315 
316  default:
317  break;
318  }
319 }
320 
321 void te::gm::WKTWriter::visit(const Polygon& visited)
322 {
323  if(m_tagged)
324  {
325  switch(visited.getGeomTypeId())
326  {
327  case PolygonType:
328  m_ostream << "polygon";
329  break;
330 
331  case PolygonZType:
332  m_ostream << "polygon z";
333  break;
334 
335  case PolygonMType:
336  m_ostream << "polygon m";
337  break;
338 
339  case PolygonZMType:
340  m_ostream << "polygon zm";
341  break;
342 
343  default:
344  break;
345  }
346  }
347 
348  m_tagged = false;
349 
350  m_ostream << "(";
351 
352  std::size_t size = visited.getNumRings();
353 
354  for(std::size_t i = 0; i < size; ++i)
355  {
356  if(i != 0)
357  m_ostream << ",";
358 
359  visit((const LineString&)(*visited[i]));
360  }
361 
362  m_ostream << ")";
363 }
364 
366 {
367  /*
368  if(tagged)
369  {
370  switch(m_gType)
371  {
372  case PolyhedralSurfaceType:
373  o << "polyhedralsurface";
374  break;
375 
376  case PolyhedralSurfaceZType:
377  o << "polyhedralsurface z";
378  break;
379 
380  case PolyhedralSurfaceMType:
381  o << "polyhedralsurface m";
382  break;
383 
384  case PolyhedralSurfaceZMType:
385  o << "polyhedralsurface zm";
386  break;
387 
388  default:
389  break;
390  }
391  }
392 
393  o << "(";
394 
395  std::size_t size = m_polygons.size();
396 
397  for(std::size_t i = 0; i < size; ++i)
398  {
399  if(i != 0)
400  o << ",";
401 
402  m_polygons[i]->asText(o, false);
403  }
404 
405  o << ")";*/
406 }
407 
408 void te::gm::WKTWriter::visit(const TIN& /*visited*/)
409 {
410  /*
411  if(tagged)
412  {
413  switch(m_gType)
414  {
415  case TINType:
416  o << "tin";
417  break;
418 
419  case TINZType:
420  o << "tin z";
421  break;
422 
423  case TINMType:
424  o << "tin m";
425  break;
426 
427  case TINZMType:
428  o << "tin zm";
429  break;
430 
431  default:
432  break;
433  }
434  }
435 
436  PolyhedralSurface::asText(o, false);*/
437 }
438 
439 void te::gm::WKTWriter::visit(const Triangle& /*visited*/)
440 {
441  /*if(tagged)
442  {
443  switch(m_gType)
444  {
445  case TriangleType:
446  o << "triangle";
447  break;
448 
449  case TriangleZType:
450  o << "triangle z";
451  break;
452 
453  case TriangleMType:
454  o << "triangle m";
455  break;
456 
457  case TriangleZMType:
458  o << "triangle zm";
459  break;
460 
461  default:
462  break;
463  }
464  }
465 
466  Polygon::asText(o, false);*/
467 }
468 
PolyhedralSurface is a contiguous collection of polygons, which share common boundary segments...
std::size_t getNumRings() const
It returns the number of rings in this CurvePolygon.
Definition: CurvePolygon.h:153
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
std::ostream & m_ostream
The output stream used to serialize the geometry to WKT.
Definition: WKTWriter.h:126
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
double y
y-coordinate.
Definition: Coord2D.h:114
A class that serializes a geometry to the WKT format.
Definition: WKTWriter.h:54
A LinearRing is a LineString that is both closed and simple.
Coord2D * getCoordinates() const
It returns a pointer to the internal array of coordinates.
Definition: LineString.h:456
double x
x-coordinate.
Definition: Coord2D.h:113
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
A point with x and y coordinate values.
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
PolyhedralSurface is a contiguous collection of polygons, which share common boundary segments...
MultiPoint is a GeometryCollection whose elements are restricted to points.
TIN (triangulated irregular network) is a PolyhedralSurface consisting only of Triangle patches...
Definition: TIN.h:50
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
Triangle is a polygon with 3 distinct, non-collinear vertices and no interior boundary.
Definition: Triangle.h:50
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:152
A point with x and y coordinate values.
Definition: Point.h:50
MultiLineString is a MultiCurve whose elements are LineStrings.
void write(const Geometry *geom)
It serializes the geometry to a WKT representation.
Definition: WKTWriter.cpp:52
virtual void visit(const Curve &)
Definition: WKTWriter.h:104
MultiPolygon is a MultiSurface whose elements are Polygons.
const double & getZ() const
It returns the Point z-coordinate value, if it has one or DoubleNotANumber otherwise.
Definition: Point.h:166
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
MultiLineString is a MultiCurve whose elements are LineStrings.
~WKTWriter()
Destructor.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
WKTWriter(std::ostream &o)
It constructs a new WKT writer.
Definition: WKTWriter.cpp:44
const double & getM() const
It returns the Point m-coordinate value, if it has one or DoubleNotANumber otherwise.
Definition: Point.h:180
const double & getZ(std::size_t i) const
It returns the n-th z coordinate value.
const double & getM(std::size_t i) const
It returns the n-th m measure value.
It is a collection of other geometric objects.
An exception class for the Geometry module.
bool m_tagged
This flag controls how geometry are write to the output stream.
Definition: WKTWriter.h:127
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:138
A class that serializes a geometry to the WKT format.
It is a collection of other geometric objects.
std::size_t size() const
It returns the number of points (vertexes) in the geometry.
Definition: LineString.h:262