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 "../BuildConfig.h"
28 #include "../common/Exception.h"
29 #include "../common/Translator.h"
30 #include "../srs/Converter.h"
31 #include "Coord2D.h"
32 #include "Envelope.h"
33 #include "Exception.h"
34 
35 // STL
36 #include <algorithm>
37 #include <cassert>
38 #include <cmath>
39 #include <memory>
40 
42 {
43  return Coord2D(m_llx, m_lly);
44 }
45 
47 {
48  return Coord2D(m_urx, m_ury);
49 }
50 
52 {
53  return Coord2D(m_llx + (m_urx - m_llx) / 2.0, m_lly + (m_ury - m_lly) / 2.0);
54 }
55 
56 double te::gm::Envelope::distance(const Envelope& rhs) const
57 {
58  if(intersects(rhs))
59  return 0.0;
60 
61  double dx = 0.0;
62 
63 // is this envelope to the left?
64  if(m_urx < rhs.m_llx)
65  dx = rhs.m_llx - m_urx;
66 
67 // is this envelope to the right?
68  if(m_llx > rhs.m_urx)
69  dx = m_llx - rhs.m_urx;
70 
71  double dy = 0.0;
72 
73 // is this envelope below?
74  if(m_ury < rhs.m_lly)
75  dy = rhs.m_lly - m_ury;
76 
77 // is this envelope above?
78  if(m_lly > rhs.m_ury)
79  dy = m_lly - rhs.m_ury;
80 
81 // are the envelopes aligned horizontally?
82  if(dx == 0.0)
83  return dy;
84 
85 // are the envelopes aligned vertically?
86  if(dy == 0.0)
87  return dx;
88 
89  return sqrt((dx * dx) + (dy * dy));
90 }
91 
92 void te::gm::Envelope::transform(int oldsrid, int newsrid)
93 {
94 #ifdef TERRALIB_MOD_SRS_ENABLED
95  if(oldsrid == newsrid)
96  return;
97 
98  std::auto_ptr<te::srs::Converter> converter(new te::srs::Converter());
99 
100  try
101  {
102  converter->setSourceSRID(oldsrid);
103  converter->setTargetSRID(newsrid);
104  }
105  catch (te::common::Exception& /* ex */)
106  {
107  return;
108  }
109 
110  double x1, x2, x3, x4;
111  double y1, y2, y3, y4;
112 
113  // convert the four corners
114  converter->convert(m_llx, m_lly, x1, y1);
115  converter->convert(m_urx, m_lly, x2, y2);
116  converter->convert(m_urx, m_ury, x3, y3);
117  converter->convert(m_llx, m_ury, x4, y4);
118 
119  // evaluate the minimum box that includes all four corner
120  m_llx = std::min(std::min(x1,x4),std::min(x2,x3));
121  m_urx = std::max(std::max(x1,x4),std::max(x2,x3));
122  m_lly = std::min(std::min(y1,y4),std::min(y2,y3));
123  m_ury = std::max(std::max(y1,y4),std::max(y2,y3));
124 #else
125  throw Exception(TE_TR("transform method is not supported!"));
126 #endif // TERRALIB_MOD_SRS_ENABLED
127 }
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:346
Coord2D getCenter() const
It returns the rectangle's center coordinate.
Definition: Envelope.cpp:51
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:56
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:46
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
Definition: Envelope.cpp:41
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:92