All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Raster.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/Raster.h
22 
23  \brief An abstract class for raster data strucutures.
24 */
25 
26 #ifndef __TERRALIB_RASTER_INTERNAL_RASTER_H
27 #define __TERRALIB_RASTER_INTERNAL_RASTER_H
28 
29 // TerraLib
30 #include "../common/Enums.h"
31 #include "../datatype/AbstractData.h"
32 #include "../datatype/Enums.h"
33 #include "../geometry/Geometry.h"
34 #include "Config.h"
35 
36 // STL
37 #include <complex>
38 #include <map>
39 #include <string>
40 #include <vector>
41 
42 // Boost
43 #include <boost/shared_ptr.hpp>
44 
45 namespace te
46 {
47 // Forward declaration
48  namespace gm { class Envelope; }
49 
50  namespace rst
51  {
52 // Forward declaration
53  class Band;
54  class BandProperty;
55  class Grid;
56 
57  /*!
58  \class Raster
59 
60  \brief An abstract class for raster data strucutures.
61 
62  A raster data structure represents a geograhical area divided into rows and columns,
63  which form a regular grid structure of cells. Each cell contains location co-ordinates and
64  one or more attribute values.
65 
66  This interface should be implemented by drivers that provide the real access to raster data.
67 
68  \ingroup rst
69  */
71  {
72  public:
73 
74  /*! \brief Default constructor. */
75  Raster();
76 
77  /*!
78  \brief Constructor to create a raster from parameters.
79 
80  \param grid The grid definition. The Raster will take its ownership.
81  \param p Access Policy.
82  */
84 
85  /*!
86  \brief Copy constructor.
87 
88  \param rhs The right-hand side Raster.
89  */
90  Raster(const Raster& rhs);
91 
92  /*! \brief Virtual destructor. */
93  virtual ~Raster();
94 
95  /*!
96  \brief Opens a raster.
97 
98  \param rinfo The information needed by each driver to open the raster.
99  \param p Access Policy.
100  */
101  virtual void open(const std::map<std::string, std::string>& rinfo, te::common::AccessPolicy p = te::common::RAccess) = 0;
102 
103  /*!
104  \brief Sets the raster name.
105 
106  \param name The new raster name.
107  */
108  void setName(const std::string name);
109 
110  /*! \brief Returns the raster name. */
111  const std::string& getName() const;
112 
113  /*!
114  \brief Sets the raster access policy.
115 
116  \param p The new raster access policy.
117  */
119 
120  /*! \brief Returns the raster access policy. */
121  te::common::AccessPolicy getAccessPolicy() const;
122 
123  /*!
124  \brief It returns additional information about the raster.
125 
126  The return of thi smethod is driver dependent.
127 
128  \return Additional information about the raster.
129  */
130  virtual std::map<std::string, std::string> getInfo() const = 0;
131 
132  /*! \brief It returns the raster grid. */
133  Grid* getGrid();
134 
135  /*! \brief It returns the raster grid. */
136  const Grid* getGrid() const;
137 
138  /*! \brief Returns the geographic extension of the raster data. */
139  te::gm::Envelope* getExtent();
140 
141  /*! \brief Returns the geographic extension of the raster data. */
142  const te::gm::Envelope* getExtent() const;
143 
144  /*!
145  \brief Returns the geographic extension of the raster data, in a given SRS (distinct from its original one).
146 
147  \param srid The target SRS id.
148  \param roi Pointer to a region of interest. If NULL the original extenstion will be considered.
149 
150  \return A pointer the raster extension in a given SRS or NULL if it fails.
151 
152  \note The caller will take the ownership of the returned pointer.
153  */
154  te::gm::Envelope* getExtent(int srid, te::gm::Envelope* roi = 0) const;
155 
156  /*! \brief Returns the raster spatial reference system identifier. */
157  int getSRID() const;
158 
159  /*! \brief Returns the raster number of rows. */
160  unsigned int getNumberOfRows() const;
161 
162  /*! \brief Returns the raster number of columns. */
163  unsigned int getNumberOfColumns() const;
164 
165  /*! \brief Returns the raster horizontal (x-axis) resolution. */
166  double getResolutionX() const;
167 
168  /*! \brief Returns the raster vertical (y-axis) resolution. */
169  double getResolutionY() const;
170 
171  /*! \brief Returns the number of bands (dimension of cells attribute values) in the raster. */
172  virtual std::size_t getNumberOfBands() const = 0;
173 
174  /*!
175  \brief Returns the data type in a particular band (or dimension).
176 
177  \note The data types are listed in terralib/datatype/DataTypes.h
178 
179  \param i The desired band.
180 
181  \return The data type in a particular band (or dimension).
182  */
183  virtual int getBandDataType(std::size_t i) const = 0;
184 
185  /*!
186  \brief Returns the raster i-th band.
187 
188  \param i The band index.
189 
190  \warning The caller is reponsible for providing a valid band index.
191  */
192  virtual const Band* getBand(std::size_t i) const = 0;
193 
194  /*!
195  \brief Returns the raster i-th band.
196 
197  \param i The band index.
198 
199  \warning The caller is reponsible for providing a valid band index.
200  */
201  virtual Band* getBand(std::size_t i) = 0;
202 
203  /*!
204  \brief Access band in i position.
205 
206  \param i The band index.
207 
208  \warning The caller is reponsible for providing a valid band index.
209 
210  \return A reference to the i-th band.
211  */
212  virtual const Band& operator[](std::size_t i) const = 0;
213 
214  /*!
215  \brief Access band in i position.
216 
217  \param i The band index.
218 
219  \warning The caller is reponsible for providing a valid band index.
220 
221  \return A reference to the i-th band.
222  */
223  virtual Band& operator[](std::size_t i) = 0;
224 
225  /*!
226  \brief Returns the attribute value of a band of a cell.
227 
228  \param c The column location of the cell.
229  \param r The row location of the cell.
230  \param value To return the attribute value.
231  \param b A particular band of the cell attribute.
232 
233  \warning The caller is responsible for providing correct values for the range [c x r x b].
234 
235  \exception Exception Subclasses may throw an exception if the data value can not be read.
236  */
237  virtual void getValue(unsigned int c, unsigned int r, double& value, std::size_t b = 0) const;
238 
239  /*!
240  \brief Sets the attribute value in a band of a cell.
241 
242  \param c The column location of the cell.
243  \param r The row location of the cell.
244  \param value The attribute value to be assigned.
245  \param b A particular band of the cell attribute.
246 
247  \warning The caller is responsible for providing correct values for the range [c x r x b].
248 
249  \exception Exception Subclasses may throw an exception if the data value can not be write.
250  */
251  virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b = 0);
252 
253  /*!
254  \brief Returns the imaginary attribute value in a complex band of a cell.
255 
256  \param c The column location of the cell.
257  \param r The row location of the cell.
258  \param value The attribute value to be assigned.
259  \param b A particular band of the cell attribute. Default value 0.
260 
261  \warning The caller is responsible for providing correct values for the range [c x r x b].
262 
263  \exception Exception Subclasses may throw an exception if the data value can not be read.
264  */
265  virtual void getIValue(unsigned int c, unsigned int r, double& value, std::size_t b = 0) const;
266 
267  /*!
268  \brief Sets the imaginary attribute value in a complex band of a cell.
269 
270  \param c The column location of the cell.
271  \param r The row location of the cell.
272  \param value The attribute value to be assigned.
273  \param b A particular band of the cell attribute. Default value 0.
274 
275  \warning The caller is responsible for providing correct values for the range [c x r x b].
276 
277  \exception Exception Subclasses may throw an exception if the data value can not be write.
278  */
279  virtual void setIValue(unsigned int c, unsigned int r, const double value, std::size_t b = 0);
280 
281  /*!
282  \brief Returns the imaginary attribute value in a complex band of a cell.
283 
284  \param c The column location of the cell.
285  \param r The row location of the cell.
286  \param value The complex attribute value to be assigned (real, imaginary).
287  \param b A particular band of the cell attribute. Default value 0.
288 
289  \warning The caller is responsible for providing correct values for the range [c x r x b].
290 
291  \exception Exception Subclasses may throw an exception if the data value can not be read.
292  */
293  virtual void getValue(unsigned int c, unsigned int r, std::complex<double>& value, std::size_t b = 0) const;
294 
295  /*!
296  \brief Sets the imaginary attribute value in a complex band of a cell.
297 
298  \param c The column location of the cell.
299  \param r The row location of the cell.
300  \param value The complex attribute value to be assigned (real, imaginary).
301  \param b A particular band of the cell attribute. Default value 0.
302 
303  \warning The caller is responsible for providing correct values for the range [c x r x b].
304 
305  \exception Exception Subclasses may throw an exception if the data value can not be write.
306  */
307  virtual void setValue(unsigned int c, unsigned int r, const std::complex<double>& value, std::size_t b = 0);
308 
309  /*!
310  \brief Returns the imaginary attribute values in all complex bands of a cell.
311 
312  \param c The column location of the cell.
313  \param r The row location of the cell.
314  \param values A vector of attribute values to be assigned (not complex values).
315 
316  \warning The caller is responsible for providing correct values for the range [c x r].
317 
318  \exception Exception Subclasses may throw an exception if the data value can not be read.
319  */
320  virtual void getValues(unsigned int c, unsigned int r, std::vector<double>& values) const;
321 
322  /*!
323  \brief Returns the imaginary attribute values in all complex bands of a cell.
324 
325  \param c The column location of the cell.
326  \param r The row location of the cell.
327  \param values A vector of complex attribute values to be assigned (real, imaginary).
328 
329  \warning The caller is responsible for providing correct values for the range [c x r].
330 
331  \exception Exception Subclasses may throw an exception if the data value can not be read.
332  */
333  virtual void getValues(unsigned int c, unsigned int r, std::vector<std::complex<double> >& values) const;
334 
335  /*!
336  \brief Sets the imaginary attribute values in all complex bands of a cell.
337 
338  \param c The column location of the cell.
339  \param r The row location of the cell.
340  \param values A vector of attribute values to be assigned (not complex values).
341 
342  \warning The caller is responsible for providing correct values for the range [c x r].
343 
344  \exception Exception Subclasses may throw an exception if the data value can not be write.
345  */
346  virtual void setValues(unsigned int c, unsigned int r, const std::vector<double>& values);
347 
348  /*!
349  \brief Sets the imaginary attribute values in all complex bands of a cell.
350 
351  \param c The column location of the cell.
352  \param r The row location of the cell.
353  \param values A vector of complex attribute values to be assigned (real, imaginary).
354 
355  \warning The caller is responsible for providing correct values for the range [c x r].
356 
357  \exception Exception Subclasses may throw an exception if the data value can not be write.
358  */
359  virtual void setValues(unsigned int c, unsigned int r, const std::vector<std::complex<double> >& values);
360 
361  int getTypeCode() const { return te::dt::RASTER_TYPE; }
362 
363  std::string toString(void) const;
364 
365  /*!
366  \brief It calls a parameter function f to apply in all pixels from two rasters, e.g. pixel = f(lhs, rhs);
367 
368  \param (*f) a function with the signature complex<double>(*f)(complex<double>, complex<double>), const Raster& rhs)
369 
370  \param rhs The rhs raster to apply the function.
371 
372  \return The resultant raster.
373  */
374  Raster& callOperator(std::complex<double>(*f)(std::complex<double>, std::complex<double>), const Raster& rhs);
375 
376  /*!
377  \brief It returns the raster sum (pixel by pixel).
378 
379  \param rhs The raster to be added, right-hand side.
380 
381  \note Both rasters must have the same properties, #bands, #columns,
382  #rows, and data type.
383 
384  \note The caller is responsible to guarantee that resultant values
385  will not exceed the range of the data type.
386 
387  \return The raster sum.
388  */
389  virtual Raster& operator+=(Raster& rhs);
390 
391  /*!
392  \brief It returns the raster subtraction (pixel by pixel).
393 
394  \param rhs The raster to be subtracted, right-hand side.
395 
396  \note Both rasters must have the same properties, #bands, #columns,
397  #rows, and data type.
398 
399  \note The caller is responsible to guarantee that resultant values
400  will not exceed the range of the data type.
401 
402  \return The raster subtraction.
403  */
404  virtual Raster& operator-=(Raster& rhs);
405 
406  /*!
407  \brief It returns the raster product (pixel by pixel).
408 
409  \param rhs The raster to be multiplied, right-hand side.
410 
411  \note Both rasters must have the same properties, #bands, #columns,
412  #rows, and data type.
413 
414  \note The caller is responsible to guarantee that resultant values
415  will not exceed the range of the data type.
416 
417  \return The raster product.
418  */
419  virtual Raster& operator*=(Raster& rhs);
420 
421  /*!
422  \brief It returns the raster division (pixel by pixel).
423 
424  \param rhs The raster to be divided, right-hand side.
425 
426  \note Both rasters must have the same properties, #bands, #columns,
427  #rows, and data type.
428 
429  \note The caller is responsible to guarantee that resultant values
430  will not exceed the range of the data type.
431 
432  \return The raster division.
433  */
434  virtual Raster& operator/=(Raster& rhs);
435 
436  /*!
437  \brief Assignment operator.
438 
439  \param rhs The right-hand-side copy that would be used to copy from.
440 
441  \return A reference to this object.
442  */
443  virtual Raster& operator=(const Raster& rhs);
444 
445  /*!
446  \brief Subsetting operation for trimming (cropping) the raster.
447 
448  \param env The envelope (inside original extent) to crop the raster.
449  \param rinfo The parameters needed to build the output raster (see RasterFactory documentation).
450 
451  \return A pointer to the trimmed raster if success and a null pointer otherwise.
452 
453  \note The caller will take the ownership of the returned pointer.
454  */
455  virtual Raster* trim(const te::gm::Envelope* env, const std::map<std::string, std::string>& rinfo);
456 
457  /*!
458  \brief Resample a subset of the raster, given a box.
459 
460  \param method The method of interpolation. \sa te::rst::Interpolator
461  \param drow The starting row to make a subset of the image.
462  \param dcolumn The starting column to make a subset of the image.
463  \param height The height of the subset.
464  \param width The width of the subset.
465  \param newheight The resampled height of the new raster.
466  \param newwidth The resampled width of the new raster.
467  \param rinfo The parameters needed to build the output raster (see RasterFactory documentation).
468 
469  \return A pointer to the resampled raster if success and a null pointer otherwise.
470 
471  \note The caller will take the ownership of the returned pointer.
472 
473  \warning A scale of 0 is not allowed. Use 1, 2, 3, or -1, -2, 3...
474  */
475  virtual Raster* resample(int method, unsigned int drow, unsigned int dcolumn, unsigned int height, unsigned int width, unsigned int newheight, unsigned int newwidth, const std::map<std::string, std::string>& rinfo);
476 
477  /*!
478  \brief Resample raster.
479 
480  \param method The method of interpolation.
481  \param scale The scale of interpolation (use - to shrink or + to enlarge).
482  \param rinfo The parameters needed to build the output raster (see RasterFactory documentation).
483 
484  \return A pointer to the resampled raster if success and a null pointer otherwise.
485 
486  \note The caller will take the ownership of the returned pointer.
487 
488  \warning A scale of 0 is not allowed. Use 1, 2, 3, or -1, -2, 3...
489  */
490  virtual Raster* resample(int method, int scale, const std::map<std::string, std::string>& rinfo);
491 
492  /*!
493  \brief Return the raster grid for a specific scale.
494 
495  \param scale The desired sacale of interpolation (use - to shrink or + to enlarge).
496 
497  \return The raster grid.
498 
499  \note The caller will take the ownership of the returned pointer.
500  */
501  Grid* getResampledGrid(int scale);
502 
503  /*!
504  \brief Reprojects this raster to a distinct SRS.
505  This method reprojects this raster to a distinct SRS. The output resolution
506  is calculated in order to maintain the same number of pixels as in this raster.
507 
508  \param srid The target SRS identifier.
509  \param rinfo The parameters needed to build the output raster (see RasterFactory documentation).
510  \param m The method of interpolation to apply (default 1 = nearest neighbor). \sa te::rst::Interpolator
511 
512 
513  \return A pointer to the reprojected raster if success and a null pointer otherwise.
514 
515  \note The caller will take the ownership of the returned pointer.
516  */
517  virtual Raster* transform(int srid, const std::map<std::string, std::string>& rinfo, int m = 1) const;
518 
519  /*!
520  \brief Reprojects a squared portion of this raster to a distinct SRS.
521  This method reprojects a squared portion of this raster to a distinct SRS.
522  The output resolution is calculated in order to maintain the same number
523  of pixels as in the desired portion of this raster.
524 
525  \param srid The target SRS identifier.
526  \param llx Lower-left X-coordinate of the portion to be reprojected (in the original SRS).
527  \param lly Lower-left Y-coordinate of the portion to be reprojected (in the original SRS).
528  \param urx Upper-Right X-coordinate of the portion to be reprojected (in the original SRS).
529  \param url Upper-Right Y-coordinate of the portion to be reprojected (in the original SRS).
530  \param rinfo The parameters needed to build the output raster (see RasterFactory documentation).
531  \param m The method of interpolation to apply (default 1 = nearest neighbor). \sa te::rst::Interpolator
532 
533  \return A pointer to the reprojected raster if success and a null pointer otherwise.
534 
535  \note The caller will take the ownership of the returned pointer.
536  */
537  virtual Raster* transform(int srid, double llx, double lly, double urx, double ury, const std::map<std::string, std::string>& rinfo, int m = 1) const;
538 
539  /*!
540  \brief Reprojects a squared portion of this raster to another SRS with a desired resolution.
541 
542  This method reprojects a squared portion of this raster to another SRS. The number of pixels
543  in the output will be calculated according to the portion being reprojected and the asked resolution.
544 
545  \param srid The SRS id of the target SRS.
546  \param llx Lower-left X-coordinate of the portion to be reprojected (in the original SRS).
547  \param lly Lower-left Y-coordinate of the portion to be reprojected (in the original SRS).
548  \param urx Upper-Right X-coordinate of the portion to be reprojected (in the original SRS).
549  \param url Upper-Right Y-coordinate of the portion to be reprojected (in the original SRS).
550  \param resx The output x resolution (in units of the target SRS).
551  \param resx The output y resolution (in units of the target SRS).
552  \param rinfo The parameters needed to build the output raster (see RasterFactory documentation).
553  \param m The method of interpolation to apply (default 1 = nearest neighbor). \sa te::rst::Interpolator
554 
555  \return A pointer to the reprojected raster if success and a null pointer otherwise.
556 
557  \note The caller will take the ownership of the returned pointer.
558  */
559  virtual Raster* transform(int srid, double llx, double lly, double urx, double ury, double resx, double resy, const std::map<std::string, std::string>& rinfo, int m = 1) const;
560 
561  /*!
562  \brief Vectorizes a given raster band, using GDALPolygonize function.
563 
564  \param b The band index to vectorize.
565  \param g A reference to a vector of geometries.
566  Will be filled with geometries found in band.
567  */
568  virtual void vectorize(std::size_t /*b*/, std::vector<te::gm::Geometry*>& /*g*/) {};
569 
570  /*!
571  \brief Rasterizes a given vector of geometries.
572 
573  \param g A vector of geometries to be rasterized.
574  \param vp A vector of pixel values for each geometry.
575  Can have the same size of the vector of geometries, or be null.
576  \param b The band index to rasterize.
577  */
578  virtual void rasterize(std::vector<te::gm::Geometry*> g, std::vector<double> vp, std::size_t b = 0);
579 
580  protected:
581 
582  /*!
583  \brief Scales a value according to a specific resampling scale.
584 
585  \param i The scale to apply.
586  \param v The value to be scaled.
587 
588  \return The scaled value.
589  */
590  double applyScale(int i, const double& v);
591 
592  protected:
593 
594  std::string m_name; //!< The raster name.
595  Grid* m_grid; //!< The spatial support for raster data.
596  te::common::AccessPolicy m_policy; //!< The access policy, can be te::common::{NoAccess, RAccess, RWAccess, WAccess}.
597  };
598 
599  typedef boost::shared_ptr<Raster> RasterPtr;
600 
601  } // end namespace rst
602 } // end namespace te
603 
604 #endif //__TERRALIB_RASTER_INTERNAL_RASTER_H
int getTypeCode() const
It returns the data type code associated to the data value.
Definition: Raster.h:361
std::string m_name
The raster name.
Definition: Raster.h:594
virtual void vectorize(std::size_t, std::vector< te::gm::Geometry * > &)
Vectorizes a given raster band, using GDALPolygonize function.
Definition: Raster.h:568
Grid * m_grid
The spatial support for raster data.
Definition: Raster.h:595
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
A raster band description.
Definition: Band.h:63
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
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
te::common::AccessPolicy m_policy
The access policy, can be te::common::{NoAccess, RAccess, RWAccess, WAccess}.
Definition: Raster.h:596
An abstract class for raster data strucutures.
Definition: Raster.h:70
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
Configuration flags for the Raster module of TerraLib.
boost::shared_ptr< Raster > RasterPtr
Definition: Raster.h:599
capabilities setAccessPolicy(te::common::RWAccess)