examples/geometry/main.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 main.cpp
22 
23  \brief TerraLib Geometry module examples.
24  */
25 
26 // Examples
27 #include "GeometryExamples.h"
28 
29 // TerraLib
30 #include <terralib/common.h>
32 #include <terralib/geometry.h>
33 
34 // STL
35 #include <cassert>
36 #include <cstdlib>
37 #include <iostream>
38 
39 //@{ Geometries
40 
41 // Points
45 
46 // Lines
50 
51 // Polygons
54 
55 // GeometryCollection
57 
58 //@}
59 
61 {
62  std::cout << "Creating geometries..." << std::endl;
63 
64 // Create points
65  pt1 = createPoint(0.0, 0.0);
66  assert(pt1);
67  std::cout << pt1->asText() << std::endl;
68 
69  pt2 = createPoint(5.0, 5.0);
70  assert(pt2);
71  std::cout << pt2->asText() << std::endl;
72 
73  pt3 = createPointZ(5.0, 5.0, 5.0);
74  assert(pt3);
75  std::cout << pt3->asText() << std::endl;
76 
77 // Create lines
78  l1 = createLineString(0.0, 0.0, 10.0, 10.0);
79  assert(l1);
80  std::cout << l1->asText() << std::endl;
81 
82  l2 = createLineString(0.0, 10.0, 10.0, 0.0);
83  assert(l2);
84  std::cout << l2->asText() << std::endl;
85 
86  l3 = createLineString(0.0, 20.0, 20.0, 20.0);
87  assert(l3);
88  std::cout << l3->asText() << std::endl;
89 
90 // Create polygons
91  p = createPolygon();
92  assert(p);
93  std::cout << p->asText() << std::endl;
94 
95  pHole = createPolygonWithHole();
96  assert(pHole);
97  std::cout << pHole->asText() << std::endl;
98 
100  assert(gc);
101  std::cout << gc->asText() << std::endl;
102 }
103 
105 {
106  std::cout << "Calculating spatial relations..." << std::endl;
107 
108 // Verifies some spatial relations
109  bool result;
110 
111 // Points
112  result = spatialRelation(pt1, pt2, te::gm::DISJOINT); // true
113  result = spatialRelation(pt1, pt2, te::gm::TOUCHES); // false
114 
115 // Points and lines //???
116 result = spatialRelation(pt1, l1, te::gm::TOUCHES); // true
117 result = spatialRelation(pt1, l2, te::gm::TOUCHES); // false
118 
119 // Points and polygons
120  result = spatialRelation(pt2, p, te::gm::INTERSECTS); // true
121  result = spatialRelation(pt2, pHole, te::gm::INTERSECTS); // false
122 
123 // Lines
124  result = spatialRelation(l1, l1, te::gm::EQUALS); // true
125  result = spatialRelation(l1, l2, te::gm::CROSSES); // true
126  result = spatialRelation(l1, l3, te::gm::CROSSES); // false
127 
128 // Lines and polygons
129  result = spatialRelation(l1, p, te::gm::INTERSECTS); // true
130  result = spatialRelation(l3, p, te::gm::INTERSECTS); // false
131 
132 // Polygons
133  result = spatialRelation(p, pHole, te::gm::COVERS); // true
134 }
135 
137 {
138  std::cout << "Executing set operations..." << std::endl;
139  std::cout << "Results:" << std::endl;
140 
141 // Applies set operations
142  te::gm::Geometry* mpt = Union(pt1, pt2); // return a multi point
143  assert(mpt);
144  std::cout << mpt->asText() << std::endl;
145  delete mpt;
146 
147  te::gm::Geometry* mgeom = Union(pt2, l3); // return a geometry collection
148  assert(mgeom);
149  std::cout << mgeom->asText() << std::endl;
150  delete mgeom;
151 
152  te::gm::Geometry* ml = Union(l1, l2); // return a multi line
153  assert(ml);
154  std::cout << ml->asText() << std::endl;
155  delete ml;
156 
157  te::gm::Geometry* ptInt = intersection(l1, l2); // return a point
158  assert(ptInt);
159  std::cout << ptInt->asText() << std::endl;
160  delete ptInt;
161 
162  te::gm::Geometry* pDiff = difference(p, pHole); // return a polygon
163  assert(pDiff);
164  std::cout << pDiff->asText() << std::endl;
165  delete pDiff;
166 
167  te::gm::Geometry* lInt = intersection(l1, pHole); // return a multi line
168  assert(lInt);
169  std::cout << lInt->asText() << std::endl;
170  delete lInt;
171 }
172 
174 {
175  std::cout << "Executing wkb conversion..." << std::endl;
176  std::cout << "Results:" << std::endl;
177  char* wkb = new char[pt1->getWkbSize()];
178 
179  te::gm::WKBWriter::write(pt1, wkb);
181  assert(g->equals(pt1));
182  std::cout << g->asText() << std::endl;
183 
184  delete g;
185  delete[] wkb;
186 
187  wkb = new char[pt2->getWkbSize()];
188  te::gm::WKBWriter::write(pt2, wkb);
189  g = te::gm::WKBReader::read(wkb);
190  assert(g->equals(pt2));
191  std::cout << g->asText() << std::endl;
192 
193  delete g;
194  delete[] wkb;
195 
196  wkb = new char[l1->getWkbSize()];
197  te::gm::WKBWriter::write(l1, wkb);
198  g = te::gm::WKBReader::read(wkb);
199  assert(g->equals(l1));
200  std::cout << g->asText() << std::endl;
201 
202  delete g;
203  delete[] wkb;
204 
205  wkb = new char[l2->getWkbSize()];
206  te::gm::WKBWriter::write(l2, wkb);
207  g = te::gm::WKBReader::read(wkb);
208  assert(g->equals(l2));
209  std::cout << g->asText() << std::endl;
210 
211  delete g;
212  delete[] wkb;
213 
214  wkb = new char[l3->getWkbSize()];
215  te::gm::WKBWriter::write(l3, wkb);
216  g = te::gm::WKBReader::read(wkb);
217  assert(g->equals(l3));
218  std::cout << g->asText() << std::endl;
219 
220  delete g;
221  delete[] wkb;
222 
223  wkb = new char[p->getWkbSize()];
224  te::gm::WKBWriter::write(p, wkb);
225  g = te::gm::WKBReader::read(wkb);
226  assert(g->equals(p));
227  std::cout << g->asText() << std::endl;
228 
229  delete g;
230  delete[] wkb;
231 
232  wkb = new char[pHole->getWkbSize()];
233  te::gm::WKBWriter::write(pHole, wkb);
234  g = te::gm::WKBReader::read(wkb);
235  assert(g->equals(pHole));
236  std::cout << g->asText() << std::endl;
237 
238  delete g;
239  delete[] wkb;
240 
241 }
242 
244 {
245  delete pt1;
246  delete pt2;
247  delete l1;
248  delete l2;
249  delete l3;
250  delete p;
251  delete pHole;
252 }
253 
254 int main(int /*argc*/, char** /*argv*/)
255 {
256 
258 
259 // Call of examples methods
264  readWkts("./geometries.wkt");
265  //readWkts("./wkt_geom.txt");
266 
268 
270 
271  return EXIT_SUCCESS;
272 }
void createGeometries()
int main(int, char **)
te::gm::Polygon * createPolygon()
te::gm::LineString * l3
void readWkts(const std::string &filePath)
Definition: Wkt.cpp:12
std::size_t getWkbSize() const _NOEXCEPT_OP(true)
It returns the size required by a WKB representation for this geometric object.
An static class with global definitions.
te::gm::Point * pt2
void write(const Geometry *geom)
It serializes the geometry to a WKB representation into the specified buffer.
Definition: WKBWriter.cpp:145
void deleteGeometries()
te::gm::GeometryCollection * gc
te::gm::Geometry * intersection(te::gm::Geometry *g1, te::gm::Geometry *g2)
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
void finalize()
It finalizes the TerraLib Platform.
static TerraLib & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
std::string asText() const _NOEXCEPT_OP(true)
It returns an string with the Well-Known Text Representation for the geometry.
te::gm::Point * pt1
te::gm::LineString * l2
te::gm::Point * createPointZ(const double &x, const double &y, const double &z)
te::gm::Polygon * p
te::gm::Point * createPoint(const double &x, const double &y)
te::gm::Polygon * pHole
te::gm::Polygon * createPolygonWithHole()
te::gm::Geometry * difference(te::gm::Geometry *g1, te::gm::Geometry *g2)
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
te::gm::GeometryCollection * createGeometryCollection()
bool spatialRelation(te::gm::Geometry *g1, te::gm::Geometry *g2, te::gm::SpatialRelation relation)
void initialize()
It initializes the TerraLib Platform.
void spatialRelationsExamples()
void wkbConversionExamples()
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
te::gm::Geometry * Union(te::gm::Geometry *g1, te::gm::Geometry *g2)
te::gm::Point * pt3
te::gm::LineString * l1
This file contains include headers for the TerraLib Common Runtime module.
A set of geometry examples.
This file contains include headers for the Vector Geometry model of TerraLib.
It is a collection of other geometric objects.
static Geometry * read(const char *wkb)
It returns a valid geometry from a given WKB.
Definition: WKBReader.cpp:48
virtual bool equals(const Geometry *const rhs, const bool exact=false) const _NOEXCEPT_OP(false)
It returns true if the geometry object is spatially equal to rhs geometry.
te::gm::LineString * createLineString(const double &xi, const double &yi, const double &xf, const double &yf)
void setOperationsExamples()