All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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/rp/Functions.h
22  \brief Raster Processing functions.
23  \ingroup rp
24  */
25 
26 #ifndef __TERRALIB_RP_INTERNAL_FUNCTIONS_H
27 #define __TERRALIB_RP_INTERNAL_FUNCTIONS_H
28 
29 #include "Config.h"
30 
31 #include "Matrix.h"
32 #include "Macros.h"
33 #include "../dataaccess/datasource/DataSource.h"
34 #include "../raster/Raster.h"
35 #include "../raster/RasterFactory.h"
36 #include "../raster/Grid.h"
37 #include "../raster/BandProperty.h"
38 #include "../raster/RasterFactory.h"
39 
40 // Boost
41 #include <boost/numeric/ublas/lu.hpp>
42 #include <boost/numeric/ublas/matrix.hpp>
43 
44 // STL
45 #include <memory>
46 #include <map>
47 #include <vector>
48 #include <string>
49 #include <limits>
50 
51 namespace te
52 {
53  namespace rst
54  {
55  class BandProperty;
56  class Grid;
57  };
58 
59  namespace rp
60  {
61  class RasterHandler;
62 
63  /*!
64  \brief Create a new raster into the givem data source.
65  \param rasterGrid The template grid used to create the output raster.
66  \param bandsProperties The template band properties used to create the output raster.
67  \param outDataSetName The data set name to give to the created data set.
68  \param dataSourceType The data source type string (i.e. GDAL, MEM.).
69  \param outRasterHandler The created raster handler.
70  \return true if OK, false on errors.
71  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
72  */
73  bool TERPEXPORT CreateNewRaster( const te::rst::Grid& rasterGrid,
74  const std::vector< te::rst::BandProperty* >& bandsProperties,
75  const std::string& outDataSetName,
76  const std::string& dataSourceType,
77  RasterHandler& outRasterHandler );
78 
79  /*!
80  \brief Create a new raster into the givem data source.
81  \param rasterGrid The template grid used to create the output raster.
82  \param bandsProperties The template band properties used to create the output raster.
83  \param outDataSetName The data set name to give to the created data set.
84  \param outDataSource The data source to use when creating the output data set.
85  \param outRasterHandler The created raster handler.
86  \return true if OK, false on errors.
87  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
88  */
89  bool TERPEXPORT CreateNewRaster( const te::rst::Grid& rasterGrid,
90  const std::vector< te::rst::BandProperty* >& bandsProperties,
91  const std::string& outDataSetName,
92  te::da::DataSource& outDataSource,
93  RasterHandler& outRasterHandler );
94 
95  /*!
96  \brief Create a new raster into a new memory datasource.
97  \param rasterGrid The template grid used to create the output raster.
98  \param bandsProperties The template band properties used to create the output raster.
99  \param outRasterHandler The created raster handler.
100  \return true if OK, false on errors.
101  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
102  */
103  bool TERPEXPORT CreateNewMemRaster( const te::rst::Grid& rasterGrid,
104  std::vector< te::rst::BandProperty* > bandsProperties,
105  RasterHandler& outRasterHandler );
106 
107  /*!
108  \brief Create a new raster into a GDAL datasource.
109  \param rasterGrid The template grid used to create the output raster.
110  \param bandsProperties The template band properties used to create the output raster.
111  \param fileName The output tif file name.
112  \param outRasterHandler The created raster handler.
113  \return true if OK, false on errors.
114  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
115  */
116  bool TERPEXPORT CreateNewGdalRaster( const te::rst::Grid& rasterGrid,
117  std::vector< te::rst::BandProperty* > bandsProperties,
118  const std::string& fileName,
119  RasterHandler& outRasterHandler );
120 
121  /*!
122  \brief Returns the real data type range (all values that can be represented by the given data type).
123  \param dataType The data type.
124  \param min The minimum value.
125  \param max The maximum value.
126  \note The types are listed in terralib/datatype/DataTypes.h
127  */
128  void TERPEXPORT GetDataTypeRange( const int dataType, double& min, double& max );
129 
130  /*!
131  \brief Convert vector elements.
132  \param inputVector Input vector.
133  \param inputVectorDataType Input vector data type.
134  \param inputVectorSize The numer of input vector elements.
135  \param outputVector A pré-allocated output vector.
136  \note The types are listed in terralib/datatype/DataTypes.h
137  */
138  void TERPEXPORT Convert2DoublesVector( void* inputVector, const int inputVectorDataType,
139  unsigned int inputVectorSize, double* outputVector );
140 
141  /*!
142  \brief Convert a doubles vector.
143  \param inputVector Input vector.
144  \param inputVectorSize The numer of input vector elements.
145  \param inputVectorDataType Input vector data type.
146  \param outputVector A pré-allocated output vector.
147  \note The types are listed in terralib/datatype/DataTypes.h
148  */
149  void TERPEXPORT ConvertDoublesVector( double* inputVector,
150  unsigned int inputVectorSize, const int outputVectorDataType,
151  void* outputVector );
152 
153  /*!
154  \brief Create a tiff file from a matrix.
155  \param matrix The matrix.
156  \param normalize Enable/disable pixel normalization (8bit);
157  \param tifFileName Tif file name.
158  \param return true if OK, false on errors.
159  */
160  template< typename MatrixElementT >
162  const bool normalize, const std::string& fileName )
163  {
164  std::map<std::string, std::string> rInfo;
165  rInfo["URI"] = fileName;
166 
167  std::vector<te::rst::BandProperty*> bandsProperties;
168  if( normalize )
169  bandsProperties.push_back(new te::rst::BandProperty( 0, te::dt::UCHAR_TYPE, "" ));
170  else
171  bandsProperties.push_back(new te::rst::BandProperty( 0, te::dt::DOUBLE_TYPE, "" ));
172  bandsProperties[0]->m_colorInterp = te::rst::GrayIdxCInt;
173  bandsProperties[0]->m_noDataValue = -1.0 * std::numeric_limits<double>::max();
174 
175  te::rst::Grid* newgrid = new te::rst::Grid( matrix.getColumnsNumber(),
176  matrix.getLinesNumber(), 0, -1 );
177 
178  std::auto_ptr< te::rst::Raster > outputRasterPtr(
179  te::rst::RasterFactory::make( "GDAL", newgrid, bandsProperties, rInfo, 0, 0));
180  TERP_TRUE_OR_RETURN_FALSE( outputRasterPtr.get(), "Output raster creation error");
181 
182  unsigned int line = 0;
183  unsigned int col = 0;
184  const unsigned int nLines = matrix.getLinesNumber();
185  const unsigned int nCols = matrix.getColumnsNumber();
186  MatrixElementT matrixValue = 0;
187 
188  MatrixElementT gain = 1.0;
189  MatrixElementT offset = 0.0;
190  if( normalize )
191  {
192  MatrixElementT matrixValueMin = std::numeric_limits< MatrixElementT >::max();
193  MatrixElementT matrixValueMax = -1.0 * matrixValueMin;
194  for( line = 0 ; line < nLines ; ++line )
195  {
196  for( col = 0 ; col < nCols ; ++col )
197  {
198  matrixValue = matrix[ line ][ col ];
199  if( matrixValue < matrixValueMin )
200  matrixValueMin = matrixValue;
201  if( matrixValue > matrixValueMax )
202  matrixValueMax = matrixValue;
203  }
204  }
205 
206  if( matrixValueMax == matrixValueMin )
207  {
208  gain = 0.0;
209  offset = 0.0;
210  }
211  else
212  {
213  gain = 255.0 / ( matrixValueMax - matrixValueMin );
214  offset = -1.0 * ( matrixValueMin );
215  }
216  }
217 
218  const MatrixElementT min0 = 0;
219  const MatrixElementT max255 = 255;
220 
221  for( line = 0 ; line < nLines ; ++line )
222  {
223  for( col = 0 ; col < nCols ; ++col )
224  {
225  matrixValue = matrix[ line ][ col ];
226 
227  if( normalize )
228  {
229  matrixValue += offset;
230  matrixValue *= gain;
231  matrixValue = std::max( min0, matrixValue );
232  matrixValue = std::min( max255, matrixValue );
233  }
234 
235  outputRasterPtr->setValue( col, line, (double)matrixValue, 0 );
236  }
237  }
238 
239  return true;
240  };
241 
242  /*! \brief Returns a vector os with band's names. */
243  TERPEXPORT std::vector<std::string> GetBandNames();
244 
245  /*! \brief Returns the maximun and minimum reflectance values of a given sensor/band. */
246  TERPEXPORT std::pair<double, double> GetSpectralBandInfo(std::string bandName);
247 
248  /*! \brief Returns the minimum reflectance value of a given sensor/band. */
249  TERPEXPORT double GetSpectralBandMin(std::string bandName);
250 
251  /*! \brief Returns the maximum reflectance value of a given sensor/band. */
252  TERPEXPORT double GetSpectralBandMax(std::string bandName);
253 
254  /*! \brief Returns the maximun and minimum digital numbers of a given sensor/band. */
255  TERPEXPORT std::pair<double, double> GetDigitalNumberBandInfo(std::string bandName);
256 
257  /*! \brief Returns the maximum digital number of a given sensor/band. */
258  TERPEXPORT double GetDigitalNumberBandMax(std::string bandName);
259 
260  /*!
261  \brief Normalizes one raster in a given interval.
262 
263  \param inputRaster The given raster.
264  \param nmin The new minimum value (default = 0.0).
265  \param nmax The new maximum value (default = 255.0).
266 
267  \return true if normalization occurs and false otherwise.
268  */
269  TERPEXPORT bool NormalizeRaster(te::rst::Raster& inputRaster, double nmin = 0.0, double nmax = 255.0);
270 
271  /*!
272  \brief Creates a vector of random positions (points) inside the raster.
273 
274  \param inputRaster The given raster.
275  \param numberOfPoints The number of random positions to be created (default = 1000).
276 
277  \return A vector of random positions (points).
278  */
279  TERPEXPORT std::vector<te::gm::Point*> GetRandomPointsInRaster(const te::rst::Raster& inputRaster, unsigned int numberOfPoints = 1000);
280 
281  /*!
282  \brief RGB to IHS conversion.
283  \param inputRGBRaster The input raster.
284  \param redBandIdx The red band index.
285  \param greenBandIdx The red band index.
286  \param blueBandIdx The red band index.
287  \param rgbRangeMin The minimum RGB value.
288  \param rgbRangeMax The maximum RGB value.
289  \param outputIHSRaster The output IHS raster.
290  \return true if OK, false on errors.
291  \note IHS data with the following channels ranges: I:[0,1] H:[0,2pi] (radians) S:[0,1].
292  */
293  TERPEXPORT bool ConvertRBG2IHS( const te::rst::Raster& inputRGBRaster,
294  const unsigned int redBandIdx, const unsigned int greenBandIdx,
295  const unsigned int blueBandIdx, const double rgbRangeMin,
296  const double rgbRangeMax, te::rst::Raster& outputIHSRaster );
297 
298  /*!
299  \brief IHS to RGB conversion.
300  \param inputIHSRaster The input raster.
301  \param intensityBandIdx The intensity band index.
302  \param hueBandIdx The hue band index.
303  \param saturationBandIdx The saturation band index.
304  \param rgbRangeMin The minimum RGB value.
305  \param rgbRangeMax The maximum RGB value.
306  \param outputRGBRaster The output RGB raster.
307  \return true if OK, false on errors.
308  \note IHS data with the following channels ranges: I:[0,1] H:[0,2pi] (radians) S:[0,1].
309  */
310  TERPEXPORT bool ConvertIHS2RGB( const te::rst::Raster& inputIHSRaster,
311  const unsigned int intensityBandIdx, const unsigned int hueBandIdx,
312  const unsigned int saturationBandIdx, const double rgbRangeMin,
313  const double rgbRangeMax, te::rst::Raster& outputRGBRaster );
314 
315  } // end namespace rp
316 } // end namespace te
317 
318 #endif // __TERRALIB_RP_INTERNAL_FUNCTIONS_H
319 
Generic template matrix.
std::vector< te::gm::Point * > GetRandomPointsInRaster(const te::rst::Raster &inputRaster, unsigned int numberOfPoints)
Creates a vector of random positions (points) inside the raster.
Definition: Functions.cpp:700
bool NormalizeRaster(te::rst::Raster &inraster, double nmin, double nmax)
Normalizes one raster in a given interval.
Definition: Functions.cpp:656
double GetSpectralBandMax(std::string bandName)
Returns the maximum reflectance value of a given sensor/band.
Definition: Functions.cpp:601
static Raster * make()
It creates and returns an empty raster with default raster driver.
unsigned int getLinesNumber() const
The number of current matrix lines.
Definition: Matrix.h:665
double GetDigitalNumberBandMax(std::string bandName)
Returns the maximum digital number of a given sensor/band.
Definition: Functions.cpp:651
bool ConvertRBG2IHS(const te::rst::Raster &inputRGBRaster, const unsigned int redBandIdx, const unsigned int greenBandIdx, const unsigned int blueBandIdx, const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster &outputIHSRaster)
RGB to IHS conversion.
Definition: Functions.cpp:719
void Convert2DoublesVector(void *inputVector, const int inputVectorDataType, unsigned int inputVectorSize, double *outputVector)
Convert vector elements.
Definition: Functions.cpp:286
#define TERP_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged and a return of context wi...
Definition: Macros.h:183
double GetSpectralBandMin(std::string bandName)
Returns the minimum reflectance value of a given sensor/band.
Definition: Functions.cpp:596
A rectified grid is the spatial support for raster data.
Definition: Grid.h:55
std::pair< double, double > GetSpectralBandInfo(std::string bandName)
Returns the maximun and minimum reflectance values of a given sensor/band.
Definition: Functions.cpp:551
bool CreateNewGdalRaster(const te::rst::Grid &rasterGrid, std::vector< te::rst::BandProperty * > bandsProperties, const std::string &fileName, RasterHandler &outRasterHandler)
Create a new raster into a GDAL datasource.
Definition: Functions.cpp:183
std::vector< std::string > GetBandNames()
Returns a vector os with band&#39;s names.
Definition: Functions.cpp:511
bool CreateNewRaster(const te::rst::Grid &rasterGrid, const std::vector< te::rst::BandProperty * > &bandsProperties, const std::string &outDataSetName, const std::string &dataSourceType, RasterHandler &outRasterHandler)
Create a new raster into the givem data source.
Definition: Functions.cpp:67
bool CreateNewMemRaster(const te::rst::Grid &rasterGrid, std::vector< te::rst::BandProperty * > bandsProperties, RasterHandler &outRasterHandler)
Create a new raster into a new memory datasource.
Definition: Functions.cpp:172
std::pair< double, double > GetDigitalNumberBandInfo(std::string bandName)
Returns the maximun and minimum digital numbers of a given sensor/band.
Definition: Functions.cpp:606
Configuration flags for the Raster Processing module of TerraLib.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:116
bool CreateRasterFileFromMatrix(const te::rp::Matrix< MatrixElementT > &matrix, const bool normalize, const std::string &fileName)
Create a tiff file from a matrix.
Definition: Functions.h:161
A raster band description.
Definition: BandProperty.h:61
A generic template matrix.
Definition: Matrix.h:50
void GetDataTypeRange(const int dataType, double &min, double &max)
Returns the real data type range (all values that can be represented by the given data type)...
Definition: Functions.cpp:228
unsigned int getColumnsNumber() const
The number of current matrix columns.
Definition: Matrix.h:672
An abstract class for raster data strucutures.
Definition: Raster.h:70
void ConvertDoublesVector(double *inputVector, unsigned int inputVectorSize, const int outputVectorDataType, void *outputVector)
Convert a doubles vector.
Definition: Functions.cpp:398
Index into a lookup table.
Definition: Enums.h:57
bool ConvertIHS2RGB(const te::rst::Raster &inputIHSRaster, const unsigned int intensityBandIdx, const unsigned int hueBandIdx, const unsigned int saturationBandIdx, const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster &outputRGBRaster)
IHS to RGB conversion.
Definition: Functions.cpp:847
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:91