All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../common/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 "PointM.h"
35 #include "PointZ.h"
36 #include "PointZM.h"
37 #include "Polygon.h"
38 #include "PolyhedralSurface.h"
39 #include "WKTWriter.h"
40 
41 // STL
42 #include <cassert>
43 #include <cstring>
44 #include <sstream>
45 
47  : m_ostream(o),
48  m_tagged(true)
49 {
50 }
51 
53 {
54 }
55 
57 {
58  geom->accept(*this);
59 }
60 
62 {
63  geom.accept(*this);
64 }
65 
66 void te::gm::WKTWriter::write(const Geometry* geom, std::ostream& o)
67 {
68  WKTWriter w(o);
69  w.write(geom);
70 }
71 
72 void te::gm::WKTWriter::write(const Geometry& geom, std::ostream& o)
73 {
74  WKTWriter w(o);
75  w.write(geom);
76 }
77 
79 {
80  if(m_tagged)
81  {
82  switch(visited.getGeomTypeId())
83  {
85  m_ostream << "geometrycollection";
86  break;
87 
89  m_ostream << "geometrycollection z";
90  break;
91 
93  m_ostream << "geometrycollection m";
94  break;
95 
97  m_ostream << "geometrycollection zm";
98  break;
99 
100  default:
101  break;
102  }
103  }
104 
105  m_ostream << "(";
106 
107  std::size_t size = visited.getNumGeometries();
108 
109  for(std::size_t i = 0; i < size; ++i)
110  {
111  if(i != 0)
112  m_ostream << ",";
113 
114  write(visited.getGeometryN(i));
115  }
116 
117  m_ostream << ")";
118 }
119 
120 void te::gm::WKTWriter::visit(const LinearRing& /*visited*/)
121 {
122 }
123 
125 {
126  if(m_tagged)
127  {
128  switch(visited.getGeomTypeId())
129  {
130  case LineStringType:
131  m_ostream << "linestring";
132  break;
133 
134  case LineStringZType:
135  m_ostream << "linestring z";
136  break;
137 
138  case LineStringMType:
139  m_ostream << "linestring m";
140  break;
141 
142  case LineStringZMType:
143  m_ostream << "linestring zm";
144  break;
145 
146  default:
147  break;
148  }
149  }
150 
151  m_ostream << "(";
152 
153  const std::size_t nPts = visited.size();
154 
155  if(visited.getZ() == 0 && visited.getM() == 0)
156  {
157  for(std::size_t i = 0; i < nPts; ++i)
158  {
159  if(i != 0)
160  m_ostream << ",";
161 
162  m_ostream << visited.getCoordinates()[i].x << " " << visited.getCoordinates()[i].y;
163  }
164  }
165  else if(visited.getM() == 0)
166  {
167  for(std::size_t i = 0; i < nPts; ++i)
168  {
169  if(i != 0)
170  m_ostream << ",";
171 
172  m_ostream << visited.getCoordinates()[i].x << " " << visited.getCoordinates()[i].y << " " << visited.getZ()[i];
173  }
174  }
175  else if(visited.getZ() == 0)
176  {
177  for(std::size_t i = 0; i < nPts; ++i)
178  {
179  if(i != 0)
180  m_ostream << ",";
181 
182  m_ostream << visited.getCoordinates()[i].x << " " << visited.getCoordinates()[i].y << " " << visited.getM()[i];
183  }
184  }
185  else
186  {
187  for(std::size_t i = 0; i < nPts; ++i)
188  {
189  if(i != 0)
190  m_ostream << ",";
191 
192  m_ostream << visited.getCoordinates()[i].x << " " << visited.getCoordinates()[i].y << " " << visited.getZ()[i] << " " << visited.getM()[i];
193  }
194  }
195 
196  m_ostream << ")";
197 }
198 
200 {
201  if(m_tagged)
202  {
203  switch(visited.getGeomTypeId())
204  {
205  case MultiLineStringType:
206  m_ostream << "multilinestring";
207  break;
208 
210  m_ostream << "multilinestring z";
211  break;
212 
214  m_ostream << "multilinestring m";
215  break;
216 
218  m_ostream << "multilinestring zm";
219  break;
220 
221  default:
222  break;
223  }
224  }
225 
226  m_tagged = false;
227 
228  visit(static_cast<const GeometryCollection&>(visited));
229 }
230 
232 {
233  if(m_tagged)
234  {
235  switch(visited.getGeomTypeId())
236  {
237  case MultiPointType:
238  m_ostream << "multipoint";
239  break;
240 
241  case MultiPointZType:
242  m_ostream << "multipoint z";
243  break;
244 
245  case MultiPointMType:
246  m_ostream << "multipoint m";
247  break;
248 
249  case MultiPointZMType:
250  m_ostream << "multipoint zm";
251  break;
252 
253  default:
254  break;
255  }
256  }
257 
258  m_tagged = false;
259 
260  visit(static_cast<const GeometryCollection&>(visited));
261 }
262 
264 {
265  if(m_tagged)
266  {
267  switch(visited.getGeomTypeId())
268  {
269  case MultiPolygonType:
270  m_ostream << "multipolygon";
271  break;
272 
273  case MultiPolygonZType:
274  m_ostream << "multipolygon z";
275  break;
276 
277  case MultiPolygonMType:
278  m_ostream << "multipolygon m";
279  break;
280 
281  case MultiPolygonZMType:
282  m_ostream << "multipolygon zm";
283  break;
284 
285  default:
286  break;
287  }
288  }
289 
290  m_tagged = false;
291 
292  visit(static_cast<const GeometryCollection&>(visited));
293 }
294 
295 void te::gm::WKTWriter::visit(const Point& visited)
296 {
297  if(m_tagged)
298  m_ostream << "point";
299 
300  m_ostream << "(" << visited.getX() << " " << visited.getY() << ")";
301 }
302 
303 void te::gm::WKTWriter::visit(const PointM& visited)
304 {
305  if(m_tagged)
306  m_ostream << "point m";
307 
308  m_ostream << "(" << visited.getX() << " " << visited.getY() << " " << visited.getM() << ")";
309 }
310 
311 void te::gm::WKTWriter::visit(const PointZ& visited)
312 {
313  if(m_tagged)
314  m_ostream << "point z";
315 
316  m_ostream << "(" << visited.getX() << " " << visited.getY() << " " << visited.getZ() << ")";
317 }
318 
319 void te::gm::WKTWriter::visit(const PointZM& visited)
320 {
321  if(m_tagged)
322  m_ostream << "point zm";
323 
324  m_ostream << "(" << visited.getX() << " " << visited.getY() << " " << visited.getZ() << " " << visited.getM() << ")";
325 }
326 
327 void te::gm::WKTWriter::visit(const Polygon& visited)
328 {
329  if(m_tagged)
330  {
331  switch(visited.getGeomTypeId())
332  {
333  case PolygonType:
334  m_ostream << "polygon";
335  break;
336 
337  case PolygonZType:
338  m_ostream << "polygon z";
339  break;
340 
341  case PolygonMType:
342  m_ostream << "polygon m";
343  break;
344 
345  case PolygonZMType:
346  m_ostream << "polygon zm";
347  break;
348 
349  default:
350  break;
351  }
352  }
353 
354  m_tagged = false;
355 
356  m_ostream << "(";
357 
358  std::size_t size = visited.getNumRings();
359 
360  for(std::size_t i = 0; i < size; ++i)
361  {
362  if(i != 0)
363  m_ostream << ",";
364 
365  visit((const LineString&)(*visited[i]));
366  }
367 
368  m_ostream << ")";
369 }
370 
372 {
373  /*
374  if(tagged)
375  {
376  switch(m_gType)
377  {
378  case PolyhedralSurfaceType:
379  o << "polyhedralsurface";
380  break;
381 
382  case PolyhedralSurfaceZType:
383  o << "polyhedralsurface z";
384  break;
385 
386  case PolyhedralSurfaceMType:
387  o << "polyhedralsurface m";
388  break;
389 
390  case PolyhedralSurfaceZMType:
391  o << "polyhedralsurface zm";
392  break;
393 
394  default:
395  break;
396  }
397  }
398 
399  o << "(";
400 
401  std::size_t size = m_polygons.size();
402 
403  for(std::size_t i = 0; i < size; ++i)
404  {
405  if(i != 0)
406  o << ",";
407 
408  m_polygons[i]->asText(o, false);
409  }
410 
411  o << ")";*/
412 }
413 
414 void te::gm::WKTWriter::visit(const TIN& /*visited*/)
415 {
416  /*
417  if(tagged)
418  {
419  switch(m_gType)
420  {
421  case TINType:
422  o << "tin";
423  break;
424 
425  case TINZType:
426  o << "tin z";
427  break;
428 
429  case TINMType:
430  o << "tin m";
431  break;
432 
433  case TINZMType:
434  o << "tin zm";
435  break;
436 
437  default:
438  break;
439  }
440  }
441 
442  PolyhedralSurface::asText(o, false);*/
443 }
444 
445 void te::gm::WKTWriter::visit(const Triangle& /*visited*/)
446 {
447  /*if(tagged)
448  {
449  switch(m_gType)
450  {
451  case TriangleType:
452  o << "triangle";
453  break;
454 
455  case TriangleZType:
456  o << "triangle z";
457  break;
458 
459  case TriangleMType:
460  o << "triangle m";
461  break;
462 
463  case TriangleZMType:
464  o << "triangle zm";
465  break;
466 
467  default:
468  break;
469  }
470  }
471 
472  Polygon::asText(o, false);*/
473 }
474 
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.
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
PolyhedralSurface is a contiguous collection of polygons, which share common boundary segments...
MultiPoint is a GeometryCollection whose elements are restricted to points.
A point with a z-coordinate value and an associated measurement.
Definition: PointZM.h:51
A point with an associated measure.
Definition: PointM.h:51
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
A point with z-coordinate value.
Definition: PointZ.h:51
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:150
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:56
const double & getZ() const
It returns the Point z-coordinate value.
Definition: PointZM.h:160
virtual void visit(const Curve &)
Definition: WKTWriter.h:104
MultiPolygon is a MultiSurface whose elements are Polygons.
const double & getM() const
It returns the Point z-coordinate value.
Definition: PointM.h:160
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
An exception class for the Geometry module.
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.
const double & getZ() const
It returns the Point z-coordinate value.
Definition: PointZ.h:138
~WKTWriter()
Destructor.
Definition: WKTWriter.cpp:52
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
WKTWriter(std::ostream &o)
It constructs a new WKT writer.
Definition: WKTWriter.cpp:46
const double & getM() const
It returns the Point z-coordinate value.
Definition: PointZM.h:174
const double & getZ(std::size_t i) const
It returns the n-th z coordinate value.
Definition: LineString.cpp:397
const double & getM(std::size_t i) const
It returns the n-th m measure value.
Definition: LineString.cpp:403
It is a collection of other geometric objects.
A point with z-coordinate value.
A point with a z-coordinate value and an associated measurement.
A point with an associated measure.
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
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