All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functions.cpp
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.cpp
22 
23  \brief Raster Processing functions.
24 */
25 
26 // TerraLib
27 
28 #include "Functions.h"
29 
30 #include "../dataaccess/dataset/DataSetType.h"
31 #include "../dataaccess/datasource/DataSourceFactory.h"
32 #include "../dataaccess/utils/Utils.h"
33 #include "../datatype/Enums.h"
34 #include "../raster/BandProperty.h"
35 #include "../raster/Grid.h"
36 #include "../raster/RasterFactory.h"
37 #include "../raster/RasterProperty.h"
38 #include "../raster/RasterIterator.h"
39 #include "../raster/Utils.h"
40 #include "../geometry/Point.h"
41 #include "../common/MatrixUtils.h"
42 #include "Exception.h"
43 #include "Macros.h"
44 #include "RasterHandler.h"
45 
46 // Boost
47 #include <boost/filesystem.hpp>
48 #include <boost/numeric/ublas/io.hpp>
49 #include <boost/numeric/ublas/matrix.hpp>
50 #include <boost/shared_ptr.hpp>
51 #include <boost/shared_array.hpp>
52 #include <boost/lexical_cast.hpp>
53 #include <boost/thread.hpp>
54 #include <boost/numeric/ublas/lu.hpp>
55 #include <boost/numeric/ublas/matrix.hpp>
56 
57 
58 // STL
59 #include <cstring>
60 #include <string>
61 #include <limits>
62 #include <map>
63 #include <memory>
64 
65 #ifndef M_PI
66  #define M_PI 3.14159265358979323846
67 #endif
68 
69 namespace te
70 {
71  namespace rp
72  {
74  {
77  double* m_meanValuePtr;
80  boost::mutex* m_mutexPtr;
81  };
82 
84  {
89  double m_meanValue;
91  boost::mutex* m_mutexPtr;
92  };
93 
95  {
102  double m_mean1Value;
103  double m_mean2Value;
104  boost::mutex* m_mutexPtr;
105  };
106 
108  {
110  std::vector< unsigned int > const * m_inputRasterBandsPtr;
112  std::vector< unsigned int > const * m_outputRasterBandsPtr;
113  boost::numeric::ublas::matrix< double > const * m_remapMatrixPtr;
116  boost::mutex* m_mutexPtr;
117  };
118 
119  bool CreateNewRaster( const te::rst::Grid& rasterGrid,
120  const std::vector< te::rst::BandProperty* >& bandsProperties,
121  const std::string& outDataSetName,
122  const std::string& dataSourceType,
123  RasterHandler& outRasterHandler )
124  {
125  // Creating a new datasource
126 
127  std::auto_ptr< te::da::DataSource > dataSourcePtr(
128  te::da::DataSourceFactory::make( dataSourceType ) );
129  if( dataSourcePtr.get() == 0 ) return false;
130 
131  RasterHandler internalRasterHandler;
132 
133  if( CreateNewRaster( rasterGrid, bandsProperties,
134  outDataSetName, *dataSourcePtr, internalRasterHandler ) )
135  {
136  std::auto_ptr< te::da::DataSource > dummyDataSourcePtr;
137  std::auto_ptr< te::da::DataSourceTransactor > transactorPtr;
138  std::auto_ptr< te::da::DataSet > dataSetPtr;
139  std::auto_ptr< te::rst::Raster > rasterPtr;
140 
141  internalRasterHandler.release( dummyDataSourcePtr, transactorPtr,
142  dataSetPtr, rasterPtr );
143 
144  outRasterHandler.reset( dataSourcePtr.release(), transactorPtr.release(),
145  dataSetPtr.release(), rasterPtr.release() );
146 
147  return true;
148  }
149  else
150  {
151  return false;
152  }
153  }
154 
155  bool CreateNewRaster( const te::rst::Grid& rasterGrid,
156  const std::vector< te::rst::BandProperty* >& bandsProperties,
157  const std::string& outDataSetName,
158  te::da::DataSource& outDataSource,
159  RasterHandler& outRasterHandler )
160  {
161  // Defining the raster properties
162 
163  std::auto_ptr< te::rst::RasterProperty > rasterPropertyPtr(
164  new te::rst::RasterProperty( new te::rst::Grid( rasterGrid ),
165  bandsProperties, std::map< std::string, std::string >(),
166  false, 0, 0 ) );
167 
168  // acquiring a transactor instance
169 
170  std::auto_ptr< te::da::DataSourceTransactor > transactorPtr(
171  outDataSource.getTransactor() );
172 
173  if( transactorPtr.get() == 0 )
174  {
175  return false;
176  }
177 
178  // Creating a data set instance
179 
180  if( transactorPtr->dataSetExists( outDataSetName ) )
181  {
182  transactorPtr->dropDataSet( outDataSetName );
183  }
184 
185  std::auto_ptr< te::da::DataSetType > dataSetTypePtr(
186  new te::da::DataSetType( outDataSetName ) );
187  dataSetTypePtr->add( rasterPropertyPtr.release() );
188 
189  try
190  {
191  transactorPtr->createDataSet( dataSetTypePtr.get(),
192  std::map< std::string, std::string >() );
193  }
194  catch( ... )
195  {
196  return false;
197  }
198 
199  if( ! transactorPtr->dataSetExists( outDataSetName ) ) return false;
200 
201  std::auto_ptr< te::da::DataSet > dataSetPtr( transactorPtr->getDataSet(
202  outDataSetName, te::common::FORWARDONLY, true, te::common::RWAccess ) );
203 
204  if( dataSetPtr.get() == 0 )
205  {
206  return false;
207  }
208 
209  // Creating a raster instance
210 
211  std::auto_ptr< te::rst::Raster > rasterPtr( dataSetPtr->getRaster( 0 ) );
212 
213  if( rasterPtr.get() )
214  {
215  outRasterHandler.reset( transactorPtr.release(), dataSetPtr.release(), rasterPtr.release() );
216  return true;
217  }
218  else
219  {
220  return false;
221  }
222  }
223 
224  bool CreateNewMemRaster( const te::rst::Grid& rasterGrid,
225  std::vector< te::rst::BandProperty* > bandsProperties,
226  RasterHandler& outRasterHandler )
227  {
228  std::string dataSetName = std::string( "createNewMemRaster" ) +
229  boost::lexical_cast< std::string >( &outRasterHandler );
230 
231  return CreateNewRaster( rasterGrid, bandsProperties, dataSetName,
232  "MEM", outRasterHandler );
233  }
234 
235  bool CreateNewGdalRaster( const te::rst::Grid& rasterGrid,
236  std::vector< te::rst::BandProperty* > bandsProperties,
237  const std::string& fileName,
238  RasterHandler& outRasterHandler )
239  {
240  std::auto_ptr< te::rst::Raster > outRasterPtr;
241  if( CreateNewGdalRaster( rasterGrid, bandsProperties, fileName, outRasterPtr ) )
242  {
243  outRasterHandler.reset( outRasterPtr.release() );
244  return true;
245  }
246  else
247  {
248  outRasterHandler.reset();
249  return false;
250  }
251  }
252 
253  bool CreateNewGdalRaster( const te::rst::Grid& rasterGrid,
254  std::vector< te::rst::BandProperty* > bandsProperties,
255  const std::string& fileName,
256  std::auto_ptr< te::rst::Raster >& outRasterPtr )
257  {
258  outRasterPtr.reset();
259 
260  std::map< std::string, std::string > rInfo;
261  rInfo[ "URI" ] = fileName;
262 
263  outRasterPtr.reset( te::rst::RasterFactory::make(
264  "GDAL",
265  new te::rst::Grid( rasterGrid ),
266  bandsProperties,
267  rInfo,
268  0,
269  0 ) );
270 
271  if( outRasterPtr.get() )
272  {
273  return true;
274  }
275  else
276  {
277  return false;
278  }
279  }
280 
281  bool TERPEXPORT Copy2DiskRaster( const te::rst::Raster& inputRaster,
282  const std::string& fileName )
283  {
284  if( !(inputRaster.getAccessPolicy() & te::common::RAccess ) )
285  {
286  return false;
287  };
288 
289  const unsigned int nBands = inputRaster.getNumberOfBands();
290  const unsigned int nCols = inputRaster.getNumberOfColumns();
291  const unsigned int nRows = inputRaster.getNumberOfRows();
292  unsigned int bandIdx =0;
293  unsigned int col = 0;
294  unsigned int row = 0;
295 
296  std::vector< te::rst::BandProperty* > bandsProperties;
297  for( bandIdx = 0 ; bandIdx < nBands ; ++bandIdx )
298  {
299  bandsProperties.push_back( new te::rst::BandProperty(
300  *( inputRaster.getBand( bandIdx )->getProperty() ) ) );
301  }
302 
303  RasterHandler outRasterHandler;
304 
305  if( ! CreateNewGdalRaster( *( inputRaster.getGrid() ), bandsProperties,
306  fileName, outRasterHandler ) )
307  {
308  return false;
309  }
310 
311  double value = 0;
312 
313  for( bandIdx = 0 ; bandIdx < nBands ; ++bandIdx )
314  {
315  const te::rst::Band& inBand = *inputRaster.getBand( bandIdx );
316  te::rst::Band& outBand = *outRasterHandler.getRasterPtr()->getBand( bandIdx );
317 
318  for( row = 0 ; row < nRows ; ++row )
319  {
320  for( col = 0 ; col < nCols ; ++col )
321  {
322  inBand.getValue( col, row, value );
323  outBand.setValue( col, row, value );
324  }
325  }
326  }
327 
328  return true;
329  }
330 
331  void Convert2DoublesVector( void* inputVector, const int inputVectorDataType,
332  unsigned int inputVectorSize, double* outputVector )
333  {
334  switch( inputVectorDataType )
335  {
336  case te::dt::CHAR_TYPE :
337  {
338  char* vPtr = (char*)inputVector;
339  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
340  outputVector[ idx ] = (double)vPtr[ idx ];
341  break;
342  }
343  case te::dt::BIT_TYPE :
344  case te::dt::UCHAR_TYPE :
345  {
346  unsigned char* vPtr = (unsigned char*)inputVector;
347  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
348  outputVector[ idx ] = (double)vPtr[ idx ];
349  break;
350  }
351  case te::dt::INT16_TYPE :
352  {
353  short int* vPtr = (short int*)inputVector;
354  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
355  outputVector[ idx ] = (double)vPtr[ idx ];
356  break;
357  }
358  case te::dt::CINT16_TYPE :
359  {
360  std::complex< short int >* vPtr = (std::complex< short int >*)
361  inputVector;
362  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
363  outputVector[ idx ] = (double)vPtr[ idx ].real();
364  break;
365  }
366  case te::dt::UINT16_TYPE :
367  {
368  unsigned short int* vPtr = (unsigned short int*)inputVector;
369  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
370  outputVector[ idx ] = (double)vPtr[ idx ];
371  break;
372  }
373  case te::dt::INT32_TYPE :
374  {
375  int* vPtr = (int*)inputVector;
376  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
377  outputVector[ idx ] = (double)vPtr[ idx ];
378  break;
379  }
380  case te::dt::CINT32_TYPE :
381  {
382  std::complex< int >* vPtr = (std::complex< int >*)
383  inputVector;
384  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
385  outputVector[ idx ] = (double)vPtr[ idx ].real();
386  break;
387  }
388  case te::dt::UINT32_TYPE :
389  {
390  unsigned int* vPtr = (unsigned int*)inputVector;
391  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
392  outputVector[ idx ] = (double)vPtr[ idx ];
393  break;
394  }
395  case te::dt::INT64_TYPE :
396  {
397  long int* vPtr = (long int*)inputVector;
398  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
399  outputVector[ idx ] = (double)vPtr[ idx ];
400  break;
401  }
402  case te::dt::UINT64_TYPE :
403  {
404  unsigned long int* vPtr = (unsigned long int*)inputVector;
405  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
406  outputVector[ idx ] = (double)vPtr[ idx ];
407  break;
408  }
409  case te::dt::FLOAT_TYPE :
410  {
411  float* vPtr = (float*)inputVector;
412  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
413  outputVector[ idx ] = (double)vPtr[ idx ];
414  break;
415  }
416  case te::dt::CFLOAT_TYPE :
417  {
418  std::complex< float >* vPtr = (std::complex< float >*)
419  inputVector;
420  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
421  outputVector[ idx ] = (double)vPtr[ idx ].real();
422  break;
423  }
424  case te::dt::DOUBLE_TYPE :
425  {
426  memcpy( outputVector, inputVector, inputVectorSize * sizeof( double ) );
427  break;
428  }
429  case te::dt::CDOUBLE_TYPE :
430  {
431  std::complex< double >* vPtr = (std::complex< double >*)
432  inputVector;
433  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
434  outputVector[ idx ] = (double)vPtr[ idx ].real();
435  break;
436  }
437  default :
438  throw te::rp::Exception( "Invalid data type" );
439  break;
440  };
441  }
442 
443  void ConvertDoublesVector( double* inputVector,
444  unsigned int inputVectorSize, const int outputVectorDataType,
445  void* outputVector )
446  {
447  switch( outputVectorDataType )
448  {
449  case te::dt::CHAR_TYPE :
450  {
451  char* vPtr = (char*)outputVector;
452  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
453  vPtr[ idx ] = (char)inputVector[ idx ];
454  break;
455  }
456  case te::dt::BIT_TYPE :
457  case te::dt::UCHAR_TYPE :
458  {
459  unsigned char* vPtr = (unsigned char*)outputVector;
460  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
461  vPtr[ idx ] = (unsigned char)inputVector[ idx ];
462  break;
463  }
464  case te::dt::INT16_TYPE :
465  {
466  short int* vPtr = (short int*)outputVector;
467  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
468  vPtr[ idx ] = (short int)inputVector[ idx ];
469  break;
470  }
471  case te::dt::CINT16_TYPE :
472  {
473  std::complex< short int >* vPtr = (std::complex< short int >*)
474  outputVector;
475  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
476  vPtr[ idx ]= ( (short int)inputVector[ idx ] );
477  break;
478  }
479  case te::dt::UINT16_TYPE :
480  {
481  unsigned short int* vPtr = (unsigned short int*)outputVector;
482  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
483  vPtr[ idx ] = (unsigned short int)inputVector[ idx ];
484  break;
485  }
486  case te::dt::INT32_TYPE :
487  {
488  int* vPtr = (int*)outputVector;
489  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
490  vPtr[ idx ] = (int)inputVector[ idx ];
491  break;
492  }
493  case te::dt::CINT32_TYPE :
494  {
495  std::complex< int >* vPtr = (std::complex< int >*)
496  outputVector;
497  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
498  vPtr[ idx ] = ( (int)inputVector[ idx ] );
499  break;
500  }
501  case te::dt::UINT32_TYPE :
502  {
503  unsigned int* vPtr = (unsigned int*)outputVector;
504  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
505  vPtr[ idx ] = (unsigned int)inputVector[ idx ];
506  break;
507  }
508  case te::dt::INT64_TYPE :
509  {
510  long int* vPtr = (long int*)outputVector;
511  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
512  vPtr[ idx ] = (long int)inputVector[ idx ];
513  break;
514  }
515  case te::dt::UINT64_TYPE :
516  {
517  unsigned long int* vPtr = (unsigned long int*)outputVector;
518  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
519  vPtr[ idx ] = (unsigned long int)inputVector[ idx ];
520  break;
521  }
522  case te::dt::FLOAT_TYPE :
523  {
524  float* vPtr = (float*)outputVector;
525  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
526  vPtr[ idx ] = (float)inputVector[ idx ];
527  break;
528  }
529  case te::dt::CFLOAT_TYPE :
530  {
531  std::complex< float >* vPtr = (std::complex< float >*)
532  outputVector;
533  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
534  vPtr[ idx ] = ( (float)inputVector[ idx ] );
535  break;
536  }
537  case te::dt::DOUBLE_TYPE :
538  {
539  memcpy( outputVector, inputVector, inputVectorSize * sizeof( double ) );
540  break;
541  }
542  case te::dt::CDOUBLE_TYPE :
543  {
544  std::complex< double >* vPtr = (std::complex< double >*)
545  outputVector;
546  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
547  vPtr[ idx ] = ( (double)inputVector[ idx ] );
548  break;
549  }
550  default :
551  throw te::rp::Exception( "Invalid data type" );
552  break;
553  };
554  }
555 
556  std::vector<std::string> GetBandNames()
557  {
558  static std::vector<std::string> bandNames;
559 
560  bandNames.push_back("CBERS2_CCD_1_BLUE");
561  bandNames.push_back("CBERS2_CCD_2_GREEN");
562  bandNames.push_back("CBERS2_CCD_3_RED");
563  bandNames.push_back("CBERS2_CCD_4_NIR");
564  bandNames.push_back("CBERS2_CCD_5_PAN");
565 
566  bandNames.push_back("LANDSAT5_TM_1_BLUE");
567  bandNames.push_back("LANDSAT5_TM_2_GREEN");
568  bandNames.push_back("LANDSAT5_TM_3_RED");
569  bandNames.push_back("LANDSAT5_TM_4_NIR");
570  bandNames.push_back("LANDSAT5_TM_5_SWIR");
571  bandNames.push_back("LANDSAT5_TM_6_THERMAL");
572  bandNames.push_back("LANDSAT5_TM_7_MIR");
573 
574  bandNames.push_back("LANDSAT7_ETM+_1_BLUE");
575  bandNames.push_back("LANDSAT7_ETM+_2_GREEN");
576  bandNames.push_back("LANDSAT7_ETM+_3_RED");
577  bandNames.push_back("LANDSAT7_ETM+_4_NIR");
578  bandNames.push_back("LANDSAT7_ETM+_5_SWIR");
579  bandNames.push_back("LANDSAT7_ETM+_6_THERMAL");
580  bandNames.push_back("LANDSAT7_ETM+_7_MIR");
581  bandNames.push_back("LANDSAT7_ETM+_8_PAN");
582 
583  bandNames.push_back("WV2_MUL_1_COASTAL");
584  bandNames.push_back("WV2_MUL_2_BLUE");
585  bandNames.push_back("WV2_MUL_3_GREEN");
586  bandNames.push_back("WV2_MUL_4_YELLOW");
587  bandNames.push_back("WV2_MUL_5_RED");
588  bandNames.push_back("WV2_MUL_6_REDEDGE");
589  bandNames.push_back("WV2_MUL_7_NIR1");
590  bandNames.push_back("WV2_MUL_8_NIR2");
591  bandNames.push_back("WV2_PAN");
592 
593  return bandNames;
594  }
595 
596  std::pair<double, double> GetSpectralBandInfo(std::string bandName)
597  {
598  static std::map<std::string, std::pair<double, double> > BandInfo;
599 
600  BandInfo["CBERS2_CCD_1_BLUE"] = std::pair<double, double> (0.45, 0.52);
601  BandInfo["CBERS2_CCD_2_GREEN"] = std::pair<double, double> (0.52, 0.59);
602  BandInfo["CBERS2_CCD_3_RED"] = std::pair<double, double> (0.63, 0.69);
603  BandInfo["CBERS2_CCD_4_NIR"] = std::pair<double, double> (0.77, 0.89);
604  BandInfo["CBERS2_CCD_5_PAN"] = std::pair<double, double> (0.51, 0.73);
605 
606  BandInfo["LANDSAT5_TM_1_BLUE"] = std::pair<double, double> (0.45, 0.52);
607  BandInfo["LANDSAT5_TM_2_GREEN"] = std::pair<double, double> (0.52, 0.60);
608  BandInfo["LANDSAT5_TM_3_RED"] = std::pair<double, double> (0.63, 0.69);
609  BandInfo["LANDSAT5_TM_4_NIR"] = std::pair<double, double> (0.76, 0.90);
610  BandInfo["LANDSAT5_TM_5_SWIR"] = std::pair<double, double> (1.55, 1.75);
611  BandInfo["LANDSAT5_TM_6_THERMAL"] = std::pair<double, double> (10.40, 12.50);
612  BandInfo["LANDSAT5_TM_7_MIR"] = std::pair<double, double> (2.08, 2.35);
613 
614  BandInfo["LANDSAT7_ETM+_1_BLUE"] = std::pair<double, double> (0.45, 0.515);
615  BandInfo["LANDSAT7_ETM+_2_GREEN"] = std::pair<double, double> (0.525, 0.605);
616  BandInfo["LANDSAT7_ETM+_3_RED"] = std::pair<double, double> (0.63, 0.69);
617  BandInfo["LANDSAT7_ETM+_4_NIR"] = std::pair<double, double> (0.75, 0.90);
618  BandInfo["LANDSAT7_ETM+_5_SWIR"] = std::pair<double, double> (1.55, 1.75);
619  BandInfo["LANDSAT7_ETM+_6_THERMAL"] = std::pair<double, double> (10.40, 12.5);
620  BandInfo["LANDSAT7_ETM+_7_MIR"] = std::pair<double, double> (2.09, 2.35);
621  BandInfo["LANDSAT7_ETM+_8_PAN"] = std::pair<double, double> (0.52, 0.90);
622 
623  BandInfo["WV2_MUL_1_COASTAL"] = std::pair<double, double> (0.4, 0.45);
624  BandInfo["WV2_MUL_2_BLUE"] = std::pair<double, double> (0.45, 0.51);
625  BandInfo["WV2_MUL_3_GREEN"] = std::pair<double, double> (0.51, 0.58);
626  BandInfo["WV2_MUL_4_YELLOW"] = std::pair<double, double> (0.585, 0.625);
627  BandInfo["WV2_MUL_5_RED"] = std::pair<double, double> (0.66, 0.69);
628  BandInfo["WV2_MUL_6_REDEDGE"] = std::pair<double, double> (0.705, 0.745);
629  BandInfo["WV2_MUL_7_NIR1"] = std::pair<double, double> (0.77, 0.895);
630  BandInfo["WV2_MUL_8_NIR2"] = std::pair<double, double> (0.86, 0.104);
631  BandInfo["WV2_PAN"] = std::pair<double, double> (0.45, 0.8);
632 
633  // needs more Band Info from other sensors/bands
634 
635  if (BandInfo.find(bandName) == BandInfo.end())
636  return std::pair<double, double> (0.0, 1.0);
637 
638  return BandInfo[bandName];
639  }
640 
641  double GetSpectralBandMin(std::string bandName)
642  {
643  return GetSpectralBandInfo(bandName).first;
644  }
645 
646  double GetSpectralBandMax(std::string bandName)
647  {
648  return GetSpectralBandInfo(bandName).second;
649  }
650 
651  std::pair<double, double> GetDigitalNumberBandInfo(std::string bandName)
652  {
653  static std::map<std::string, std::pair<double, double> > DNBandInfo;
654 
655  DNBandInfo["CBERS2_CCD_1_BLUE"] = std::pair<double, double> (0.0, 255.0);
656  DNBandInfo["CBERS2_CCD_2_GREEN"] = std::pair<double, double> (0.0, 255.0);
657  DNBandInfo["CBERS2_CCD_3_RED"] = std::pair<double, double> (0.0, 255.0);
658  DNBandInfo["CBERS2_CCD_4_NIR"] = std::pair<double, double> (0.0, 255.0);
659  DNBandInfo["CBERS2_CCD_5_PAN"] = std::pair<double, double> (0.0, 255.0);
660 
661  DNBandInfo["LANDSAT5_TM_1_BLUE"] = std::pair<double, double> (0.0, 255.0);
662  DNBandInfo["LANDSAT5_TM_2_GREEN"] = std::pair<double, double> (0.0, 255.0);
663  DNBandInfo["LANDSAT5_TM_3_RED"] = std::pair<double, double> (0.0, 255.0);
664  DNBandInfo["LANDSAT5_TM_4_NIR"] = std::pair<double, double> (0.0, 255.0);
665  DNBandInfo["LANDSAT5_TM_5_SWIR"] = std::pair<double, double> (0.0, 255.0);
666  DNBandInfo["LANDSAT5_TM_6_THERMAL"] = std::pair<double, double> (0.0, 255.0);
667  DNBandInfo["LANDSAT5_TM_7_MIR"] = std::pair<double, double> (0.0, 255.0);
668 
669  DNBandInfo["LANDSAT7_ETM+_1_BLUE"] = std::pair<double, double> (0.0, 255.0);
670  DNBandInfo["LANDSAT7_ETM+_2_GREEN"] = std::pair<double, double> (0.0, 255.0);
671  DNBandInfo["LANDSAT7_ETM+_3_RED"] = std::pair<double, double> (0.0, 255.0);
672  DNBandInfo["LANDSAT7_ETM+_4_NIR"] = std::pair<double, double> (0.0, 255.0);
673  DNBandInfo["LANDSAT7_ETM+_5_SWIR"] = std::pair<double, double> (0.0, 255.0);
674  DNBandInfo["LANDSAT7_ETM+_6_THERMAL"] = std::pair<double, double> (0.0, 255.0);
675  DNBandInfo["LANDSAT7_ETM+_7_MIR"] = std::pair<double, double> (0.0, 255.0);
676  DNBandInfo["LANDSAT7_ETM+_8_PAN"] = std::pair<double, double> (0.0, 255.0);
677 
678  DNBandInfo["WV2_MUL_1_COASTAL"] = std::pair<double, double> (0.0, 2048.0);
679  DNBandInfo["WV2_MUL_2_BLUE"] = std::pair<double, double> (0.0, 2048.0);
680  DNBandInfo["WV2_MUL_3_GREEN"] = std::pair<double, double> (0.0, 2048.0);
681  DNBandInfo["WV2_MUL_4_YELLOW"] = std::pair<double, double> (0.0, 2048.0);
682  DNBandInfo["WV2_MUL_5_RED"] = std::pair<double, double> (0.0, 2048.0);
683  DNBandInfo["WV2_MUL_6_REDEDGE"] = std::pair<double, double> (0.0, 2048.0);
684  DNBandInfo["WV2_MUL_7_NIR1"] = std::pair<double, double> (0.0, 2048.0);
685  DNBandInfo["WV2_MUL_7_NIR2"] = std::pair<double, double> (0.0, 2048.0);
686  DNBandInfo["WV2_PAN"] = std::pair<double, double> (0.0, 2048.0);
687 
688  // needs more Band Info from other sensors/bands
689 
690  if (DNBandInfo.find(bandName) == DNBandInfo.end())
691  return std::pair<double, double> (0.0, 255.0);
692 
693  return DNBandInfo[bandName];
694  }
695 
696  double GetDigitalNumberBandMax(std::string bandName)
697  {
698  return GetDigitalNumberBandInfo(bandName).second;
699  }
700 
701  bool NormalizeRaster(te::rst::Raster& inraster, double nmin, double nmax)
702  {
703  if (nmin > nmax)
704  return false;
705 
706  // computing old maximuns and minimums for each band
707  std::vector<double> omins;
708  std::vector<double> omaxs;
709  std::vector<unsigned int> bands;
710 
711  for (unsigned int b = 0; b < inraster.getNumberOfBands(); b++)
712  {
713  omins.push_back(inraster.getBand(b)->getMinValue(true).real());
714  omaxs.push_back(inraster.getBand(b)->getMaxValue(true).real());
715 
716  bands.push_back(b);
717  }
718 
719  // calculating amplitudes to avoid multiple subtractions
720  double value;
721  const double namplitude = nmax - nmin;
722  std::vector<double> oamplitude;
723  for (unsigned int b = 0; b < inraster.getNumberOfBands(); b++)
724  oamplitude.push_back(omaxs[b] - omins[b]);
725 
726  // iterating over raster to normalize pixel values
729 
730  while (it != itend)
731  {
732  for (unsigned int b = 0; b < inraster.getNumberOfBands(); b++)
733  {
734  value = ((*it)[b] - omins[b]) * namplitude / oamplitude[b] + nmin;
735 
736  inraster.setValue(it.getColumn(), it.getRow(), value, b);
737  }
738 
739  ++it;
740  }
741 
742  return true;
743  }
744 
745  bool ConvertRGB2IHS( const te::rst::Raster& inputRGBRaster,
746  const unsigned int redBandIdx, const unsigned int greenBandIdx,
747  const unsigned int blueBandIdx, const double rgbRangeMin,
748  const double rgbRangeMax, te::rst::Raster& outputIHSRaster )
749  {
750  if( ( inputRGBRaster.getAccessPolicy() & te::common::RAccess ) == 0 )
751  return false;
752  if( redBandIdx > inputRGBRaster.getNumberOfBands() ) return false;
753  if( greenBandIdx > inputRGBRaster.getNumberOfBands() ) return false;
754  if( blueBandIdx > inputRGBRaster.getNumberOfBands() ) return false;
755  if( ( outputIHSRaster.getAccessPolicy() & te::common::WAccess ) == 0 )
756  return false;
757  if( outputIHSRaster.getNumberOfBands() < 3 ) return false;
758  if( inputRGBRaster.getNumberOfColumns() != outputIHSRaster.getNumberOfColumns() )
759  return false;
760  if( inputRGBRaster.getNumberOfRows() != outputIHSRaster.getNumberOfRows() )
761  return false;
762 
763  const te::rst::Band& redBand = *inputRGBRaster.getBand( redBandIdx );
764  const te::rst::Band& greenBand = *inputRGBRaster.getBand( greenBandIdx );
765  const te::rst::Band& blueBand = *inputRGBRaster.getBand( blueBandIdx );
766 
767  const unsigned int outNCols = outputIHSRaster.getNumberOfColumns();
768  const unsigned int outNRows = outputIHSRaster.getNumberOfRows();
769  const double redNoData = redBand.getProperty()->m_noDataValue;
770  const double greenNoData = greenBand.getProperty()->m_noDataValue;
771  const double blueNoData = inputRGBRaster.getBand(
772  blueBandIdx )->getProperty()->m_noDataValue;
773 
774  unsigned int outRow = 0;
775  unsigned int outCol = 0;
776  double red = 0;
777  double green = 0;
778  double blue = 0;
779  double teta = 0;
780  double redNorm = 0, greenNorm = 0, blueNorm = 0;
781  double rMinusG = 0, rMinusB = 0;
782  double rgbSum = 0;
783  double cosValue = 0;
784  const double twoPi = 2.0 * ((double)M_PI);
785  const double pi2 = ((double)M_PI) / 2.0;
786  const double rgbNormFac = ( rgbRangeMax == rgbRangeMin ) ? 0.0 :
787  ( 1.0 / ( rgbRangeMax - rgbRangeMin ) );
788  double intensity = 0;
789  double hue = 0;
790  double saturation = 0;
791 
792  for( outRow = 0 ; outRow < outNRows ; ++outRow )
793  {
794  for( outCol = 0 ; outCol < outNCols ; ++outCol )
795  {
796  redBand.getValue( outCol, outRow, red );
797  greenBand.getValue( outCol, outRow, green );
798  blueBand.getValue( outCol, outRow, blue );
799 
800  if( ( red == redNoData ) || ( green == greenNoData ) ||
801  ( blue == blueNoData ) )
802  {
803  intensity = 0.0;
804  hue = 0.0;
805  saturation = 0.0;
806  }
807  else
808  {
809  if( ( red == green ) && ( green == blue ) )
810  { // Gray scale case
811  // From Wikipedia:
812  // h = 0 is used for grays though the hue has no geometric
813  // meaning there, where the saturation s = 0. Similarly,
814  // the choice of 0 as the value for s when l is equal to 0 or 1
815  // is arbitrary.
816 
817  hue = 0.0;
818  saturation = 0.0;
819  intensity = ( red * rgbNormFac ); // or green or blue since they all are the same.
820  }
821  else
822  { // Color case
823  redNorm = ( red - rgbRangeMin ) * rgbNormFac;
824  greenNorm = ( green - rgbRangeMin ) * rgbNormFac;
825  blueNorm = ( blue - rgbRangeMin ) * rgbNormFac;
826 
827  rMinusG = redNorm - greenNorm;
828  rMinusB = redNorm - blueNorm;
829 
830  cosValue = std::sqrt( ( rMinusG * rMinusG ) + ( rMinusB *
831  ( greenNorm - blueNorm ) ) );
832 
833  if( cosValue == 0.0 )
834  {
835  teta = pi2;
836  }
837  else
838  {
839  cosValue = ( 0.5 * ( rMinusG + rMinusB ) ) /
840  cosValue;
841  teta = std::acos( cosValue );
842  }
843 
844  assert( ( cosValue >= (-1.0) ) && ( cosValue <= (1.0) ) );
845 
846  if( blueNorm > greenNorm )
847  {
848  hue = ( twoPi - teta );
849  }
850  else
851  {
852  hue = teta;
853  }
854 
855  rgbSum = ( redNorm + greenNorm + blueNorm );
856 
857  saturation = ( 1.0 - ( 3 * std::min( std::min( redNorm, greenNorm ), blueNorm ) /
858  rgbSum ) );
859 
860  intensity = ( rgbSum / 3.0 );
861  }
862  }
863 
864  outputIHSRaster.setValue( outCol, outRow, intensity, 0 );
865  outputIHSRaster.setValue( outCol, outRow, hue, 1 );
866  outputIHSRaster.setValue( outCol, outRow, saturation, 2 );
867  }
868  }
869 
870  return true;
871  }
872 
873  bool ConvertIHS2RGB( const te::rst::Raster& inputIHSRaster,
874  const unsigned int intensityBandIdx, const unsigned int hueBandIdx,
875  const unsigned int saturationBandIdx, const double rgbRangeMin,
876  const double rgbRangeMax, te::rst::Raster& outputRGBRaster )
877  {
878  if( ( inputIHSRaster.getAccessPolicy() & te::common::RAccess ) == 0 )
879  return false;
880  if( intensityBandIdx > inputIHSRaster.getNumberOfBands() ) return false;
881  if( hueBandIdx > inputIHSRaster.getNumberOfBands() ) return false;
882  if( saturationBandIdx > inputIHSRaster.getNumberOfBands() ) return false;
883  if( ( outputRGBRaster.getAccessPolicy() & te::common::WAccess ) == 0 )
884  return false;
885  if( outputRGBRaster.getNumberOfBands() < 3 ) return false;
886  if( inputIHSRaster.getNumberOfColumns() != outputRGBRaster.getNumberOfColumns() )
887  return false;
888  if( inputIHSRaster.getNumberOfRows() != outputRGBRaster.getNumberOfRows() )
889  return false;
890 
891  const unsigned int nCols = outputRGBRaster.getNumberOfColumns();
892  const unsigned int nRows = outputRGBRaster.getNumberOfRows();
893  const double intensityNoData = inputIHSRaster.getBand(
894  intensityBandIdx )->getProperty()->m_noDataValue;
895  const double hueNoData = inputIHSRaster.getBand(
896  hueBandIdx )->getProperty()->m_noDataValue;
897  const double saturationNoData = inputIHSRaster.getBand(
898  saturationBandIdx )->getProperty()->m_noDataValue;
899 
900  const double rgbNormFac = ( rgbRangeMax == rgbRangeMin ) ? 0.0 :
901  ( rgbRangeMax - rgbRangeMin );
902  const double pi3 = M_PI / 3.0; // 60
903  const double twoPi3 = 2.0 * M_PI / 3.0; // 120
904  const double fourPi3 = 4.0 * M_PI / 3.0; // 240
905  unsigned int row = 0;
906  unsigned int col = 0;
907  double hue = 0;
908  double lig = 0;
909  double sat = 0;
910  double red = 0;
911  double green = 0;
912  double blue = 0;
913  const te::rst::Band& intensityBand = *inputIHSRaster.getBand( intensityBandIdx );
914  const te::rst::Band& hueBand = *inputIHSRaster.getBand( hueBandIdx );
915  const te::rst::Band& saturationBand = *inputIHSRaster.getBand( saturationBandIdx );
916  te::rst::Band& redBand = *outputRGBRaster.getBand( 0 );
917  te::rst::Band& greenBand = *outputRGBRaster.getBand( 1 );
918  te::rst::Band& blueBand = *outputRGBRaster.getBand( 2 );
919 
920  for( row = 0 ; row < nRows ; ++row )
921  {
922  for( col = 0 ; col < nCols ; ++col )
923  {
924  intensityBand.getValue( col, row, lig );
925  hueBand.getValue( col, row, hue );
926  saturationBand.getValue( col, row, sat );
927 
928  if( ( lig == intensityNoData ) || ( hue == hueNoData ) ||
929  ( sat == saturationNoData ) )
930  {
931  red = green = blue = 0;
932  }
933  else
934  {
935  if( ( hue == 0.0 ) && ( sat == 0.0 ) )
936  { // Gray scale case
937  red = green = blue = ( lig * rgbNormFac );
938  }
939  else
940  { // color case
941  /* Hue inside RG sector */
942  if( hue < twoPi3 )
943  {
944  blue = lig * ( 1.0 - sat );
945  red = lig * ( 1.0 + ( sat * std::cos( hue ) /
946  std::cos( pi3 - hue ) ) );
947  green = ( 3.0 * lig ) - ( red + blue );
948  }
949  else if( hue < fourPi3 )
950  { /* Hue inside GB sector */
951 
952  hue -= twoPi3;
953 
954  red = lig * ( 1.0 - sat );
955  green = lig * ( 1.0 + ( sat * std::cos( hue ) /
956  std::cos( pi3 - hue ) ) );
957  blue = ( 3.0 * lig ) - ( red + green );
958  }
959  else
960  { /* Hue inside BR sector */
961 
962  hue -= fourPi3;
963 
964  green = lig * ( 1.0 - sat );
965  blue = lig * ( 1.0 + ( sat * std::cos( hue ) /
966  std::cos( pi3 - hue ) ) );
967  red = ( 3.0 * lig ) - ( green + blue );
968  }
969 
970  red = ( red * rgbNormFac ) + rgbRangeMin;
971  green = ( green * rgbNormFac ) + rgbRangeMin;
972  blue = ( blue * rgbNormFac ) + rgbRangeMin;
973  }
974  }
975 
976  red = MIN( red, rgbRangeMax );
977  green = MIN( green, rgbRangeMax );
978  blue = MIN( blue, rgbRangeMax );
979 
980  red = MAX( red, rgbRangeMin );
981  green = MAX( green, rgbRangeMin );
982  blue = MAX( blue, rgbRangeMin );
983 
984  redBand.setValue( col, row, red );
985  greenBand.setValue( col, row, green );
986  blueBand.setValue( col, row, blue );
987  }
988  }
989 
990  return true;
991  }
992 
994  {
995  paramsPtr->m_mutexPtr->lock();
996  const unsigned int blockElementsNumber = paramsPtr->m_inputBandPtr->getProperty()->m_blkh *
997  paramsPtr->m_inputBandPtr->getProperty()->m_blkw;
998  const int blockDataType = paramsPtr->m_inputBandPtr->getProperty()->getType();
999  const int blockSizeBytes = paramsPtr->m_inputBandPtr->getBlockSize();
1000  const double noDataValue = paramsPtr->m_inputBandPtr->getProperty()->m_noDataValue;
1001  paramsPtr->m_mutexPtr->unlock();
1002 
1003  boost::scoped_array< unsigned char > blockBuffer( new unsigned char[ blockSizeBytes ] );
1004  boost::scoped_array< double > doubleBuffer( new double[ blockElementsNumber ] );
1005  unsigned blkX = 0;
1006  unsigned int elementIdx = 0;
1007  double mean = 0;
1008  double meanElementsNumber = 0;
1009 
1010  for( unsigned blkY = 0 ; blkY < paramsPtr->m_rasterBlocksStatusPtr->getLinesNumber() ;
1011  ++blkY )
1012  {
1013  for( blkX = 0 ; blkX < paramsPtr->m_rasterBlocksStatusPtr->getColumnsNumber() ;
1014  ++blkX )
1015  {
1016  paramsPtr->m_mutexPtr->lock();
1017 
1018  if( paramsPtr->m_rasterBlocksStatusPtr->operator()( blkY, blkX ) )
1019  {
1020  paramsPtr->m_mutexPtr->unlock();
1021  }
1022  else
1023  {
1024  paramsPtr->m_rasterBlocksStatusPtr->operator()( blkY, blkX ) = true;
1025 
1026  paramsPtr->m_inputBandPtr->read( blkX, blkY, blockBuffer.get() );
1027 
1028  paramsPtr->m_mutexPtr->unlock();
1029 
1030  Convert2DoublesVector( blockBuffer.get(), blockDataType, blockElementsNumber,
1031  doubleBuffer.get() );
1032 
1033  for( elementIdx = 0 ; elementIdx < blockElementsNumber ; ++elementIdx )
1034  {
1035  if( noDataValue != doubleBuffer[ elementIdx ] )
1036  {
1037  mean =
1038  (
1039  (
1040  mean
1041  *
1042  meanElementsNumber
1043  )
1044  +
1045  doubleBuffer[ elementIdx ]
1046  )
1047  /
1048  (
1049  meanElementsNumber
1050  +
1051  1.0
1052  );
1053 
1054  meanElementsNumber = meanElementsNumber + 1.0;
1055  }
1056  }
1057  }
1058  }
1059  }
1060 
1061  paramsPtr->m_mutexPtr->lock();
1062  ( *(paramsPtr->m_meanValuePtr) ) =
1063  (
1064  (
1065  ( *(paramsPtr->m_meanValuePtr) )
1066  *
1067  ( *( paramsPtr->m_pixelsNumberValuePtr ) )
1068  )
1069  +
1070  (
1071  mean
1072  *
1073  meanElementsNumber
1074  )
1075  )
1076  /
1077  (
1078  ( *( paramsPtr->m_pixelsNumberValuePtr ) )
1079  +
1080  meanElementsNumber
1081  );
1082  ( *( paramsPtr->m_pixelsNumberValuePtr ) ) += meanElementsNumber;
1083  paramsPtr->m_mutexPtr->unlock();
1084  }
1085 
1086  bool GetMeanValue( const te::rst::Band& band,
1087  const unsigned int maxThreads,
1088  double& meanValue )
1089  {
1090  Matrix< bool > rasterBlocksStatus;
1091  if( ! rasterBlocksStatus.reset( band.getProperty()->m_nblocksy,
1093  {
1094  return false;
1095  }
1096  for( unsigned int row = 0 ; row < rasterBlocksStatus.getLinesNumber() ;
1097  ++row )
1098  {
1099  for( unsigned int col = 0 ; col < rasterBlocksStatus.getColumnsNumber() ;
1100  ++col )
1101  {
1102  rasterBlocksStatus( row, col ) = false;
1103  }
1104  }
1105 
1106  meanValue = 0.0;
1107 
1108  double pixelsNumber = 0.0;
1109 
1110  boost::mutex mutex;
1111 
1112  GetMeanValueThreadParams params;
1113  params.m_inputBandPtr = &band;
1114  params.m_rasterBlocksStatusPtr = &rasterBlocksStatus;
1115  params.m_meanValuePtr = &meanValue;
1116  params.m_pixelsNumberValuePtr = &pixelsNumber;
1117  params.m_returnStatus = true;
1118  params.m_mutexPtr = &mutex;
1119 
1120  if( maxThreads == 1 )
1121  {
1122  GetMeanValueThread( &params );
1123  }
1124  else
1125  {
1126  unsigned int threadsNumber = maxThreads ? maxThreads : te::common::GetPhysProcNumber();
1127 
1128  boost::thread_group threads;
1129 
1130  for( unsigned int threadIdx = 0 ; threadIdx < threadsNumber ;
1131  ++threadIdx )
1132  {
1133  threads.add_thread( new boost::thread( GetMeanValueThread,
1134  &params ) );
1135  };
1136 
1137  threads.join_all();
1138  }
1139 
1140  return params.m_returnStatus;
1141  }
1142 
1144  {
1145  paramsPtr->m_mutexPtr->lock();
1146  const unsigned int blockElementsNumber = paramsPtr->m_inputBandPtr->getProperty()->m_blkh *
1147  paramsPtr->m_inputBandPtr->getProperty()->m_blkw;
1148  const int blockDataType = paramsPtr->m_inputBandPtr->getProperty()->getType();
1149  const int blockSizeBytes = paramsPtr->m_inputBandPtr->getBlockSize();
1150  const double noDataValue = paramsPtr->m_inputBandPtr->getProperty()->m_noDataValue;
1151  const double& meanValue = paramsPtr->m_meanValue;
1152  paramsPtr->m_mutexPtr->unlock();
1153 
1154  boost::scoped_array< unsigned char > blockBuffer( new unsigned char[ blockSizeBytes ] );
1155  boost::scoped_array< double > doubleBuffer( new double[ blockElementsNumber ] );
1156  unsigned blkX = 0;
1157  unsigned int elementIdx = 0;
1158  double diff = 0;
1159  double squaresDifsSum = 0;
1160  double elementsNumber = 0;
1161 
1162 
1163  for( unsigned blkY = 0 ; blkY < paramsPtr->m_rasterBlocksStatusPtr->getLinesNumber() ;
1164  ++blkY )
1165  {
1166  for( blkX = 0 ; blkX < paramsPtr->m_rasterBlocksStatusPtr->getColumnsNumber() ;
1167  ++blkX )
1168  {
1169  paramsPtr->m_mutexPtr->lock();
1170 
1171  if( paramsPtr->m_rasterBlocksStatusPtr->operator()( blkY, blkX ) )
1172  {
1173  paramsPtr->m_mutexPtr->unlock();
1174  }
1175  else
1176  {
1177  paramsPtr->m_rasterBlocksStatusPtr->operator()( blkY, blkX ) = true;
1178 
1179  paramsPtr->m_inputBandPtr->read( blkX, blkY, blockBuffer.get() );
1180 
1181  paramsPtr->m_mutexPtr->unlock();
1182 
1183  Convert2DoublesVector( blockBuffer.get(), blockDataType, blockElementsNumber,
1184  doubleBuffer.get() );
1185 
1186  for( elementIdx = 0 ; elementIdx < blockElementsNumber ; ++elementIdx )
1187  {
1188  if( noDataValue != doubleBuffer[ elementIdx ] )
1189  {
1190  diff = doubleBuffer[ elementIdx ] - meanValue;
1191  diff *= diff;
1192  squaresDifsSum += diff;
1193  elementsNumber = elementsNumber + 1.0;
1194  }
1195  }
1196  }
1197  }
1198  }
1199 
1200  paramsPtr->m_mutexPtr->lock();
1201  ( *(paramsPtr->m_stdDevValuePtr) ) =
1202  (
1203  ( *(paramsPtr->m_stdDevValuePtr) )
1204  +
1205  squaresDifsSum
1206  );
1207  ( *( paramsPtr->m_pixelsNumberValuePtr ) ) += elementsNumber;
1208  paramsPtr->m_mutexPtr->unlock();
1209  }
1210 
1211  bool GetStdDevValue( const te::rst::Band& band,
1212  const unsigned int maxThreads, double const * const meanValuePtr,
1213  double& stdDevValue )
1214  {
1215  double mean = 0;
1216  if( meanValuePtr )
1217  {
1218  mean = (*meanValuePtr);
1219  }
1220  else
1221  {
1222  if( ! GetMeanValue( band, maxThreads, mean ) )
1223  {
1224  return false;
1225  }
1226  }
1227 
1228  Matrix< bool > rasterBlocksStatus;
1229  if( ! rasterBlocksStatus.reset( band.getProperty()->m_nblocksy,
1231  {
1232  return false;
1233  }
1234  for( unsigned int row = 0 ; row < rasterBlocksStatus.getLinesNumber() ;
1235  ++row )
1236  {
1237  for( unsigned int col = 0 ; col < rasterBlocksStatus.getColumnsNumber() ;
1238  ++col )
1239  {
1240  rasterBlocksStatus( row, col ) = false;
1241  }
1242  }
1243 
1244  stdDevValue = 0.0;
1245 
1246  double pixelsNumber = 0.0;
1247 
1248  boost::mutex mutex;
1249 
1251  params.m_inputBandPtr = &band;
1252  params.m_rasterBlocksStatusPtr = &rasterBlocksStatus;
1253  params.m_stdDevValuePtr = &stdDevValue;
1254  params.m_pixelsNumberValuePtr = &pixelsNumber;
1255  params.m_meanValue = mean;
1256  params.m_returnStatus = true;
1257  params.m_mutexPtr = &mutex;
1258 
1259  if( maxThreads == 1 )
1260  {
1261  GetStdDevValueThread( &params );
1262  }
1263  else
1264  {
1265  unsigned int threadsNumber = maxThreads ? maxThreads : te::common::GetPhysProcNumber();
1266 
1267  boost::thread_group threads;
1268 
1269  for( unsigned int threadIdx = 0 ; threadIdx < threadsNumber ;
1270  ++threadIdx )
1271  {
1272  threads.add_thread( new boost::thread( GetStdDevValueThread,
1273  &params ) );
1274  };
1275 
1276  threads.join_all();
1277  }
1278 
1279  stdDevValue = std::sqrt( stdDevValue / pixelsNumber );
1280 
1281  return params.m_returnStatus;
1282  }
1283 
1285  {
1286  paramsPtr->m_mutexPtr->lock();
1287  const unsigned int blockElementsNumber = paramsPtr->m_inputBand1Ptr->getProperty()->m_blkh *
1288  paramsPtr->m_inputBand1Ptr->getProperty()->m_blkw;
1289  const int blockDataType1 = paramsPtr->m_inputBand1Ptr->getProperty()->getType();
1290  const int blockDataType2 = paramsPtr->m_inputBand2Ptr->getProperty()->getType();
1291  const int blockSizeBytes1 = paramsPtr->m_inputBand1Ptr->getBlockSize();
1292  const int blockSizeBytes2 = paramsPtr->m_inputBand2Ptr->getBlockSize();
1293  const double noDataValue1 = paramsPtr->m_inputBand1Ptr->getProperty()->m_noDataValue;
1294  const double noDataValue2 = paramsPtr->m_inputBand2Ptr->getProperty()->m_noDataValue;
1295  paramsPtr->m_mutexPtr->unlock();
1296 
1297  boost::scoped_array< unsigned char > blockBuffer1( new unsigned char[ blockSizeBytes1 ] );
1298  boost::scoped_array< unsigned char > blockBuffer2( new unsigned char[ blockSizeBytes2 ] );
1299  boost::scoped_array< double > doubleBuffer1( new double[ blockElementsNumber ] );
1300  boost::scoped_array< double > doubleBuffer2( new double[ blockElementsNumber ] );
1301  unsigned blkX = 0;
1302  unsigned int elementIdx = 0;
1303  double covariance = 0;
1304  double elementsNumber = 0;
1305  double diff1 = 0;
1306  double diff2 = 0;
1307 
1308  for( unsigned blkY = 0 ; blkY < paramsPtr->m_rasterBlocksStatusPtr->getLinesNumber() ;
1309  ++blkY )
1310  {
1311  for( blkX = 0 ; blkX < paramsPtr->m_rasterBlocksStatusPtr->getColumnsNumber() ;
1312  ++blkX )
1313  {
1314  paramsPtr->m_mutexPtr->lock();
1315 
1316  if( paramsPtr->m_rasterBlocksStatusPtr->operator()( blkY, blkX ) )
1317  {
1318  paramsPtr->m_mutexPtr->unlock();
1319  }
1320  else
1321  {
1322  paramsPtr->m_rasterBlocksStatusPtr->operator()( blkY, blkX ) = true;
1323 
1324  paramsPtr->m_inputBand1Ptr->read( blkX, blkY, blockBuffer1.get() );
1325  paramsPtr->m_inputBand2Ptr->read( blkX, blkY, blockBuffer2.get() );
1326 
1327  paramsPtr->m_mutexPtr->unlock();
1328 
1329  Convert2DoublesVector( blockBuffer1.get(), blockDataType1, blockElementsNumber,
1330  doubleBuffer1.get() );
1331  Convert2DoublesVector( blockBuffer2.get(), blockDataType2, blockElementsNumber,
1332  doubleBuffer2.get() );
1333 
1334  for( elementIdx = 0 ; elementIdx < blockElementsNumber ; ++elementIdx )
1335  {
1336  if( ( noDataValue1 != doubleBuffer1[ elementIdx ] ) &&
1337  ( noDataValue2 != doubleBuffer2[ elementIdx ] ) )
1338  {
1339  diff1 = doubleBuffer1[ elementIdx ] - paramsPtr->m_mean1Value;
1340  diff2 = doubleBuffer2[ elementIdx ] - paramsPtr->m_mean2Value;
1341 
1342  covariance += ( diff1 * diff2 );
1343 
1344  elementsNumber = elementsNumber + 1.0;
1345  }
1346  }
1347  }
1348  }
1349  }
1350 
1351  paramsPtr->m_mutexPtr->lock();
1352  ( *(paramsPtr->m_covarianceValuePtr) ) += covariance;
1353  ( *( paramsPtr->m_pixelsNumberValuePtr ) ) += elementsNumber;
1354  paramsPtr->m_mutexPtr->unlock();
1355  }
1356 
1357  bool GetCovarianceValue( const te::rst::Band& band1,
1358  const te::rst::Band& band2, const unsigned int maxThreads,
1359  double const * const mean1ValuePtr, double const * const mean2ValuePtr,
1360  double& covarianceValue )
1361  {
1362  if( ( band1.getProperty()->m_blkw * band1.getProperty()->m_nblocksx ) !=
1363  ( band2.getProperty()->m_blkw * band2.getProperty()->m_nblocksx ) )
1364  {
1365  return false;
1366  }
1367 
1368  if( ( band1.getProperty()->m_blkh * band1.getProperty()->m_nblocksy ) !=
1369  ( band2.getProperty()->m_blkh * band2.getProperty()->m_nblocksy ) )
1370  {
1371  return false;
1372  }
1373 
1374  double mean1 = 0;
1375  if( mean1ValuePtr )
1376  {
1377  mean1 = (*mean1ValuePtr);
1378  }
1379  else
1380  {
1381  if( ! GetMeanValue( band1, maxThreads, mean1 ) )
1382  {
1383  return false;
1384  }
1385  }
1386 
1387  double mean2 = 0;
1388  if( mean2ValuePtr )
1389  {
1390  mean2 = (*mean2ValuePtr);
1391  }
1392  else
1393  {
1394  if( ! GetMeanValue( band2, maxThreads, mean2 ) )
1395  {
1396  return false;
1397  }
1398  }
1399 
1400  if( ( band1.getProperty()->m_blkw == band2.getProperty()->m_blkw )
1401  && ( band1.getProperty()->m_blkh == band2.getProperty()->m_blkh )
1402  && ( band1.getProperty()->m_nblocksx == band2.getProperty()->m_nblocksx )
1403  && ( band1.getProperty()->m_nblocksy == band2.getProperty()->m_nblocksy ) )
1404  {
1405  Matrix< bool > rasterBlocksStatus;
1406  if( ! rasterBlocksStatus.reset( band1.getProperty()->m_nblocksy,
1408  {
1409  return false;
1410  }
1411  for( unsigned int row = 0 ; row < rasterBlocksStatus.getLinesNumber() ;
1412  ++row )
1413  {
1414  for( unsigned int col = 0 ; col < rasterBlocksStatus.getColumnsNumber() ;
1415  ++col )
1416  {
1417  rasterBlocksStatus( row, col ) = false;
1418  }
1419  }
1420 
1421  covarianceValue = 0.0;
1422 
1423  double pixelsNumber = 0.0;
1424 
1425  boost::mutex mutex;
1426 
1428  params.m_inputBand1Ptr = &band1;
1429  params.m_inputBand2Ptr = &band2;
1430  params.m_rasterBlocksStatusPtr = &rasterBlocksStatus;
1431  params.m_covarianceValuePtr = &covarianceValue;
1432  params.m_pixelsNumberValuePtr = &pixelsNumber;
1433  params.m_returnStatus = true;
1434  params.m_mutexPtr = &mutex;
1435  params.m_mean1Value = mean1;
1436  params.m_mean2Value = mean2;
1437 
1438  if( maxThreads == 1 )
1439  {
1440  GetCovarianceValueThread( &params );
1441  }
1442  else
1443  {
1444  unsigned int threadsNumber = maxThreads ? maxThreads : te::common::GetPhysProcNumber();
1445 
1446  boost::thread_group threads;
1447 
1448  for( unsigned int threadIdx = 0 ; threadIdx < threadsNumber ;
1449  ++threadIdx )
1450  {
1451  threads.add_thread( new boost::thread( GetCovarianceValueThread,
1452  &params ) );
1453  };
1454 
1455  threads.join_all();
1456  }
1457 
1458  if( pixelsNumber != 0.0 )
1459  {
1460  covarianceValue /= pixelsNumber;
1461  }
1462 
1463  return params.m_returnStatus;
1464  }
1465  else
1466  {
1467  const unsigned int nCols = band1.getProperty()->m_blkw * band1.getProperty()->m_nblocksx;
1468  const unsigned int nRows = band1.getProperty()->m_blkh * band1.getProperty()->m_nblocksy;
1469  double value1 = 0;
1470  double value2 = 0;
1471  const double noDataValue1 = band1.getProperty()->m_noDataValue;
1472  const double noDataValue2 = band2.getProperty()->m_noDataValue;
1473  unsigned int col = 0;
1474  double pixelsNumber = 0.0;
1475 
1476  covarianceValue = 0;
1477 
1478  for( unsigned int row = 0 ; row < nRows ; ++row )
1479  {
1480  for( col = 0 ; col < nCols ; ++col )
1481  {
1482  band1.getValue( col, row, value1 );
1483  band2.getValue( col, row, value2 );
1484 
1485  if( ( noDataValue1 != value1 ) &&
1486  ( noDataValue2 != value2 ) )
1487  {
1488  covarianceValue += ( ( value1 - mean1 ) * ( value2 - mean2 ) );
1489 
1490  pixelsNumber = pixelsNumber + 1.0;
1491  }
1492  }
1493  }
1494 
1495  if( pixelsNumber != 0.0 )
1496  {
1497  covarianceValue /= pixelsNumber;
1498  }
1499 
1500  return true;
1501  }
1502  }
1503 
1505  const te::rst::Raster& inputRaster,
1506  const std::vector< unsigned int >& inputRasterBands,
1507  boost::numeric::ublas::matrix< double >& pcaMatrix,
1508  te::rst::Raster& pcaRaster,
1509  const std::vector< unsigned int >& pcaRasterBands,
1510  const unsigned int maxThreads )
1511  {
1512  if( ( inputRaster.getAccessPolicy() & te::common::RAccess ) == 0 )
1513  {
1514  return false;
1515  }
1516  if( ( pcaRaster.getAccessPolicy() & te::common::WAccess ) == 0 )
1517  {
1518  return false;
1519  }
1520  if( inputRaster.getNumberOfBands() != pcaRaster.getNumberOfBands() )
1521  {
1522  return false;
1523  }
1524  if( pcaRaster.getNumberOfBands() != inputRasterBands.size() )
1525  {
1526  return false;
1527  }
1528  if( inputRaster.getNumberOfRows() != pcaRaster.getNumberOfRows() )
1529  {
1530  return false;
1531  }
1532  if( inputRaster.getNumberOfColumns() != pcaRaster.getNumberOfColumns() )
1533  {
1534  return false;
1535  }
1536 
1537  // Covariance matrix
1538 
1539  boost::numeric::ublas::matrix< double > covarianceMatrix;
1540 
1541  covarianceMatrix.resize( inputRasterBands.size(), inputRasterBands.size() );
1542 
1543  for( unsigned int covMatrixIdx1 = 0 ; covMatrixIdx1 < inputRasterBands.size() ;
1544  ++covMatrixIdx1 )
1545  {
1546  if( inputRasterBands[ covMatrixIdx1 ] >= inputRaster.getNumberOfBands() )
1547  {
1548  return false;
1549  }
1550 
1551  for( unsigned int covMatrixIdx2 = 0 ; covMatrixIdx2 < inputRasterBands.size() ;
1552  ++covMatrixIdx2 )
1553  {
1554  if( inputRasterBands[ covMatrixIdx2 ] >= inputRaster.getNumberOfBands() )
1555  {
1556  return false;
1557  }
1558 
1559  if( covMatrixIdx1 > covMatrixIdx2 )
1560  {
1561  covarianceMatrix( covMatrixIdx1, covMatrixIdx2 ) =
1562  covarianceMatrix( covMatrixIdx2, covMatrixIdx1 );
1563  }
1564  else
1565  {
1566  if( ! GetCovarianceValue( *( inputRaster.getBand( inputRasterBands[ covMatrixIdx1 ] ) ),
1567  *( inputRaster.getBand( inputRasterBands[ covMatrixIdx2 ] ) ),
1568  maxThreads, 0, 0, covarianceMatrix( covMatrixIdx1, covMatrixIdx2 ) ) )
1569  {
1570  return false;
1571  }
1572  }
1573  }
1574  }
1575 
1576 //std::cout << std::endl << "Covariance matrix:" << covarianceMatrix << std::endl;
1577 
1578  // Eigen stuff
1579 
1580  boost::numeric::ublas::matrix< double > eigenValues;
1581  boost::numeric::ublas::matrix< double > eigenVectors;
1582 
1583  if( ! te::common::EigenVectors( covarianceMatrix, eigenVectors, eigenValues ) )
1584  {
1585  return false;
1586  }
1587 
1588  pcaMatrix = boost::numeric::ublas::trans( eigenVectors );
1589 
1590  return RemapValues( inputRaster, inputRasterBands, pcaMatrix, pcaRaster,
1591  pcaRasterBands, maxThreads );
1592  }
1593 
1595  const te::rst::Raster& pcaRaster,
1596  const boost::numeric::ublas::matrix< double >& pcaMatrix,
1597  te::rst::Raster& outputRaster,
1598  const std::vector< unsigned int >& outputRasterBands,
1599  const unsigned int maxThreads )
1600  {
1601  boost::numeric::ublas::matrix< double > inversePcaMatrix;
1602  if( ! te::common::GetInverseMatrix( pcaMatrix, inversePcaMatrix ) )
1603  {
1604  return false;
1605  }
1606 
1607  std::vector< unsigned int > inputRasterBands;
1608 
1609  for( unsigned int bandIdx = 0 ; bandIdx < pcaRaster.getNumberOfBands() ;
1610  ++bandIdx )
1611  {
1612  inputRasterBands.push_back( bandIdx );
1613  }
1614 
1615  return RemapValues( pcaRaster, inputRasterBands, inversePcaMatrix,
1616  outputRaster, outputRasterBands, maxThreads );
1617  }
1618 
1620  {
1621  paramsPtr->m_mutexPtr->lock();
1622 
1623  assert( paramsPtr->m_inputRasterBandsPtr->operator[]( 0 ) <
1624  paramsPtr->m_inputRasterPtr->getNumberOfBands() );
1625  const unsigned int blockElementsNumber = paramsPtr->m_inputRasterPtr->getBand(
1626  paramsPtr->m_inputRasterBandsPtr->operator[]( 0 ) )->getProperty()->m_blkh *
1627  paramsPtr->m_inputRasterPtr->getBand( paramsPtr->m_inputRasterBandsPtr->operator[]( 0 ) )->getProperty()->m_blkw;
1628 
1629  const std::vector< unsigned int > inputRasterBands = *( paramsPtr->m_inputRasterBandsPtr );
1630  const unsigned int inputRasterBandsSize = (unsigned int)inputRasterBands.size();
1631  const std::vector< unsigned int > outputRasterBands = *( paramsPtr->m_inputRasterBandsPtr );
1632  assert( inputRasterBandsSize == paramsPtr->m_outputRasterPtr->getNumberOfBands() );
1633  assert( inputRasterBandsSize == outputRasterBands.size() );
1634 
1635  unsigned int maxBlocksSizesBytes = 0;
1636  std::vector< double > inputBandsNoDataValues( inputRasterBandsSize, 0.0 );
1637  std::vector< double > outputBandsNoDataValues( inputRasterBandsSize, 0.0 );
1638  std::vector< boost::shared_array< double > > inDoubleBuffersHandlers( inputRasterBandsSize );
1639  std::vector< boost::shared_array< double > > outDoubleBuffersHandlers( inputRasterBandsSize );
1640  unsigned int inputRasterBandsIdx = 0;
1641  boost::numeric::ublas::matrix< double > remapMatrix = *( paramsPtr->m_remapMatrixPtr );
1642  std::vector< double > outputMinValue( inputRasterBandsSize );
1643  std::vector< double > outputMaxValue( inputRasterBandsSize );
1644  boost::shared_array< double* > inDoubleBuffersPtrsHandler( new double*[ inputRasterBandsSize ] );
1645  boost::shared_array< double* > outDoubleBuffersPtrsHandler( new double*[ inputRasterBandsSize ] );
1646  double** inDoubleBuffers = inDoubleBuffersPtrsHandler.get();
1647  double** outDoubleBuffers = outDoubleBuffersPtrsHandler.get();
1648 
1649  for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBandsSize ;
1650  ++inputRasterBandsIdx )
1651  {
1652  const unsigned int& inBandIdx = inputRasterBands[ inputRasterBandsIdx ];
1653  assert( inBandIdx < paramsPtr->m_inputRasterPtr->getNumberOfBands() );
1654 
1655  te::rst::Band const * const inBandPtr = paramsPtr->m_inputRasterPtr->getBand( inBandIdx );
1656  te::rst::Band * const outBandPtr = paramsPtr->m_outputRasterPtr->getBand(
1657  outputRasterBands[ inputRasterBandsIdx ] );
1658 
1659  maxBlocksSizesBytes = std::max( maxBlocksSizesBytes, (unsigned int)inBandPtr->getBlockSize() );
1660  maxBlocksSizesBytes = std::max( maxBlocksSizesBytes, (unsigned int)outBandPtr->getBlockSize() );
1661 
1662  inputBandsNoDataValues[ inputRasterBandsIdx ] = inBandPtr->getProperty()->m_noDataValue;
1663  outputBandsNoDataValues[ inputRasterBandsIdx ] = outBandPtr->getProperty()->m_noDataValue;
1664 
1665  inDoubleBuffersHandlers[ inputRasterBandsIdx ].reset( new double[ blockElementsNumber ] );
1666  inDoubleBuffers[ inputRasterBandsIdx ] = inDoubleBuffersHandlers[ inputRasterBandsIdx ].get();
1667 
1668  outDoubleBuffersHandlers[ inputRasterBandsIdx ].reset( new double[ blockElementsNumber ] );
1669  outDoubleBuffers[ inputRasterBandsIdx ] = outDoubleBuffersHandlers[ inputRasterBandsIdx ].get();
1670 
1672  outBandPtr->getProperty()->getType(),
1673  outputMinValue[ inputRasterBandsIdx ],
1674  outputMaxValue[ inputRasterBandsIdx ] );
1675  }
1676 
1677  paramsPtr->m_mutexPtr->unlock();
1678 
1679  boost::scoped_array< unsigned char > blockBuffer( new unsigned char[ maxBlocksSizesBytes ] );
1680  unsigned blkX = 0;
1681  unsigned int elementIdx = 0;
1682  boost::numeric::ublas::matrix< double > pixelValues( inputRasterBandsSize, 1 );
1683  boost::numeric::ublas::matrix< double > remappedPixelValues( inputRasterBandsSize, 1 );
1684  bool elementIsValid = false;
1685 
1686  for( unsigned blkY = 0 ; blkY < paramsPtr->m_rasterBlocksStatusPtr->getLinesNumber() ;
1687  ++blkY )
1688  {
1689  for( blkX = 0 ; blkX < paramsPtr->m_rasterBlocksStatusPtr->getColumnsNumber() ;
1690  ++blkX )
1691  {
1692  paramsPtr->m_mutexPtr->lock();
1693 
1694  if( paramsPtr->m_rasterBlocksStatusPtr->operator()( blkY, blkX ) )
1695  {
1696  paramsPtr->m_mutexPtr->unlock();
1697  }
1698  else
1699  {
1700  paramsPtr->m_rasterBlocksStatusPtr->operator()( blkY, blkX ) = true;
1701 
1702  // reading the input blocks
1703 
1704  for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBandsSize ;
1705  ++inputRasterBandsIdx )
1706  {
1707  const unsigned int& inBandIdx = inputRasterBands[ inputRasterBandsIdx ];
1708  te::rst::Band const * const inBandPtr = paramsPtr->m_inputRasterPtr->getBand( inBandIdx );
1709 
1710  inBandPtr->read( blkX, blkY, blockBuffer.get() );
1711  Convert2DoublesVector( blockBuffer.get(),
1712  inBandPtr->getProperty()->getType(),
1713  blockElementsNumber,
1714  inDoubleBuffers[ inputRasterBandsIdx ] );
1715  }
1716 
1717  paramsPtr->m_mutexPtr->unlock();
1718 
1719  // Remapping the values using the remap matrix
1720 
1721  for( elementIdx = 0 ; elementIdx < blockElementsNumber ; ++elementIdx )
1722  {
1723  elementIsValid = true;
1724 
1725  for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBandsSize ;
1726  ++inputRasterBandsIdx )
1727  {
1728  if( inDoubleBuffers[ inputRasterBandsIdx ][ elementIdx ] ==
1729  inputBandsNoDataValues[ inputRasterBandsIdx ] )
1730  {
1731  elementIsValid = false;
1732  break;
1733  }
1734  else
1735  {
1736  pixelValues( inputRasterBandsIdx, 0 ) =
1737  inDoubleBuffers[ inputRasterBandsIdx ][ elementIdx ];
1738  }
1739  }
1740 
1741  if( elementIsValid )
1742  {
1743 //std::cout << std::endl << "pixelValues:" << pixelValues << std::endl;
1744 
1745  remappedPixelValues = boost::numeric::ublas::prod( remapMatrix, pixelValues );
1746 
1747 //std::cout << std::endl << "remappedPixelValues:" << remappedPixelValues << std::endl;
1748 
1749  for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBandsSize ;
1750  ++inputRasterBandsIdx )
1751  {
1752  outDoubleBuffers[ inputRasterBandsIdx ][ elementIdx ] =
1753  std::max(
1754  outputMinValue[ inputRasterBandsIdx ],
1755  std::min(
1756  outputMaxValue[ inputRasterBandsIdx ],
1757  remappedPixelValues( inputRasterBandsIdx, 0 )
1758  )
1759  );
1760  }
1761  }
1762  }
1763 
1764  // Writing the remmaped blocks
1765 
1766  paramsPtr->m_mutexPtr->lock();
1767 
1768  for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBandsSize ;
1769  ++inputRasterBandsIdx )
1770  {
1771  te::rst::Band* outBandPtr = paramsPtr->m_outputRasterPtr->getBand(
1772  outputRasterBands[ inputRasterBandsIdx ] );
1773 
1774  ConvertDoublesVector( outDoubleBuffers[ inputRasterBandsIdx ],
1775  blockElementsNumber, outBandPtr->getProperty()->getType(),
1776  blockBuffer.get() );
1777 
1778  outBandPtr->write( blkX, blkY, blockBuffer.get() );
1779  }
1780 
1781  paramsPtr->m_mutexPtr->unlock();
1782  }
1783  }
1784  }
1785 
1786  }
1787 
1789  const te::rst::Raster& inputRaster,
1790  const std::vector< unsigned int >& inputRasterBands,
1791  const boost::numeric::ublas::matrix< double >& remapMatrix,
1792  te::rst::Raster& outputRaster,
1793  const std::vector< unsigned int >& outputRasterBands,
1794  const unsigned int maxThreads )
1795  {
1796  if( ( inputRaster.getAccessPolicy() & te::common::RAccess ) == 0 )
1797  {
1798  return false;
1799  }
1800  if( ( outputRaster.getAccessPolicy() & te::common::WAccess ) == 0 )
1801  {
1802  return false;
1803  }
1804  if( remapMatrix.size1() != inputRasterBands.size() )
1805  {
1806  return false;
1807  }
1808  if( remapMatrix.size2() != inputRasterBands.size() )
1809  {
1810  return false;
1811  }
1812  if( outputRaster.getNumberOfBands() != inputRasterBands.size() )
1813  {
1814  return false;
1815  }
1816  if( inputRaster.getNumberOfRows() != outputRaster.getNumberOfRows() )
1817  {
1818  return false;
1819  }
1820  if( inputRaster.getNumberOfColumns() != outputRaster.getNumberOfColumns() )
1821  {
1822  return false;
1823  }
1824  if( remapMatrix.size1() != outputRasterBands.size() )
1825  {
1826  return false;
1827  }
1828  if( remapMatrix.size2() != outputRasterBands.size() )
1829  {
1830  return false;
1831  }
1832  for( unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBands.size() ;
1833  ++inputRasterBandsIdx )
1834  {
1835  if( inputRasterBands[ inputRasterBandsIdx ] >= inputRaster.getNumberOfBands() )
1836  {
1837  return false;
1838  }
1839  }
1840  for( unsigned int outputRasterBandsIdx = 0 ; outputRasterBandsIdx < outputRasterBands.size() ;
1841  ++outputRasterBandsIdx )
1842  {
1843  if( outputRasterBands[ outputRasterBandsIdx ] >= outputRaster.getNumberOfBands() )
1844  {
1845  return false;
1846  }
1847  }
1848 
1849  // Checking if optmized PCA can be executed
1850 
1851  bool useOptimizedRemap = true;
1852 
1853  {
1854  for( unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
1855  inputRaster.getNumberOfBands() ; ++inputRasterBandsIdx )
1856  {
1857  if(
1858  (
1859  inputRaster.getBand( inputRasterBands[ inputRasterBandsIdx ] )->getProperty()->m_blkw
1860  !=
1861  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_blkw
1862  )
1863  ||
1864  (
1865  inputRaster.getBand( inputRasterBands[ inputRasterBandsIdx ] )->getProperty()->m_blkh
1866  !=
1867  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_blkh
1868  )
1869  ||
1870  (
1871  inputRaster.getBand( inputRasterBands[ inputRasterBandsIdx ] )->getProperty()->m_nblocksx
1872  !=
1873  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_nblocksx
1874  )
1875  ||
1876  (
1877  inputRaster.getBand( inputRasterBands[ inputRasterBandsIdx ] )->getProperty()->m_nblocksy
1878  !=
1879  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_nblocksy
1880  )
1881  ||
1882  (
1883  outputRaster.getBand( inputRasterBandsIdx )->getProperty()->m_blkw
1884  !=
1885  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_blkw
1886  )
1887  ||
1888  (
1889  outputRaster.getBand( inputRasterBandsIdx )->getProperty()->m_blkh
1890  !=
1891  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_blkh
1892  )
1893  ||
1894  (
1895  outputRaster.getBand( inputRasterBandsIdx )->getProperty()->m_nblocksx
1896  !=
1897  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_nblocksx
1898  )
1899  ||
1900  (
1901  outputRaster.getBand( inputRasterBandsIdx )->getProperty()->m_nblocksy
1902  !=
1903  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_nblocksy
1904  )
1905  )
1906  {
1907  useOptimizedRemap = false;
1908  break;
1909  }
1910  }
1911  }
1912 
1913  // remapping
1914 
1915  if( useOptimizedRemap )
1916  {
1917  Matrix< bool > rasterBlocksStatus;
1918  if( ! rasterBlocksStatus.reset(
1919  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_nblocksy,
1920  inputRaster.getBand( inputRasterBands[ 0 ] )->getProperty()->m_nblocksx,
1922  {
1923  return false;
1924  }
1925  for( unsigned int row = 0 ; row < rasterBlocksStatus.getLinesNumber() ;
1926  ++row )
1927  {
1928  for( unsigned int col = 0 ; col < rasterBlocksStatus.getColumnsNumber() ;
1929  ++col )
1930  {
1931  rasterBlocksStatus( row, col ) = false;
1932  }
1933  }
1934 
1935  boost::mutex mutex;
1936 
1937  RemapValuesThreadParams params;
1938  params.m_inputRasterPtr = &inputRaster;
1939  params.m_inputRasterBandsPtr = &inputRasterBands;
1940  params.m_outputRasterPtr = &outputRaster;
1941  params.m_outputRasterBandsPtr = &outputRasterBands;
1942  params.m_remapMatrixPtr = &remapMatrix;
1943  params.m_rasterBlocksStatusPtr = &rasterBlocksStatus;
1944  params.m_returnStatus = true;
1945  params.m_mutexPtr = &mutex;
1946 
1947  if( maxThreads == 1 )
1948  {
1949  RemapValuesThread( &params );
1950  }
1951  else
1952  {
1953  unsigned int threadsNumber = maxThreads ? maxThreads : te::common::GetPhysProcNumber();
1954 
1955  boost::thread_group threads;
1956 
1957  for( unsigned int threadIdx = 0 ; threadIdx < threadsNumber ;
1958  ++threadIdx )
1959  {
1960  threads.add_thread( new boost::thread( RemapValuesThread,
1961  &params ) );
1962  };
1963 
1964  threads.join_all();
1965  }
1966 
1967  return params.m_returnStatus;
1968  }
1969  else
1970  {
1971  const unsigned int inputRasterBandsSize = inputRasterBands.size();
1972  const unsigned int nRows = inputRaster.getNumberOfRows();
1973  const unsigned int nCols = inputRaster.getNumberOfColumns();
1974  boost::numeric::ublas::matrix< double > pixelValues( inputRasterBands.size(), 1 );
1975  boost::numeric::ublas::matrix< double > remappedPixelValues( inputRasterBands.size(), 1 );
1976  std::vector< double > inputNoDataValues( inputRasterBandsSize );
1977  std::vector< double > outputNoDataValues( inputRasterBandsSize );
1978  std::vector< double > outputMinValue( inputRasterBandsSize );
1979  std::vector< double > outputMaxValue( inputRasterBandsSize );
1980  unsigned int inputRasterBandsIdx = 0;
1981  unsigned int row = 0;
1982  unsigned int col = 0;
1983  bool pixelIsValid = false;
1984 
1985  for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBandsSize ;
1986  ++inputRasterBandsIdx )
1987  {
1988  if( inputRasterBands[ inputRasterBandsIdx ] >= inputRaster.getNumberOfBands() )
1989  {
1990  return false;
1991  }
1992 
1993  inputNoDataValues[ inputRasterBandsIdx ] = inputRaster.getBand(
1994  inputRasterBands[ inputRasterBandsIdx ] )->getProperty()->m_noDataValue;
1995 
1996  outputNoDataValues[ inputRasterBandsIdx ] = outputRaster.getBand(
1997  outputRasterBands[ inputRasterBandsIdx ] )->getProperty()->m_noDataValue;
1998 
2000  outputRaster.getBand( outputRasterBands[ inputRasterBandsIdx ] )->getProperty()->getType(),
2001  outputMinValue[ inputRasterBandsIdx ],
2002  outputMaxValue[ inputRasterBandsIdx ] );
2003  }
2004 
2005  for( row = 0 ; row < nRows ; ++row )
2006  {
2007  for( col = 0 ; col < nCols ; ++col )
2008  {
2009  pixelIsValid = true;
2010 
2011  for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBandsSize ;
2012  ++inputRasterBandsIdx )
2013  {
2014  inputRaster.getValue( col, row, pixelValues( inputRasterBandsIdx, 0 ),
2015  inputRasterBands[ inputRasterBandsIdx ] );
2016 
2017  if( pixelValues( inputRasterBandsIdx, 0 ) == inputNoDataValues[ inputRasterBandsIdx ] )
2018  {
2019  pixelIsValid = false;
2020  break;
2021  }
2022  }
2023 
2024  if( pixelIsValid )
2025  {
2026  remappedPixelValues = boost::numeric::ublas::prod( remapMatrix, pixelValues );
2027 
2028  for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBandsSize ;
2029  ++inputRasterBandsIdx )
2030  {
2031  outputRaster.setValue(
2032  col,
2033  row,
2034  std::max(
2035  outputMinValue[ inputRasterBandsIdx ]
2036  ,
2037  std::min(
2038  outputMaxValue[ inputRasterBandsIdx ]
2039  ,
2040  remappedPixelValues( inputRasterBandsIdx, 0 )
2041  )
2042  ),
2043  outputRasterBands[ inputRasterBandsIdx ] );
2044  }
2045  }
2046  else
2047  {
2048  for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBandsSize ;
2049  ++inputRasterBandsIdx )
2050  {
2051  outputRaster.setValue( col, row, outputNoDataValues[ inputRasterBandsIdx ],
2052  outputRasterBands[ inputRasterBandsIdx ] );
2053  }
2054  }
2055  }
2056  }
2057  }
2058 
2059  return true;
2060  }
2061 
2063  const te::rst::Raster& inputRaster,
2064  const std::vector< unsigned int >& inputRasterBands,
2065  const std::vector< std::map<std::string, std::string> > & outputRastersInfos,
2066  const std::string& outputDataSourceType,
2067  std::vector< boost::shared_ptr< te::rst::Raster > > & outputRastersPtrs )
2068  {
2069  outputRastersPtrs.clear();
2070 
2071  if( !( inputRaster.getAccessPolicy() & te::common::RAccess ) )
2072  {
2073  return false;
2074  }
2075  if( outputRastersInfos.size() != inputRasterBands.size() )
2076  {
2077  return false;
2078  }
2079  if( outputDataSourceType.empty() )
2080  {
2081  return false;
2082  }
2083 
2084  outputRastersPtrs.resize( inputRasterBands.size() );
2085 
2086  const unsigned int nRows = inputRaster.getNumberOfRows();
2087  const unsigned int nCols = inputRaster.getNumberOfColumns();
2088 
2089  for( unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
2090  inputRasterBands.size() ; ++inputRasterBandsIdx )
2091  {
2092  const unsigned int bandIdx = inputRasterBands[ inputRasterBandsIdx ];
2093  if( bandIdx >= inputRaster.getNumberOfBands() )
2094  {
2095  return false;
2096  }
2097 
2098  std::vector< te::rst::BandProperty* > bandsProperties;
2099 
2100  bandsProperties.push_back( new te::rst::BandProperty(
2101  *( inputRaster.getBand( bandIdx )->getProperty()) ) );
2102 
2103  outputRastersPtrs[ inputRasterBandsIdx].reset(
2104  te::rst::RasterFactory::make( outputDataSourceType,
2105  new te::rst::Grid( *inputRaster.getGrid() ), bandsProperties,
2106  outputRastersInfos[ inputRasterBandsIdx ], 0, 0 ) );
2107  if( outputRastersPtrs[ inputRasterBandsIdx].get() == 0 ) return false;
2108 
2109  unsigned int col = 0;
2110  unsigned int row = 0;
2111  double value = 0;
2112  const te::rst::Band& inBand = *(inputRaster.getBand( bandIdx ));
2113  te::rst::Band& outBand = *(outputRastersPtrs[ inputRasterBandsIdx]->getBand( 0 ));
2114 
2115  for( row = 0 ; row < nRows ; ++row )
2116  {
2117  for( col = 0 ; col < nCols ; ++col )
2118  {
2119  inBand.getValue( col, row, value );
2120  outBand.setValue( col, row, value );
2121  }
2122  }
2123  }
2124 
2125  return true;
2126  }
2127 
2129  te::rp::FeederConstRaster& feeder,
2130  const std::vector< unsigned int >& inputRasterBands,
2131  const te::rst::Interpolator::Method& interpMethod,
2132  const std::map<std::string, std::string>& outputRasterInfo,
2133  const std::string& outputDataSourceType,
2134  std::auto_ptr< te::rst::Raster >& outputRasterPtr )
2135  {
2136  outputRasterPtr.reset();
2137 
2138  if( inputRasterBands.size() != feeder.getObjsCount() )
2139  {
2140  return false;
2141  }
2142  if( outputDataSourceType.empty() )
2143  {
2144  return false;
2145  }
2146 
2147  // creating the output raster
2148 
2149  {
2150  te::rst::Raster const * inputRasterPtr = 0;
2151  std::auto_ptr< te::rst::Grid > outputGridPtr;
2152  std::vector< te::rst::BandProperty* > bandsProperties;
2153 
2154  feeder.reset();
2155  while( inputRasterPtr = feeder.getCurrentObj() )
2156  {
2157  if( inputRasterBands[ feeder.getCurrentOffset() ] >=
2158  inputRasterPtr->getNumberOfBands() )
2159  {
2160  return false;
2161  }
2162 
2163  if( feeder.getCurrentOffset() == 0 )
2164  {
2165  outputGridPtr.reset( new te::rst::Grid( *inputRasterPtr->getGrid() ) );
2166  }
2167 
2168  bandsProperties.push_back( new te::rst::BandProperty(
2169  *( inputRasterPtr->getBand( inputRasterBands[
2170  feeder.getCurrentOffset() ] )->getProperty()) ) );
2171 
2172  feeder.moveNext();
2173  }
2174 
2175  outputRasterPtr.reset( te::rst::RasterFactory::make( outputDataSourceType,
2176  outputGridPtr.release(), bandsProperties, outputRasterInfo, 0, 0 ) );
2177  if( outputRasterPtr.get() == 0 ) return false;
2178  }
2179 
2180  // copying data from each input band
2181 
2182  {
2183  te::rst::Raster const * inputRasterPtr = 0;
2184 
2185  feeder.reset();
2186  while( inputRasterPtr = feeder.getCurrentObj() )
2187  {
2188  const unsigned int inBandIdx = inputRasterBands[
2189  feeder.getCurrentOffset() ];
2190  te::rst::Interpolator interp( inputRasterPtr, interpMethod );
2191  unsigned int outRow = 0;
2192  unsigned int outCol = 0;
2193  const unsigned int nOutRows = outputRasterPtr->getNumberOfRows();
2194  const unsigned int nOutCols = outputRasterPtr->getNumberOfColumns();
2195  te::rst::Band& outBand = *outputRasterPtr->getBand(
2196  feeder.getCurrentOffset() );
2197  const te::rst::Grid& inGrid = *inputRasterPtr->getGrid();
2198  const te::rst::Grid& outGrid = *outputRasterPtr->getGrid();
2199  double xOutCoord = 0;
2200  double yOutCoord = 0;
2201  double xInCoord = 0;
2202  double yInCoord = 0;
2203  double inRow = 0;
2204  double inCol = 0;
2205  std::complex< double > value = 0;
2206  te::srs::Converter conv( outputRasterPtr->getSRID(),
2207  inputRasterPtr->getSRID() );
2208 
2209  if( inputRasterPtr->getSRID() == outputRasterPtr->getSRID() )
2210  {
2211  for( outRow = 0 ; outRow < nOutRows ; ++outRow )
2212  {
2213  for( outCol = 0 ; outCol < nOutCols ; ++outCol )
2214  {
2215  outGrid.gridToGeo( (double)outCol, (double)outRow, xOutCoord, yOutCoord );
2216  inGrid.geoToGrid( xOutCoord, yOutCoord, inCol, inRow );
2217  interp.getValue( inCol, inRow, value, inBandIdx );
2218  outBand.setValue( outCol, outRow, value );
2219  }
2220  }
2221  }
2222  else
2223  {
2224  for( outRow = 0 ; outRow < nOutRows ; ++outRow )
2225  {
2226  for( outCol = 0 ; outCol < nOutCols ; ++outCol )
2227  {
2228  outGrid.gridToGeo( (double)outCol, (double)outRow, xOutCoord, yOutCoord );
2229  conv.convert( xOutCoord, yOutCoord, xInCoord, yInCoord );
2230  inGrid.geoToGrid( xInCoord, yInCoord, inCol, inRow );
2231  interp.getValue( inCol, inRow, value, inBandIdx );
2232  outBand.setValue( outCol, outRow, value );
2233  }
2234  }
2235  }
2236 
2237  feeder.moveNext();
2238  }
2239  }
2240 
2241  return true;
2242  }
2243 
2244  bool GetDetailedExtent( const te::rst::Grid& grid, te::gm::LinearRing& detailedExtent )
2245  {
2246  const int nCols = (int)grid.getNumberOfColumns();
2247  const int nRows = (int)grid.getNumberOfRows();
2248  if( ( nCols == 0 ) || ( nRows == 0 ) )
2249  {
2250  return false;
2251  }
2252 
2253  const unsigned int ringSize = ( 2 * ( nCols + 1 ) ) +
2254  ( 2 * ( nRows - 1 ) ) + 1;
2255 
2256  te::gm::LinearRing ring( ringSize , te::gm::LineStringType,
2257  grid.getSRID(), 0 );
2258 
2259  const double lLX = grid.getExtent()->getLowerLeftX();
2260  const double lLY = grid.getExtent()->getLowerLeftY();
2261  const double uRX = grid.getExtent()->getUpperRightX();
2262  const double uRY = grid.getExtent()->getUpperRightY();
2263  const double& resX = grid.getResolutionX();
2264  const double& resY = grid.getResolutionY();
2265  unsigned int ringIdx = 0;
2266  int row = 0;
2267  int col = 0;
2268 
2269  ring.setPoint( 0, lLX, uRY );
2270 
2271  for( col = 0 ; col < nCols ; ++col )
2272  {
2273  ring.setPoint( ++ringIdx, lLX + ( ((double)( col + 1 ) ) * resX ), uRY );
2274  }
2275 
2276  for( row = 0 ; row < nRows ; ++row )
2277  {
2278  ring.setPoint( ++ringIdx, uRX, uRY - ( ((double)( row + 1 ) ) * resY ) );
2279  }
2280 
2281  for( col = nCols - 1 ; col > -1 ; --col )
2282  {
2283  ring.setPoint( ++ringIdx, lLX + ( ((double)( col + 1 ) ) * resX ), lLY );
2284  }
2285 
2286  for( row = nRows - 1 ; row > 0 ; --row )
2287  {
2288  ring.setPoint( ++ringIdx, lLX, uRY - ( ((double)( row + 1 ) ) * resY ) );
2289  }
2290 
2291  ring.setPoint( ringSize - 1, lLX, uRY );
2292 
2293  detailedExtent = ring;
2294 
2295  return true;
2296  }
2297 
2299  te::gm::LinearRing& indexedDetailedExtent )
2300  {
2301  const int nCols = (int)grid.getNumberOfColumns();
2302  const int nRows = (int)grid.getNumberOfRows();
2303  if( ( nCols == 0 ) || ( nRows == 0 ) )
2304  {
2305  return false;
2306  }
2307 
2308  const unsigned int ringSize = ( 2 * ( nCols + 1 ) ) +
2309  ( 2 * ( nRows - 1 ) ) + 1;
2310 
2311  te::gm::LinearRing ring( ringSize , te::gm::LineStringType,
2312  0, 0 );
2313 
2314  const double lLY = ((double)nRows) - 0.5;
2315  const double uRX = ((double)nCols) - 0.5;
2316  unsigned int ringIdx = 0;
2317  int row = 0;
2318  int col = 0;
2319 
2320  ring.setPoint( 0, -0.5, -0.5 );
2321 
2322  for( col = 0 ; col < nCols ; ++col )
2323  {
2324  ring.setPoint( ++ringIdx, 0.5 + ((double)col), (-0.5) );
2325  }
2326 
2327  for( row = 0 ; row < nRows ; ++row )
2328  {
2329  ring.setPoint( ++ringIdx, uRX, 0.5 + ((double)row) );
2330  }
2331 
2332  for( col = nCols - 1 ; col > -1 ; --col )
2333  {
2334  ring.setPoint( ++ringIdx, ((double)col) - 0.5, lLY );
2335  }
2336 
2337  for( row = nRows - 1 ; row > -1 ; --row )
2338  {
2339  ring.setPoint( ++ringIdx, (-0.5), ((double)row) - 0.5 );
2340  }
2341 
2342  ring.setPoint( ringSize - 1, -0.5, -0.5 );
2343 
2344  indexedDetailedExtent = ring;
2345 
2346  return true;
2347  }
2348 
2349  boost::numeric::ublas::matrix< double > CreateWaveletAtrousFilter(
2350  const WaveletAtrousFilterType& filterType )
2351  {
2352  boost::numeric::ublas::matrix< double > emptyFilter;
2353 
2354  switch( filterType )
2355  {
2356  case B3SplineWAFilter :
2357  {
2358  boost::numeric::ublas::matrix< double > internalFilter( 5, 5 );
2359  const double weight = 256;
2360 
2361  internalFilter(0,0) = 1/weight; internalFilter(0,1) = 4/weight; internalFilter(0,2) = 6/weight; internalFilter(0,3) = 4/weight; internalFilter(0,4) = 1/weight;
2362  internalFilter(1,0) = 4/weight; internalFilter(1,1) = 16/weight; internalFilter(1,2) = 24/weight; internalFilter(1,3) = 16/weight; internalFilter(1,4) = 4/weight;
2363  internalFilter(2,0) = 6/weight; internalFilter(2,1) = 24/weight; internalFilter(2,2) = 36/weight; internalFilter(2,3) = 24/weight; internalFilter(2,4) = 6/weight;
2364  internalFilter(3,0) = 4/weight; internalFilter(3,1) = 16/weight; internalFilter(3,2) = 24/weight; internalFilter(3,3) = 16/weight; internalFilter(3,4) = 4/weight;
2365  internalFilter(4,0) = 1/weight; internalFilter(4,1) = 4/weight; internalFilter(4,2) = 6/weight; internalFilter(4,3) = 4/weight; internalFilter(4,4) = 1/weight;
2366 
2367  return internalFilter;
2368 
2369  break;
2370  }
2371  case TriangleWAFilter :
2372  {
2373  boost::numeric::ublas::matrix< double > internalFilter( 3, 3 );
2374  const double weight = 16;
2375 
2376  internalFilter(0,0) = 1/weight; internalFilter(0,1) = 2/weight; internalFilter(0,2) = 1/weight;
2377  internalFilter(1,0) = 2/weight; internalFilter(1,1) = 4/weight; internalFilter(1,2) = 2/weight;
2378  internalFilter(2,0) = 1/weight; internalFilter(2,1) = 2/weight; internalFilter(2,2) = 1/weight;
2379 
2380  return internalFilter;
2381 
2382  break;
2383  }
2384  default :
2385  {
2386  throw te::rp::Exception( "Invalid filter type" );
2387  break;
2388  }
2389  }
2390 
2391  return emptyFilter;
2392  }
2393 
2395  const te::rst::Raster& inputRaster,
2396  const std::vector< unsigned int >& inputRasterBands,
2397  te::rst::Raster& waveletRaster,
2398  const unsigned int levelsNumber,
2399  const boost::numeric::ublas::matrix< double >& filter )
2400  {
2401  if( ! ( inputRaster.getAccessPolicy() & te::common::RAccess ) )
2402  {
2403  return false;
2404  }
2405  for( unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
2406  inputRasterBands.size() ; ++inputRasterBandsIdx )
2407  {
2408  if( inputRasterBands[ inputRasterBandsIdx ] >= inputRaster.getNumberOfBands() )
2409  {
2410  return false;
2411  }
2412  }
2413  if(
2414  ( ( waveletRaster.getAccessPolicy() & te::common::WAccess ) == 0 )
2415  ||
2416  ( waveletRaster.getNumberOfColumns() != inputRaster.getNumberOfColumns() )
2417  ||
2418  ( waveletRaster.getNumberOfRows() != inputRaster.getNumberOfRows() )
2419  ||
2420  ( waveletRaster.getNumberOfBands() < ( 2 * levelsNumber * inputRasterBands.size() ) )
2421  )
2422  {
2423  return false;
2424  }
2425  if( levelsNumber == 0 )
2426  {
2427  return false;
2428  }
2429  if( ( filter.size1() == 0 ) || ( filter.size2() == 0 ) ||
2430  ( filter.size1() != filter.size2() )
2431  )
2432  {
2433  return false;
2434  }
2435 
2436  // Creating the coeficients and the resitual planes for each required
2437  // wavelet level
2438 
2439  const int filterWidth = filter.size1();
2440  const int offset = filterWidth / 2;
2441  const int nLines = inputRaster.getNumberOfRows();
2442  const int nCols = inputRaster.getNumberOfColumns();
2443 
2444  for( unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx < inputRasterBands.size() ;
2445  ++inputRasterBandsIdx )
2446  {
2447  for( unsigned int levelIndex = 0; levelIndex < levelsNumber; ++levelIndex)
2448  {
2449  const unsigned int currentSmoothBandIdx = ( 2 * levelsNumber *
2450  inputRasterBandsIdx ) + ( 2 * levelIndex );
2451  te::rst::Band& currentSmoothBand = *waveletRaster.getBand( currentSmoothBandIdx );
2452 
2453  const unsigned int currentWaveletBandIdx = currentSmoothBandIdx + 1;
2454  te::rst::Band& currentWaveletBand = *waveletRaster.getBand(
2455  currentWaveletBandIdx );
2456 
2457  const unsigned int prevSmoothBandIdx = currentSmoothBandIdx - 2;
2458  te::rst::Band const* prevSmoothBandPtr = 0;
2459  if( levelIndex == 0 )
2460  {
2461  prevSmoothBandPtr = inputRaster.getBand( inputRasterBands[ inputRasterBandsIdx ] );
2462  }
2463  else
2464  {
2465  prevSmoothBandPtr = waveletRaster.getBand( prevSmoothBandIdx );
2466  }
2467  const te::rst::Band& prevSmoothBand = *prevSmoothBandPtr;
2468 
2469  const int filterScale = (int)std::pow(2.0, (double)levelIndex);
2470 
2471  int col = 0;
2472  int row = 0;
2473  int convolutionCenterCol = 0;
2474  int convolutionCenterRow = 0;
2475  int filterCol = 0;
2476  int filterRow = 0;
2477  double valueOriginal = 0.0;
2478  double valuePrev = 0.0;
2479  double valueNew = 0.0;
2480 
2481  for (convolutionCenterRow = 0; convolutionCenterRow < nLines; convolutionCenterRow++)
2482  {
2483  for (convolutionCenterCol = 0; convolutionCenterCol < nCols; convolutionCenterCol++)
2484  {
2485  valueNew = 0.0;
2486 
2487  for (filterRow = 0; filterRow < filterWidth; filterRow++)
2488  {
2489  for (filterCol = 0; filterCol < filterWidth; filterCol++)
2490  {
2491  col = convolutionCenterCol+(filterCol-offset)*filterScale;
2492  row = convolutionCenterRow+(filterRow-offset)*filterScale;
2493 
2494  if (col < 0)
2495  col = nCols + col;
2496  else if (col >= nCols)
2497  col = col - nCols;
2498  if (row < 0)
2499  row = nLines + row;
2500  else if (row >= nLines)
2501  row = row - nLines;
2502 
2503  prevSmoothBand.getValue( col, row, valueOriginal );
2504 
2505  valueNew += valueOriginal * filter( filterRow, filterCol );
2506  }
2507  }
2508 
2509  currentSmoothBand.setValue( convolutionCenterCol, convolutionCenterRow,
2510  valueNew );
2511  prevSmoothBand.getValue( convolutionCenterCol, convolutionCenterRow,
2512  valueOriginal );
2513  currentWaveletBand.setValue( convolutionCenterCol, convolutionCenterRow,
2514  valueOriginal - valueNew );
2515  }
2516  }
2517  }
2518  }
2519 
2520  return true;
2521  }
2522 
2524  const te::rst::Raster& waveletRaster,
2525  const unsigned int levelsNumber,
2526  te::rst::Raster& outputRaster,
2527  const std::vector< unsigned int >& outputRasterBands )
2528  {
2529  if(
2530  ( ! ( waveletRaster.getAccessPolicy() & te::common::RAccess ) )
2531  ||
2532  ( waveletRaster.getNumberOfBands() < ( 2 * levelsNumber * outputRasterBands.size() ) )
2533  )
2534  {
2535  return false;
2536  }
2537  if( levelsNumber == 0 )
2538  {
2539  return false;
2540  }
2541  if(
2542  ( ( outputRaster.getAccessPolicy() & te::common::WAccess ) == 0 )
2543  ||
2544  ( waveletRaster.getNumberOfColumns() != outputRaster.getNumberOfColumns() )
2545  ||
2546  ( waveletRaster.getNumberOfRows() != outputRaster.getNumberOfRows() )
2547  )
2548  {
2549  return false;
2550  }
2551  for( unsigned int outputRasterBandsIdx = 0 ; outputRasterBandsIdx <
2552  outputRasterBands.size() ; ++outputRasterBandsIdx )
2553  {
2554  if( outputRasterBands[ outputRasterBandsIdx ] >= outputRaster.getNumberOfBands() )
2555  {
2556  return false;
2557  }
2558  }
2559 
2560  for( unsigned int outputRasterBandsIdx = 0 ; outputRasterBandsIdx <
2561  outputRasterBands.size() ; ++outputRasterBandsIdx )
2562  {
2563  const unsigned int outputRasterBandIdx = outputRasterBands[ outputRasterBandsIdx ];
2564  const unsigned int nRows = waveletRaster.getNumberOfRows();
2565  const unsigned int nCols = waveletRaster.getNumberOfColumns();
2566  const unsigned int firstSmoothBandIdx = outputRasterBandsIdx *
2567  levelsNumber * 2;
2568  const unsigned int firstWaveletBandIdx = firstSmoothBandIdx
2569  + 1;
2570  const unsigned int lastSmoothBandIdx = firstSmoothBandIdx
2571  + ( 2 * levelsNumber ) - 2;
2572  const unsigned int lastWaveletBandIdx = lastSmoothBandIdx + 1;
2573  unsigned int col = 0;
2574  unsigned int row = 0;
2575  double value = 0;
2576  double sum = 0;
2577  unsigned int waveletRasterBand = 0;
2578 
2579  double bandAllowedMinValue = 0;
2580  double bandAllowedMaxValue = 0;
2581  te::rst::GetDataTypeRanges( outputRaster.getBand(
2582  outputRasterBandIdx )->getProperty()->m_type, bandAllowedMinValue,
2583  bandAllowedMaxValue );
2584 
2585 
2586  for( row = 0 ; row < nRows ; ++row )
2587  {
2588  for( col = 0 ; col < nCols ; ++col )
2589  {
2590  sum = 0.0;
2591 
2592  for( waveletRasterBand = firstWaveletBandIdx ;
2593  waveletRasterBand <= lastWaveletBandIdx ;
2594  waveletRasterBand += 2)
2595  {
2596  waveletRaster.getValue( col, row, value, waveletRasterBand );
2597  sum += value;
2598  }
2599 
2600  waveletRaster.getValue( col, row, value, lastSmoothBandIdx );
2601  sum += value;
2602 
2603  sum = std::max( sum, bandAllowedMinValue );
2604  sum = std::min( sum, bandAllowedMaxValue );
2605 
2606  outputRaster.setValue( col, row, sum, outputRasterBandIdx );
2607  }
2608  }
2609  }
2610 
2611  return true;
2612  }
2613 
2615  const te::rst::Raster& inputRaster,
2616  const std::vector< unsigned int >& inputRasterBands,
2617  const te::rst::Interpolator::Method interpMethod,
2618  const unsigned int firstRow,
2619  const unsigned int firstColumn,
2620  const unsigned int height,
2621  const unsigned int width,
2622  const unsigned int newheight,
2623  const unsigned int newwidth,
2624  const std::map<std::string, std::string>& rinfo,
2625  const std::string& dataSourceType,
2626  std::auto_ptr< te::rst::Raster >& resampledRasterPtr )
2627  {
2628  if( ( firstRow + height ) > inputRaster.getNumberOfRows() )
2629  {
2630  return false;
2631  }
2632  if( ( firstColumn + width ) > inputRaster.getNumberOfColumns() )
2633  {
2634  return false;
2635  }
2636  for (std::size_t inputRasterBandsIdx = 0; inputRasterBandsIdx <
2637  inputRasterBands.size(); inputRasterBandsIdx++)
2638  {
2639  if( inputRasterBands[ inputRasterBandsIdx ] >= inputRaster.getNumberOfBands() )
2640  {
2641  return false;
2642  }
2643  }
2644 
2645  te::gm::Coord2D ulc = inputRaster.getGrid()->gridToGeo( ((double)firstColumn)
2646  - 0.5, ((double)firstRow) - 0.5);
2647  te::gm::Coord2D lrc = inputRaster.getGrid()->gridToGeo( ((double)(firstColumn
2648  + width)) - 0.5, ((double)(firstRow + height)) - 0.5);
2649 
2650  std::auto_ptr< te::gm::Envelope > newEnvelopePtr( new te::gm::Envelope(
2651  ulc.x, lrc.y, lrc.x, ulc.y ) );
2652 
2653  // create output parameters and raster
2654 
2655  std::auto_ptr< te::rst::Grid > gridPtr( new te::rst::Grid(
2656  newwidth, newheight, newEnvelopePtr.release(),
2657  inputRaster.getSRID()) );
2658 
2659  std::vector<te::rst::BandProperty*> bands;
2660 
2661  for (std::size_t inputRasterBandsIdx = 0; inputRasterBandsIdx <
2662  inputRasterBands.size(); inputRasterBandsIdx++)
2663  {
2664  bands.push_back( new te::rst::BandProperty(
2665  *(inputRaster.getBand( inputRasterBands[ inputRasterBandsIdx ] )->getProperty())));
2666  bands[ inputRasterBandsIdx ]->m_blkh = 1;
2667  bands[ inputRasterBandsIdx ]->m_blkw = newwidth;
2668  bands[ inputRasterBandsIdx ]->m_nblocksx = 1;
2669  bands[ inputRasterBandsIdx ]->m_nblocksy = newheight;
2670  }
2671 
2672  resampledRasterPtr.reset( te::rst::RasterFactory::make( dataSourceType,
2673  gridPtr.release(), bands, rinfo, 0 ) );
2674  if( resampledRasterPtr.get() == 0 )
2675  {
2676  return false;
2677  }
2678 
2679  // fill output raster
2680 
2681  std::complex<double> interpValue;
2682  te::rst::Interpolator interp( &inputRaster, interpMethod);
2683  const double rowsFactor = ((double)(height-1)) / ((double)(newheight-1));
2684  const double colsFactor = ((double)(width-1)) / ((double)(newwidth-1));
2685  double inputRow = 0;
2686  double inputCol = 0;
2687  unsigned int outputCol = 0;
2688  unsigned int outputRow = 0;
2689  unsigned int inputBandIdx = 0;
2690  double allowedMin = 0;
2691  double allowedMax = 0;
2692 
2693  for (std::size_t inputRasterBandsIdx = 0; inputRasterBandsIdx <
2694  inputRasterBands.size(); inputRasterBandsIdx++)
2695  {
2696  te::rst::Band& outputBand = *resampledRasterPtr->getBand( inputRasterBandsIdx );
2697  inputBandIdx = inputRasterBands[ inputRasterBandsIdx ];
2698  te::rp::GetDataTypeRange( outputBand.getProperty()->m_type, allowedMin,
2699  allowedMax );
2700 
2701  for ( outputRow = 0; outputRow < newheight; ++outputRow)
2702  {
2703  inputRow = ( ((double)( outputRow ) ) * rowsFactor ) + ((double)firstRow);
2704 
2705  for ( outputCol = 0; outputCol < newwidth; ++outputCol )
2706  {
2707  inputCol = ( ((double)( outputCol ) ) * colsFactor ) + ((double)firstColumn);
2708 
2709  interp.getValue(inputCol, inputRow, interpValue, inputBandIdx);
2710 
2711  interpValue.real( std::max( allowedMin, interpValue.real() ) );
2712  interpValue.real( std::min( allowedMax, interpValue.real() ) );
2713 
2714  outputBand.setValue(outputCol, outputRow, interpValue);
2715  }
2716  }
2717  }
2718 
2719  return true;
2720  }
2721 
2722  } // end namespace rp
2723 } // end namespace te
virtual unsigned int getObjsCount() const =0
Return the total number of feeder objects.
bool GetMeanValue(const te::rst::Band &band, const unsigned int maxThreads, double &meanValue)
Get the mean of band pixel values.
Definition: Functions.cpp:1086
double GetDigitalNumberBandMax(std::string bandName)
Returns the maximum digital number of a given sensor/band.
Definition: Functions.cpp:696
unsigned int getNumberOfRows() const
Returns the grid number of rows.
Definition: Grid.cpp:209
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
Definition: Raster.cpp:233
void reset()
Reset the internal state (all internal pointed objects are deleted).
te::rst::Raster * m_outputRasterPtr
Definition: Functions.cpp:111
double y
y-coordinate.
Definition: Coord2D.h:114
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
Definition: Utils.cpp:334
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
Matrix< bool > * m_rasterBlocksStatusPtr
Definition: Functions.cpp:76
unsigned int getRow() const
Returns the current row in iterator.
A raster band description.
Definition: BandProperty.h:61
double x
x-coordinate.
Definition: Coord2D.h:113
int getSRID() const
Returns the grid spatial reference system identifier.
Definition: Grid.cpp:265
A class that models the description of a dataset.
Definition: DataSetType.h:72
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
int m_nblocksx
The number of blocks in x.
Definition: BandProperty.h:145
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Spline filter type.
Definition: Functions.h:77
It interpolates one pixel based on a selected algorithm. Methods currently available are Nearest Neig...
Definition: Interpolator.h:55
int m_nblocksy
The number of blocks in y.
Definition: BandProperty.h:146
This class implements and iterator to "navigate" over a raster, with a predefined number of bands...
Triangle filter type.
Definition: Functions.h:78
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:410
virtual std::auto_ptr< DataSourceTransactor > getTransactor()=0
It returns an object that can execute transactions in the context of a data source.
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:873
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:180
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::auto_ptr< te::rst::Raster > &outputRasterPtr)
Compose a set of bands into one multi-band raster.
Definition: Functions.cpp:2128
std::vector< std::string > GetBandNames()
Returns a vector os with band's names.
Definition: Functions.cpp:556
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:224
#define MIN(a, b)
Macro that returns min between two values.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:400
void release(std::auto_ptr< te::da::DataSource > &dataSourcePtr, std::auto_ptr< te::da::DataSourceTransactor > &transactorPtr, std::auto_ptr< te::da::DataSet > &dataSetPtr, std::auto_ptr< te::rst::Raster > &rasterPtr)
Relase the internal state and give the ownership to the given pointers.
virtual void read(int x, int y, void *buffer) const =0
It reads a data block to the specified buffer.
Matrix< bool > * m_rasterBlocksStatusPtr
Definition: Functions.cpp:114
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
void getValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Get the interpolated value at specific band.
Definition: Interpolator.h:92
int m_type
The data type of the elements in the band.
Definition: BandProperty.h:133
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:118
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
Definition: BandProperty.h:136
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:420
te::rst::Band const * m_inputBandPtr
Definition: Functions.cpp:75
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.
Definition: Functions.cpp:1504
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.
Definition: Functions.cpp:2062
InterpolationMethod
Allowed interpolation methods.
Definition: Enums.h:92
double GetSpectralBandMax(std::string bandName)
Returns the maximum reflectance value of a given sensor/band.
Definition: Functions.cpp:646
#define MAX(a, b)
Macro that returns max between two values.
Raster property.
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.
Definition: Functions.cpp:1788
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
Definition: Grid.cpp:307
Exception class.
Raster Processing functions.
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
Definition: Grid.cpp:259
bool ConvertRGB2IHS(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:745
std::vector< unsigned int > const * m_inputRasterBandsPtr
Definition: Functions.cpp:110
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Definition: Raster.cpp:89
bool TERPEXPORT Copy2DiskRaster(const te::rst::Raster &inputRaster, const std::string &fileName)
Create a new raster into a GDAL datasource.
Definition: Functions.cpp:281
#define M_PI
Definition: Functions.cpp:66
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
TECOMMONEXPORT unsigned int GetPhysProcNumber()
Returns the number of physical processors.
te::rst::Band const * m_inputBand1Ptr
Definition: Functions.cpp:96
virtual std::complex< double > getMaxValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the maximum occurring value in a window of the band.
Definition: Band.cpp:139
bool NormalizeRaster(te::rst::Raster &inraster, double nmin, double nmax)
Normalizes one raster in a given interval.
Definition: Functions.cpp:701
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
Definition: LineString.cpp:353
virtual void reset()=0
Reset the feeder to the first position (subsequent accesses will start from the first sequence obejct...
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
An abstract class for raster data strucutures.
Definition: Raster.h:71
static RasterIterator begin(Raster *r, const std::vector< unsigned int > &bands)
Returns an iterator referring to the first value.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
unsigned int getColumnsNumber() const
The number of current matrix columns.
Definition: Matrix.h:678
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.
Definition: Functions.cpp:2523
static std::auto_ptr< DataSource > make(const std::string &dsType)
WaveletAtrousFilterType
Wavelet Atrous Filter types.
Definition: Functions.h:74
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:428
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
std::pair< double, double > GetDigitalNumberBandInfo(std::string bandName)
Returns the maximun and minimum digital numbers of a given sensor/band.
Definition: Functions.cpp:651
te::rst::Raster const * m_inputRasterPtr
Definition: Functions.cpp:109
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
Definition: Grid.cpp:193
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.
Definition: Functions.cpp:2394
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:141
double getResolutionX() const
Returns the grid horizontal (x-axis) resolution.
Definition: Grid.cpp:253
Raster tuple.
virtual void write(int x, int y, void *buffer)=0
It writes a data block from the specified buffer.
void Convert2DoublesVector(void *inputVector, const int inputVectorDataType, unsigned int inputVectorSize, double *outputVector)
Convert vector elements.
Definition: Functions.cpp:331
te::rst::Band const * m_inputBand2Ptr
Definition: Functions.cpp:97
RasterHandler.
Definition: RasterHandler.h:47
bool EigenVectors(const boost::numeric::ublas::matrix< T > &inputMatrix, boost::numeric::ublas::matrix< T > &eigenVectorsMatrix, boost::numeric::ublas::matrix< T > &eigenValuesMatrix)
Computes the eigenvectors of a given matrix.
Definition: MatrixUtils.h:211
void GetMeanValueThread(GetMeanValueThreadParams *paramsPtr)
Definition: Functions.cpp:993
A raster band description.
Definition: Band.h:63
Matrix< bool > * m_rasterBlocksStatusPtr
Definition: Functions.cpp:86
bool GetDetailedExtent(const te::rst::Grid &grid, te::gm::LinearRing &detailedExtent)
Create a datailed extent from the given grid.
Definition: Functions.cpp:2244
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
te::rst::Band const * m_inputBandPtr
Definition: Functions.cpp:85
void reset()
Reset (clear) the active instance data.
Definition: Matrix.h:480
virtual void getValue(unsigned int c, unsigned int r, double &value, std::size_t b=0) const
Returns the attribute value of a band of a cell.
Definition: Raster.cpp:228
void RemapValuesThread(RemapValuesThreadParams *paramsPtr)
Definition: Functions.cpp:1619
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
A Converter is responsible for the conversion of coordinates between different Coordinate Systems (CS...
Definition: Converter.h:53
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:119
boost::numeric::ublas::matrix< double > CreateWaveletAtrousFilter(const WaveletAtrousFilterType &filterType)
Create a Wavele Atrous Filter.
Definition: Functions.cpp:2349
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.
Definition: Functions.cpp:1357
int getSRID() const
Returns the raster spatial reference system identifier.
Definition: Raster.cpp:203
double GetSpectralBandMin(std::string bandName)
Returns the minimum reflectance value of a given sensor/band.
Definition: Functions.cpp:641
Feeder from a input rasters.
Definition: FeedersRaster.h:46
virtual std::complex< double > getMinValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the minimum occurring value in a window of the band.
Definition: Band.cpp:82
static Raster * make()
It creates and returns an empty raster with default raster driver.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
bool GetInverseMatrix(const boost::numeric::ublas::matrix< T > &inputMatrix, boost::numeric::ublas::matrix< T > &outputMatrix)
Matrix inversion.
Definition: MatrixUtils.h:104
bool GetIndexedDetailedExtent(const te::rst::Grid &grid, te::gm::LinearRing &indexedDetailedExtent)
Create a indexed (lines,columns) datailed extent from the given grid.
Definition: Functions.cpp:2298
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
Definition: Grid.cpp:275
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.
Definition: Functions.cpp:1594
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
Definition: Grid.cpp:301
std::vector< unsigned int > const * m_outputRasterBandsPtr
Definition: Functions.cpp:112
static RasterIterator end(Raster *r, const std::vector< unsigned int > &bands)
Returns an iterator referring to after the end of the iterator.
te::rst::Raster * getRasterPtr()
Returns a pointer the the handled raster instance or NULL if no instance is handled.
unsigned int getColumn() const
Returns the current column in iterator.
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
virtual bool moveNext()=0
Advances to the next sequence obeject.
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::auto_ptr< te::rst::Raster > &resampledRasterPtr)
Resample a subset of the raster, given a box.
Definition: Functions.cpp:2614
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:235
int m_blkh
Block height (pixels).
Definition: BandProperty.h:144
std::pair< double, double > GetSpectralBandInfo(std::string bandName)
Returns the maximun and minimum reflectance values of a given sensor/band.
Definition: Functions.cpp:596
virtual te::rst::Raster const * getCurrentObj() const =0
Return the current sequence object.
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
boost::numeric::ublas::matrix< double > const * m_remapMatrixPtr
Definition: Functions.cpp:113
void GetCovarianceValueThread(GetCovarianceValueThreadParams *paramsPtr)
Definition: Functions.cpp:1284
virtual int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
Definition: Band.cpp:630
void GetStdDevValueThread(GetStdDevValueThreadParams *paramsPtr)
Definition: Functions.cpp:1143
unsigned int getLinesNumber() const
The number of current matrix lines.
Definition: Matrix.h:671
virtual unsigned int getCurrentOffset() const =0
Return the index of the current object.
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.
Definition: Functions.cpp:1211
void ConvertDoublesVector(double *inputVector, unsigned int inputVectorSize, const int outputVectorDataType, void *outputVector)
Convert a doubles vector.
Definition: Functions.cpp:443