All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Envelope.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-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/geometry/Envelope.cpp
22 
23  \brief An Envelope defines a 2D rectangular region.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../common/Translator.h"
29 #include "../srs/Converter.h"
30 #include "Coord2D.h"
31 #include "Envelope.h"
32 #include "Exception.h"
33 
34 // STL
35 #include <cassert>
36 #include <cmath>
37 #include <memory>
38 
40 {
41  return Coord2D(m_llx, m_lly);
42 }
43 
45 {
46  return Coord2D(m_urx, m_ury);
47 }
48 
50 {
51  return Coord2D(m_llx + (m_urx - m_llx) / 2.0, m_lly + (m_ury - m_lly) / 2.0);
52 }
53 
54 double te::gm::Envelope::distance(const Envelope& rhs) const
55 {
56  if(intersects(rhs))
57  return 0.0;
58 
59  double dx = 0.0;
60 
61 // is this envelope to the left?
62  if(m_urx < rhs.m_llx)
63  dx = rhs.m_llx - m_urx;
64 
65 // is this envelope to the right?
66  if(m_llx > rhs.m_urx)
67  dx = m_llx - rhs.m_urx;
68 
69  double dy = 0.0;
70 
71 // is this envelope below?
72  if(m_ury < rhs.m_lly)
73  dy = rhs.m_lly - m_ury;
74 
75 // is this envelope above?
76  if(m_lly > rhs.m_ury)
77  dy = m_lly - rhs.m_ury;
78 
79 // are the envelopes aligned horizontally?
80  if(dx == 0.0)
81  return dy;
82 
83 // are the envelopes aligned vertically?
84  if(dy == 0.0)
85  return dx;
86 
87  return sqrt((dx * dx) + (dy * dy));
88 }
89 
90 void te::gm::Envelope::transform(int oldsrid, int newsrid)
91 {
92 #ifdef TERRALIB_MOD_SRS_ENABLED
93  if(oldsrid == newsrid)
94  return;
95 
96  std::auto_ptr<te::srs::Converter> converter(new te::srs::Converter());
97 
98  try
99  {
100  converter->setSourceSRID(oldsrid);
101  converter->setTargetSRID(newsrid);
102  }
103  catch (te::common::Exception& /* ex */)
104  {
105  return;
106  }
107 
108  double x1, x2, x3, x4;
109  double y1, y2, y3, y4;
110 
111  // convert the four corners
112  converter->convert(m_llx, m_lly, x1, y1);
113  converter->convert(m_urx, m_lly, x2, y2);
114  converter->convert(m_urx, m_ury, x3, y3);
115  converter->convert(m_llx, m_ury, x4, y4);
116 
117  // evaluate the minimum box that includes all four corner
118  m_llx = std::min(std::min(x1,x4),std::min(x2,x3));
119  m_urx = std::max(std::max(x1,x4),std::max(x2,x3));
120  m_lly = std::min(std::min(y1,y4),std::min(y2,y3));
121  m_ury = std::max(std::max(y1,y4),std::max(y2,y3));
122 #else
123  throw Exception(TE_TR("transform method is not supported!"));
124 #endif // TERRALIB_MOD_SRS_ENABLED
125 }
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
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:345
Coord2D getCenter() const
It returns the rectangle's center coordinate.
Definition: Envelope.cpp:49
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
An Envelope defines a 2D rectangular region.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
double distance(const Envelope &rhs) const
It returns the shortest distance between any two points in the two envelopes.
Definition: Envelope.cpp:54
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
A Converter is responsible for the conversion of coordinates between different Coordinate Systems (CS...
Definition: Converter.h:53
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
Definition: Envelope.cpp:44
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
Definition: Envelope.cpp:39
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.
Definition: Envelope.cpp:90