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 "Interpolator.h"
36 #include "../common/MathUtils.h"
37 
38 // STL
39 #include <memory>
40 #include <map>
41 #include <vector>
42 
43 namespace te
44 {
45  namespace rst
46  {
47  /*! \brief Returns the byte size of a given datatype. */
48  TERASTEREXPORT int GetPixelSize(int datatype);
49 
50  /*!
51  \brief Returns a vector of band properties, based on a given raster info.
52 
53  \param rinfo The map of raster informations.
54 
55  \return A vector of band properties.
56 
57  \note The caller will take the ownership of the returned pointer.
58 
59  \warning The variable rinfo must have the properties NBANDS and BANDSTYPE defined.
60  */
61  TERASTEREXPORT std::vector<BandProperty*> GetBandProperties(const std::map<std::string, std::string>& rinfo);
62 
63  /*!
64  \brief Returns a grid based on a given raster info.
65 
66  \param rinfo The map of raster informations.
67 
68  \return A grid info.
69 
70  \note The caller will take the ownership of the returned pointer.
71 
72  \warning The parameter rinfo must have the properties NCOLS and NROWS defined.
73  \warning Optional properties in parameter rinfo are RESX, RESY, SRID, ULX and ULY.
74  */
75  TERASTEREXPORT Grid* GetGrid(const std::map<std::string, std::string>& rinfo);
76 
77  /*!
78  \brief Copies the pixel values from one raster to another.
79 
80  \param rin The input raster.
81  \param rout The output raster.
82 
83  \warning Both rasters must have the same properties of grid and number/type of bands.
84  */
85  TERASTEREXPORT void Copy(const Raster& rin, Raster& rout);
86 
87  /*!
88  \brief Copies the pixel values from one band to another.
89 
90  \param bin The input band.
91  \param bout The output band.
92 
93  \warning Both bands must have the same properties of type and grid.
94  */
95  TERASTEREXPORT void Copy(const Band& bin, Band& bout);
96 
97  /*!
98  \brief Copy a subset of the raster, given a box.
99 
100  \param method The method of interpolation. \sa te::rst::Interpolator
101  \param drow The starting row to make a subset of the image.
102  \param dcolumn The starting column to make a subset of the image.
103  \param height The height of the subset.
104  \param width The width of the subset.
105  \param rin Pointer to valid output raster.
106  \param rout Pointer to valid output raster.
107  */
108  TERASTEREXPORT void Copy(unsigned int drow, unsigned int dcolumn, unsigned int height, unsigned int width, const Raster& rin, Raster& rout);
109 
110  /*!
111  \brief Round a double value to a integer value.
112 
113  \param val The double value.
114 
115  \return A integer value.
116  */
117 
118  TERASTEREXPORT inline int Round(double val) { return te::common::Round< double, int >( val ); }
119 
120  /*!
121  \brief Create a new raster from existing one.
122 
123  \param rin The input raster.
124  \param uri The output raster URI.
125  \param rType The name of the specific driver to create the raster.
126 
127  \return A valid pointer to the created raster or a null (invalid) pointer if an error ocurred.
128  */
129  TERASTEREXPORT te::rst::RasterPtr CreateCopy(const te::rst::Raster& rin, const std::string& uri,
130  const std::string& rType = std::string("GDAL"));
131 
132  /*!
133  \brief Return the values range of a given data type.
134 
135  \param dataType The given data type.
136  \param min The mininmum value allowed for the given data type.
137  \param min The maximum value allowed for the given data type.
138  */
139  TERASTEREXPORT void GetDataTypeRanges(const int& dataType, double& min, double& max);
140 
141  /*!
142  \brief Fill a Raster with provided value (all bands).
143 
144  \param rin The input raster.
145  \param value The value to fill all the bands of the Raster.
146  */
147  TERASTEREXPORT void FillRaster(te::rst::Raster* rin, const std::complex<double>& value);
148 
149  /*!
150  \brief Fill a Raster Band with provided value.
151 
152  \param bin The input band.
153  \param value The value to fill all the bands of the Raster.
154  */
155  TERASTEREXPORT void FillBand(te::rst::Band* bin, const std::complex<double>& value);
156 
157  /*!
158  \brief Creates a raster crop using a polygon delimiter.
159 
160  \param rin The input raster.
161  \param pin The input polygon, to be used as a delimiter.
162  \param rinfo The map of raster informations.
163  \param rType The name of the specific driver to create the raster.
164 
165  \return A valid pointer to the created raster or a null (invalid) pointer if an error ocurred.
166  \note The area outside the polygon will be filled with nodata values.
167  */
169  const te::gm::Polygon& pin,
170  const std::map<std::string, std::string>& rinfo,
171  const std::string& rType = std::string("GDAL"));
172 
173  /*!
174  \brief Creates a raster crop using a polygon delimiter.
175 
176  \param rin The input raster.
177  \param geometries A vector of pointers to valid geometries.
178  \param rinfo The map of raster informations.
179  \param rType The name of the specific driver to create the raster.
180 
181  \return A valid pointer to the created raster or a null (invalid) pointer if an error ocurred.
182  \note The area outside the polygon will be filled with nodata values.
183  \note Accepted geometry types: te::gm::Polygon, te::gm::MultiPolygon.
184  */
185  TERASTEREXPORT std::unique_ptr< te::rst::Raster > CropRaster(const te::rst::Raster& rin,
186  const std::vector< te::gm::Geometry const *> geometries,
187  const std::map<std::string, std::string>& rinfo,
188  const std::string& rType );
189 
190  /*!
191  \brief Creates a vector of random positions (points) inside the raster.
192 
193  \param inputRaster The given raster.
194  \param numberOfPoints The number of random positions to be created (default = 1000).
195  \param ignoreNoDataValues Flag used to inform that pixel with no data value will not be considered.
196 
197  \return A vector of random positions (points).
198  \ingroup rp_func
199  */
200  TERASTEREXPORT std::vector<te::gm::Point*> GetRandomPointsInRaster(const te::rst::Raster& inputRaster, unsigned int numberOfPoints = 1000, bool ignoreNoDataValues = false);
201 
202  /*!
203  \brief Function used to convert from a Color Interp Enum to a string
204 
205  \param ci The color interpretation enum
206 
207  \return A string with the color interpreation name.
208  */
210 
211  /*!
212  \brief Function used to convert from a Pallete Interp Enum to a string
213 
214  \param pi The pallete interpretation enum
215 
216  \return A string with the pallete interpreation name.
217  */
219 
220  /*!
221  \brief Compute and return the real histogram of the band.
222 
223  \param raster Input raster.
224  \param bandIndex Band index.
225  \param rHistogram The generated histogram (real).
226  \return true if ok, false on errors
227  \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).
228  */
230  const te::rst::Raster& raster,
231  const unsigned int bandIndex,
232  std::map<double, unsigned>& rHistogram );
233 
234  /*!
235  \brief Compute and return the histogram soccurring values (real and imaginary) in a window of the band.
236 
237  \param raster Input raster.
238  \param bandIndex Band index.
239  \param rowStart The starting row.
240  \param colStart The starting column.
241  \param finalRow The final row.
242  \param finalCol The final column.
243  \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.
244  \param sampleStep The row/column step used when reading pixels (to read all pixels sampleStep=1, to read half of pixels use sampleStep=2 );
245  \param maxThreads Maximum number of used threads to use (0-Automatic, 1-No threads used).
246  \param rHistogram The generated histogram (real).
247  \param iHistogram The generated histogram (imaginary).
248  \warning The caller is responsible for providing correct values for the range [rs, cs x rf, cf].
249  \return true if ok, false on errors
250  \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).
251  */
253  const te::rst::Raster& raster,
254  const unsigned int bandIndex,
255  const unsigned int rowStart,
256  const unsigned int colStart,
257  const unsigned int finalRow,
258  const unsigned int finalCol,
259  const unsigned int histoBins,
260  const unsigned int sampleStep,
261  const unsigned int maxThreads,
262  std::map<double, unsigned>& rHistogram,
263  std::map<double, unsigned>& iHistogram );
264 
265  /*!
266  \brief Compute statiscts from the given histogram.
267 
268  \param histogram Input histogram.
269  \param min Histogram minimun value.
270  \param max Histogram maximum value.
271  \param mean Histogram mean value.
272  \param stdDev Histogram standard deviation value.
273  */
275  const std::map<double, unsigned>& histogram,
276  double& min,
277  double& max,
278  double& mean,
279  double& stdDev );
280 
281  /*!
282  \brief Create a interpolated histogram from the input histogram with the new bins number.
283  \param inputHistogram Input histogram.
284  \param outputHistogram Output histogram.
285  \param outputBinsNumber Output Histogram bins number.
286  \param outputMinValue Output histogram minimum value.
287  \param outputMaxValue Output histogram maximum value.
288  \return true if ok, false on errors.
289  \note if both outputMinValue and outputMaxValue are zero they will be calculated from the input histogram.
290  */
292  const std::map<double, unsigned>& inputHistogram,
293  std::map<double, unsigned>& outputHistogram,
294  const unsigned int outputBinsNumber,
295  const double outputMinValue,
296  const double outputMaxValue );
297 
298  /*!
299  \brief Create a a new histogram with all missing integer values filled with zero frequencies.
300  \param inputHistogram Input histogram.
301  \param outputHistogram Output histogram.
302  */
304  const std::map<double, unsigned>& inputHistogram,
305  std::map<double, unsigned>& outputHistogram );
306 
307  /*!
308  \brief Resample a subset of the raster, given a box.
309  \param inputRaster Input raster.
310  \param inputRasterBands Input raster bands to process.
311  \param interpMethod The method of interpolation. \sa te::rst::Interpolator
312  \param interpWindowRadius Interpolator windows radius around the target pixel (when applicable).
313  \param firstRow The starting row to make a subset of the image.
314  \param firstColumn The starting column to make a subset of the image.
315  \param height The height of the subset.
316  \param width The width of the subset.
317  \param newheight The resampled height of the new raster.
318  \param newwidth The resampled width of the new raster.
319  \param maxThreads Maximum number of used threads to use (0-Automatic, 1-No threads used).
320  \param rinfo The parameters needed to build the output raster (see RasterFactory documentation).
321  \param dataSourceType Data source type (raster type. I.E. GDAL) or an empty string for de default driver.
322  \param resampledRasterPtr The resampled raster pointer.
323  \return true if ok, false on errors.
324  */
326  const te::rst::Raster& inputRaster,
327  const std::vector< unsigned int >& inputRasterBands,
328  const te::rst::Interpolator::Method interpMethod,
329  const unsigned int interpWindowRadius,
330  const unsigned int firstRow,
331  const unsigned int firstColumn,
332  const unsigned int height,
333  const unsigned int width,
334  const unsigned int newheight,
335  const unsigned int newwidth,
336  const unsigned int maxThreads,
337  const std::map<std::string, std::string>& rinfo,
338  const std::string& dataSourceType,
339  std::unique_ptr< te::rst::Raster >& resampledRasterPtr );
340 
341  } // end namespace rst
342 } // end namespace te
343 
344 #endif // __TERRALIB_RASTER_INTERNAL_UTILS_H
Raster.h
Raster implementaton for TerraLib 4.x.
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::rst::Band
A raster band description.
Definition: Band.h:64
te::rst::GetDataTypeRanges
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
te::rst::FillRaster
TERASTEREXPORT void FillRaster(te::rst::Raster *rin, const std::complex< double > &value)
Fill a Raster with provided value (all bands).
te::rst::PaletteInterpretation
PaletteInterpretation
Palette interpratation types.
Definition: Enums.h:81
te::rst::Raster
An abstract class for raster data strucutures.
Definition: Raster.h:72
te::rst::getHistograms
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.
te::rst::CreateCopy
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.
Grid.h
A rectified grid is the spatial support for raster data.
Band.h
Band implementation for TerraLib 4.x.
te::rst::GetPixelSize
TERASTEREXPORT int GetPixelSize(int datatype)
Returns the byte size of a given datatype.
te::rst::CropRaster
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.
te::rst::fillHistogramGaps
TERASTEREXPORT void fillHistogramGaps(const std::map< double, unsigned > &inputHistogram, std::map< double, unsigned > &outputHistogram)
Create a a new histogram with all missing integer values filled with zero frequencies.
te::gm::Polygon
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:51
te::rst::GetRandomPointsInRaster
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.
te::rst::Grid
A rectified grid is the spatial support for raster data.
Definition: Grid.h:69
te::rst::getHistogramStats
TERASTEREXPORT void getHistogramStats(const std::map< double, unsigned > &histogram, double &min, double &max, double &mean, double &stdDev)
Compute statiscts from the given histogram.
TERASTEREXPORT
#define TERASTEREXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:63
Interpolator.h
It interpolates one pixel based on a selected algorithm.
te::rst::InterpolationMethod
InterpolationMethod
Allowed interpolation methods.
Definition: Enums.h:93
te::rst::Round
TERASTEREXPORT int Round(double val)
Round a double value to a integer value.
Definition: Utils.h:118
te::rst::getRealHistogram
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.
te::rst::Copy
TERASTEREXPORT void Copy(const Raster &rin, Raster &rout)
Copies the pixel values from one raster to another.
te::rst::GetGrid
TERASTEREXPORT Grid * GetGrid(const std::map< std::string, std::string > &rinfo)
Returns a grid based on a given raster info.
te::rst::RasterPtr
boost::shared_ptr< Raster > RasterPtr
Definition: Raster.h:703
te::rst::rasterResample
TERASTEREXPORT bool rasterResample(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const te::rst::Interpolator::Method interpMethod, const unsigned int interpWindowRadius, const unsigned int firstRow, const unsigned int firstColumn, const unsigned int height, const unsigned int width, const unsigned int newheight, const unsigned int newwidth, const unsigned int maxThreads, const std::map< std::string, std::string > &rinfo, const std::string &dataSourceType, std::unique_ptr< te::rst::Raster > &resampledRasterPtr)
Resample a subset of the raster, given a box.
te::rst::ColorInterp
ColorInterp
Color model component use.
Definition: Enums.h:55
te::rst::GetBandProperties
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.
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
BandProperty.h
It describes one band (or dimension) of a raster.
te::rst::FillBand
TERASTEREXPORT void FillBand(te::rst::Band *bin, const std::complex< double > &value)
Fill a Raster Band with provided value.
te::rst::ConvertColorInterpTypeToString
TERASTEREXPORT std::string ConvertColorInterpTypeToString(const te::rst::ColorInterp &ci)
Function used to convert from a Color Interp Enum to a string.
te::rst::ConvertPalleteInterpTypeToString
TERASTEREXPORT std::string ConvertPalleteInterpTypeToString(const te::rst::PaletteInterpretation &pi)
Function used to convert from a Pallete Interp Enum to a string.
te::rst::interpolateHistogram
TERASTEREXPORT bool interpolateHistogram(const std::map< double, unsigned > &inputHistogram, std::map< double, unsigned > &outputHistogram, const unsigned int outputBinsNumber, const double outputMinValue, const double outputMaxValue)
Create a interpolated histogram from the input histogram with the new bins number.