Functions.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/rp/Functions.h
22  \brief Raster Processing functions.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_FUNCTIONS_H
26 #define __TERRALIB_RP_INTERNAL_FUNCTIONS_H
27 
28 #include "Config.h"
29 
30 #include "FeedersRaster.h"
31 #include "Matrix.h"
32 #include "Macros.h"
33 #include "RasterHandler.h"
34 #include "../dataaccess/datasource/DataSource.h"
35 #include "../raster/Raster.h"
36 #include "../raster/RasterFactory.h"
37 #include "../raster/Grid.h"
38 #include "../raster/BandProperty.h"
39 #include "../raster/RasterFactory.h"
40 #include "../raster/Interpolator.h"
41 #include "../raster/Utils.h"
42 #include "../srs/Converter.h"
43 #include "../geometry/LinearRing.h"
44 #include "../geometry/GTParameters.h"
45 #include "../geometry/MultiPoint.h"
46 #include "../geometry/Surface.h"
47 
48 // STL
49 #include <memory>
50 #include <map>
51 #include <vector>
52 #include <string>
53 #include <limits>
54 
55 // Boost
56 #include <boost/numeric/ublas/matrix.hpp>
57 #include <boost/shared_ptr.hpp>
58 
59 namespace te
60 {
61  namespace rst
62  {
63  class BandProperty;
64  class Grid;
65  }
66 
67  namespace rp
68  {
69  /*!
70  \brief Wavelet Atrous Filter types.
71  \ingroup rp_func
72  */
74  {
75  InvalidWAFilter, //!< Invalid filter type
76  B3SplineWAFilter, //!< Spline filter type.
77  TriangleWAFilter //!< Triangle filter type.
78  };
79 
81  {
83 
84  SpectralSensorParams(const int &band, const double &lower, const double &upper, const double &min, const double &max) :
85  m_band(band),
86  m_lower(lower),
87  m_upper(upper),
88  m_min(min),
89  m_max(max)
90  {}
91 
92  int m_band;
93  double m_lower;
94  double m_upper;
95  double m_min;
96  double m_max;
97  };
98 
99  /*!
100  \brief Create a new raster into the givem data source.
101  \param rasterGrid The template grid used to create the output raster.
102  \param bandsProperties The template band properties used to create the output raster.
103  \param outDataSetName The data set name to give to the created data set.
104  \param dataSourceType The data source type string (i.e. GDAL, MEM.)( See te::rst::RasterFactory dictionary for valid registered values ).
105  \param outRasterHandler The created raster handler.
106  \return true if OK, false on errors.
107  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
108  \ingroup rp_func
109  */
110  bool TERPEXPORT CreateNewRaster( const te::rst::Grid& rasterGrid,
111  const std::vector< te::rst::BandProperty* >& bandsProperties,
112  const std::string& outDataSetName,
113  const std::string& dataSourceType,
114  RasterHandler& outRasterHandler );
115 
116  /*!
117  \brief Create a new raster into the givem data source.
118  \param rasterGrid The template grid used to create the output raster.
119  \param bandsProperties The template band properties used to create the output raster.
120  \param outDataSetName The data set name to give to the created data set.
121  \param outDataSource The data source to use when creating the output data set.
122  \param outRasterHandler The created raster handler.
123  \return true if OK, false on errors.
124  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
125  \ingroup rp_func
126  */
127  bool TERPEXPORT CreateNewRaster( const te::rst::Grid& rasterGrid,
128  const std::vector< te::rst::BandProperty* >& bandsProperties,
129  const std::string& outDataSetName,
130  te::da::DataSource& outDataSource,
131  RasterHandler& outRasterHandler );
132 
133  /*!
134  \brief Create a new raster into the givem data source.
135  \param rasterGrid The template grid used to create the output raster.
136  \param bandsProperties The template band properties used to create the output raster.
137  \param rasterInfo The output raster info (specific driver info).
138  \param rasterType The output raster type (specific driver name - See te::rst::RasterFactory dictorary for more info).
139  \param outRasterPtr The created raster pointer.
140  \return true if OK, false on errors.
141  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
142  \ingroup rp_func
143  */
144  bool TERPEXPORT CreateNewRaster( const te::rst::Grid& rasterGrid,
145  const std::vector< te::rst::BandProperty* >& bandsProperties,
146  const std::map< std::string, std::string>& rasterInfo,
147  const std::string& rasterType,
148  std::unique_ptr< te::rst::Raster >& outRasterPtr );
149 
150  /*!
151  \brief Create a new raster into a new memory datasource.
152  \param rasterGrid The template grid used to create the output raster.
153  \param bandsProperties The template band properties used to create the output raster.
154  \param outRasterHandler The created raster handler.
155  \return true if OK, false on errors.
156  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
157  \ingroup rp_func
158  */
159  bool TERPEXPORT CreateNewMemRaster( const te::rst::Grid& rasterGrid,
160  std::vector< te::rst::BandProperty* > bandsProperties,
161  RasterHandler& outRasterHandler );
162 
163  /*!
164  \brief Create a new raster into a GDAL datasource.
165  \param rasterGrid The template grid used to create the output raster.
166  \param bandsProperties The template band properties used to create the output raster.
167  \param fileName The output tif file name.
168  \param outRasterHandler The created raster handler.
169  \return true if OK, false on errors.
170  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
171  \ingroup rp_func
172  */
173  bool TERPEXPORT CreateNewGdalRaster( const te::rst::Grid& rasterGrid,
174  std::vector< te::rst::BandProperty* > bandsProperties,
175  const std::string& fileName,
176  RasterHandler& outRasterHandler );
177 
178  /*!
179  \brief Create a new raster into a GDAL datasource.
180  \param rasterGrid The template grid used to create the output raster.
181  \param bandsProperties The template band properties used to create the output raster.
182  \param fileName The output tif file name.
183  \param outRasterPtr The created raster pointer.
184  \return true if OK, false on errors.
185  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
186  \ingroup rp_func
187  */
188  bool TERPEXPORT CreateNewGdalRaster( const te::rst::Grid& rasterGrid,
189  std::vector< te::rst::BandProperty* > bandsProperties,
190  const std::string& fileName,
191  std::unique_ptr< te::rst::Raster >& outRasterPtr );
192 
193  /*!
194  \brief Create a new raster into a GDAL datasource.
195  \param rasterGrid The template grid used to create the output raster.
196  \param bandsProperties The template band properties used to create the output raster.
197  \param fileName The output tif file name.
198  \param outRasterHandler The created raster handler.
199  \return true if OK, false on errors.
200  \note All bandsProperties pointed objects will be acquired by this function and must not be deleted.
201  \ingroup rp_func
202  */
204  const te::rst::Raster& inputRaster,
205  const std::string& fileName );
206 
207  /*!
208  \brief Returns the real data type range (all values that can be represented by the given data type).
209  \param dataType The data type.
210  \param min The minimum value.
211  \param max The maximum value.
212  \note The types are listed in terralib/datatype/DataTypes.h
213  \ingroup rp_func
214  */
215  inline void TERPEXPORT GetDataTypeRange( const int dataType, double& min, double& max )
216  {
217  te::rst::GetDataTypeRanges( dataType, min, max );
218  }
219 
220  /*!
221  \brief Convert vector elements.
222  \param inputVector Input vector.
223  \param inputVectorDataType Input vector data type.
224  \param inputVectorSize The numer of input vector elements.
225  \param outputVector A pré-allocated output vector.
226  \note The types are listed in terralib/datatype/DataTypes.h
227  \ingroup rp_func
228  */
229  void TERPEXPORT Convert2DoublesVector( void* inputVector, const int inputVectorDataType,
230  unsigned int inputVectorSize, double* outputVector );
231 
232  /*!
233  \brief Convert a doubles vector.
234  \param inputVector Input vector.
235  \param inputVectorSize The numer of input vector elements.
236  \param inputVectorDataType Input vector data type.
237  \param outputVector A pré-allocated output vector.
238  \note The types are listed in terralib/datatype/DataTypes.h
239  \ingroup rp_func
240  */
241  void TERPEXPORT ConvertDoublesVector( double* inputVector,
242  unsigned int inputVectorSize, const int outputVectorDataType,
243  void* outputVector );
244 
245  /*!
246  \brief Create a tiff file from a matrix.
247  \param matrix The matrix.
248  \param normalize Enable/disable pixel normalization (8bit);
249  \param tifFileName Tif file name.
250  \param return true if OK, false on errors.
251  \ingroup rp_func
252  */
253  template< typename MatrixElementT >
255  const bool normalize, const std::string& fileName )
256  {
257  std::map<std::string, std::string> rInfo;
258  rInfo["URI"] = fileName;
259 
260  std::vector<te::rst::BandProperty*> bandsProperties;
261  if( normalize )
262  bandsProperties.push_back(new te::rst::BandProperty( 0, te::dt::UCHAR_TYPE, "" ));
263  else
264  bandsProperties.push_back(new te::rst::BandProperty( 0, te::dt::DOUBLE_TYPE, "" ));
265  bandsProperties[0]->m_colorInterp = te::rst::GrayIdxCInt;
266  bandsProperties[0]->m_noDataValue = -1.0 * std::numeric_limits<double>::max();
267 
268  te::rst::Grid* newgrid = new te::rst::Grid( matrix.getColumnsNumber(),
269  matrix.getLinesNumber(), 0, -1 );
270 
271  std::unique_ptr< te::rst::Raster > outputRasterPtr(
272  te::rst::RasterFactory::make( "GDAL", newgrid, bandsProperties, rInfo, 0, 0));
273  TERP_TRUE_OR_RETURN_FALSE( outputRasterPtr.get(), "Output raster creation error");
274 
275  unsigned int line = 0;
276  unsigned int col = 0;
277  const unsigned int nLines = matrix.getLinesNumber();
278  const unsigned int nCols = matrix.getColumnsNumber();
279  MatrixElementT matrixValue = 0;
280 
281  MatrixElementT gain = 1.0;
282  MatrixElementT offset = 0.0;
283  if( normalize )
284  {
285  MatrixElementT matrixValueMin = std::numeric_limits< MatrixElementT >::max();
286  MatrixElementT matrixValueMax = -1.0 * matrixValueMin;
287  for( line = 0 ; line < nLines ; ++line )
288  {
289  for( col = 0 ; col < nCols ; ++col )
290  {
291  matrixValue = matrix[ line ][ col ];
292  if( matrixValue < matrixValueMin )
293  matrixValueMin = matrixValue;
294  if( matrixValue > matrixValueMax )
295  matrixValueMax = matrixValue;
296  }
297  }
298 
299  if( matrixValueMax == matrixValueMin )
300  {
301  gain = 0.0;
302  offset = 0.0;
303  }
304  else
305  {
306  gain = 255.0 / ( matrixValueMax - matrixValueMin );
307  offset = -1.0 * ( matrixValueMin );
308  }
309  }
310 
311  const MatrixElementT min0 = 0;
312  const MatrixElementT max255 = 255;
313 
314  for( line = 0 ; line < nLines ; ++line )
315  {
316  for( col = 0 ; col < nCols ; ++col )
317  {
318  matrixValue = matrix[ line ][ col ];
319 
320  if( normalize )
321  {
322  matrixValue += offset;
323  matrixValue *= gain;
324  matrixValue = std::max( min0, matrixValue );
325  matrixValue = std::min( max255, matrixValue );
326  }
327 
328  outputRasterPtr->setValue( col, line, static_cast<double>(matrixValue), 0 );
329  }
330  }
331 
332  return true;
333  }
334 
335  /*!
336  \brief Returns a json filename with spectral sensors parameters.
337  \ingroup rp_func
338  */
339  TERPEXPORT std::string GetSensorFilename();
340 
341  /*!
342  \brief Returns a vector os with band's names.
343  \ingroup rp_func
344  */
345  TERPEXPORT std::vector<std::string> GetBandNames();
346 
347  /*!
348  \brief Returns a map with spectral sensors parameters defined in SpectralSensor.json file.
349  \ingroup rp_func
350  */
351  TERPEXPORT std::map<std::string, SpectralSensorParams > getSensorParams();
352 
353  /*!
354  \brief Saves in SpectralSensor.json file the spectral sensors parameters.
355  \ingroup rp_func
356  */
357  TERPEXPORT void SaveSensorParams(std::map<std::string, SpectralSensorParams > &);
358 
359  /*!
360  \brief Returns the maximun and minimum reflectance values of a given sensor/band.
361  \ingroup rp_func
362  */
364 
365  /*!
366  \brief Returns the minimum reflectance value of a given sensor/band.
367  \ingroup rp_func
368  */
369  TERPEXPORT double GetSpectralBandMin(std::string bandName);
370 
371  /*!
372  \brief Returns the maximum reflectance value of a given sensor/band.
373  \ingroup rp_func
374  */
375  TERPEXPORT double GetSpectralBandMax(std::string bandName);
376 
377  /*!
378  \brief Returns the maximun and minimum digital numbers of a given sensor/band.
379  \ingroup rp_func
380  */
381  TERPEXPORT std::pair<double, double> GetDigitalNumberBandInfo(std::string bandName);
382 
383  /*!
384  \brief Returns the maximum digital number of a given sensor/band.
385  \ingroup rp_func
386  */
387  TERPEXPORT double GetDigitalNumberBandMax(std::string bandName);
388 
389  /*!
390  \brief Returns the minimum digital number of a given sensor/band.
391  \ingroup rp_func
392  */
393  TERPEXPORT double GetDigitalNumberBandMin(std::string bandName);
394 
395  /*!
396  \brief Normalizes one raster in a given interval.
397 
398  \param inputRaster The given raster.
399  \param nmin The new minimum value (default = 0.0).
400  \param nmax The new maximum value (default = 255.0).
401 
402  \return true if normalization occurs and false otherwise.
403  \ingroup rp_func
404  */
405  TERPEXPORT bool NormalizeRaster(te::rst::Raster& inputRaster, double nmin = 0.0, double nmax = 255.0);
406 
407  /*!
408  \brief RGB to IHS conversion.
409  \param inputRGBRaster The input raster.
410  \param redBandIdx The red band index.
411  \param greenBandIdx The red band index.
412  \param blueBandIdx The red band index.
413  \param rgbRangeMin The minimum RGB value.
414  \param rgbRangeMax The maximum RGB value.
415  \param outputIHSRaster An output pré-initiated raster (with the same dimensions of inputRGBRaster) where the IHS data will be written (double or float as the data type).
416  \return true if OK, false on errors.
417  \note The outputIHSRaster mas have a float or double data type.
418  \note IHS data with the following channels ranges: I:[0,1] H:[0,2pi] (radians) S:[0,1].
419  \ingroup rp_func
420  */
421  TERPEXPORT bool ConvertRGB2IHS( const te::rst::Raster& inputRGBRaster,
422  const unsigned int redBandIdx, const unsigned int greenBandIdx,
423  const unsigned int blueBandIdx, const double rgbRangeMin,
424  const double rgbRangeMax, te::rst::Raster& outputIHSRaster );
425 
426  /*!
427  \brief RGB to IHS conversion.
428  \param inputRedRaster The input red raster.
429  \param redBandIdx The red band index.
430  \param inputGreenRaster The input green raster.
431  \param greenBandIdx The green band index.
432  \param inputBlueRaster The input blue raster.
433  \param blueBandIdx The blue band index.
434  \param rgbRangeMin The minimum RGB value.
435  \param rgbRangeMax The maximum RGB value.
436  \param outputIHSRaster An output pré-initiated raster (with the same dimensions of inputRGBRaster) where the IHS data will be written (double or float as the data type).
437  \return true if OK, false on errors.
438  \note The outputIHSRaster mas have a float or double data type.
439  \note IHS data with the following channels ranges: I:[0,1] H:[0,2pi] (radians) S:[0,1].
440  \ingroup rp_func
441  */
442  TERPEXPORT bool ConvertRGB2IHS(const te::rst::Raster& inputRedRaster, const unsigned int redBandIdx,
443  const te::rst::Raster& inputGreenRaster, const unsigned int greenBandIdx,
444  const te::rst::Raster& inputBlueRaster, const unsigned int blueBandIdx,
445  const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster& outputIHSRaster);
446 
447  /*!
448  \brief IHS to RGB conversion.
449  \param inputIHSRaster The input raster.
450  \param intensityBandIdx The intensity band index.
451  \param hueBandIdx The hue band index.
452  \param saturationBandIdx The saturation band index.
453  \param rgbRangeMin The minimum RGB value.
454  \param rgbRangeMax The maximum RGB value.
455  \param outputRGBRaster An output pré-initiated raster (with the same dimensions of inputIHSRaster) where the RGB data will be written.
456  \return true if OK, false on errors.
457  \note IHS data with the following channels ranges: I:[0,1] H:[0,2pi] (radians) S:[0,1].
458  \ingroup rp_func
459  */
460  TERPEXPORT bool ConvertIHS2RGB( const te::rst::Raster& inputIHSRaster,
461  const unsigned int intensityBandIdx, const unsigned int hueBandIdx,
462  const unsigned int saturationBandIdx, const double rgbRangeMin,
463  const double rgbRangeMax, te::rst::Raster& outputRGBRaster );
464 
465  /*!
466  \brief IHS to RGB conversion.
467  \param inputIRaster The input intensity raster.
468  \param intensityBandIdx The intensity band index.
469  \param inputHRaster The input hue raster.
470  \param hueBandIdx The hue band index.
471  \param inputSRaster The input saturation raster.
472  \param saturationBandIdx The saturation band index.
473  \param rgbRangeMin The minimum RGB value.
474  \param rgbRangeMax The maximum RGB value.
475  \param outputRGBRaster An output pré-initiated raster (with the same dimensions of inputIHSRaster) where the RGB data will be written.
476  \return true if OK, false on errors.
477  \note IHS data with the following channels ranges: I:[0,1] H:[0,2pi] (radians) S:[0,1].
478  \ingroup rp_func
479  */
480  TERPEXPORT bool ConvertIHS2RGB(const te::rst::Raster& inputIRaster, const unsigned int intensityBandIdx,
481  const te::rst::Raster& inputHRaster, const unsigned int hueBandIdx,
482  const te::rst::Raster& inputSRaster, const unsigned int saturationBandIdx,
483  const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster& outputRGBRaster);
484 
485  /*!
486  \brief RGB to HLS conversion.
487  \param inputRGBRaster The input raster.
488  \param redBandIdx The red band index.
489  \param greenBandIdx The red band index.
490  \param blueBandIdx The red band index.
491  \param rgbRangeMin The minimum RGB value.
492  \param rgbRangeMax The maximum RGB value.
493  \param outputHLSRaster An output pré-initiated raster (with the same dimensions of inputRGBRaster) where the HLS data will be written (double or float as the data type).
494  \return true if OK, false on errors.
495  \note The outputHLSRaster mas have a float or double data type.
496  \ingroup rp_func
497  */
498  TERPEXPORT bool ConvertRGB2HLS(const te::rst::Raster& inputRGBRaster,
499  const unsigned int redBandIdx, const unsigned int greenBandIdx,
500  const unsigned int blueBandIdx, const double rgbRangeMin,
501  const double rgbRangeMax, te::rst::Raster& outputHLSRaster);
502 
503  /*!
504  \brief RGB to HLS conversion.
505  \param inputRedRaster The input red raster.
506  \param redBandIdx The red band index.
507  \param inputGreenRaster The input green raster.
508  \param greenBandIdx The green band index.
509  \param inputBlueRaster The input blue raster.
510  \param blueBandIdx The blue band index.
511  \param rgbRangeMin The minimum RGB value.
512  \param rgbRangeMax The maximum RGB value.
513  \param outputHLSRaster An output pré-initiated raster (with the same dimensions of inputRGBRaster) where the HLS data will be written (double or float as the data type).
514  \return true if OK, false on errors.
515  \note The outputHLSRaster mas have a float or double data type.
516  \ingroup rp_func
517  */
518  TERPEXPORT bool ConvertRGB2HLS(const te::rst::Raster& inputRedRaster, const unsigned int redBandIdx,
519  const te::rst::Raster& inputGreenRaster, const unsigned int greenBandIdx,
520  const te::rst::Raster& inputBlueRaster, const unsigned int blueBandIdx,
521  const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster& outputHLSRaster);
522 
523  /*!
524  \brief HLS to RGB conversion.
525  \param inputHLSRaster The input raster.
526  \param hueBandIdx The hue band index.
527  \param lightBandIdx The light band index.
528  \param saturationBandIdx The saturation band index.
529  \param rgbRangeMin The minimum RGB value.
530  \param rgbRangeMax The maximum RGB value.
531  \param outputRGBRaster An output pré-initiated raster (with the same dimensions of inputHLSRaster) where the RGB data will be written.
532  \return true if OK, false on errors.
533  \ingroup rp_func
534  */
535  TERPEXPORT bool ConvertHLS2RGB(const te::rst::Raster& inputHLSRaster,
536  const unsigned int hueBandIdx, const unsigned int lightBandIdx,
537  const unsigned int saturationBandIdx, const double rgbRangeMin,
538  const double rgbRangeMax, te::rst::Raster& outputRGBRaster);
539 
540  /*!
541  \brief HLS to RGB conversion.
542  \param inputHRaster The input hue raster.
543  \param hueBandIdx The hue band index.
544  \param inputLRaster The input light raster.
545  \param lightBandIdx The light band index.
546  \param inputSRaster The input saturation raster.
547  \param saturationBandIdx The saturation band index.
548  \param rgbRangeMin The minimum RGB value.
549  \param rgbRangeMax The maximum RGB value.
550  \param outputRGBRaster An output pré-initiated raster (with the same dimensions of inputHLSRaster) where the RGB data will be written.
551  \return true if OK, false on errors.
552  \ingroup rp_func
553  */
554  TERPEXPORT bool ConvertHLS2RGB(const te::rst::Raster& inputHRaster, const unsigned int hueBandIdx,
555  const te::rst::Raster& inputLRaster, const unsigned int lightBandIdx,
556  const te::rst::Raster& inputSRaster, const unsigned int saturationBandIdx,
557  const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster& outputRGBRaster);
558 
559  /*!
560  \brief Get the mean of band pixel values.
561  \param band The input raster band.
562  \param maxThreads The maximum number of threads to use (0-auto, 1-single thread used).
563  \param meanValue The calculated mean value.
564  \return true if OK, false on errors.
565  \note Optimized for rasters where the used bands have the same blocking scheme.
566  \ingroup rp_func
567  */
568  TERPEXPORT bool GetMeanValue(
569  const te::rst::Band& band,
570  const unsigned int maxThreads,
571  double& meanValue );
572 
573  /*!
574  \brief Get the standard deviation of band pixel values.
575  \param band The input raster band.
576  \param maxThreads The maximum number of threads to use (0-auto, 1-single thread used).
577  \param meanValuePtr A pointer to a pre-calculated band mean value or zero if it does not exist.
578  \param stdDevValue The calculated standard deviation value.
579  \return true if OK, false on errors.
580  \note Optimized for rasters where the used bands have the same blocking scheme.
581  \ingroup rp_func
582  */
584  const te::rst::Band& band,
585  const unsigned int maxThreads,
586  double const * const meanValuePtr,
587  double& stdDevValue );
588 
589  /*!
590  \brief Get the covariance of band pixel values.
591  \param band1 The input band 1.
592  \param band2 The input band 2.
593  \param maxThreads The maximum number of threads to use (0-auto, 1-single thread used).
594  \param mean1ValuePtr A pointer to a pre-calculated band1 mean value or zero if it does not exist.
595  \param mean2ValuePtr A pointer to a pre-calculated band2 mean value or zero if it does not exist.
596  \param covarianceValue The calculated covariance value.
597  \return true if OK, false on errors.
598  \note Optimized for rasters where the used bands have the same blocking scheme.
599  \ingroup rp_func
600  */
602  const te::rst::Band& band1,
603  const te::rst::Band& band2,
604  const unsigned int maxThreads,
605  double const * const mean1ValuePtr,
606  double const * const mean2ValuePtr,
607  double& covarianceValue );
608 
609  /*!
610  \brief Generate all principal components from the given input raster.
611  \param inputRaster Input raster.
612  \param inputRasterBands Input raster bands.
613  \param pcaMatrix The matrix generated over the principal components process.
614  \param pcaRaster The pré-initiated output PCA raster (with the same dimensions of inputRaster) and double as the data type.
615  \param pcaRasterBands Output raster bands.
616  \param maxThreads The maximum number of threads to use (0-auto, 1-single thread used).
617  \return true if OK, false on errors.
618  \note Optimized for rasters where the used bands have the same blocking scheme.
619  \ingroup rp_func
620  */
622  const te::rst::Raster& inputRaster,
623  const std::vector< unsigned int >& inputRasterBands,
624  boost::numeric::ublas::matrix< double >& pcaMatrix,
625  te::rst::Raster& pcaRaster,
626  const std::vector< unsigned int >& pcaRasterBands,
627  const unsigned int maxThreads );
628 
629  /*!
630  \brief Regenerate the original raster from its principal components.
631  \param pcaRaster The principal components raster (with the same dimensions of outputRaster).
632  \param pcaMatrix The matrix generated by the direct principal components process.
633  \param outputRaster The regenerated output raster (with the same dimentions.
634  \param outputRasterBands Output raster bands.
635  \param maxThreads The maximum number of threads to use (0-auto, 1-single thread used).
636  \return true if OK, false on errors.
637  \note Optimized for rasters where the used bands have the same blocking scheme.
638  \ingroup rp_func
639  */
641  const te::rst::Raster& pcaRaster,
642  const boost::numeric::ublas::matrix< double >& pcaMatrix,
643  te::rst::Raster& outputRaster,
644  const std::vector< unsigned int >& outputRasterBands,
645  const unsigned int maxThreads );
646 
647  /*!
648  \brief Remap pixel values using a remap function matrix.
649  \param inputRaster Input raster.
650  \param inputRasterBands Input raster bands.
651  \param remapMatrix The remap function matrix.
652  \param outputRaster The pré-initiated output raster (with the same dimentions of inputRaster).
653  \param outputRasterBands Output raster bands.
654  \param maxThreads The maximum number of threads to use (0-auto, 1-single thread used).
655  \return true if OK, false on errors.
656  \note Optimized for rasters where the used bands have the same blocking scheme.
657  \ingroup rp_func
658  */
659  TERPEXPORT bool RemapValues(
660  const te::rst::Raster& inputRaster,
661  const std::vector< unsigned int >& inputRasterBands,
662  const boost::numeric::ublas::matrix< double >& remapMatrix,
663  te::rst::Raster& outputRaster,
664  const std::vector< unsigned int >& outputRasterBands,
665  const unsigned int maxThreads );
666 
667  /*!
668  \brief Decompose a multi-band raster into a set of one-band rasters.
669  \param inputRaster Input raster.
670  \param inputRasterBands Input raster bands.
671  \param outputRastersInfos Output rasters connections infos. (one info for each decomposed band).
672  \param outputDataSourceType Output raster datasource type.
673  \param outputRastersPtrs Pointers to the generated outputs rasters.
674  \return true if OK, false on errors.
675  \ingroup rp_func
676  */
678  const te::rst::Raster& inputRaster,
679  const std::vector< unsigned int >& inputRasterBands,
680  const std::vector< std::map<std::string, std::string> > & outputRastersInfos,
681  const std::string& outputDataSourceType,
682  std::vector< boost::shared_ptr< te::rst::Raster > > & outputRastersPtrs );
683 
684  /*!
685  \brief Compose a set of bands into one multi-band raster.
686  \param feeder Input rasters feeder.
687  \param inputRasterBands Input raster bands (one band for each input raster).
688  \param interpMethod The interpolator method to use.
689  \param outputRasterInfo Output raster connection info.
690  \param outputDataSourceType Output raster datasource type.
691  \param outputRasterPtr A pointer to the generated output raster.
692  \return true if OK, false on errors.
693  \note The first raster Grid will be taken as reference for the composed raster.
694  \ingroup rp_func
695  */
696  TERPEXPORT bool ComposeBands(
698  const std::vector< unsigned int >& inputRasterBands,
699  const te::rst::Interpolator::Method& interpMethod,
700  const std::map<std::string, std::string>& outputRasterInfo,
701  const std::string& outputDataSourceType,
702  std::unique_ptr< te::rst::Raster >& outputRasterPtr );
703 
704  /*!
705  \brief Create a datailed extent from the given grid.
706  \param grid Input grid.
707  \param detailedExtent The created detailed extent.
708  \return true if ok, false on errors.
709  \ingroup rp_func
710  */
711  TERPEXPORT bool GetDetailedExtent( const te::rst::Grid& grid,
712  te::gm::LinearRing& detailedExtent );
713 
714  /*!
715  \brief Create a indexed (lines,columns) datailed extent from the given grid.
716  \param grid Input grid.
717  \param indexedDetailedExtent The created detailed extent.
718  \return true if ok, false on errors.
719  \ingroup rp_func
720  */
722  te::gm::LinearRing& indexedDetailedExtent );
723 
724  /*!
725  \brief Create a Wavele Atrous Filter.
726  \param filterType The filter type.
727  \return the created filter.
728  \ingroup rp_func
729  */
730  TERPEXPORT boost::numeric::ublas::matrix< double >
732 
733  /*!
734  \brief Generate all wavelet planes from the given input raster.
735  \param inputRaster Input raster.
736  \param inputRasterBands Input raster bands.
737  \param waveletRaster The pré-initiated output wavelet raster (with the same dimensions of inputRaster) and double as the data type.
738  \param levelsNumber The number of decomposed wavelet levels to generate for each input raster band;
739  \param filter The square filter to use.
740  \return true if OK, false on errors.
741  \note The band order of the generated wavelet levels: { [ band0-smoothed0, band0-wavelet0, ... , band0-smoothedN, band0-waveletN ], ... }
742  \note The number of bands of waveletRaster must be ( inputRasterBands.size() * 2 * levelsNumber ) at least.
743  \ingroup rp_func
744  */
746  const te::rst::Raster& inputRaster,
747  const std::vector< unsigned int >& inputRasterBands,
748  te::rst::Raster& waveletRaster,
749  const unsigned int levelsNumber,
750  const boost::numeric::ublas::matrix< double >& filter );
751 
752  /*!
753  \brief Regenerate the original raster from its wavelets planes.
754  \param waveletRaster The input wavelet raster (with the same dimensions of outputRaster).
755  \param levelsNumber The number of decomposed wavelet levels present inside the wavelet raster.
756  \param outputRaster The regenerated output raster.
757  \param outputRasterBands Output raster bands.
758  \return true if OK, false on errors.
759  \note The band order of the expected wavelet levels: { [ band0-smoothed0, band0-wavelet0, ... , band0-smoothedN, band0-waveletN ], ... }
760  \note The number of bands of waveletRaster must be ( outputRasterBands.size() * 2 * levelsNumber ) at least.
761  \note Only the wavelet bands and the last smoothed band are used.
762  \ingroup rp_func
763  */
765  const te::rst::Raster& waveletRaster,
766  const unsigned int levelsNumber,
767  te::rst::Raster& outputRaster,
768  const std::vector< unsigned int >& outputRasterBands );
769 
770  /*!
771  \brief Resample a subset of the raster, given a box.
772  \param inputRaster Input raster.
773  \param inputRasterBands Input raster bands to process.
774  \param interpMethod The method of interpolation. \sa te::rst::Interpolator
775  \param firstRow The starting row to make a subset of the image.
776  \param firstColumn The starting column to make a subset of the image.
777  \param height The height of the subset.
778  \param width The width of the subset.
779  \param newheight The resampled height of the new raster.
780  \param newwidth The resampled width of the new raster.
781  \param rinfo The parameters needed to build the output raster (see RasterFactory documentation).
782  \param dataSourceType Data source type (raster type. I.E. GDAL).
783  \param resampledRasterPtr The resampled raster pointer.
784  \return true if ok, false on errors.
785  */
787  const te::rst::Raster& inputRaster,
788  const std::vector< unsigned int >& inputRasterBands,
789  const te::rst::Interpolator::Method interpMethod,
790  const unsigned int firstRow,
791  const unsigned int firstColumn,
792  const unsigned int height,
793  const unsigned int width,
794  const unsigned int newheight,
795  const unsigned int newwidth,
796  const std::map<std::string, std::string>& rinfo,
797  const std::string& dataSourceType,
798  std::unique_ptr< te::rst::Raster >& resampledRasterPtr );
799 
800  /*!
801  \brief Returns the tie points converx hull area.
802  \param tiePoints Input tie-points (container of te::gm::GTParameters::TiePoint).
803  \param useTPSecondCoordPair If true the sencond tie-point component (te::gm::GTParameters::TiePoint::second) will be used for the area calcule, otherwize the first component will be used.
804  \return Returns the tie points converx hull area.
805  */
806  template< typename ContainerT >
807  double GetTPConvexHullArea( const ContainerT& tiePoints,
808  const bool useTPSecondCoordPair )
809  {
810  if( tiePoints.size() < 3 )
811  {
812  return 0;
813  }
814  else
815  {
817 
818  typename ContainerT::const_iterator it =
819  tiePoints.begin();
820  const typename ContainerT::const_iterator itE =
821  tiePoints.end();
822 
823  while( it != itE )
824  {
825  if( useTPSecondCoordPair )
826  points.add(new te::gm::Point(it->second.x, it->second.y));
827  else
828  points.add(new te::gm::Point(it->first.x, it->first.y));
829 
830  ++it;
831  }
832 
833  std::unique_ptr< te::gm::Geometry > convexHullPolPtr( points.convexHull() );
834 
835  if( dynamic_cast< te::gm::Surface* >( convexHullPolPtr.get() ) )
836  {
837  return ( (te::gm::Surface*)( convexHullPolPtr.get() ) )->getArea();
838  }
839  else
840  {
841  return 0.0;
842  }
843  }
844  }
845 
846  /*!
847  \brief Create a fixed step sequential color palette.
848  \param paletteSize The desired palette size.
849  \param palette The crated palette.
850  */
852  const unsigned int paletteSize,
853  const bool randomize,
854  std::vector< te::rst::BandProperty::ColorEntry >& palette );
855 
856  /*!
857  \brief Generate all wavelet planes from the given input raster.
858  \param inputRaster Input raster.
859  \param inputRasterBand Input raster band.
860  \param createPaletteRaster If true a paletted raster will be created instead of a multi-band raster..
861  \param slicesNumber The number of historgram slices to create;
862  \param eqHistogram If true, the input raster will historgram will be equalized before the slicing process.
863  \param rasterInfo The output raster info (specific driver info).
864  \param rasterType The output raster type (specific driver name - See te::rst::RasterFactory dictorary for more info).
865  \param enableProgress Enable/disable the progress interface.
866  \param palettePtr A pointer to an optional user given palette (if null, an fixed step pallete will be generated).
867  \param outRasterPtr The output sliced raster pointer.
868  \return true if OK, false on errors.
869  \ingroup rp_func
870  */
872  const te::rst::Raster& inputRaster,
873  const unsigned int inputRasterBand,
874  const bool createPaletteRaster,
875  const unsigned int slicesNumber,
876  const bool eqHistogram,
877  const std::map< std::string, std::string >& rasterInfo,
878  const std::string& rasterType,
879  const bool enableProgress,
880  std::vector< te::rst::BandProperty::ColorEntry > const * const palettePtr,
881  std::unique_ptr< te::rst::Raster >& outRasterPtr );
882 
883  } // end namespace rp
884 } // end namespace te
885 
886 #endif // __TERRALIB_RP_INTERNAL_FUNCTIONS_H
887 
TERPEXPORT bool GetMeanValue(const te::rst::Band &band, const unsigned int maxThreads, double &meanValue)
Get the mean of band pixel values.
TERPEXPORT double GetDigitalNumberBandMax(std::string bandName)
Returns the maximum digital number of a given sensor/band.
TERPEXPORT bool ConvertIHS2RGB(const te::rst::Raster &inputIRaster, const unsigned int intensityBandIdx, const te::rst::Raster &inputHRaster, const unsigned int hueBandIdx, const te::rst::Raster &inputSRaster, const unsigned int saturationBandIdx, const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster &outputRGBRaster)
IHS to RGB conversion.
Index into a lookup table.
Definition: Enums.h:57
A raster band description.
Definition: BandProperty.h:61
double GetTPConvexHullArea(const ContainerT &tiePoints, const bool useTPSecondCoordPair)
Returns the tie points converx hull area.
Definition: Functions.h:807
Spline filter type.
Definition: Functions.h:76
TERPEXPORT void SaveSensorParams(std::map< std::string, SpectralSensorParams > &)
Saves in SpectralSensor.json file the spectral sensors parameters.
Triangle filter type.
Definition: Functions.h:77
void TERPEXPORT 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.h:215
bool TERPEXPORT CreateNewMemRaster(const te::rst::Grid &rasterGrid, std::vector< te::rst::BandProperty * > bandsProperties, RasterHandler &outRasterHandler)
Create a new raster into a new memory datasource.
TERPEXPORT std::vector< std::string > GetBandNames()
Returns a vector os with band&#39;s names.
TERPEXPORT bool ConvertRGB2IHS(const te::rst::Raster &inputRedRaster, const unsigned int redBandIdx, const te::rst::Raster &inputGreenRaster, const unsigned int greenBandIdx, const te::rst::Raster &inputBlueRaster, const unsigned int blueBandIdx, const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster &outputIHSRaster)
RGB to IHS conversion.
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
TERPEXPORT bool ComposeBands(te::rp::FeederConstRaster &feeder, const std::vector< unsigned int > &inputRasterBands, const te::rst::Interpolator::Method &interpMethod, const std::map< std::string, std::string > &outputRasterInfo, const std::string &outputDataSourceType, std::unique_ptr< te::rst::Raster > &outputRasterPtr)
Compose a set of bands into one multi-band raster.
TERPEXPORT std::pair< double, double > GetDigitalNumberBandInfo(std::string bandName)
Returns the maximun and minimum digital numbers of a given sensor/band.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:119
TERPEXPORT bool DirectPrincipalComponents(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, boost::numeric::ublas::matrix< double > &pcaMatrix, te::rst::Raster &pcaRaster, const std::vector< unsigned int > &pcaRasterBands, const unsigned int maxThreads)
Generate all principal components from the given input raster.
TERPEXPORT bool DecomposeBands(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< std::map< std::string, std::string > > &outputRastersInfos, const std::string &outputDataSourceType, std::vector< boost::shared_ptr< te::rst::Raster > > &outputRastersPtrs)
Decompose a multi-band raster into a set of one-band rasters.
TERPEXPORT double GetDigitalNumberBandMin(std::string bandName)
Returns the minimum digital number of a given sensor/band.
InterpolationMethod
Allowed interpolation methods.
Definition: Enums.h:92
TERPEXPORT double GetSpectralBandMax(std::string bandName)
Returns the maximum reflectance value of a given sensor/band.
TERPEXPORT bool RemapValues(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const boost::numeric::ublas::matrix< double > &remapMatrix, te::rst::Raster &outputRaster, const std::vector< unsigned int > &outputRasterBands, const unsigned int maxThreads)
Remap pixel values using a remap function matrix.
TERPEXPORT bool ConvertRGB2HLS(const te::rst::Raster &inputRedRaster, const unsigned int redBandIdx, const te::rst::Raster &inputGreenRaster, const unsigned int greenBandIdx, const te::rst::Raster &inputBlueRaster, const unsigned int blueBandIdx, const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster &outputHLSRaster)
RGB to HLS conversion.
#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:184
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:254
bool TERPEXPORT Copy2DiskRaster(const te::rst::Raster &inputRaster, const std::string &fileName)
Create a new raster into a GDAL datasource.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
TERPEXPORT std::map< std::string, SpectralSensorParams > getSensorParams()
Returns a map with spectral sensors parameters defined in SpectralSensor.json file.
A point with x and y coordinate values.
Definition: Point.h:50
TERPEXPORT bool NormalizeRaster(te::rst::Raster &inputRaster, double nmin=0.0, double nmax=255.0)
Normalizes one raster in a given interval.
An abstract class for raster data strucutures.
Definition: Raster.h:71
unsigned int getColumnsNumber() const
The number of current matrix columns.
Definition: Matrix.h:681
TERPEXPORT bool InverseWaveletAtrous(const te::rst::Raster &waveletRaster, const unsigned int levelsNumber, te::rst::Raster &outputRaster, const std::vector< unsigned int > &outputRasterBands)
Regenerate the original raster from its wavelets planes.
WaveletAtrousFilterType
Wavelet Atrous Filter types.
Definition: Functions.h:73
virtual Geometry * convexHull() const _NOEXCEPT_OP(false)
This method calculates the Convex Hull of a geometry.
URI C++ Library.
TERPEXPORT bool ConvertHLS2RGB(const te::rst::Raster &inputHRaster, const unsigned int hueBandIdx, const te::rst::Raster &inputLRaster, const unsigned int lightBandIdx, const te::rst::Raster &inputSRaster, const unsigned int saturationBandIdx, const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster &outputRGBRaster)
HLS to RGB conversion.
TERPEXPORT bool DirectWaveletAtrous(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, te::rst::Raster &waveletRaster, const unsigned int levelsNumber, const boost::numeric::ublas::matrix< double > &filter)
Generate all wavelet planes from the given input raster.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
bool TERPEXPORT CreateNewRaster(const te::rst::Grid &rasterGrid, const std::vector< te::rst::BandProperty * > &bandsProperties, const std::map< std::string, std::string > &rasterInfo, const std::string &rasterType, std::unique_ptr< te::rst::Raster > &outRasterPtr)
Create a new raster into the givem data source.
Raster tuple.
void TERPEXPORT Convert2DoublesVector(void *inputVector, const int inputVectorDataType, unsigned int inputVectorSize, double *outputVector)
Convert vector elements.
RasterHandler.
Definition: RasterHandler.h:47
A raster band description.
Definition: Band.h:63
TERPEXPORT bool GetDetailedExtent(const te::rst::Grid &grid, te::gm::LinearRing &detailedExtent)
Create a datailed extent from the given grid.
Generic template matrix.
SpectralSensorParams(const int &band, const double &lower, const double &upper, const double &min, const double &max)
Definition: Functions.h:84
Invalid filter type.
Definition: Functions.h:75
TERPEXPORT boost::numeric::ublas::matrix< double > CreateWaveletAtrousFilter(const WaveletAtrousFilterType &filterType)
Create a Wavele Atrous Filter.
TERPEXPORT bool GetCovarianceValue(const te::rst::Band &band1, const te::rst::Band &band2, const unsigned int maxThreads, double const *const mean1ValuePtr, double const *const mean2ValuePtr, double &covarianceValue)
Get the covariance of band pixel values.
TERPEXPORT bool RasterResample(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const te::rst::Interpolator::Method interpMethod, 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 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.
Configuration flags for the Raster Processing module of TerraLib.
TERPEXPORT double GetSpectralBandMin(std::string bandName)
Returns the minimum reflectance value of a given sensor/band.
TERPEXPORT bool RasterSlicing(const te::rst::Raster &inputRaster, const unsigned int inputRasterBand, const bool createPaletteRaster, const unsigned int slicesNumber, const bool eqHistogram, const std::map< std::string, std::string > &rasterInfo, const std::string &rasterType, const bool enableProgress, std::vector< te::rst::BandProperty::ColorEntry > const *const palettePtr, std::unique_ptr< te::rst::Raster > &outRasterPtr)
Generate all wavelet planes from the given input raster.
TERPEXPORT SpectralSensorParams GetSpectralBandInfo(std::string bandName)
Returns the maximun and minimum reflectance values of a given sensor/band.
Feeder from a input rasters.
Definition: FeedersRaster.h:46
void add(Geometry *g)
It adds the geometry into the collection.
A generic template matrix.
Definition: Matrix.h:54
TERPEXPORT bool GetIndexedDetailedExtent(const te::rst::Grid &grid, te::gm::LinearRing &indexedDetailedExtent)
Create a indexed (lines,columns) datailed extent from the given grid.
TERPEXPORT bool InversePrincipalComponents(const te::rst::Raster &pcaRaster, const boost::numeric::ublas::matrix< double > &pcaMatrix, te::rst::Raster &outputRaster, const std::vector< unsigned int > &outputRasterBands, const unsigned int maxThreads)
Regenerate the original raster from its principal components.
TERPEXPORT void CreateFixedStepPalette(const unsigned int paletteSize, const bool randomize, std::vector< te::rst::BandProperty::ColorEntry > &palette)
Create a fixed step sequential color palette.
static Raster * make()
It creates and returns an empty raster with default raster driver.
Raster objects feeders.
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
TERPEXPORT std::string GetSensorFilename()
Returns a json filename with spectral sensors parameters.
unsigned int getLinesNumber() const
The number of current matrix lines.
Definition: Matrix.h:674
TERPEXPORT bool GetStdDevValue(const te::rst::Band &band, const unsigned int maxThreads, double const *const meanValuePtr, double &stdDevValue)
Get the standard deviation of band pixel values.
bool TERPEXPORT CreateNewGdalRaster(const te::rst::Grid &rasterGrid, std::vector< te::rst::BandProperty * > bandsProperties, const std::string &fileName, std::unique_ptr< te::rst::Raster > &outRasterPtr)
Create a new raster into a GDAL datasource.
void TERPEXPORT ConvertDoublesVector(double *inputVector, unsigned int inputVectorSize, const int outputVectorDataType, void *outputVector)
Convert a doubles vector.
Surface is an abstract class that represents a 2-dimensional geometric objects.
Definition: Surface.h:54