All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Grid.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/raster/Grid.cpp
22 
23  \brief A rectified grid is the spatial support for raster data.
24 */
25 
26 // TerraLib
27 #include "../geometry/Coord2D.h"
28 #include "../geometry/Envelope.h"
29 #include "Grid.h"
30 
31 // STL
32 #include <algorithm>
33 #include <cmath>
34 #include <cstring>
35 
36 te::rst::Grid::Grid(unsigned int nCols,
37  unsigned int nRows,
38  te::gm::Envelope* mbr,
39  int srid)
40  : m_extent(mbr),
41  m_nCols(nCols),
42  m_nRows(nRows),
43  m_srid(srid)
44 {
45  if(m_extent)
46  {
47  // Resolution X and Y
48  m_geoT[1] = m_extent->getWidth() / static_cast<double>(nCols);
49  m_geoT[5] = -1.0 * m_extent->getHeight() / static_cast<double>(nRows);
50 
51  // Pixell 0,0 upper-left coods
52  m_geoT[0] = m_extent->m_llx + ( m_geoT[1] / 2.0 );
53  m_geoT[3] = m_extent->m_ury + ( m_geoT[5] / 2.0 );
54 
55  m_geoT[2] = 0.0;
56  m_geoT[4] = 0.0;
57 
58  }
59  else
60  {
61  setGeoreference(te::gm::Coord2D(0, 0), srid, 1.0, 1.0);
62  computeExtent();
63  }
64 }
65 
66 te::rst::Grid::Grid(unsigned int nCols, unsigned int nRows,
67  double resX, double resY,
68  const te::gm::Coord2D* ulc, int srid)
69  : m_extent(0),
70  m_nCols(nCols),
71  m_nRows(nRows),
72  m_srid(srid)
73 {
74  if(ulc)
75  setGeoreference(*ulc, srid, resX, resY);
76  else
77  setGeoreference(te::gm::Coord2D(0, 0), srid, resX, resY);
78  computeExtent();
79 }
80 
81 te::rst::Grid::Grid(unsigned int nCols, unsigned int nRows,
82  double resX, double resY,
83  te::gm::Envelope* mbr, int srid)
84  : m_extent(mbr),
85  m_nCols(nCols),
86  m_nRows(nRows),
87  m_srid(srid)
88 {
89  if(m_extent)
90  {
91  // Resolution X and Y
92  m_geoT[1] = m_extent->getWidth() / static_cast<double>(nCols);
93  m_geoT[5] = -1.0 * m_extent->getHeight() / static_cast<double>(nRows);
94 
95  // Pixell 0,0 upper-left coods
96  m_geoT[0] = m_extent->m_llx + ( m_geoT[1] / 2.0 );
97  m_geoT[3] = m_extent->m_ury + ( m_geoT[5] / 2.0 );
98 
99  m_geoT[2] = 0.0;
100  m_geoT[4] = 0.0;
101  }
102  else
103  {
104  setGeoreference(te::gm::Coord2D(0, 0), srid, resX, resY);
105  computeExtent();
106  }
107 }
108 
110  double resY,
111  te::gm::Envelope* mbr,
112  int srid)
113  : m_extent(mbr),
114  m_nCols(0),
115  m_nRows(0),
116  m_srid(srid)
117 {
118  m_nCols = static_cast<unsigned int> (mbr->getWidth() / resX + 0.5);
119  m_nRows = static_cast<unsigned int> (mbr->getHeight() / resY + 0.5);
120 
121  // Resolution X and Y
122  m_geoT[1] = m_extent->getWidth() / static_cast<double>(m_nCols);
123  m_geoT[5] = -1.0 * m_extent->getHeight() / static_cast<double>(m_nRows);
124 
125  // Pixell 0,0 upper-left coods
126  m_geoT[0] = m_extent->m_llx + ( m_geoT[1] / 2.0 );
127  m_geoT[3] = m_extent->m_ury + ( m_geoT[5] / 2.0 );
128 
129  m_geoT[2] = 0.0;
130  m_geoT[4] = 0.0;
131 }
132 
133 te::rst::Grid::Grid( const double geoTrans[], unsigned int nCols, unsigned int nRows,
134  int srid)
135  : m_extent(0),
136  m_nCols(nCols),
137  m_nRows(nRows),
138  m_srid(srid)
139 {
140  setGeoreference(geoTrans, srid);
141  computeExtent();
142 }
143 
145  : m_extent(0),
146  m_nCols(rhs.m_nCols),
147  m_nRows(rhs.m_nRows),
148  m_srid(rhs.m_srid)
149 {
150  memcpy(m_geoT, rhs.m_geoT, sizeof(double) * 6);
151 
152  if( rhs.m_extent )
153  {
154  m_extent = new te::gm::Envelope(*rhs.m_extent);
155  }
156  else
157  {
158  computeExtent();
159  }
160 }
161 
163 {
164  if (m_extent)
165  {
166  delete m_extent;
167  }
168 }
169 
171 {
172  if(this != &rhs)
173  {
174  m_nCols = rhs.m_nCols;
175 
176  m_nRows = rhs.m_nRows;
177 
178  m_srid = rhs.m_srid;
179 
180  memcpy(m_geoT, rhs.m_geoT, sizeof(double) * 6);
181 
182  if (m_extent)
183  {
184  delete m_extent;
185  m_extent = 0;
186  }
187  if( rhs.m_extent )
188  {
189  m_extent = new te::gm::Envelope(*rhs.m_extent);
190  }
191  else
192  {
193  computeExtent();
194  }
195  }
196 
197  return *this;
198 }
199 
200 void te::rst::Grid::setNumberOfColumns(unsigned int nCols)
201 {
202  m_nCols = nCols;
203 }
204 
206 {
207  return m_nCols;
208 }
209 
210 void te::rst::Grid::setNumberOfRows(unsigned int nRows)
211 {
212  m_nRows = nRows;
213 }
214 
215 unsigned int te::rst::Grid::getNumberOfRows() const
216 {
217  return m_nRows;
218 }
219 
220 void te::rst::Grid::setGeoreference(const te::gm::Coord2D& ulLocation, int srid, double resX, double resY)
221 {
222  m_srid = srid;
223 
224  // Resolution X and Y
225  m_geoT[1] = resX;
226  m_geoT[5] = -1.0 * resY;
227 
228  // Pixell 0,0 upper-left coods
229  m_geoT[0] = ulLocation.x + ( m_geoT[1] / 2.0 );
230  m_geoT[3] = ulLocation.y + ( m_geoT[5] / 2.0 );
231 
232  m_geoT[2] = 0.0;
233  m_geoT[4] = 0.0;
234 
235  computeExtent();
236 }
237 
238 void te::rst::Grid::setGeoreference(const double geoTrans[], int srid)
239 {
240  m_srid = srid;
241  m_geoT[0] = geoTrans[0];
242  m_geoT[1] = geoTrans[1];
243  m_geoT[2] = geoTrans[2];
244  m_geoT[3] = geoTrans[3];
245  m_geoT[4] = geoTrans[4];
246  m_geoT[5] = geoTrans[5];
247 
248  computeExtent();
249 }
250 
251 const double* te::rst::Grid::getGeoreference() const
252 {
253  return m_geoT;
254 }
255 
257 {
258  return std::abs( m_geoT[1] );
259 }
260 
262 {
263  return ( -1.0 * m_geoT[5] );
264 }
265 
267 {
268  return m_srid;
269 }
270 
272 {
273  m_srid = srid;
274 }
275 
277 {
278  return m_extent;
279 }
280 
282 {
283  return m_extent;
284 }
285 
287 {
288  te::gm::Coord2D ll = gridToGeo( -0.5, ((double)m_nRows) - 0.5 );
289  te::gm::Coord2D lr = gridToGeo( ((double)m_nCols) - 0.5, ((double)m_nRows) - 0.5 );
290  te::gm::Coord2D ur = gridToGeo( ((double)m_nCols) - 0.5, -0.5 );
291  te::gm::Coord2D ul = gridToGeo( -0.5, -0.5 );
292 
293  if(!m_extent)
294  m_extent = new te::gm::Envelope;
295 
296  m_extent->init(std::min(ll.x, ul.x),
297  std::min(ll.y, lr.y),
298  std::max(ur.x, lr.x),
299  std::max(ul.y, ur.y));
300 }
301 
302 void te::rst::Grid::gridToGeo(const double& col, const double& row, double& x, double& y) const
303 {
304  x = m_geoT[0] + col * m_geoT[1] + row * m_geoT[2];
305  y = m_geoT[3] + col * m_geoT[4] + row * m_geoT[5];
306 }
307 
308 void te::rst::Grid::geoToGrid(const double& x, const double& y, double& col, double& row) const
309 {
310  col = (m_geoT[5] * (x - m_geoT[0]) - m_geoT[2] * (y - m_geoT[3])) / (m_geoT[1] * m_geoT[5] - m_geoT[2] * m_geoT[4]);
311  row = (y - m_geoT[3] - col * m_geoT[4]) / m_geoT[5];
312 }
313 
314 bool te::rst::Grid::operator==(const Grid& rhs) const
315 {
316  computeExtent();
317  rhs.computeExtent();
318 
319  if (!m_extent->equals(*rhs.m_extent) ||
320  m_nCols != rhs.m_nCols ||
321  m_nRows != rhs.m_nRows ||
322  m_srid != rhs.m_srid ||
323  getGeoreference()[0] != rhs.getGeoreference()[0] ||
324  getGeoreference()[1] != rhs.getGeoreference()[1] ||
325  getGeoreference()[2] != rhs.getGeoreference()[2] ||
326  getGeoreference()[3] != rhs.getGeoreference()[3] ||
327  getGeoreference()[4] != rhs.getGeoreference()[4] ||
328  getGeoreference()[5] != rhs.getGeoreference()[5])
329  return false;
330  else
331  return true;
332 }
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
void computeExtent() const
Computes the geographic extension of the grid.
Definition: Grid.cpp:286
te::gm::Envelope * m_extent
The grid extent.
Definition: Grid.h:288
int m_srid
The associated SRS.
Definition: Grid.h:291
double y
y-coordinate.
Definition: Coord2D.h:87
void setNumberOfColumns(unsigned int nCols)
Sets the grid number of columns.
Definition: Grid.cpp:200
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
Definition: Grid.cpp:261
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448
A rectified grid is the spatial support for raster data.
void setGeoreference(const te::gm::Coord2D &ulLocation, int srid, double resX, double resY)
Sets the information needed to georeference the grid.
Definition: Grid.cpp:220
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
unsigned int getNumberOfRows() const
Returns the grid number of rows.
Definition: Grid.cpp:215
A rectified grid is the spatial support for raster data.
Definition: Grid.h:55
double m_geoT[6]
A list of 6 coefficients describing an affine transformation to georeference a grid. In a north up image, m_geoT[1] is the pixel width, and m_geoT[5] is the pixel height. The upper left corner of the upper left pixel is at position (m_geoT[0],m_geoT[3]).
Definition: Grid.h:292
~Grid()
Destructor.
Definition: Grid.cpp:162
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
Definition: Grid.cpp:276
void init(const double &llx, const double &lly, const double &urx, const double &ury)
It initializes (sets) the envelope bounds.
Definition: Envelope.h:381
Grid & operator=(const Grid &rhs)
Assignment operator.
Definition: Grid.cpp:170
double getResolutionX() const
Returns the grid horizontal (x-axis) resolution.
Definition: Grid.cpp:256
Grid(unsigned int nCols=0, unsigned int nRows=0, te::gm::Envelope *mbr=0, int srid=TE_UNKNOWN_SRS)
Constructor.
Definition: Grid.cpp:36
bool operator==(const Grid &rhs) const
Equal operator.
Definition: Grid.cpp:314
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
Definition: Grid.cpp:302
unsigned int m_nRows
Number of rows.
Definition: Grid.h:290
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
Definition: Grid.cpp:205
int getSRID() const
Returns the grid spatial reference system identifier.
Definition: Grid.cpp:266
void setSRID(int srid)
Just sets the grid spatial reference system identifier.
Definition: Grid.cpp:271
double x
x-coordinate.
Definition: Coord2D.h:86
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
Definition: Grid.cpp:308
const double * getGeoreference() const
Returns a list of 6 coefficients describing an affine transformation to georeference a grid...
Definition: Grid.cpp:251
void setNumberOfRows(unsigned int nRows)
Sets the grid number of rows.
Definition: Grid.cpp:210
unsigned int m_nCols
Number of columns.
Definition: Grid.h:289