geometry/Envelope.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/Envelope.cpp
22 
23  \brief An Envelope defines a 2D rectangular region.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../core/translator/Translator.h"
29 #include "../srs/Converter.h"
30 #include "Coord2D.h"
31 #include "Envelope.h"
32 #include "Exception.h"
33 #include "LineString.h"
34 #include "Polygon.h"
35 #include "Utils.h"
36 
37 // STL
38 #include <algorithm>
39 #include <cassert>
40 #include <cmath>
41 #include <memory>
42 
44 {
45  return Coord2D(m_llx, m_lly);
46 }
47 
49 {
50  return Coord2D(m_urx, m_ury);
51 }
52 
54 {
55  return Coord2D(m_llx + (m_urx - m_llx) / 2.0, m_lly + (m_ury - m_lly) / 2.0);
56 }
57 
58 double te::gm::Envelope::distance(const Envelope& rhs) const
59 {
60  if(intersects(rhs))
61  return 0.0;
62 
63  double dx = 0.0;
64 
65 // is this envelope to the left?
66  if(m_urx < rhs.m_llx)
67  dx = rhs.m_llx - m_urx;
68 
69 // is this envelope to the right?
70  if(m_llx > rhs.m_urx)
71  dx = m_llx - rhs.m_urx;
72 
73  double dy = 0.0;
74 
75 // is this envelope below?
76  if(m_ury < rhs.m_lly)
77  dy = rhs.m_lly - m_ury;
78 
79 // is this envelope above?
80  if(m_lly > rhs.m_ury)
81  dy = m_lly - rhs.m_ury;
82 
83 // are the envelopes aligned horizontally?
84  if(dx == 0.0)
85  return dy;
86 
87 // are the envelopes aligned vertically?
88  if(dy == 0.0)
89  return dx;
90 
91  return sqrt((dx * dx) + (dy * dy));
92 }
93 
94 void te::gm::Envelope::transform(int oldsrid, int newsrid)
95 {
96 #ifdef TERRALIB_MOD_SRS_ENABLED
97 
98  if (oldsrid == newsrid)
99  return;
100 
101  te::gm::Envelope env(*this);
102 
103  te::gm::Polygon polygon = CreatePolygon(env, oldsrid, 32);
104 
105  polygon.transform(newsrid);
106 
107  const te::gm::Envelope* transEnv = polygon.getMBR();
108 
109  m_llx = transEnv->m_llx;
110  m_urx = transEnv->m_urx;
111  m_lly = transEnv->m_lly;
112  m_ury = transEnv->m_ury;
113 
114 #else
115  throw Exception(TE_TR("transform method is not supported!"));
116 #endif // TERRALIB_MOD_SRS_ENABLED
117 }
118 
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
void transform(int srid)
It converts the coordinate values of the geometry to the new spatial reference system.
static te::dt::Date dx(2010, 12, 31)
Base exception class for plugin module.
double m_urx
Upper right corner x-coordinate.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
Utility functions for the Geometry Module.
Coord2D getCenter() const
It returns the rectangle&#39;s center coordinate.
double m_llx
Lower left corner x-coordinate.
An Envelope defines a 2D rectangular region.
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
An Envelope defines a 2D rectangular region.
double distance(const Envelope &rhs) const
It returns the shortest distance between any two points in the two envelopes.
double m_lly
Lower left corner y-coordinate.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
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.
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
LineString is a curve with linear interpolation between points.
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
TEGEOMEXPORT te::gm::Polygon CreatePolygon(const te::gm::Envelope &box, const int &srid, const int &numberOfIntermediateCoords)
Creates a Polygon from the given envelope and with the given number of intermediate coordinates in ea...
An utility struct for representing 2D coordinates.
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.