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) 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/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/Envelope.h"
30 #include "../geometry/LinearRing.h"
31 #include "../geometry/LineString.h"
32 #include "../geometry/Point.h"
33 #include "../geometry/Polygon.h"
34 #include "../geometry/MultiPolygon.h"
35 #include "../geometry/MultilineString.h"
36 #include "../geometry/MultiPoint.h"
37 #include "../geometry/Utils.h"
38 #include "Config.h"
39 #include "Exception.h"
40 #include "GeomReader.h"
41 #include "Utils.h"
42 
43 // Terralib 4.x
44 #include <terralib4/kernel/TeGeometry.h>
45 #include <terralib4/kernel/TeRepresentation.h>
46 
47 std::auto_ptr<te::gm::Point> terralib4::GeomReader::getPoint(const TePoint& pt)
48 {
49  std::auto_ptr<te::gm::Point> geom(new te::gm::Point(pt.srid()));
50  geom->setX(pt.box().x1());
51  geom->setY(pt.box().y1());
52  return geom;
53 }
54 
55 std::auto_ptr<te::gm::LineString> terralib4::GeomReader::getLineString(const TeLine2D& line)
56 {
57  std::auto_ptr<te::gm::LineString> geom(new te::gm::LineString(line.size(), te::gm::LineStringType, line.srid()));
58 
59  TeComposite<TeCoord2D>::iterator it = line.begin();
60 
61  int count = 0;
62  while(it != line.end())
63  {
64  geom->setPoint(count, it->x(), it->y());
65 
66  ++count;
67  ++it;
68  }
69 
70  return geom;
71 }
72 
73 std::auto_ptr<te::gm::LinearRing> terralib4::GeomReader::getLinearRing(const TeLinearRing& ring)
74 {
75  std::auto_ptr<te::gm::LinearRing> geom(new te::gm::LinearRing(ring.size(),te::gm::LineStringType, ring.srid()));
76 
77  TeComposite<TeCoord2D>::iterator it = ring.begin();
78 
79  int count = 0;
80  while(it != ring.end())
81  {
82  geom->setPoint(count, it->x(), it->y());
83 
84  ++count;
85  ++it;
86  }
87 
88  return geom;
89 }
90 
91 std::auto_ptr<te::gm::Polygon> terralib4::GeomReader::getPolygon(const TePolygon& poly)
92 {
93  std::auto_ptr<te::gm::Polygon> geom(new te::gm::Polygon(poly.size(), te::gm::PolygonType, poly.srid()));
94 
95  TeComposite<TeLinearRing>::iterator it = poly.begin();
96 
97  std::size_t count = 0;
98  while(it != poly.end())
99  {
100  geom->setRingN(count, getLinearRing(*it).release());
101 
102  ++count;
103  ++it;
104  }
105 
106  return geom;
107 }
108 
109 std::auto_ptr<te::gm::MultiPolygon> terralib4::GeomReader::getMultiPolygon(const TePolygonSet& polySet)
110 {
111  std::auto_ptr<te::gm::MultiPolygon> geom(new te::gm::MultiPolygon(polySet.size(),te::gm::MultiPolygonType, polySet.srid()));
112 
113  TeComposite<TePolygon>::iterator it = polySet.begin();
114 
115  while(it != polySet.end())
116  {
117  geom->add(getPolygon(*it).release());
118 
119  ++it;
120  }
121 
122  return geom;
123 }
124 
125 std::auto_ptr<te::gm::MultiLineString> terralib4::GeomReader::getMultiLineString(const TeLineSet& lineSet)
126 {
127  std::auto_ptr<te::gm::MultiLineString> geom(new te::gm::MultiLineString(lineSet.size(), te::gm::MultiLineStringType, lineSet.srid()));
128 
129  TeComposite<TeLine2D>::iterator it = lineSet.begin();
130 
131  while(it != lineSet.end())
132  {
133  geom->add(getLineString(*it).release());
134 
135  ++it;
136  }
137 
138  return geom;
139 }
140 
141 std::auto_ptr<te::gm::MultiPoint> terralib4::GeomReader::getMultiPoint(const TePointSet& pointSet)
142 {
143  std::auto_ptr<te::gm::MultiPoint> geom(new te::gm::MultiPoint(pointSet.size(), te::gm::MultiPointType, pointSet.srid()));
144 
145  TeComposite<TePoint>::iterator it = pointSet.begin();
146 
147  while(it != pointSet.end())
148  {
149  geom->add(getPoint(*it).release());
150 
151  ++it;
152  }
153 
154  return geom;
155 }
156 
157 std::auto_ptr<te::gm::Polygon> terralib4::GeomReader::getPolygon(const TeCell& cell)
158 {
159  TeBox cellBox = cell.box();
160 
161  te::gm::Envelope* env = new te::gm::Envelope(cellBox.x1(), cellBox.y1(), cellBox.x2(), cellBox.y2());
162 
163  std::auto_ptr<te::gm::Geometry> geom(te::gm::GetGeomFromEnvelope(env, 0));
164 
165  std::auto_ptr<te::gm::Polygon> finalGeom(dynamic_cast<te::gm::Polygon*>(geom.release()));
166 
167  return finalGeom;
168 }
169 
170 std::auto_ptr<te::gm::Geometry> terralib4::GeomReader::getGeometry(const TeGeometry& geom)
171 {
172  TeGeomRep rep = geom.elemType();
173 
174  switch(rep)
175  {
176  case TePOLYGONS:
177  {
178  TePolygon p = dynamic_cast<const TePolygon&>(geom);
179  return std::auto_ptr<te::gm::Geometry>(getPolygon(p).release());
180  }
181 
182  case TePOINTS:
183  {
184  TePoint p = dynamic_cast<const TePoint&>(geom);
185  return std::auto_ptr<te::gm::Geometry>(getPoint(p).release());
186  }
187 
188  case TeLINES:
189  {
190  TeLine2D p = dynamic_cast<const TeLine2D&>(geom);
191  return std::auto_ptr<te::gm::Geometry>(getLineString(p).release());
192  }
193 
194  case TeCELLS:
195  {
196  TeCell p = dynamic_cast<const TeCell&>(geom);
197  return std::auto_ptr<te::gm::Geometry>(getPolygon(p).release());
198  }
199 
200  case TeNODES:
201  {
202  TePoint p = dynamic_cast<const TePoint&>(geom);
203  return std::auto_ptr<te::gm::Geometry>(getPoint(p).release());
204  }
205 
206  default:
207  throw Exception(TE_TR("Geometry Type Not supported!"));
208 
209  }
210 
211 
212  return std::auto_ptr<te::gm::Geometry>(0);
213 }
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:170
static std::auto_ptr< te::gm::LineString > getLineString(const TeLine2D &line)
Definition: GeomReader.cpp:55
static std::auto_ptr< te::gm::MultiLineString > getMultiLineString(const TeLineSet &lineSet)
Definition: GeomReader.cpp:125
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
Configuration flags for the TerraLib 4 driver.
static std::auto_ptr< te::gm::LinearRing > getLinearRing(const TeLinearRing &ring)
Definition: GeomReader.cpp:73
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
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
static std::auto_ptr< te::gm::MultiPolygon > getMultiPolygon(const TePolygonSet &polySet)
Definition: GeomReader.cpp:109
static std::auto_ptr< te::gm::Polygon > getPolygon(const TePolygon &poly)
Definition: GeomReader.cpp:91
Utility functions for the Geometry Module.
static std::auto_ptr< te::gm::Point > getPoint(const TePoint &pt)
Definition: GeomReader.cpp:47
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:141
An utility class for converting a TerraLib 4.x geometry to a TerraLib 5.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:38