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