All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.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/Utils.cpp
22 
23  \brief Utility functions for the Geometry Module.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "Coord2D.h"
29 #include "Envelope.h"
30 #include "Exception.h"
31 #include "Geometry.h"
32 #include "LinearRing.h"
33 #include "LineString.h"
34 #include "Point.h"
35 #include "Polygon.h"
36 #include "Utils.h"
37 
39 {
40 // create an outer ring with the same envelope as our envelope
41  LinearRing* r = new LinearRing(5, LineStringType, srid, new Envelope(*e));
42 
43  r->setPoint(0, e->m_llx, e->m_lly);
44  r->setPoint(1, e->m_urx, e->m_lly);
45  r->setPoint(2, e->m_urx, e->m_ury);
46  r->setPoint(3, e->m_llx, e->m_ury);
47  r->setPoint(4, e->m_llx, e->m_lly);
48 
49 // create the polygon
50  Polygon* p = new Polygon(1, PolygonType, srid, new Envelope(*e));
51  p->setRingN(0, r);
52 
53  return p;
54 }
55 
57  const Geometry* g2,
58  SpatialRelation relation)
59 {
60  switch(relation)
61  {
62  case CONTAINS:
63  return g1->contains(g2);
64 
65  case COVEREDBY:
66  return g1->coveredBy(g2);
67 
68  case COVERS:
69  return g1->covers(g2);
70 
71  case CROSSES:
72  return g1->crosses(g2);
73 
74  case DISJOINT:
75  return g1->disjoint(g2);
76 
77  case EQUALS:
78  return g1->equals(g2);
79 
80  case INTERSECTS:
81  return g1->intersects(g2);
82 
83  case OVERLAPS:
84  return g1->overlaps(g2);
85 
86  case TOUCHES:
87  return g1->touches(g2);
88 
89  case WITHIN:
90  return g1->within(g2);
91 
92  default:
93  throw Exception(TE_TR("Invalid spatial relation!"));
94  }
95 }
96 
97 te::gm::Envelope te::gm::AdjustToCut(const Envelope & env, double bWidth, double bHeight)
98 {
99  double auxD;
100  int auxI;
101 
102  int magicX;
103  auxD = env.m_llx/bWidth;
104  auxI = (int)(env.m_llx/bWidth);
105  if (env.m_llx < 0 && (auxD - auxI) != 0)
106  magicX = (int) (env.m_llx/bWidth - 1);
107  else
108  magicX = auxI;
109 
110  int magicY;
111  auxD = env.m_lly/bHeight;
112  auxI = (int)(env.m_lly/bHeight);
113  if (env.m_lly < 0 && (auxD - auxI) != 0)
114  magicY = (int)(env.m_lly/bHeight - 1);
115  else
116  magicY = auxI;
117 
118  double xi = magicX*bWidth;
119  double yi = magicY*bHeight;
120 
121  int magicX2;
122  auxD = env.m_urx/bWidth;
123  auxI = (int)(env.m_urx/bWidth);
124  if ((env.m_urx < 0) || (auxD - auxI) == 0)
125  magicX2 = (int) (env.m_urx/bWidth);
126  else
127  magicX2 = (int) (env.m_urx/bWidth + 1);
128 
129  int magicY2;
130  auxD = env.m_ury/bHeight;
131  auxI = (int)(env.m_ury/bHeight);
132  if ((env.m_ury < 0) || (auxD - auxI) == 0)
133  magicY2 = (int) (env.m_ury/bHeight);
134  else
135  magicY2 = (int) (env.m_ury/bHeight + 1);
136 
137  double xf = (magicX2)*bWidth;
138  double yf = (magicY2)*bHeight;
139 
140  return te::gm::Envelope(xi,yi,xf,yf);
141 }
142 
143 template<class T1, class T2> bool te::gm::Intersects(const T1& o1, const T2& o2)
144 {
145  return o1->intersects(o2);
146 }
147 
148 template<> bool te::gm::Intersects(const te::gm::Point& point, const te::gm::Envelope& e)
149 {
150  // point to the right of envelope
151  if(point.getX() > e.m_urx)
152  return false;
153 
154  // point to the left of envelope
155  if(e.m_llx > point.getX())
156  return false;
157 
158  // point is above envelope
159  if(point.getY() > e.m_ury)
160  return false;
161 
162  // point is below envelope
163  if(e.m_lly > point.getY())
164  return false;
165 
166  return true;
167 }
168 
169 te::gm::Coord2D* te::gm::locateAlong(const LineString* line, double initial, double final, double target)
170 {
171  double tTof; // Distance of target to fist point
172 
173  std::map<int, double> pointLenghtFromFirst;
174 
175  double fullLineLenght = 0;
176 
177  pointLenghtFromFirst[0] = 0;
178  for(std::size_t i = 1; i < line->getNPoints(); i++)
179  {
180  fullLineLenght += line->getPointN(i)->distance(line->getPointN(i-1));
181  pointLenghtFromFirst[i] = fullLineLenght;
182  }
183 
184  double diference = final-initial;
185 
186  tTof = ((target-initial)*fullLineLenght)/diference;
187 
188  int onTheFly = -1;
189 
190  int pointBeforeTarget;
191  for(std::size_t i = 0; i < pointLenghtFromFirst.size(); i++)
192  {
193  if(pointLenghtFromFirst[i] == tTof)
194  {
195  onTheFly = i;
196  break;
197  }
198  if(tTof >= pointLenghtFromFirst[i] && tTof < pointLenghtFromFirst[i+1])
199  {
200  pointBeforeTarget = i;
201  break;
202  }
203  }
204 
205  te::gm::Coord2D* targetCoord = new te::gm::Coord2D();
206 
207  if(onTheFly >= 0)
208  {
209  targetCoord->x = line->getPointN(onTheFly)->getX();
210  targetCoord->y = line->getPointN(onTheFly)->getY();
211 
212  return targetCoord;
213  }
214 
215  Point* p1 = line->getPointN(pointBeforeTarget);
216  Point* p2 = line->getPointN(pointBeforeTarget+1);
217 
218  // Partial distance: target to p1
219  double pd = tTof - pointLenghtFromFirst[pointBeforeTarget];
220 
221  // Total distance: p2 to p1
222  double td = pointLenghtFromFirst[pointBeforeTarget+1] - pointLenghtFromFirst[pointBeforeTarget];
223 
224  targetCoord->x = ((pd*(p2->getX()-p1->getX()))/td)+p1->getX();
225 
226  targetCoord->y = ((pd*(p2->getY()-p1->getY()))/td)+p1->getY();
227 
228  return targetCoord;
229 }
double y
y-coordinate.
Definition: Coord2D.h:114
A LinearRing is a LineString that is both closed and simple.
virtual bool covers(const Geometry *const rhs) const
It returns true if this geometry object spatially covers the rhs geometry.
Definition: Geometry.cpp:410
virtual bool intersects(const Geometry *const rhs) const
It returns true if the geometry object spatially intersects rhs geometry.
Definition: Geometry.cpp:254
double x
x-coordinate.
Definition: Coord2D.h:113
Point * getPointN(std::size_t i) const
It returns the specified point in this LineString.
Definition: LineString.cpp:323
A point with x and y coordinate values.
TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry *g1, const Geometry *g2, SpatialRelation relation)
It returns if two geometries satisfy a given spatial relation.
Definition: Utils.cpp:56
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
virtual bool contains(const Geometry *const rhs) const
It returns true if this geometry object spatially contains rhs geometry.
Definition: Geometry.cpp:330
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
virtual bool crosses(const Geometry *const rhs) const
It returns true if the geometry object spatially crosses rhs geometry.
Definition: Geometry.cpp:292
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
An Envelope defines a 2D rectangular region.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
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
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
Definition: LineString.cpp:353
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual bool disjoint(const Geometry *const rhs) const
It returns true if the geometry object is spatially disjoint from rhs geometry.
Definition: Geometry.cpp:235
TEGEOMEXPORT Coord2D * locateAlong(const LineString *line, double initial, double final, double target)
Make the line interpolation to find a target.
Definition: Utils.cpp:169
std::size_t getNPoints() const
It returns the number of points (vertexes) in the linestring.
Definition: LineString.h:193
Utility functions for the Geometry Module.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
virtual bool overlaps(const Geometry *const rhs) const
It returns true if this geometry object spatially overlaps rhs geometry.
Definition: Geometry.cpp:349
bool Intersects(const T1 &o1, const T2 &o2)
Definition: Utils.cpp:143
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
An exception class for the Geometry module.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
virtual bool touches(const Geometry *const rhs) const
It returns true if the geometry object spatially touches rhs geometry.
Definition: Geometry.cpp:273
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
virtual bool equals(const Geometry *const rhs, const bool exact=false) const
It returns true if the geometry object is spatially equal to rhs geometry.
Definition: Geometry.cpp:214
virtual bool coveredBy(const Geometry *const rhs) const
It returns true if this geometry object is spatially covered by rhs geometry.
Definition: Geometry.cpp:429
TEGEOMEXPORT Envelope AdjustToCut(const Envelope &env, double bWidth, double bHeight)
Finds the correspondent smallest box that allows a box to be cut in blocks of a given size...
Definition: Utils.cpp:97
LineString is a curve with linear interpolation between points.
An utility struct for representing 2D coordinates.
virtual double distance(const Geometry *const rhs) const
It returns the shortest distance between any two points in the two geometry objects.
Definition: Geometry.cpp:453
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:38
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.
virtual bool within(const Geometry *const rhs) const
It returns true if the geometry object is spatially within rhs geometry.
Definition: Geometry.cpp:311