All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/rp/Functions.cpp
22 
23  \brief Raster Processing functions.
24 */
25 
26 // TerraLib
27 #include "../dataaccess/dataset/DataSetType.h"
28 #include "../dataaccess/datasource/DataSourceFactory.h"
29 #include "../dataaccess/utils/Utils.h"
30 #include "../datatype/Enums.h"
31 #include "../raster/BandProperty.h"
32 #include "../raster/Grid.h"
33 #include "../raster/RasterFactory.h"
34 #include "../raster/RasterProperty.h"
35 #include "../raster/RasterIterator.h"
36 #include "../geometry/Point.h"
37 #include "Exception.h"
38 #include "Functions.h"
39 #include "Macros.h"
40 #include "RasterHandler.h"
41 
42 // Boost
43 #include <boost/filesystem.hpp>
44 #include <boost/numeric/ublas/io.hpp>
45 #include <boost/numeric/ublas/matrix.hpp>
46 #include <boost/shared_ptr.hpp>
47 #include <boost/random.hpp>
48 #include <boost/random/uniform_int_distribution.hpp>
49 #include <boost/lexical_cast.hpp>
50 
51 
52 // STL
53 #include <cstring>
54 #include <string>
55 #include <limits>
56 #include <map>
57 #include <memory>
58 
59 #ifndef M_PI
60  #define M_PI 3.14159265358979323846
61 #endif
62 
63 namespace te
64 {
65  namespace rp
66  {
67  bool CreateNewRaster( const te::rst::Grid& rasterGrid,
68  const std::vector< te::rst::BandProperty* >& bandsProperties,
69  const std::string& outDataSetName,
70  const std::string& dataSourceType,
71  RasterHandler& outRasterHandler )
72  {
73  // Creating a new datasource
74 
75  std::auto_ptr< te::da::DataSource > dataSourcePtr(
76  te::da::DataSourceFactory::make( dataSourceType ) );
77  if( dataSourcePtr.get() == 0 ) return false;
78 
79  RasterHandler internalRasterHandler;
80 
81  if( CreateNewRaster( rasterGrid, bandsProperties,
82  outDataSetName, *dataSourcePtr, internalRasterHandler ) )
83  {
84  std::auto_ptr< te::da::DataSource > dummyDataSourcePtr;
85  std::auto_ptr< te::da::DataSourceTransactor > transactorPtr;
86  std::auto_ptr< te::da::DataSet > dataSetPtr;
87  std::auto_ptr< te::rst::Raster > rasterPtr;
88 
89  internalRasterHandler.release( dummyDataSourcePtr, transactorPtr,
90  dataSetPtr, rasterPtr );
91 
92  outRasterHandler.reset( dataSourcePtr.release(), transactorPtr.release(),
93  dataSetPtr.release(), rasterPtr.release() );
94 
95  return true;
96  }
97  else
98  {
99  return false;
100  }
101  }
102 
103  bool CreateNewRaster( const te::rst::Grid& rasterGrid,
104  const std::vector< te::rst::BandProperty* >& bandsProperties,
105  const std::string& outDataSetName,
106  te::da::DataSource& outDataSource,
107  RasterHandler& outRasterHandler )
108  {
109  // Defining the raster properties
110 
111  std::auto_ptr< te::rst::RasterProperty > rasterPropertyPtr(
112  new te::rst::RasterProperty( new te::rst::Grid( rasterGrid ),
113  bandsProperties, std::map< std::string, std::string >(),
114  false, 0, 0 ) );
115 
116  // acquiring a transactor instance
117 
118  std::auto_ptr< te::da::DataSourceTransactor > transactorPtr(
119  outDataSource.getTransactor() );
120 
121  if( transactorPtr.get() == 0 )
122  {
123  return false;
124  }
125 
126  // Creating a data set instance
127 
128  if( transactorPtr->dataSetExists( outDataSetName ) )
129  {
130  transactorPtr->dropDataSet( outDataSetName );
131  }
132 
133  std::auto_ptr< te::da::DataSetType > dataSetTypePtr(
134  new te::da::DataSetType( outDataSetName ) );
135  dataSetTypePtr->add( rasterPropertyPtr.release() );
136 
137  try
138  {
139  transactorPtr->createDataSet( dataSetTypePtr.get(),
140  std::map< std::string, std::string >() );
141  }
142  catch( ... )
143  {
144  return false;
145  }
146 
147  if( ! transactorPtr->dataSetExists( outDataSetName ) ) return false;
148 
149  std::auto_ptr< te::da::DataSet > dataSetPtr( transactorPtr->getDataSet(
150  outDataSetName, te::common::FORWARDONLY, true, te::common::RWAccess ) );
151 
152  if( dataSetPtr.get() == 0 )
153  {
154  return false;
155  }
156 
157  // Creating a raster instance
158 
159  std::auto_ptr< te::rst::Raster > rasterPtr( dataSetPtr->getRaster( 0 ) );
160 
161  if( rasterPtr.get() )
162  {
163  outRasterHandler.reset( transactorPtr.release(), dataSetPtr.release(), rasterPtr.release() );
164  return true;
165  }
166  else
167  {
168  return false;
169  }
170  }
171 
172  bool CreateNewMemRaster( const te::rst::Grid& rasterGrid,
173  std::vector< te::rst::BandProperty* > bandsProperties,
174  RasterHandler& outRasterHandler )
175  {
176  std::string dataSetName = std::string( "createNewMemRaster" ) +
177  boost::lexical_cast< std::string >( &outRasterHandler );
178 
179  return CreateNewRaster( rasterGrid, bandsProperties, dataSetName,
180  "MEM", outRasterHandler );
181  }
182 
183  bool CreateNewGdalRaster( const te::rst::Grid& rasterGrid,
184  std::vector< te::rst::BandProperty* > bandsProperties,
185  const std::string& fileName,
186  RasterHandler& outRasterHandler )
187  {
188 
189  boost::filesystem::path pathInfo( fileName );
190 
191  // Creating a new datasource
192 
193  std::auto_ptr< te::da::DataSource > dataSourcePtr(
195  if( dataSourcePtr.get() == 0 ) return false;
196 
197  std::map<std::string, std::string> outputRasterInfo;
198  outputRasterInfo["SOURCE"] = pathInfo.parent_path().string();
199 
200  dataSourcePtr->setConnectionInfo(outputRasterInfo);
201  dataSourcePtr->open();
202  if( ! dataSourcePtr->isOpened() ) return false;
203 
204  RasterHandler internalRasterHandler;
205 
206  if( CreateNewRaster( rasterGrid, bandsProperties,
207  pathInfo.filename().string(), *dataSourcePtr, internalRasterHandler ) )
208  {
209  std::auto_ptr< te::da::DataSource > dummyDataSourcePtr;
210  std::auto_ptr< te::da::DataSourceTransactor > transactorPtr;
211  std::auto_ptr< te::da::DataSet > dataSetPtr;
212  std::auto_ptr< te::rst::Raster > rasterPtr;
213 
214  internalRasterHandler.release( dummyDataSourcePtr, transactorPtr,
215  dataSetPtr, rasterPtr );
216 
217  outRasterHandler.reset( dataSourcePtr.release(), transactorPtr.release(),
218  dataSetPtr.release(), rasterPtr.release() );
219 
220  return true;
221  }
222  else
223  {
224  return false;
225  }
226  }
227 
228  void GetDataTypeRange( const int dataType, double& min, double& max )
229  {
230  switch( dataType )
231  {
232  case te::dt::BIT_TYPE :
233  min = 0;
234  max = 1;
235  break;
236  case te::dt::CHAR_TYPE :
237  min = -128;
238  max = 128;
239  break;
240  case te::dt::UCHAR_TYPE :
241  min = 0;
242  max = 255;
243  break;
244  case te::dt::INT16_TYPE :
245  case te::dt::CINT16_TYPE :
246  min = -1.0 * (double)std::numeric_limits<short int>::max();
247  max = (double)std::numeric_limits<short int>::max();
248  break;
249  case te::dt::UINT16_TYPE :
250  min = 0;
251  max = (double)std::numeric_limits<unsigned short int>::max();
252  break;
253  case te::dt::INT32_TYPE :
254  case te::dt::CINT32_TYPE :
255  min = -1.0 * (double)std::numeric_limits<int>::max();
256  max = (double)std::numeric_limits<int>::max();
257  break;
258  case te::dt::UINT32_TYPE :
259  min = 0;
260  max = (double)std::numeric_limits<unsigned int>::max();
261  break;
262  case te::dt::INT64_TYPE :
263  min = -1.0 * (double)std::numeric_limits<long int>::max();
264  max = (double)std::numeric_limits<long int>::max();
265  break;
266  case te::dt::UINT64_TYPE :
267  min = 0;
268  max = (double)std::numeric_limits<unsigned long int>::max();
269  break;
270  case te::dt::FLOAT_TYPE :
271  case te::dt::CFLOAT_TYPE :
272  min = -1.0 * (double)std::numeric_limits<float>::max();
273  max = (double)std::numeric_limits<float>::max();
274  break;
275  case te::dt::DOUBLE_TYPE :
276  case te::dt::CDOUBLE_TYPE :
277  min = -1.0 * (double)std::numeric_limits<double>::max();
278  max = (double)std::numeric_limits<double>::max();
279  break;
280  default :
281  throw te::rp::Exception( "Invalid data type" );
282  break;
283  };
284  }
285 
286  void Convert2DoublesVector( void* inputVector, const int inputVectorDataType,
287  unsigned int inputVectorSize, double* outputVector )
288  {
289  switch( inputVectorDataType )
290  {
291  case te::dt::CHAR_TYPE :
292  {
293  char* vPtr = (char*)inputVector;
294  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
295  outputVector[ idx ] = (double)vPtr[ idx ];
296  break;
297  }
298  case te::dt::BIT_TYPE :
299  case te::dt::UCHAR_TYPE :
300  {
301  unsigned char* vPtr = (unsigned char*)inputVector;
302  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
303  outputVector[ idx ] = (double)vPtr[ idx ];
304  break;
305  }
306  case te::dt::INT16_TYPE :
307  {
308  short int* vPtr = (short int*)inputVector;
309  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
310  outputVector[ idx ] = (double)vPtr[ idx ];
311  break;
312  }
313  case te::dt::CINT16_TYPE :
314  {
315  std::complex< short int >* vPtr = (std::complex< short int >*)
316  inputVector;
317  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
318  outputVector[ idx ] = (double)vPtr[ idx ].real();
319  break;
320  }
321  case te::dt::UINT16_TYPE :
322  {
323  unsigned short int* vPtr = (unsigned short int*)inputVector;
324  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
325  outputVector[ idx ] = (double)vPtr[ idx ];
326  break;
327  }
328  case te::dt::INT32_TYPE :
329  {
330  int* vPtr = (int*)inputVector;
331  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
332  outputVector[ idx ] = (double)vPtr[ idx ];
333  break;
334  }
335  case te::dt::CINT32_TYPE :
336  {
337  std::complex< int >* vPtr = (std::complex< int >*)
338  inputVector;
339  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
340  outputVector[ idx ] = (double)vPtr[ idx ].real();
341  break;
342  }
343  case te::dt::UINT32_TYPE :
344  {
345  unsigned int* vPtr = (unsigned int*)inputVector;
346  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
347  outputVector[ idx ] = (double)vPtr[ idx ];
348  break;
349  }
350  case te::dt::INT64_TYPE :
351  {
352  long int* vPtr = (long int*)inputVector;
353  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
354  outputVector[ idx ] = (double)vPtr[ idx ];
355  break;
356  }
357  case te::dt::UINT64_TYPE :
358  {
359  unsigned long int* vPtr = (unsigned long int*)inputVector;
360  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
361  outputVector[ idx ] = (double)vPtr[ idx ];
362  break;
363  }
364  case te::dt::FLOAT_TYPE :
365  {
366  float* vPtr = (float*)inputVector;
367  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
368  outputVector[ idx ] = (double)vPtr[ idx ];
369  break;
370  }
371  case te::dt::CFLOAT_TYPE :
372  {
373  std::complex< float >* vPtr = (std::complex< float >*)
374  inputVector;
375  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
376  outputVector[ idx ] = (double)vPtr[ idx ].real();
377  break;
378  }
379  case te::dt::DOUBLE_TYPE :
380  {
381  memcpy( outputVector, inputVector, inputVectorSize * sizeof( double ) );
382  break;
383  }
384  case te::dt::CDOUBLE_TYPE :
385  {
386  std::complex< double >* vPtr = (std::complex< double >*)
387  inputVector;
388  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
389  outputVector[ idx ] = (double)vPtr[ idx ].real();
390  break;
391  }
392  default :
393  throw te::rp::Exception( "Invalid data type" );
394  break;
395  };
396  }
397 
398  void ConvertDoublesVector( double* inputVector,
399  unsigned int inputVectorSize, const int outputVectorDataType,
400  void* outputVector )
401  {
402  switch( outputVectorDataType )
403  {
404  case te::dt::CHAR_TYPE :
405  {
406  char* vPtr = (char*)outputVector;
407  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
408  vPtr[ idx ] = (char)inputVector[ idx ];
409  break;
410  }
411  case te::dt::BIT_TYPE :
412  case te::dt::UCHAR_TYPE :
413  {
414  unsigned char* vPtr = (unsigned char*)outputVector;
415  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
416  vPtr[ idx ] = (unsigned char)inputVector[ idx ];
417  break;
418  }
419  case te::dt::INT16_TYPE :
420  {
421  short int* vPtr = (short int*)outputVector;
422  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
423  vPtr[ idx ] = (short int)inputVector[ idx ];
424  break;
425  }
426  case te::dt::CINT16_TYPE :
427  {
428  std::complex< short int >* vPtr = (std::complex< short int >*)
429  outputVector;
430  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
431  vPtr[ idx ]= ( (short int)inputVector[ idx ] );
432  break;
433  }
434  case te::dt::UINT16_TYPE :
435  {
436  unsigned short int* vPtr = (unsigned short int*)outputVector;
437  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
438  vPtr[ idx ] = (unsigned short int)inputVector[ idx ];
439  break;
440  }
441  case te::dt::INT32_TYPE :
442  {
443  int* vPtr = (int*)outputVector;
444  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
445  vPtr[ idx ] = (int)inputVector[ idx ];
446  break;
447  }
448  case te::dt::CINT32_TYPE :
449  {
450  std::complex< int >* vPtr = (std::complex< int >*)
451  outputVector;
452  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
453  vPtr[ idx ] = ( (int)inputVector[ idx ] );
454  break;
455  }
456  case te::dt::UINT32_TYPE :
457  {
458  unsigned int* vPtr = (unsigned int*)outputVector;
459  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
460  vPtr[ idx ] = (unsigned int)inputVector[ idx ];
461  break;
462  }
463  case te::dt::INT64_TYPE :
464  {
465  long int* vPtr = (long int*)outputVector;
466  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
467  vPtr[ idx ] = (long int)inputVector[ idx ];
468  break;
469  }
470  case te::dt::UINT64_TYPE :
471  {
472  unsigned long int* vPtr = (unsigned long int*)outputVector;
473  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
474  vPtr[ idx ] = (unsigned long int)inputVector[ idx ];
475  break;
476  }
477  case te::dt::FLOAT_TYPE :
478  {
479  float* vPtr = (float*)outputVector;
480  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
481  vPtr[ idx ] = (float)inputVector[ idx ];
482  break;
483  }
484  case te::dt::CFLOAT_TYPE :
485  {
486  std::complex< float >* vPtr = (std::complex< float >*)
487  outputVector;
488  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
489  vPtr[ idx ] = ( (float)inputVector[ idx ] );
490  break;
491  }
492  case te::dt::DOUBLE_TYPE :
493  {
494  memcpy( outputVector, outputVector, inputVectorSize * sizeof( double ) );
495  break;
496  }
497  case te::dt::CDOUBLE_TYPE :
498  {
499  std::complex< double >* vPtr = (std::complex< double >*)
500  outputVector;
501  for( register unsigned int idx = 0 ; idx < inputVectorSize ; ++idx )
502  vPtr[ idx ] = ( (double)inputVector[ idx ] );
503  break;
504  }
505  default :
506  throw te::rp::Exception( "Invalid data type" );
507  break;
508  };
509  }
510 
511  std::vector<std::string> GetBandNames()
512  {
513  static std::vector<std::string> bandNames;
514 
515  bandNames.push_back("CBERS2_CCD_1_BLUE");
516  bandNames.push_back("CBERS2_CCD_2_GREEN");
517  bandNames.push_back("CBERS2_CCD_3_RED");
518  bandNames.push_back("CBERS2_CCD_4_NIR");
519  bandNames.push_back("CBERS2_CCD_5_PAN");
520 
521  bandNames.push_back("LANDSAT5_TM_1_BLUE");
522  bandNames.push_back("LANDSAT5_TM_2_GREEN");
523  bandNames.push_back("LANDSAT5_TM_3_RED");
524  bandNames.push_back("LANDSAT5_TM_4_NIR");
525  bandNames.push_back("LANDSAT5_TM_5_SWIR");
526  bandNames.push_back("LANDSAT5_TM_6_THERMAL");
527  bandNames.push_back("LANDSAT5_TM_7_MIR");
528 
529  bandNames.push_back("LANDSAT7_ETM+_1_BLUE");
530  bandNames.push_back("LANDSAT7_ETM+_2_GREEN");
531  bandNames.push_back("LANDSAT7_ETM+_3_RED");
532  bandNames.push_back("LANDSAT7_ETM+_4_NIR");
533  bandNames.push_back("LANDSAT7_ETM+_5_SWIR");
534  bandNames.push_back("LANDSAT7_ETM+_6_THERMAL");
535  bandNames.push_back("LANDSAT7_ETM+_7_MIR");
536  bandNames.push_back("LANDSAT7_ETM+_8_PAN");
537 
538  bandNames.push_back("WV2_MUL_1_COASTAL");
539  bandNames.push_back("WV2_MUL_2_BLUE");
540  bandNames.push_back("WV2_MUL_3_GREEN");
541  bandNames.push_back("WV2_MUL_4_YELLOW");
542  bandNames.push_back("WV2_MUL_5_RED");
543  bandNames.push_back("WV2_MUL_6_REDEDGE");
544  bandNames.push_back("WV2_MUL_7_NIR1");
545  bandNames.push_back("WV2_MUL_8_NIR2");
546  bandNames.push_back("WV2_PAN");
547 
548  return bandNames;
549  }
550 
551  std::pair<double, double> GetSpectralBandInfo(std::string bandName)
552  {
553  static std::map<std::string, std::pair<double, double> > BandInfo;
554 
555  BandInfo["CBERS2_CCD_1_BLUE"] = std::pair<double, double> (0.45, 0.52);
556  BandInfo["CBERS2_CCD_2_GREEN"] = std::pair<double, double> (0.52, 0.59);
557  BandInfo["CBERS2_CCD_3_RED"] = std::pair<double, double> (0.63, 0.69);
558  BandInfo["CBERS2_CCD_4_NIR"] = std::pair<double, double> (0.77, 0.89);
559  BandInfo["CBERS2_CCD_5_PAN"] = std::pair<double, double> (0.51, 0.73);
560 
561  BandInfo["LANDSAT5_TM_1_BLUE"] = std::pair<double, double> (0.45, 0.52);
562  BandInfo["LANDSAT5_TM_2_GREEN"] = std::pair<double, double> (0.52, 0.60);
563  BandInfo["LANDSAT5_TM_3_RED"] = std::pair<double, double> (0.63, 0.69);
564  BandInfo["LANDSAT5_TM_4_NIR"] = std::pair<double, double> (0.76, 0.90);
565  BandInfo["LANDSAT5_TM_5_SWIR"] = std::pair<double, double> (1.55, 1.75);
566  BandInfo["LANDSAT5_TM_6_THERMAL"] = std::pair<double, double> (10.40, 12.50);
567  BandInfo["LANDSAT5_TM_7_MIR"] = std::pair<double, double> (2.08, 2.35);
568 
569  BandInfo["LANDSAT7_ETM+_1_BLUE"] = std::pair<double, double> (0.45, 0.515);
570  BandInfo["LANDSAT7_ETM+_2_GREEN"] = std::pair<double, double> (0.525, 0.605);
571  BandInfo["LANDSAT7_ETM+_3_RED"] = std::pair<double, double> (0.63, 0.69);
572  BandInfo["LANDSAT7_ETM+_4_NIR"] = std::pair<double, double> (0.75, 0.90);
573  BandInfo["LANDSAT7_ETM+_5_SWIR"] = std::pair<double, double> (1.55, 1.75);
574  BandInfo["LANDSAT7_ETM+_6_THERMAL"] = std::pair<double, double> (10.40, 12.5);
575  BandInfo["LANDSAT7_ETM+_7_MIR"] = std::pair<double, double> (2.09, 2.35);
576  BandInfo["LANDSAT7_ETM+_8_PAN"] = std::pair<double, double> (0.52, 0.90);
577 
578  BandInfo["WV2_MUL_1_COASTAL"] = std::pair<double, double> (0.4, 0.45);
579  BandInfo["WV2_MUL_2_BLUE"] = std::pair<double, double> (0.45, 0.51);
580  BandInfo["WV2_MUL_3_GREEN"] = std::pair<double, double> (0.51, 0.58);
581  BandInfo["WV2_MUL_4_YELLOW"] = std::pair<double, double> (0.585, 0.625);
582  BandInfo["WV2_MUL_5_RED"] = std::pair<double, double> (0.66, 0.69);
583  BandInfo["WV2_MUL_6_REDEDGE"] = std::pair<double, double> (0.705, 0.745);
584  BandInfo["WV2_MUL_7_NIR1"] = std::pair<double, double> (0.77, 0.895);
585  BandInfo["WV2_MUL_8_NIR2"] = std::pair<double, double> (0.86, 0.104);
586  BandInfo["WV2_PAN"] = std::pair<double, double> (0.45, 0.8);
587 
588  // needs more Band Info from other sensors/bands
589 
590  if (BandInfo.find(bandName) == BandInfo.end())
591  return std::pair<double, double> (0.0, 1.0);
592 
593  return BandInfo[bandName];
594  }
595 
596  double GetSpectralBandMin(std::string bandName)
597  {
598  return GetSpectralBandInfo(bandName).first;
599  }
600 
601  double GetSpectralBandMax(std::string bandName)
602  {
603  return GetSpectralBandInfo(bandName).second;
604  }
605 
606  std::pair<double, double> GetDigitalNumberBandInfo(std::string bandName)
607  {
608  static std::map<std::string, std::pair<double, double> > DNBandInfo;
609 
610  DNBandInfo["CBERS2_CCD_1_BLUE"] = std::pair<double, double> (0.0, 255.0);
611  DNBandInfo["CBERS2_CCD_2_GREEN"] = std::pair<double, double> (0.0, 255.0);
612  DNBandInfo["CBERS2_CCD_3_RED"] = std::pair<double, double> (0.0, 255.0);
613  DNBandInfo["CBERS2_CCD_4_NIR"] = std::pair<double, double> (0.0, 255.0);
614  DNBandInfo["CBERS2_CCD_5_PAN"] = std::pair<double, double> (0.0, 255.0);
615 
616  DNBandInfo["LANDSAT5_TM_1_BLUE"] = std::pair<double, double> (0.0, 255.0);
617  DNBandInfo["LANDSAT5_TM_2_GREEN"] = std::pair<double, double> (0.0, 255.0);
618  DNBandInfo["LANDSAT5_TM_3_RED"] = std::pair<double, double> (0.0, 255.0);
619  DNBandInfo["LANDSAT5_TM_4_NIR"] = std::pair<double, double> (0.0, 255.0);
620  DNBandInfo["LANDSAT5_TM_5_SWIR"] = std::pair<double, double> (0.0, 255.0);
621  DNBandInfo["LANDSAT5_TM_6_THERMAL"] = std::pair<double, double> (0.0, 255.0);
622  DNBandInfo["LANDSAT5_TM_7_MIR"] = std::pair<double, double> (0.0, 255.0);
623 
624  DNBandInfo["LANDSAT7_ETM+_1_BLUE"] = std::pair<double, double> (0.0, 255.0);
625  DNBandInfo["LANDSAT7_ETM+_2_GREEN"] = std::pair<double, double> (0.0, 255.0);
626  DNBandInfo["LANDSAT7_ETM+_3_RED"] = std::pair<double, double> (0.0, 255.0);
627  DNBandInfo["LANDSAT7_ETM+_4_NIR"] = std::pair<double, double> (0.0, 255.0);
628  DNBandInfo["LANDSAT7_ETM+_5_SWIR"] = std::pair<double, double> (0.0, 255.0);
629  DNBandInfo["LANDSAT7_ETM+_6_THERMAL"] = std::pair<double, double> (0.0, 255.0);
630  DNBandInfo["LANDSAT7_ETM+_7_MIR"] = std::pair<double, double> (0.0, 255.0);
631  DNBandInfo["LANDSAT7_ETM+_8_PAN"] = std::pair<double, double> (0.0, 255.0);
632 
633  DNBandInfo["WV2_MUL_1_COASTAL"] = std::pair<double, double> (0.0, 2048.0);
634  DNBandInfo["WV2_MUL_2_BLUE"] = std::pair<double, double> (0.0, 2048.0);
635  DNBandInfo["WV2_MUL_3_GREEN"] = std::pair<double, double> (0.0, 2048.0);
636  DNBandInfo["WV2_MUL_4_YELLOW"] = std::pair<double, double> (0.0, 2048.0);
637  DNBandInfo["WV2_MUL_5_RED"] = std::pair<double, double> (0.0, 2048.0);
638  DNBandInfo["WV2_MUL_6_REDEDGE"] = std::pair<double, double> (0.0, 2048.0);
639  DNBandInfo["WV2_MUL_7_NIR1"] = std::pair<double, double> (0.0, 2048.0);
640  DNBandInfo["WV2_MUL_7_NIR2"] = std::pair<double, double> (0.0, 2048.0);
641  DNBandInfo["WV2_PAN"] = std::pair<double, double> (0.0, 2048.0);
642 
643  // needs more Band Info from other sensors/bands
644 
645  if (DNBandInfo.find(bandName) == DNBandInfo.end())
646  return std::pair<double, double> (0.0, 255.0);
647 
648  return DNBandInfo[bandName];
649  }
650 
651  double GetDigitalNumberBandMax(std::string bandName)
652  {
653  return GetDigitalNumberBandInfo(bandName).second;
654  }
655 
656  bool NormalizeRaster(te::rst::Raster& inraster, double nmin, double nmax)
657  {
658  if (nmin > nmax)
659  return false;
660 
661  // computing old maximuns and minimums for each band
662  std::vector<double> omins;
663  std::vector<double> omaxs;
664  std::vector<unsigned int> bands;
665 
666  for (unsigned int b = 0; b < inraster.getNumberOfBands(); b++)
667  {
668  omins.push_back(inraster.getBand(b)->getMinValue().real());
669  omaxs.push_back(inraster.getBand(b)->getMaxValue().real());
670 
671  bands.push_back(b);
672  }
673 
674  // calculating amplitudes to avoid multiple subtractions
675  double value;
676  const double namplitude = nmax - nmin;
677  std::vector<double> oamplitude;
678  for (unsigned int b = 0; b < inraster.getNumberOfBands(); b++)
679  oamplitude.push_back(omaxs[b] - omins[b]);
680 
681  // iterating over raster to normalize pixel values
684 
685  while (it != itend)
686  {
687  for (unsigned int b = 0; b < inraster.getNumberOfBands(); b++)
688  {
689  value = ((*it)[b] - omins[b]) * namplitude / oamplitude[b] + nmin;
690 
691  inraster.setValue(it.getColumn(), it.getRow(), value, b);
692  }
693 
694  ++it;
695  }
696 
697  return true;
698  }
699 
700  std::vector<te::gm::Point*> GetRandomPointsInRaster(const te::rst::Raster& inputRaster, unsigned int numberOfPoints)
701  {
702  std::vector<te::gm::Point*> randomPoints;
703  double randX;
704  double randY;
705 
706  boost::random::mt19937 generator((boost::random::mt19937::result_type) time(0));
707  boost::random::uniform_int_distribution<> random_rows(0, inputRaster.getNumberOfRows() - 1);
708  boost::random::uniform_int_distribution<> random_columns(0, inputRaster.getNumberOfColumns() - 1);
709 
710  for (unsigned int p = 0; p < numberOfPoints; p++)
711  {
712  inputRaster.getGrid()->gridToGeo(random_columns(generator), random_rows(generator), randX, randY);
713  randomPoints.push_back(new te::gm::Point(randX, randY, inputRaster.getSRID()));
714  }
715 
716  return randomPoints;
717  }
718 
719  bool ConvertRBG2IHS( const te::rst::Raster& inputRGBRaster,
720  const unsigned int redBandIdx, const unsigned int greenBandIdx,
721  const unsigned int blueBandIdx, const double rgbRangeMin,
722  const double rgbRangeMax, te::rst::Raster& outputIHSRaster )
723  {
724  if( ( inputRGBRaster.getAccessPolicy() & te::common::RAccess ) == 0 )
725  return false;
726  if( redBandIdx > inputRGBRaster.getNumberOfBands() ) return false;
727  if( greenBandIdx > inputRGBRaster.getNumberOfBands() ) return false;
728  if( blueBandIdx > inputRGBRaster.getNumberOfBands() ) return false;
729  if( ( outputIHSRaster.getAccessPolicy() & te::common::WAccess ) == 0 )
730  return false;
731  if( outputIHSRaster.getNumberOfBands() < 3 ) return false;
732  if( inputRGBRaster.getNumberOfColumns() != outputIHSRaster.getNumberOfColumns() )
733  return false;
734  if( inputRGBRaster.getNumberOfRows() != outputIHSRaster.getNumberOfRows() )
735  return false;
736 
737  const te::rst::Band& redBand = *inputRGBRaster.getBand( redBandIdx );
738  const te::rst::Band& greenBand = *inputRGBRaster.getBand( greenBandIdx );
739  const te::rst::Band& blueBand = *inputRGBRaster.getBand( blueBandIdx );
740 
741  const unsigned int outNCols = outputIHSRaster.getNumberOfColumns();
742  const unsigned int outNRows = outputIHSRaster.getNumberOfRows();
743  const double redNoData = redBand.getProperty()->m_noDataValue;
744  const double greenNoData = greenBand.getProperty()->m_noDataValue;
745  const double blueNoData = inputRGBRaster.getBand(
746  blueBandIdx )->getProperty()->m_noDataValue;
747 
748  unsigned int outRow = 0;
749  unsigned int outCol = 0;
750  double red = 0;
751  double green = 0;
752  double blue = 0;
753  double teta = 0;
754  double redNorm = 0, greenNorm = 0, blueNorm = 0;
755  double rMinusG = 0, rMinusB = 0;
756  double rgbSum = 0;
757  double cosValue = 0;
758  const double twoPi = 2.0 * ((double)M_PI);
759  const double pi2 = ((double)M_PI) / 2.0;
760  const double rgbNormFac = ( rgbRangeMax == rgbRangeMin ) ? 0.0 :
761  ( 1.0 / ( rgbRangeMax - rgbRangeMin ) );
762  double intensity = 0;
763  double hue = 0;
764  double saturation = 0;
765 
766  for( outRow = 0 ; outRow < outNRows ; ++outRow )
767  {
768  for( outCol = 0 ; outCol < outNCols ; ++outCol )
769  {
770  redBand.getValue( outCol, outRow, red );
771  greenBand.getValue( outCol, outRow, green );
772  blueBand.getValue( outCol, outRow, blue );
773 
774  if( ( red == redNoData ) || ( green == greenNoData ) ||
775  ( blue == blueNoData ) )
776  {
777  intensity = 0.0;
778  hue = 0.0;
779  saturation = 0.0;
780  }
781  else
782  {
783  if( ( red == green ) && ( green == blue ) )
784  { // Gray scale case
785  // From Wikipedia:
786  // h = 0 is used for grays though the hue has no geometric
787  // meaning there, where the saturation s = 0. Similarly,
788  // the choice of 0 as the value for s when l is equal to 0 or 1
789  // is arbitrary.
790 
791  hue = 0.0;
792  saturation = 0.0;
793  intensity = ( red * rgbNormFac ); // or green or blue since they all are the same.
794  }
795  else
796  { // Color case
797  redNorm = ( red - rgbRangeMin ) * rgbNormFac;
798  greenNorm = ( green - rgbRangeMin ) * rgbNormFac;
799  blueNorm = ( blue - rgbRangeMin ) * rgbNormFac;
800 
801  rMinusG = redNorm - greenNorm;
802  rMinusB = redNorm - blueNorm;
803 
804  cosValue = std::sqrt( ( rMinusG * rMinusG ) + ( rMinusB *
805  ( greenNorm - blueNorm ) ) );
806 
807  if( cosValue == 0.0 )
808  {
809  teta = pi2;
810  }
811  else
812  {
813  cosValue = ( 0.5 * ( rMinusG + rMinusB ) ) /
814  cosValue;
815  teta = std::acos( cosValue );
816  }
817 
818  assert( ( cosValue >= (-1.0) ) && ( cosValue <= (1.0) ) );
819 
820  if( blueNorm > greenNorm )
821  {
822  hue = ( twoPi - teta );
823  }
824  else
825  {
826  hue = teta;
827  }
828 
829  rgbSum = ( redNorm + greenNorm + blueNorm );
830 
831  saturation = ( 1.0 - ( 3 * std::min( std::min( redNorm, greenNorm ), blueNorm ) /
832  rgbSum ) );
833 
834  intensity = ( rgbSum / 3.0 );
835  }
836  }
837 
838  outputIHSRaster.setValue( outCol, outRow, intensity, 0 );
839  outputIHSRaster.setValue( outCol, outRow, hue, 1 );
840  outputIHSRaster.setValue( outCol, outRow, saturation, 2 );
841  }
842  }
843 
844  return true;
845  }
846 
847  bool ConvertIHS2RGB( const te::rst::Raster& inputIHSRaster,
848  const unsigned int intensityBandIdx, const unsigned int hueBandIdx,
849  const unsigned int saturationBandIdx, const double rgbRangeMin,
850  const double rgbRangeMax, te::rst::Raster& outputRGBRaster )
851  {
852  if( ( inputIHSRaster.getAccessPolicy() & te::common::RAccess ) == 0 )
853  return false;
854  if( intensityBandIdx > inputIHSRaster.getNumberOfBands() ) return false;
855  if( hueBandIdx > inputIHSRaster.getNumberOfBands() ) return false;
856  if( saturationBandIdx > inputIHSRaster.getNumberOfBands() ) return false;
857  if( ( outputRGBRaster.getAccessPolicy() & te::common::WAccess ) == 0 )
858  return false;
859  if( outputRGBRaster.getNumberOfBands() < 3 ) return false;
860  if( inputIHSRaster.getNumberOfColumns() != outputRGBRaster.getNumberOfColumns() )
861  return false;
862  if( inputIHSRaster.getNumberOfRows() != outputRGBRaster.getNumberOfRows() )
863  return false;
864 
865  const unsigned int nCols = outputRGBRaster.getNumberOfColumns();
866  const unsigned int nRows = outputRGBRaster.getNumberOfRows();
867  const double intensityNoData = inputIHSRaster.getBand(
868  intensityBandIdx )->getProperty()->m_noDataValue;
869  const double hueNoData = inputIHSRaster.getBand(
870  hueBandIdx )->getProperty()->m_noDataValue;
871  const double saturationNoData = inputIHSRaster.getBand(
872  saturationBandIdx )->getProperty()->m_noDataValue;
873 
874  const double rgbNormFac = ( rgbRangeMax == rgbRangeMin ) ? 0.0 :
875  ( rgbRangeMax - rgbRangeMin );
876  const double pi3 = M_PI / 3.0; // 60
877  const double twoPi3 = 2.0 * M_PI / 3.0; // 120
878  const double fourPi3 = 4.0 * M_PI / 3.0; // 240
879  unsigned int row = 0;
880  unsigned int col = 0;
881  double hue = 0;
882  double lig = 0;
883  double sat = 0;
884  double red = 0;
885  double green = 0;
886  double blue = 0;
887  const te::rst::Band& intensityBand = *inputIHSRaster.getBand( intensityBandIdx );
888  const te::rst::Band& hueBand = *inputIHSRaster.getBand( hueBandIdx );
889  const te::rst::Band& saturationBand = *inputIHSRaster.getBand( saturationBandIdx );
890  te::rst::Band& redBand = *outputRGBRaster.getBand( 0 );
891  te::rst::Band& greenBand = *outputRGBRaster.getBand( 1 );
892  te::rst::Band& blueBand = *outputRGBRaster.getBand( 2 );
893 
894  for( row = 0 ; row < nRows ; ++row )
895  {
896  for( col = 0 ; col < nCols ; ++col )
897  {
898  intensityBand.getValue( col, row, lig );
899  hueBand.getValue( col, row, hue );
900  saturationBand.getValue( col, row, sat );
901 
902  if( ( lig == intensityNoData ) || ( hue == hueNoData ) ||
903  ( sat == saturationNoData ) )
904  {
905  red = green = blue = 0;
906  }
907  else
908  {
909  if( ( hue == 0.0 ) && ( sat == 0.0 ) )
910  { // Gray scale case
911  red = green = blue = ( lig * rgbNormFac );
912  }
913  else
914  { // color case
915  /* Hue inside RG sector */
916  if( hue < twoPi3 )
917  {
918  blue = lig * ( 1.0 - sat );
919  red = lig * ( 1.0 + ( sat * std::cos( hue ) /
920  std::cos( pi3 - hue ) ) );
921  green = ( 3.0 * lig ) - ( red + blue );
922  }
923  else if( hue < fourPi3 )
924  { /* Hue inside GB sector */
925 
926  hue -= twoPi3;
927 
928  red = lig * ( 1.0 - sat );
929  green = lig * ( 1.0 + ( sat * std::cos( hue ) /
930  std::cos( pi3 - hue ) ) );
931  blue = ( 3.0 * lig ) - ( red + green );
932  }
933  else
934  { /* Hue inside BR sector */
935 
936  hue -= fourPi3;
937 
938  green = lig * ( 1.0 - sat );
939  blue = lig * ( 1.0 + ( sat * std::cos( hue ) /
940  std::cos( pi3 - hue ) ) );
941  red = ( 3.0 * lig ) - ( green + blue );
942  }
943 
944  red = ( red * rgbNormFac ) + rgbRangeMin;
945  green = ( green * rgbNormFac ) + rgbRangeMin;
946  blue = ( blue * rgbNormFac ) + rgbRangeMin;
947  }
948  }
949 
950  red = MIN( red, rgbRangeMax );
951  green = MIN( green, rgbRangeMax );
952  blue = MIN( blue, rgbRangeMax );
953 
954  red = MAX( red, rgbRangeMin );
955  green = MAX( green, rgbRangeMin );
956  blue = MAX( blue, rgbRangeMin );
957 
958  redBand.setValue( col, row, red );
959  greenBand.setValue( col, row, green );
960  blueBand.setValue( col, row, blue );
961  }
962  }
963 
964  return true;
965  }
966 
967  } // end namespace rp
968 } // end namespace te
Exception class.
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
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
std::vector< te::gm::Point * > GetRandomPointsInRaster(const te::rst::Raster &inputRaster, unsigned int numberOfPoints)
Creates a vector of random positions (points) inside the raster.
Definition: Functions.cpp:700
Raster Processing functions.
bool NormalizeRaster(te::rst::Raster &inraster, double nmin, double nmax)
Normalizes one raster in a given interval.
Definition: Functions.cpp:656
This class implements and iterator to &quot;navigate&quot; over a raster, with a predefined number of bands...
double GetSpectralBandMax(std::string bandName)
Returns the maximum reflectance value of a given sensor/band.
Definition: Functions.cpp:601
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
virtual std::auto_ptr< DataSourceTransactor > getTransactor()=0
It returns an object that can execute transactions in the context of a data source.
static std::auto_ptr< DataSource > make(const std::string &dsType)
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 std::complex< double > getMinValue(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:80
double GetDigitalNumberBandMax(std::string bandName)
Returns the maximum digital number of a given sensor/band.
Definition: Functions.cpp:651
bool ConvertRBG2IHS(const te::rst::Raster &inputRGBRaster, const unsigned int redBandIdx, const unsigned int greenBandIdx, const unsigned int blueBandIdx, const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster &outputIHSRaster)
RGB to IHS conversion.
Definition: Functions.cpp:719
void Convert2DoublesVector(void *inputVector, const int inputVectorDataType, unsigned int inputVectorSize, double *outputVector)
Convert vector elements.
Definition: Functions.cpp:286
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
unsigned int getRow() const
Returns the current row in iterator.
double GetSpectralBandMin(std::string bandName)
Returns the minimum reflectance value of a given sensor/band.
Definition: Functions.cpp:596
A raster band description.
Definition: Band.h:63
A rectified grid is the spatial support for raster data.
Definition: Grid.h:55
int getSRID() const
Returns the raster spatial reference system identifier.
Definition: Raster.cpp:203
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
std::pair< double, double > GetSpectralBandInfo(std::string bandName)
Returns the maximun and minimum reflectance values of a given sensor/band.
Definition: Functions.cpp:551
bool CreateNewGdalRaster(const te::rst::Grid &rasterGrid, std::vector< te::rst::BandProperty * > bandsProperties, const std::string &fileName, RasterHandler &outRasterHandler)
Create a new raster into a GDAL datasource.
Definition: Functions.cpp:183
std::vector< std::string > GetBandNames()
Returns a vector os with band&#39;s names.
Definition: Functions.cpp:511
A point with x and y coordinate values.
Definition: Point.h:50
bool CreateNewRaster(const te::rst::Grid &rasterGrid, const std::vector< te::rst::BandProperty * > &bandsProperties, const std::string &outDataSetName, const std::string &dataSourceType, RasterHandler &outRasterHandler)
Create a new raster into the givem data source.
Definition: Functions.cpp:67
bool CreateNewMemRaster(const te::rst::Grid &rasterGrid, std::vector< te::rst::BandProperty * > bandsProperties, RasterHandler &outRasterHandler)
Create a new raster into a new memory datasource.
Definition: Functions.cpp:172
void reset()
Reset the internal state (all internal pointed objects are deleted).
std::pair< double, double > GetDigitalNumberBandInfo(std::string bandName)
Returns the maximun and minimum digital numbers of a given sensor/band.
Definition: Functions.cpp:606
#define M_PI
Definition: Functions.cpp:60
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:116
RasterHandler.
Definition: RasterHandler.h:47
A class that models the description of a dataset.
Definition: DataSetType.h:72
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits&lt;double&gt;::max().
Definition: BandProperty.h:136
#define MIN(a, b)
Macro that returns min between two values.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
Definition: Grid.cpp:302
static RasterIterator end(Raster *r, const std::vector< unsigned int > &bands)
Returns an iterator referring to after the end of the iterator.
void GetDataTypeRange(const int dataType, double &min, double &max)
Returns the real data type range (all values that can be represented by the given data type)...
Definition: Functions.cpp:228
Raster property.
An abstract class for raster data strucutures.
Definition: Raster.h:70
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:370
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
void ConvertDoublesVector(double *inputVector, unsigned int inputVectorSize, const int outputVectorDataType, void *outputVector)
Convert a doubles vector.
Definition: Functions.cpp:398
unsigned int getColumn() const
Returns the current column in iterator.
#define MAX(a, b)
Macro that returns max between two values.
virtual std::complex< double > getMaxValue(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:109
static RasterIterator begin(Raster *r, const std::vector< unsigned int > &bands)
Returns an iterator referring to the first value.
bool ConvertIHS2RGB(const te::rst::Raster &inputIHSRaster, const unsigned int intensityBandIdx, const unsigned int hueBandIdx, const unsigned int saturationBandIdx, const double rgbRangeMin, const double rgbRangeMax, te::rst::Raster &outputRGBRaster)
IHS to RGB conversion.
Definition: Functions.cpp:847
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Definition: Raster.cpp:89
Raster tuple.