All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GeomReader.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2009-2013 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/terralib4/GeomReader.cpp
22 
23  \brief An utility class for converting a TerraLib 4.x geometry to a TerraLib 5.
24 */
25 
26 // Terralib 5
27 #include "../common/Translator.h"
28 #include "../geometry/Curve.h"
29 #include "../geometry/LinearRing.h"
30 #include "../geometry/LineString.h"
31 #include "../geometry/Point.h"
32 #include "../geometry/Polygon.h"
33 #include "../geometry/MultiPolygon.h"
34 #include "../geometry/MultilineString.h"
35 #include "../geometry/MultiPoint.h"
36 #include "Config.h"
37 #include "Exception.h"
38 #include "GeomReader.h"
39 #include "Utils.h"
40 
41 // Terralib 4.x
42 #include <terralib4/kernel/TeGeometry.h>
43 #include <terralib4/kernel/TeRepresentation.h>
44 
45 std::auto_ptr<te::gm::Point> terralib4::GeomReader::getPoint(const TePoint& pt)
46 {
47  std::auto_ptr<te::gm::Point> geom(new te::gm::Point(pt.srid()));
48  geom->setX(pt.box().x1());
49  geom->setY(pt.box().y1());
50  return geom;
51 }
52 
53 std::auto_ptr<te::gm::LineString> terralib4::GeomReader::getLineString(const TeLine2D& line)
54 {
55  std::auto_ptr<te::gm::LineString> geom(new te::gm::LineString(line.size(), te::gm::LineStringType, line.srid()));
56 
57  TeComposite<TeCoord2D>::iterator it = line.begin();
58 
59  int count = 0;
60  while(it != line.end())
61  {
62  geom->setPoint(count, it->x(), it->y());
63 
64  ++count;
65  ++it;
66  }
67 
68  return geom;
69 }
70 
71 std::auto_ptr<te::gm::LinearRing> terralib4::GeomReader::getLinearRing(const TeLinearRing& ring)
72 {
73  std::auto_ptr<te::gm::LinearRing> geom(new te::gm::LinearRing(ring.size(),te::gm::LineStringType, ring.srid()));
74 
75  TeComposite<TeCoord2D>::iterator it = ring.begin();
76 
77  int count = 0;
78  while(it != ring.end())
79  {
80  geom->setPoint(count, it->x(), it->y());
81 
82  ++count;
83  ++it;
84  }
85 
86  return geom;
87 }
88 
89 std::auto_ptr<te::gm::Polygon> terralib4::GeomReader::getPolygon(const TePolygon& poly)
90 {
91  std::auto_ptr<te::gm::Polygon> geom(new te::gm::Polygon(poly.size(), te::gm::PolygonType, poly.srid()));
92 
93  TeComposite<TeLinearRing>::iterator it = poly.begin();
94 
95  std::size_t count = 0;
96  while(it != poly.end())
97  {
98  geom->setRingN(count, getLinearRing(*it).release());
99 
100  ++count;
101  ++it;
102  }
103 
104  return geom;
105 }
106 
107 std::auto_ptr<te::gm::MultiPolygon> terralib4::GeomReader::getMultiPolygon(const TePolygonSet& polySet)
108 {
109  std::auto_ptr<te::gm::MultiPolygon> geom(new te::gm::MultiPolygon(polySet.size(),te::gm::MultiPolygonType, polySet.srid()));
110 
111  TeComposite<TePolygon>::iterator it = polySet.begin();
112 
113  while(it != polySet.end())
114  {
115  geom->add(getPolygon(*it).release());
116 
117  ++it;
118  }
119 
120  return geom;
121 }
122 
123 std::auto_ptr<te::gm::MultiLineString> terralib4::GeomReader::getMultiLineString(const TeLineSet& lineSet)
124 {
125  std::auto_ptr<te::gm::MultiLineString> geom(new te::gm::MultiLineString(lineSet.size(), te::gm::MultiLineStringType, lineSet.srid()));
126 
127  TeComposite<TeLine2D>::iterator it = lineSet.begin();
128 
129  while(it != lineSet.end())
130  {
131  geom->add(getLineString(*it).release());
132 
133  ++it;
134  }
135 
136  return geom;
137 }
138 
139 std::auto_ptr<te::gm::MultiPoint> terralib4::GeomReader::getMultiPoint(const TePointSet& pointSet)
140 {
141  std::auto_ptr<te::gm::MultiPoint> geom(new te::gm::MultiPoint(pointSet.size(), te::gm::MultiPointType, pointSet.srid()));
142 
143  TeComposite<TePoint>::iterator it = pointSet.begin();
144 
145  while(it != pointSet.end())
146  {
147  geom->add(getPoint(*it).release());
148 
149  ++it;
150  }
151 
152  return geom;
153 }
154 
155 std::auto_ptr<te::gm::Polygon> terralib4::GeomReader::getPolygon(const TeCell& cell)
156 {
157  std::auto_ptr<te::gm::Polygon> geom(new te::gm::Polygon(cell.size(), te::gm::PolygonType, cell.srid()));
158 
159  TeBox cellBox = cell.box();
160 
162  s->setPoint(0, cellBox.x1(), cellBox.y1());
163  s->setPoint(1, cellBox.x2()-cellBox.x1(), cellBox.y1());
164  s->setPoint(2, cellBox.x2(), cellBox.y2());
165  s->setPoint(3, cellBox.x1(), cellBox.y2()-cellBox.y1());
166  s->setPoint(4, cellBox.x1(), cellBox.y1());
167 
168  geom->add(s);
169 
170  return geom;
171 }
172 
173 std::auto_ptr<te::gm::Geometry> terralib4::GeomReader::getGeometry(const TeGeometry& geom)
174 {
175  TeGeomRep rep = geom.elemType();
176 
177  switch(rep)
178  {
179  case TePOLYGONS:
180  {
181  TePolygon p = dynamic_cast<const TePolygon&>(geom);
182  return std::auto_ptr<te::gm::Geometry>(getPolygon(p).release());
183  }
184 
185  case TePOINTS:
186  {
187  TePoint p = dynamic_cast<const TePoint&>(geom);
188  return std::auto_ptr<te::gm::Geometry>(getPoint(p).release());
189  }
190 
191  case TeLINES:
192  {
193  TeLine2D p = dynamic_cast<const TeLine2D&>(geom);
194  return std::auto_ptr<te::gm::Geometry>(getLineString(p).release());
195  }
196 
197  case TeCELLS:
198  {
199  TePolygon p = dynamic_cast<const TePolygon&>(geom);
200  return std::auto_ptr<te::gm::Geometry>(getPolygon(p).release());
201  }
202 
203  case TeNODES:
204  {
205  TePoint p = dynamic_cast<const TePoint&>(geom);
206  return std::auto_ptr<te::gm::Geometry>(getPoint(p).release());
207  }
208 
209  default:
210  throw Exception(TE_TR("Geometry Type Not supported!"));
211 
212  }
213 
214 
215  return std::auto_ptr<te::gm::Geometry>(0);
216 }
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
static std::auto_ptr< te::gm::Geometry > getGeometry(const TeGeometry &geom)
Definition: GeomReader.cpp:173
static std::auto_ptr< te::gm::LineString > getLineString(const TeLine2D &line)
Definition: GeomReader.cpp:53
static std::auto_ptr< te::gm::MultiLineString > getMultiLineString(const TeLineSet &lineSet)
Definition: GeomReader.cpp:123
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
Configuration flags for the TerraLib 4 driver.
static std::auto_ptr< te::gm::LinearRing > getLinearRing(const TeLinearRing &ring)
Definition: GeomReader.cpp:71
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
A point with x and y coordinate values.
Definition: Point.h:50
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
Definition: LineString.cpp:352
static std::auto_ptr< te::gm::MultiPolygon > getMultiPolygon(const TePolygonSet &polySet)
Definition: GeomReader.cpp:107
static std::auto_ptr< te::gm::Polygon > getPolygon(const TePolygon &poly)
Definition: GeomReader.cpp:89
static std::auto_ptr< te::gm::Point > getPoint(const TePoint &pt)
Definition: GeomReader.cpp:45
MultiLineString is a MultiCurve whose elements are LineStrings.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
static std::auto_ptr< te::gm::MultiPoint > getMultiPoint(const TePointSet &pointSet)
Definition: GeomReader.cpp:139
An utility class for converting a TerraLib 4.x geometry to a TerraLib 5.
Utilitary functions for dealing with TerraLib 5 and 4.x conversion.