Utils.h
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/raster/Utils.h
22 
23  \brief Utility functions for the raster module.
24 */
25 
26 #ifndef __TERRALIB_RASTER_INTERNAL_UTILS_H
27 #define __TERRALIB_RASTER_INTERNAL_UTILS_H
28 
29 // TerraLib
30 #include "Band.h"
31 #include "BandProperty.h"
32 #include "Config.h"
33 #include "Grid.h"
34 #include "Raster.h"
35 #include "../common/MathUtils.h"
36 
37 // STL
38 #include <memory>
39 #include <map>
40 #include <vector>
41 
42 namespace te
43 {
44  namespace rst
45  {
46  /*! \brief Returns the byte size of a given datatype. */
47  TERASTEREXPORT int GetPixelSize(int datatype);
48 
49  /*!
50  \brief Returns a vector of band properties, based on a given raster info.
51 
52  \param rinfo The map of raster informations.
53 
54  \return A vector of band properties.
55 
56  \note The caller will take the ownership of the returned pointer.
57 
58  \warning The variable rinfo must have the properties NBANDS and BANDSTYPE defined.
59  */
60  TERASTEREXPORT std::vector<BandProperty*> GetBandProperties(const std::map<std::string, std::string>& rinfo);
61 
62  /*!
63  \brief Returns a grid based on a given raster info.
64 
65  \param rinfo The map of raster informations.
66 
67  \return A grid info.
68 
69  \note The caller will take the ownership of the returned pointer.
70 
71  \warning The parameter rinfo must have the properties NCOLS and NROWS defined.
72  \warning Optional properties in parameter rinfo are RESX, RESY, SRID, ULX and ULY.
73  */
74  TERASTEREXPORT Grid* GetGrid(const std::map<std::string, std::string>& rinfo);
75 
76  /*!
77  \brief Copies the pixel values from one raster to another.
78 
79  \param rin The input raster.
80  \param rout The output raster.
81 
82  \warning Both rasters must have the same properties of grid and number/type of bands.
83  */
84  TERASTEREXPORT void Copy(const Raster& rin, Raster& rout);
85 
86  /*!
87  \brief Copies the pixel values from one band to another.
88 
89  \param bin The input band.
90  \param bout The output band.
91 
92  \warning Both bands must have the same properties of type and grid.
93  */
94  TERASTEREXPORT void Copy(const Band& bin, Band& bout);
95 
96  /*!
97  \brief Copy a subset of the raster, given a box.
98 
99  \param method The method of interpolation. \sa te::rst::Interpolator
100  \param drow The starting row to make a subset of the image.
101  \param dcolumn The starting column to make a subset of the image.
102  \param height The height of the subset.
103  \param width The width of the subset.
104  \param rin Pointer to valid output raster.
105  \param rout Pointer to valid output raster.
106  */
107  TERASTEREXPORT void Copy(unsigned int drow, unsigned int dcolumn, unsigned int height, unsigned int width, const Raster& rin, Raster& rout);
108 
109  /*!
110  \brief Round a double value to a integer value.
111 
112  \param val The double value.
113 
114  \return A integer value.
115  */
116 
117  TERASTEREXPORT inline int Round(double val) { return te::common::Round< double, int >( val ); }
118 
119  /*!
120  \brief Create a new raster from existing one.
121 
122  \param rin The input raster.
123  \param uri The output raster URI.
124  \param rType The name of the specific driver to create the raster.
125 
126  \return A valid pointer to the created raster or a null (invalid) pointer if an error ocurred.
127  */
128  TERASTEREXPORT te::rst::RasterPtr CreateCopy(const te::rst::Raster& rin, const std::string& uri,
129  const std::string& rType = std::string("GDAL"));
130 
131  /*!
132  \brief Return the values range of a given data type.
133 
134  \param dataType The given data type.
135  \param min The mininmum value allowed for the given data type.
136  \param min The maximum value allowed for the given data type.
137  */
138  TERASTEREXPORT void GetDataTypeRanges(const int& dataType, double& min, double& max);
139 
140  /*!
141  \brief Fill a Raster with provided value (all bands).
142 
143  \param rin The input raster.
144  \param value The value to fill all the bands of the Raster.
145  */
146  TERASTEREXPORT void FillRaster(te::rst::Raster* rin, const std::complex<double>& value);
147 
148  /*!
149  \brief Fill a Raster Band with provided value.
150 
151  \param bin The input band.
152  \param value The value to fill all the bands of the Raster.
153  */
154  TERASTEREXPORT void FillBand(te::rst::Band* bin, const std::complex<double>& value);
155 
156  /*!
157  \brief Creates a raster crop using a polygon delimiter.
158 
159  \param rin The input raster.
160  \param pin The input polygon, to be used as a delimiter.
161  \param rinfo The map of raster informations.
162  \param rType The name of the specific driver to create the raster.
163 
164  \return A valid pointer to the created raster or a null (invalid) pointer if an error ocurred.
165  \note The area outside the polygon will be filled with nodata values.
166  */
168  const te::gm::Polygon& pin,
169  const std::map<std::string, std::string>& rinfo,
170  const std::string& rType = std::string("GDAL"));
171 
172  /*!
173  \brief Creates a raster crop using a polygon delimiter.
174 
175  \param rin The input raster.
176  \param geometries A vector of pointers to valid geometries.
177  \param rinfo The map of raster informations.
178  \param rType The name of the specific driver to create the raster.
179 
180  \return A valid pointer to the created raster or a null (invalid) pointer if an error ocurred.
181  \note The area outside the polygon will be filled with nodata values.
182  \note Accepted geometry types: te::gm::Polygon, te::gm::MultiPolygon.
183  */
184  TERASTEREXPORT std::unique_ptr< te::rst::Raster > CropRaster(const te::rst::Raster& rin,
185  const std::vector< te::gm::Geometry const *> geometries,
186  const std::map<std::string, std::string>& rinfo,
187  const std::string& rType );
188 
189  /*!
190  \brief Creates a vector of random positions (points) inside the raster.
191 
192  \param inputRaster The given raster.
193  \param numberOfPoints The number of random positions to be created (default = 1000).
194  \param ignoreNoDataValues Flag used to inform that pixel with no data value will not be considered.
195 
196  \return A vector of random positions (points).
197  \ingroup rp_func
198  */
199  TERASTEREXPORT std::vector<te::gm::Point*> GetRandomPointsInRaster(const te::rst::Raster& inputRaster, unsigned int numberOfPoints = 1000, bool ignoreNoDataValues = false);
200 
201  /*!
202  \brief Function used to convert from a Color Interp Enum to a string
203 
204  \param ci The color interpretation enum
205 
206  \return A string with the color interpreation name.
207  */
209 
210  /*!
211  \brief Function used to convert from a Pallete Interp Enum to a string
212 
213  \param pi The pallete interpretation enum
214 
215  \return A string with the pallete interpreation name.
216  */
218 
219  /*!
220  \brief Compute and return the real histogram of the band.
221 
222  \param raster Input raster.
223  \param bandIndex Band index.
224  \param rHistogram The generated histogram (real).
225  \return true if ok, false on errors
226  \note This method will ignore storage specific tags and will read all raster data to compute the histograms (use te::rst::Band methods to use storage specifc tags).
227  */
229  const te::rst::Raster& raster,
230  const unsigned int bandIndex,
231  std::map<double, unsigned>& rHistogram );
232 
233  /*!
234  \brief Compute and return the histogram soccurring values (real and imaginary) in a window of the band.
235 
236  \param raster Input raster.
237  \param bandIndex Band index.
238  \param rowStart The starting row.
239  \param colStart The starting column.
240  \param finalRow The final row.
241  \param finalCol The final column.
242  \param histoBins The number of bins (intervals from minimum pixel to maximum). When b = 0, the histogram will be divided according to all pixel values.
243  \param sampleStep The row/column step used when reading pixels (to read all pixels sampleStep=1, to read half of pixels use sampleStep=2 );
244  \param maxThreads Maximum number of used threads to use (0-Automatic, 1-No threads used).
245  \param rHistogram The generated histogram (real).
246  \param iHistogram The generated histogram (imaginary).
247  \warning The caller is responsible for providing correct values for the range [rs, cs x rf, cf].
248  \return true if ok, false on errors
249  \note This method will ignore storage specific tags and will read all raster data to compute the histograms (use te::rst::Band methods to use storage specifc tags).
250  */
252  const te::rst::Raster& raster,
253  const unsigned int bandIndex,
254  const unsigned int rowStart,
255  const unsigned int colStart,
256  const unsigned int finalRow,
257  const unsigned int finalCol,
258  const unsigned int histoBins,
259  const unsigned int sampleStep,
260  const unsigned int maxThreads,
261  std::map<double, unsigned>& rHistogram,
262  std::map<double, unsigned>& iHistogram );
263 
264  /*!
265  \brief Compute statiscts from the given histogram.
266 
267  \param histogram Input histogram.
268  \param min Histogram minimun value.
269  \param max Histogram maximum value.
270  \param mean Histogram mean value.
271  \param stdDev Histogram standard deviation value.
272  */
274  const std::map<double, unsigned>& histogram,
275  double& min,
276  double& max,
277  double& mean,
278  double& stdDev );
279 
280  } // end namespace rst
281 } // end namespace te
282 
283 #endif // __TERRALIB_RASTER_INTERNAL_UTILS_H
#define TERASTEREXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:63
TERASTEREXPORT std::string ConvertColorInterpTypeToString(const te::rst::ColorInterp &ci)
Function used to convert from a Color Interp Enum to a string.
It describes one band (or dimension) of a raster.
ColorInterp
Color model component use.
Definition: Enums.h:54
TERASTEREXPORT std::vector< BandProperty * > GetBandProperties(const std::map< std::string, std::string > &rinfo)
Returns a vector of band properties, based on a given raster info.
TERASTEREXPORT void FillBand(te::rst::Band *bin, const std::complex< double > &value)
Fill a Raster Band with provided value.
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
TERASTEREXPORT Grid * GetGrid(const std::map< std::string, std::string > &rinfo)
Returns a grid based on a given raster info.
TERASTEREXPORT std::string ConvertPalleteInterpTypeToString(const te::rst::PaletteInterpretation &pi)
Function used to convert from a Pallete Interp Enum to a string.
TERASTEREXPORT int GetPixelSize(int datatype)
Returns the byte size of a given datatype.
TERASTEREXPORT bool getRealHistogram(const te::rst::Raster &raster, const unsigned int bandIndex, std::map< double, unsigned > &rHistogram)
Compute and return the real histogram of the band.
TERASTEREXPORT te::rst::RasterPtr CropRaster(const te::rst::Raster &rin, const te::gm::Polygon &pin, const std::map< std::string, std::string > &rinfo, const std::string &rType=std::string("GDAL"))
Creates a raster crop using a polygon delimiter.
boost::shared_ptr< Raster > RasterPtr
Definition: Raster.h:703
TERASTEREXPORT std::vector< te::gm::Point * > GetRandomPointsInRaster(const te::rst::Raster &inputRaster, unsigned int numberOfPoints=1000, bool ignoreNoDataValues=false)
Creates a vector of random positions (points) inside the raster.
An abstract class for raster data strucutures.
An abstract class for raster data strucutures.
Definition: Raster.h:71
TerraLib.
TERASTEREXPORT int Round(double val)
Round a double value to a integer value.
Definition: Utils.h:117
It gives access to values in one band (dimension) of a raster.
A raster band description.
Definition: Band.h:63
TERASTEREXPORT bool getHistograms(const te::rst::Raster &raster, const unsigned int bandIndex, const unsigned int rowStart, const unsigned int colStart, const unsigned int finalRow, const unsigned int finalCol, const unsigned int histoBins, const unsigned int sampleStep, const unsigned int maxThreads, std::map< double, unsigned > &rHistogram, std::map< double, unsigned > &iHistogram)
Compute and return the histogram soccurring values (real and imaginary) in a window of the band...
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
TERASTEREXPORT void FillRaster(te::rst::Raster *rin, const std::complex< double > &value)
Fill a Raster with provided value (all bands).
A rectified grid is the spatial support for raster data.
TERASTEREXPORT void getHistogramStats(const std::map< double, unsigned > &histogram, double &min, double &max, double &mean, double &stdDev)
Compute statiscts from the given histogram.
Configuration flags for the Raster module of TerraLib.
PaletteInterpretation
Palette interpratation types.
Definition: Enums.h:80
TERASTEREXPORT te::rst::RasterPtr CreateCopy(const te::rst::Raster &rin, const std::string &uri, const std::string &rType=std::string("GDAL"))
Create a new raster from existing one.
TERASTEREXPORT void Copy(const Raster &rin, Raster &rout)
Copies the pixel values from one raster to another.