All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Grid.h
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.h
22 
23  \brief A rectified grid is the spatial support for raster data.
24 */
25 
26 #ifndef __TERRALIB_RASTER_INTERNAL_GRID_H
27 #define __TERRALIB_RASTER_INTERNAL_GRID_H
28 
29 // TerraLib
30 #include "../geometry/Coord2D.h"
31 #include "../srs/Config.h"
32 #include "Config.h"
33 
34 // Boost
35 #include <boost/shared_ptr.hpp>
36 
37 namespace te
38 {
39 // Forward declaration
40  namespace gm { class Envelope; }
41 
42  namespace rst
43  {
44  /*!
45  \class Grid
46 
47  \brief A rectified grid is the spatial support for raster data.
48 
49  \note The upper left corner of the upper left pixel is considered the grid extent top-left reference.
50 
51  \ingroup rst
52 
53  \sa Raster
54  */
56  {
57  public:
58 
59  /*!
60  \brief Constructor.
61 
62  \param nCols The number of columns.
63  \param nRows The number of rows.
64  \param srid The SRS associated to the grid.
65  \param mbr The grid extent. The grid will take the ownership of the given envelope.
66 
67  \note If extent is not specified, no georeferencing is assumed.
68  \note The grid resolution (x-axis and y-axis) will be computed based on the number of columns/rows and the provided extent.
69  */
70  Grid(unsigned int nCols = 0, unsigned int nRows = 0,
71  te::gm::Envelope* mbr = 0, int srid = TE_UNKNOWN_SRS);
72 
73  /*!
74  \brief Constructor.
75 
76  \param nCols The number of columns.
77  \param nRows The number of rows.
78  \param resX The grid horizontal (x-axis) resolution.
79  \param resY The grid vertical (y-axis) resolution.
80  \param ulc The location of the upper-left corner of the upper-left pixel (or cell) of the grid assuming srid as the SRS.
81  \param srid The SRS associated to the grid.
82 
83  \note If ulc is not specified, no georeferencing is assumed.
84  */
85  Grid(unsigned int nCols, unsigned int nRows,
86  double resX, double resY,
87  const te::gm::Coord2D* ulc, int srid = TE_UNKNOWN_SRS);
88 
89  /*!
90  \brief Constructor.
91 
92  \param nCols The number of columns.
93  \param nRows The number of rows.
94  \param resX The grid horizontal (x-axis) resolution.
95  \param resY The grid vertical (y-axis) resolution.
96  \param mbr The grid extent. The grid will take the ownership of the given envelope.
97  \param srid The SRS associated to the grid.
98 
99  \note If extent is not specified, no georeferencing is assumed.
100  */
101  Grid(unsigned int nCols, unsigned int nRows,
102  double resX, double resY,
103  te::gm::Envelope* mbr, int srid = TE_UNKNOWN_SRS);
104 
105  /*!
106  \brief Constructor.
107 
108  \param resX The grid horizontal (x-axis) resolution.
109  \param resY The grid vertical (y-axis) resolution.
110  \param mbr The grid extent. The grid will take the ownership of the given envelope.
111  \param srid The SRS associated to the grid.
112 
113  \note The grid resolution (x-axis and y-axis) will be computed based on the number of columns/rows and the provided extent.
114  */
115  Grid(double resX, double resY,
116  te::gm::Envelope* mbr, int srid = TE_UNKNOWN_SRS);
117 
118  /*!
119  \brief Constructor.
120 
121  \param nCols The number of columns.
122  \param nRows The number of rows.
123  \param geoTrans[] A list of 6 coefficients describing an affine transformation to georeference a grid.
124  \param srid The SRS associated to the grid.
125 
126  \note If extent is not specified, no georeferencing is assumed.
127  */
128  Grid(const double geoTrans[], unsigned int nCols, unsigned int nRows,
129  int srid = TE_UNKNOWN_SRS);
130 
131  /*!
132  \brief Copy constructor.
133 
134  \param rhs The right-hand-side object to be used to copy from.
135  */
136  Grid(const Grid& rhs);
137 
138  /*! \brief Destructor. */
139  ~Grid();
140 
141  /*!
142  \brief Assignment operator.
143 
144  \param rhs The right-hand-side object to be used to copy from.
145 
146  \return A reference to this.
147  */
148  Grid& operator=(const Grid& rhs);
149 
150  /*!
151  \brief Sets the grid number of columns.
152 
153  \param nCols The number of columns.
154  */
155  void setNumberOfColumns(unsigned int nCols);
156 
157  /*! \brief Returns the grid number of columns. */
158  unsigned int getNumberOfColumns() const;
159 
160  /*!
161  \brief Sets the grid number of rows.
162 
163  \param nRows The number of rows.
164  */
165  void setNumberOfRows(unsigned int nRows);
166 
167  /*! \brief Returns the grid number of rows. */
168  unsigned int getNumberOfRows() const;
169 
170  /*!
171  \brief Sets the information needed to georeference the grid.
172 
173  Specifies a transformation that maps a grid point (col, row) to a spatial location (x, y).
174 
175  This method should be used when there is no translation associated to the mapping.
176 
177  \param ulLocation The location of the upper-left corner of the upper-left pixel (or cell) of the grid.
178  \param srid The id of the SRS that the location refers to.
179  \para resX The grid horizontal (x-axis) resolution
180  \para resY The grid vertical (y-axis) resolution
181  */
182  void setGeoreference(const te::gm::Coord2D& ulLocation, int srid, double resX, double resY);
183 
184  /*!
185  \brief Sets the information needed to georeference the grid using an affine transformation.
186 
187  An affine transformation maps grid point (col,lin) to a spatial location (x,y) using to the
188  following relationship:
189 
190  \verbatin
191  X = geoTrans(0) + col*geoTrans(1) + lin*geoTrans(2)
192  Y = geoTrans(3) + col*geoTrans(4) + lin*geoTrans(5)
193  \endverbatin
194 
195  This method should be used mainly when there is a translation associated to the mapping.
196  Otherwise use the method above.
197 
198  \param geoTrans[] A list of 6 coefficients describing an affine transformation to georeference a grid.
199  \param srid The SRS id to which the location refers to.
200  */
201  void setGeoreference(const double geoTrans[], int srid);
202 
203  /*! \brief Returns a list of 6 coefficients describing an affine transformation to georeference a grid. */
204  const double* getGeoreference() const;
205 
206  /*! \brief Returns the grid horizontal (x-axis) resolution. */
207  double getResolutionX() const;
208 
209  /*! \brief Returns the grid vertical (y-axis) resolution. */
210  double getResolutionY() const;
211 
212  /*! \brief Returns the grid spatial reference system identifier. */
213  int getSRID() const;
214 
215  /*! \brief Just sets the grid spatial reference system identifier. */
216  void setSRID(int srid);
217 
218  /*! \brief Returns the geographic extension of the grid. */
219  te::gm::Envelope* getExtent();
220 
221  /*! \brief Returns the geographic extension of the grid. */
222  const te::gm::Envelope* getExtent() const;
223 
224  /*! \brief Computes the geographic extension of the grid. */
225  void computeExtent() const;
226 
227  /*!
228  \brief Get the spatial location of a grid point.
229 
230  \param col The grid point column.
231  \param row The grid point row.
232  \param x The spatial location (X axis).
233  \param y The spatial location (Y axis).
234  */
235  void gridToGeo(const double& col, const double& row, double& x, double& y) const;
236 
237  /*!
238  \brief Get the spatial location of a grid point.
239 
240  \param col The grid point column.
241  \param row The grid point row.
242 
243  \return The spatial location.
244  */
245  te::gm::Coord2D gridToGeo(const double& col, const double& row) const;
246 
247  /*!
248  \brief Get the grid point associated to a spatial location.
249 
250  \param x The spatial x-coordiante.
251  \param y The spatial y-coordiante.
252  \param col The grid point column.
253  \param row The grid point row.
254  */
255  void geoToGrid(const double& x, const double& y, double& col, double& row) const;
256 
257  /*!
258  \brief Get the grid point associated to a spatial location.
259 
260  \param x The spatial x-coordiante.
261  \param y The spatial y-coordiante.
262 
263  \return The grid location.
264  */
265  te::gm::Coord2D geoToGrid(const double& x, const double& y) const;
266 
267  /*!
268  \brief Equal operator.
269 
270  \param rhs The source object to be compared.
271 
272  \return True if the grids have the same exact coordinates, number of cols and rows, srid, and 6 coefficients.
273  */
274  bool operator==(const Grid& rhs) const;
275 
276  /*
277  \brief Returns if point is in the grid.
278 
279  \param col The column to test.
280  \param row The row to test.
281 
282  \return True if point is in the grid, and false otherwise.
283  */
284  bool isPointInGrid(unsigned int col, unsigned int row) const;
285 
286  private:
287 
288  mutable te::gm::Envelope* m_extent; //!< The grid extent.
289  unsigned int m_nCols; //!< Number of columns.
290  unsigned int m_nRows; //!< Number of rows.
291  int m_srid; //!< The associated SRS.
292  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]).
293  };
294 
295  typedef boost::shared_ptr<Grid> GridPtr;
296 
297  inline te::gm::Coord2D Grid::gridToGeo(const double& col, const double& row) const
298  {
299  te::gm::Coord2D c;
300  gridToGeo(col, row, c.x, c.y);
301  return c;
302  }
303 
304  inline te::gm::Coord2D Grid::geoToGrid(const double& x, const double& y) const
305  {
306  te::gm::Coord2D c;
307  geoToGrid(x, y, c.x, c.y);
308  return c;
309  }
310 
311  inline bool Grid::isPointInGrid(unsigned int col, unsigned int row) const
312  {
313  return (col < m_nCols) && (row < m_nRows);
314  }
315 
316  } // end namespace rst
317 } // end namespace te
318 
319 #endif // __TERRALIB_RASTER_INTERNAL_GRID_H
boost::shared_ptr< Grid > GridPtr
Definition: Grid.h:295
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
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:72
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
te::da::Expression * operator==(const te::da::Expression &e1, const te::da::Expression &e2)
Definition: Expression.cpp:38
A rectified grid is the spatial support for raster data.
Definition: Grid.h:55
#define TERASTEREXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:93
bool isPointInGrid(unsigned int col, unsigned int row) const
Definition: Grid.h:311
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 x
x-coordinate.
Definition: Coord2D.h:86
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
Configuration flags for the Raster module of TerraLib.
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
unsigned int m_nCols
Number of columns.
Definition: Grid.h:289