TsFunctions.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/unittest/rp/functions/TsFunctions.cpp
22 
23  \brief A test suit for the Functions interface.
24 */
25 
26 // TerraLib
27 #include "../Config.h"
28 #include <terralib/rp.h>
29 #include <terralib/raster.h>
30 #include <terralib/dataaccess.h>
31 
32 // STL
33 #include <memory>
34 #include <string>
35 
36 // Boost
37 #define BOOST_TEST_NO_MAIN
38 #include <boost/test/unit_test.hpp>
39 #include <boost/lexical_cast.hpp>
40 #include <boost/timer.hpp>
41 
42 BOOST_AUTO_TEST_SUITE (functions_tests)
43 
44 BOOST_AUTO_TEST_CASE( createFixedStepPalette_test )
45 {
46  std::vector< te::rst::BandProperty * > bandsProps;
47  bandsProps.push_back( new te::rst::BandProperty( 0,
48  te::dt::UCHAR_TYPE ) );
49  bandsProps.push_back( new te::rst::BandProperty( 1,
50  te::dt::UCHAR_TYPE ) );
51  bandsProps.push_back( new te::rst::BandProperty( 2,
52  te::dt::UCHAR_TYPE ) );
53  bandsProps[ 0 ]->m_colorInterp = te::rst::RedCInt;
54  bandsProps[ 1 ]->m_colorInterp = te::rst::GreenCInt;
55  bandsProps[ 2 ]->m_colorInterp = te::rst::BlueCInt;
56  bandsProps[ 0 ]->m_noDataValue = std::numeric_limits< double >::max();
57  bandsProps[ 1 ]->m_noDataValue = std::numeric_limits< double >::max();
58  bandsProps[ 2 ]->m_noDataValue = std::numeric_limits< double >::max();
59 
60  std::map< std::string, std::string > rInfo;
61  rInfo["URI"] = "terralib_unittest_rp_functions_CreateFixedStepPalette.tif";
62 
63  std::unique_ptr< te::rst::Raster > rasterPointer( te::rst::RasterFactory::make( "GDAL",
64  new te::rst::Grid( 256, 256 ), bandsProps,
65  rInfo, 0, 0 ) );
66 
67  std::vector< te::rst::BandProperty::ColorEntry > palette;
68  te::rp::CreateFixedStepPalette( 256, true, palette );
69 
70  unsigned int line = 0;
71  unsigned int col = 0;
72 
73  for( line = 0 ; line < 256 ; ++line )
74  for( col = 0 ; col < 256 ; ++col )
75  {
76  rasterPointer->setValue( col, line, (double)palette[ line ].c1, 0 );
77  rasterPointer->setValue( col, line, (double)palette[ line ].c2, 1 );
78  rasterPointer->setValue( col, line, (double)palette[ line ].c3, 2 );
79  }
80 }
81 
82 BOOST_AUTO_TEST_CASE(rasterSlicing_test)
83 {
84  /* Openning input raster */
85 
86  std::map<std::string, std::string> auxRasterInfo;
87  std::map<double, double> limits;
88  for (int lim = 0; lim < 255; lim += 25)
89  limits.insert(std::pair<double, double>((double)lim, (double)lim + 25));
90 
91  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_hrc_crop.tif";
92  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
93  auxRasterInfo ) );
94  BOOST_CHECK( diskRasterPtr.get() );
95 
96  {
97  std::unique_ptr< te::rst::Raster > outRasterPtr;
98  std::map<std::string, std::string> auxRasterInfo2;
99  auxRasterInfo2["URI"] = "terralib_unittest_rp_functions_RasterSlicing_RGB.tif";
100  BOOST_CHECK( te::rp::RasterSlicing(
101  *diskRasterPtr.get(),
102  0,
103  false,
104  10,
105  true,
106  auxRasterInfo2,
107  "GDAL",
108  true,
109  0,
110  outRasterPtr,
111  limits) );
112  }
113 
114  {
115  std::unique_ptr< te::rst::Raster > outRasterPtr;
116  std::map<std::string, std::string> auxRasterInfo2;
117  auxRasterInfo2["URI"] = "terralib_unittest_rp_functions_RasterSlicing_palette.tif";
118  BOOST_CHECK( te::rp::RasterSlicing(
119  *diskRasterPtr.get(),
120  0,
121  true,
122  10,
123  true,
124  auxRasterInfo2,
125  "GDAL",
126  true,
127  0,
128  outRasterPtr,
129  limits) );
130  }
131 }
132 
134 {
135  /* Openning input raster */
136 
137  std::map<std::string, std::string> auxRasterInfo;
138 
139  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
140  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
141  auxRasterInfo ) );
142  BOOST_CHECK( diskRasterPtr.get() );
143 
144  auxRasterInfo.clear();
145  auxRasterInfo["MEM_RASTER_NROWS"] = boost::lexical_cast< std::string >(
146  diskRasterPtr->getNumberOfRows() );
147  auxRasterInfo["MEM_RASTER_NCOLS"] = boost::lexical_cast< std::string >(
148  diskRasterPtr->getNumberOfColumns() );
149  auxRasterInfo["MEM_RASTER_DATATYPE"] = boost::lexical_cast< std::string >((int)te::dt::DOUBLE_TYPE);
150  auxRasterInfo["MEM_RASTER_NBANDS"] = "3";
151 
152  std::unique_ptr< te::rst::Raster > ihsRasterPtr( te::rst::RasterFactory::make(
153  "MEM", 0, std::vector<te::rst::BandProperty*>(), auxRasterInfo) );
154  BOOST_CHECK( ihsRasterPtr.get() );
155 
156  BOOST_CHECK( te::rp::ConvertRGB2IHS( *diskRasterPtr, 0, 1, 2, 0, 255, *ihsRasterPtr ) );
157 
158  BOOST_CHECK( te::rp::Copy2DiskRaster( *ihsRasterPtr, "terralib_unittest_rp_functions_rgb2ihs.tif" ) ) ;
159 
160  auxRasterInfo.clear();
161  auxRasterInfo["MEM_RASTER_NROWS"] = boost::lexical_cast< std::string >(
162  diskRasterPtr->getNumberOfRows() );
163  auxRasterInfo["MEM_RASTER_NCOLS"] = boost::lexical_cast< std::string >(
164  diskRasterPtr->getNumberOfColumns() );
165  auxRasterInfo["MEM_RASTER_DATATYPE"] = boost::lexical_cast< std::string >((int)te::dt::UCHAR_TYPE);
166  auxRasterInfo["MEM_RASTER_NBANDS"] = "3";
167 
168  std::unique_ptr< te::rst::Raster > rgbRasterPtr( te::rst::RasterFactory::make(
169  "MEM", 0, std::vector<te::rst::BandProperty*>(), auxRasterInfo) );
170  BOOST_CHECK( rgbRasterPtr.get() );
171 
172  BOOST_CHECK( te::rp::ConvertIHS2RGB( *ihsRasterPtr, 0, 1, 2, 0, 255, *rgbRasterPtr ) );
173 
174  std::vector< te::rst::BandProperty * > bandProps;
175  bandProps.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 0 )->getProperty() ) ) );
176  bandProps.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 1 )->getProperty() ) ) );
177  bandProps.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 2 )->getProperty() ) ) );
178 
179  auxRasterInfo["URI"] = "terralib_unittest_rp_functions_ihs2rgb.tif";
180 
181  std::unique_ptr< te::rst::Raster > outDiskRasterPtr( te::rst::RasterFactory::make(
182  "GDAL", new te::rst::Grid( *( diskRasterPtr->getGrid() ) ), bandProps,
183  auxRasterInfo, 0, 0 ) );
184  BOOST_CHECK( outDiskRasterPtr.get() );
185 
186  const size_t nBands = outDiskRasterPtr->getNumberOfBands();
187  const unsigned int nCols = outDiskRasterPtr->getNumberOfColumns();
188  const unsigned int nRows = outDiskRasterPtr->getNumberOfRows();
189  unsigned int col = 0;
190  unsigned int row = 0;
191  double value = 0;
192 
193  for( size_t band = 0 ; band < nBands ; ++band )
194  {
195  for( row = 0 ; row < nRows ; ++row )
196  {
197  for( col = 0 ; col < nCols ; ++col )
198  {
199  rgbRasterPtr->getValue( col, row, value, band );
200  outDiskRasterPtr->setValue( col, row, value, band );
201  }
202  }
203  }
204 }
205 
207 {
208  /* Openning input raster */
209 
210  std::map<std::string, std::string> auxRasterInfo;
211 
212  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
213  std::unique_ptr< te::rst::Raster > diskRasterPtr(te::rst::RasterFactory::open(
214  auxRasterInfo));
215  BOOST_CHECK(diskRasterPtr.get());
216 
217  auxRasterInfo.clear();
218  auxRasterInfo["MEM_RASTER_NROWS"] = boost::lexical_cast< std::string >(
219  diskRasterPtr->getNumberOfRows());
220  auxRasterInfo["MEM_RASTER_NCOLS"] = boost::lexical_cast< std::string >(
221  diskRasterPtr->getNumberOfColumns());
222  auxRasterInfo["MEM_RASTER_DATATYPE"] = boost::lexical_cast< std::string >((int)te::dt::DOUBLE_TYPE);
223  auxRasterInfo["MEM_RASTER_NBANDS"] = "3";
224 
225  std::unique_ptr< te::rst::Raster > hlsRasterPtr(te::rst::RasterFactory::make(
226  "MEM", 0, std::vector<te::rst::BandProperty*>(), auxRasterInfo));
227  BOOST_CHECK(hlsRasterPtr.get());
228 
229  BOOST_CHECK(te::rp::ConvertRGB2HLS(*diskRasterPtr, 0, 1, 2, 0, 255, *hlsRasterPtr));
230 
231  BOOST_CHECK(te::rp::Copy2DiskRaster(*hlsRasterPtr, "terralib_unittest_rp_functions_rgb2hls.tif"));
232 
233  auxRasterInfo.clear();
234  auxRasterInfo["MEM_RASTER_NROWS"] = boost::lexical_cast< std::string >(
235  diskRasterPtr->getNumberOfRows());
236  auxRasterInfo["MEM_RASTER_NCOLS"] = boost::lexical_cast< std::string >(
237  diskRasterPtr->getNumberOfColumns());
238  auxRasterInfo["MEM_RASTER_DATATYPE"] = boost::lexical_cast< std::string >((int)te::dt::UCHAR_TYPE);
239  auxRasterInfo["MEM_RASTER_NBANDS"] = "3";
240 
241  std::unique_ptr< te::rst::Raster > rgbRasterPtr(te::rst::RasterFactory::make(
242  "MEM", 0, std::vector<te::rst::BandProperty*>(), auxRasterInfo));
243  BOOST_CHECK(rgbRasterPtr.get());
244 
245  BOOST_CHECK(te::rp::ConvertHLS2RGB(*hlsRasterPtr, 0, 1, 2, 0, 255, *rgbRasterPtr));
246 
247  std::vector< te::rst::BandProperty * > bandProps;
248  bandProps.push_back(new te::rst::BandProperty(*(diskRasterPtr->getBand(0)->getProperty())));
249  bandProps.push_back(new te::rst::BandProperty(*(diskRasterPtr->getBand(1)->getProperty())));
250  bandProps.push_back(new te::rst::BandProperty(*(diskRasterPtr->getBand(2)->getProperty())));
251 
252  auxRasterInfo["URI"] = "terralib_unittest_rp_functions_hls2rgb.tif";
253 
254  std::unique_ptr< te::rst::Raster > outDiskRasterPtr(te::rst::RasterFactory::make(
255  "GDAL", new te::rst::Grid(*(diskRasterPtr->getGrid())), bandProps,
256  auxRasterInfo, 0, 0));
257  BOOST_CHECK(outDiskRasterPtr.get());
258 
259  const size_t nBands = outDiskRasterPtr->getNumberOfBands();
260  const unsigned int nCols = outDiskRasterPtr->getNumberOfColumns();
261  const unsigned int nRows = outDiskRasterPtr->getNumberOfRows();
262  unsigned int col = 0;
263  unsigned int row = 0;
264  double value = 0;
265 
266  for (size_t band = 0; band < nBands; ++band)
267  {
268  for (row = 0; row < nRows; ++row)
269  {
270  for (col = 0; col < nCols; ++col)
271  {
272  rgbRasterPtr->getValue(col, row, value, band);
273  outDiskRasterPtr->setValue(col, row, value, band);
274  }
275  }
276  }
277 }
278 
279 BOOST_AUTO_TEST_CASE(getMeanValue_test)
280 {
281  /* Openning input raster */
282 
283  std::map<std::string, std::string> auxRasterInfo;
284 
285  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_hrc_crop.tif";
286  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
287  auxRasterInfo ) );
288  BOOST_CHECK( diskRasterPtr.get() );
289 
290  double meanValue = 0;
291 
292 // boost::timer timer;
293  BOOST_CHECK( te::rp::GetMeanValue( *diskRasterPtr->getBand( 0 ), 1, meanValue) );
294 // std::cout << std::endl << "Elapsed:" << timer.elapsed() << std::endl;
295  BOOST_CHECK_CLOSE( 181.340256531345, meanValue, 0.0001 );
296 }
297 
298 BOOST_AUTO_TEST_CASE(getMeanValueOptimized_test)
299 {
300  /* Openning input raster */
301 
302  std::map<std::string, std::string> auxRasterInfo;
303 
304  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_hrc_crop.tif";
305  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
306  auxRasterInfo ) );
307  BOOST_CHECK( diskRasterPtr.get() );
308 
309  double meanValue = 0;
310 
311 // boost::timer timer;
312  BOOST_CHECK( te::rp::GetMeanValue( *diskRasterPtr->getBand( 0 ), 4, meanValue) );
313 // std::cout << std::endl << "Elapsed:" << timer.elapsed() << std::endl;
314  BOOST_CHECK_CLOSE( 181.340256531345, meanValue, 0.0001 );
315 }
316 
317 BOOST_AUTO_TEST_CASE(getCovarianceValue_test)
318 {
319  /* Openning input raster */
320 
321  std::map<std::string, std::string> auxRasterInfo;
322 
323  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_hrc_crop.tif";
324  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
325  auxRasterInfo ) );
326  BOOST_CHECK( diskRasterPtr.get() );
327 
328  double covarianceValue = 0;
329 
330 // boost::timer timer;
331  BOOST_CHECK( te::rp::GetCovarianceValue( *diskRasterPtr->getBand( 0 ),
332  *diskRasterPtr->getBand( 0 ), 1, 0, 0, covarianceValue) );
333 // std::cout << std::endl << "Elapsed:" << timer.elapsed() << std::endl;
334  BOOST_CHECK_CLOSE( 2143.89743610679, covarianceValue, 0.0001 );
335 }
336 
337 BOOST_AUTO_TEST_CASE(getCovarianceValueOptimized_test)
338 {
339  /* Openning input raster */
340 
341  std::map<std::string, std::string> auxRasterInfo;
342 
343  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_hrc_crop.tif";
344  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
345  auxRasterInfo ) );
346  BOOST_CHECK( diskRasterPtr.get() );
347 
348  double covarianceValue = 0;
349 
350 // boost::timer timer;
351  BOOST_CHECK( te::rp::GetCovarianceValue( *diskRasterPtr->getBand( 0 ),
352  *diskRasterPtr->getBand( 0 ), 4, 0, 0, covarianceValue) );
353 // std::cout << std::endl << "Elapsed:" << timer.elapsed() << std::endl;
354  BOOST_CHECK_CLOSE( 2143.89743610679, covarianceValue, 0.0001 );
355 }
356 
357 BOOST_AUTO_TEST_CASE(principalComponents_test)
358 {
359  /* Openning input raster */
360 
361  std::map<std::string, std::string> auxRasterInfo;
362 
363  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
364  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
365  auxRasterInfo ) );
366  BOOST_CHECK( diskRasterPtr.get() );
367 
368  std::vector< te::rst::BandProperty * > bandProps1;
369  bandProps1.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 0 )->getProperty() ) ) );
370  bandProps1[ 0 ]->m_blkh = diskRasterPtr->getNumberOfRows();
371  bandProps1[ 0 ]->m_blkw = diskRasterPtr->getNumberOfColumns();
372  bandProps1[ 0 ]->m_nblocksx = 1;
373  bandProps1[ 0 ]->m_nblocksy = 1;
374  bandProps1[ 0 ]->m_type = te::dt::DOUBLE_TYPE;
375  bandProps1.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 1 )->getProperty() ) ) );
376  bandProps1[ 1 ]->m_blkh = diskRasterPtr->getNumberOfRows();
377  bandProps1[ 1 ]->m_blkw = diskRasterPtr->getNumberOfColumns();
378  bandProps1[ 1 ]->m_nblocksx = 1;
379  bandProps1[ 1 ]->m_nblocksy = 1;
380  bandProps1[ 1 ]->m_type = te::dt::DOUBLE_TYPE;
381  bandProps1.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 2 )->getProperty() ) ) );
382  bandProps1[ 2 ]->m_blkh = diskRasterPtr->getNumberOfRows();
383  bandProps1[ 2 ]->m_blkw = diskRasterPtr->getNumberOfColumns();
384  bandProps1[ 2 ]->m_nblocksx = 1;
385  bandProps1[ 2 ]->m_nblocksy = 1;
386  bandProps1[ 2 ]->m_type = te::dt::DOUBLE_TYPE;
387 
388  std::unique_ptr< te::rst::Raster > pcaRasterPtr( te::rst::RasterFactory::make(
389  "MEM", new te::rst::Grid( *( diskRasterPtr->getGrid() ) ), bandProps1,
390  std::map<std::string, std::string>(), 0, 0 ) );
391  BOOST_CHECK( pcaRasterPtr.get() );
392 
393  std::vector< unsigned int > inputRasterBands;
394  inputRasterBands.push_back( 0 );
395  inputRasterBands.push_back( 1 );
396  inputRasterBands.push_back( 2 );
397 
398  boost::numeric::ublas::matrix< double > pcaMatrix;
399 
401  *diskRasterPtr,
402  inputRasterBands,
403  pcaMatrix,
404  *pcaRasterPtr,
405  inputRasterBands,
406  1 ) );
407 
408  BOOST_CHECK( te::rp::Copy2DiskRaster( *pcaRasterPtr, "terralib_unittest_rp_functions_DirectPrincipalComponents.tif" ) ) ;
409 
410  auxRasterInfo.clear();
411  auxRasterInfo["URI"] = "terralib_unittest_rp_functions_InversePrincipalComponents.tif";
412 
413  std::vector< te::rst::BandProperty * > bandProps2;
414  bandProps2.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 0 )->getProperty() ) ) );
415  bandProps2.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 1 )->getProperty() ) ) );
416  bandProps2.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 2 )->getProperty() ) ) );
417 
418  std::unique_ptr< te::rst::Raster > outDiskRasterPtr( te::rst::RasterFactory::make(
419  "GDAL", new te::rst::Grid( *( diskRasterPtr->getGrid() ) ), bandProps2,
420  auxRasterInfo, 0, 0 ) );
421  BOOST_CHECK( outDiskRasterPtr.get() );
422 
424  *pcaRasterPtr,
425  pcaMatrix,
426  *outDiskRasterPtr,
427  inputRasterBands,
428  1 ) );
429 }
430 
431 BOOST_AUTO_TEST_CASE(principalComponentsOptimized_test)
432 {
433  /* Openning input raster */
434 
435  std::map<std::string, std::string> auxRasterInfo;
436 
437  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
438  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
439  auxRasterInfo ) );
440  BOOST_CHECK( diskRasterPtr.get() );
441 
442  std::vector< te::rst::BandProperty * > bandProps1;
443  bandProps1.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 0 )->getProperty() ) ) );
444  bandProps1[ 0 ]->m_type = te::dt::DOUBLE_TYPE;
445  bandProps1.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 1 )->getProperty() ) ) );
446  bandProps1[ 1 ]->m_type = te::dt::DOUBLE_TYPE;
447  bandProps1.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 2 )->getProperty() ) ) );
448  bandProps1[ 2 ]->m_type = te::dt::DOUBLE_TYPE;
449 
450  std::unique_ptr< te::rst::Raster > pcaRasterPtr( te::rst::RasterFactory::make(
451  "MEM", new te::rst::Grid( *( diskRasterPtr->getGrid() ) ), bandProps1,
452  std::map<std::string, std::string>(), 0, 0 ) );
453  BOOST_CHECK( pcaRasterPtr.get() );
454 
455  std::vector< unsigned int > inputRasterBands;
456  inputRasterBands.push_back( 0 );
457  inputRasterBands.push_back( 1 );
458  inputRasterBands.push_back( 2 );
459 
460  boost::numeric::ublas::matrix< double > pcaMatrix;
461 
463  *diskRasterPtr,
464  inputRasterBands,
465  pcaMatrix,
466  *pcaRasterPtr,
467  inputRasterBands,
468  4 ) );
469 
470  BOOST_CHECK( te::rp::Copy2DiskRaster( *pcaRasterPtr, "terralib_unittest_rp_functions_DirectPrincipalComponentsOptimized.tif" ) ) ;
471 
472  std::vector< te::rst::BandProperty * > bandProps;
473  bandProps.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 0 )->getProperty() ) ) );
474  bandProps.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 1 )->getProperty() ) ) );
475  bandProps.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 2 )->getProperty() ) ) );
476 
477  auxRasterInfo.clear();
478  auxRasterInfo["URI"] = "terralib_unittest_rp_functions_InversePrincipalComponentsOptimized.tif";
479 
480  std::unique_ptr< te::rst::Raster > outDiskRasterPtr( te::rst::RasterFactory::make(
481  "GDAL", new te::rst::Grid( *( diskRasterPtr->getGrid() ) ), bandProps,
482  auxRasterInfo, 0, 0 ) );
483  BOOST_CHECK( outDiskRasterPtr.get() );
484 
486  *pcaRasterPtr,
487  pcaMatrix,
488  *outDiskRasterPtr,
489  inputRasterBands,
490  4 ) );
491 }
492 
493 BOOST_AUTO_TEST_CASE(getStdDevValue_test)
494 {
495  /* Openning input raster */
496 
497  std::map<std::string, std::string> auxRasterInfo;
498 
499  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_hrc_crop.tif";
500  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
501  auxRasterInfo ) );
502  BOOST_CHECK( diskRasterPtr.get() );
503 
504  double stdDevValue = 0;
505 
506 // boost::timer timer;
507  BOOST_CHECK( te::rp::GetStdDevValue( *diskRasterPtr->getBand( 0 ), 1, 0, stdDevValue) );
508 // std::cout << std::endl << "Elapsed:" << timer.elapsed() << std::endl;
509  BOOST_CHECK_CLOSE( 46.302240075, stdDevValue, 0.0001 );
510 }
511 
512 BOOST_AUTO_TEST_CASE(getStdDevValueOptimized_test)
513 {
514  /* Openning input raster */
515 
516  std::map<std::string, std::string> auxRasterInfo;
517 
518  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_hrc_crop.tif";
519  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
520  auxRasterInfo ) );
521  BOOST_CHECK( diskRasterPtr.get() );
522 
523  double stdDevValue = 0;
524 
525 // boost::timer timer;
526  BOOST_CHECK( te::rp::GetStdDevValue( *diskRasterPtr->getBand( 0 ), 4, 0, stdDevValue) );
527 // std::cout << std::endl << "Elapsed:" << timer.elapsed() << std::endl;
528  BOOST_CHECK_CLOSE( 46.302240075, stdDevValue, 0.0001 );
529 }
530 
531 BOOST_AUTO_TEST_CASE(decomposeBands_test)
532 {
533  /* Openning input raster */
534 
535  std::map<std::string, std::string> auxRasterInfo;
536 
537  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
538  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
539  auxRasterInfo ) );
540  BOOST_CHECK( diskRasterPtr.get() );
541 
542  std::vector< unsigned int > inputRasterBands;
543  inputRasterBands.push_back( 2 );
544  inputRasterBands.push_back( 0 );
545 
546  std::vector< std::map<std::string, std::string> > outputRastersInfos( 2 );
547  outputRastersInfos[ 0 ][ "URI" ] = "terralib_unittest_rp_functions_DecomposeBands_2.tif";
548  outputRastersInfos[ 1 ][ "URI" ] = "terralib_unittest_rp_functions_DecomposeBands_0.tif";
549 
550  std::vector< boost::shared_ptr< te::rst::Raster > > outputRastersPtrs;
551 
552  BOOST_CHECK( te::rp::DecomposeBands( *diskRasterPtr, inputRasterBands,
553  outputRastersInfos, "GDAL", outputRastersPtrs ) );
554 }
555 
556 BOOST_AUTO_TEST_CASE(composeBandsSameSRID_test)
557 {
558  /* Openning input rasters */
559 
560  std::map<std::string, std::string> auxRasterInfo;
561 
562  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers_rgb342_crop2.tif";
563  std::unique_ptr< te::rst::Raster > diskRasterPtr1( te::rst::RasterFactory::open(
564  auxRasterInfo ) );
565  BOOST_CHECK( diskRasterPtr1.get() );
566 
567  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers_rgb342_crop3.tif";
568  std::unique_ptr< te::rst::Raster > diskRasterPtr2( te::rst::RasterFactory::open(
569  auxRasterInfo ) );
570  BOOST_CHECK( diskRasterPtr2.get() );
571 
572  std::vector< const te::rst::Raster * > rasters;
573  rasters.push_back( diskRasterPtr1.get() );
574  rasters.push_back( diskRasterPtr2.get() );
575 
576  te::rp::FeederConstRasterVector feeder( rasters );
577 
578  std::vector< unsigned int > inputRasterBands;
579  inputRasterBands.push_back( 2 );
580  inputRasterBands.push_back( 0 );
581 
582  std::map<std::string, std::string> outputRasterInfo;
583  outputRasterInfo["URI"] = "terralib_unittest_rp_functions_ComposeBandsSameSRID.tif";
584 
585  std::unique_ptr< te::rst::Raster > outputRasterPtr;
586 
587  BOOST_CHECK( te::rp::ComposeBands( feeder, inputRasterBands,
588  te::rst::NearestNeighbor, outputRasterInfo,
589  "GDAL", outputRasterPtr ) );
590 }
591 
592 BOOST_AUTO_TEST_CASE(composeBandsDifSRID_test)
593 {
594  /* Openning input rasters */
595 
596  std::map<std::string, std::string> auxRasterInfo;
597 
598  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers_rgb342_crop2.tif";
599  std::unique_ptr< te::rst::Raster > diskRasterPtr1( te::rst::RasterFactory::open(
600  auxRasterInfo ) );
601  BOOST_CHECK( diskRasterPtr1.get() );
602 
603  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers_rgb342_crop3_EPSG_22522.tif";
604  std::unique_ptr< te::rst::Raster > diskRasterPtr2( te::rst::RasterFactory::open(
605  auxRasterInfo ) );
606  BOOST_CHECK( diskRasterPtr2.get() );
607 
608  std::vector< const te::rst::Raster * > rasters;
609  rasters.push_back( diskRasterPtr1.get() );
610  rasters.push_back( diskRasterPtr2.get() );
611 
612  te::rp::FeederConstRasterVector feeder( rasters );
613 
614  std::vector< unsigned int > inputRasterBands;
615  inputRasterBands.push_back( 2 );
616  inputRasterBands.push_back( 0 );
617 
618  std::map<std::string, std::string> outputRasterInfo;
619  outputRasterInfo["URI"] = "terralib_unittest_rp_functions_ComposeBandsDifSRID.tif";
620 
621  std::unique_ptr< te::rst::Raster > outputRasterPtr;
622 
623  BOOST_CHECK( te::rp::ComposeBands( feeder, inputRasterBands,
624  te::rst::NearestNeighbor, outputRasterInfo,
625  "GDAL", outputRasterPtr ) );
626 }
627 
628 BOOST_AUTO_TEST_CASE(getDetailedExtent_test)
629 {
630  te::rst::Grid grid( 2u, 2u, new te::gm::Envelope( 0.0, 0.0, 2.0, 2.0 ), 12345 );
631 
633 
634  BOOST_CHECK( te::rp::GetDetailedExtent( grid, lr ) );
635  BOOST_CHECK( lr.size() == 9 );
636 
637  BOOST_CHECK_CLOSE( lr.getX( 0 ), 0.0 , 0.0000000001 );
638  BOOST_CHECK_CLOSE( lr.getY( 0 ), 2.0 , 0.0000000001 );
639 
640  BOOST_CHECK_CLOSE( lr.getX( 1 ), 1.0 , 0.0000000001 );
641  BOOST_CHECK_CLOSE( lr.getY( 1 ), 2.0 , 0.0000000001 );
642 
643  BOOST_CHECK_CLOSE( lr.getX( 2 ), 2.0 , 0.0000000001 );
644  BOOST_CHECK_CLOSE( lr.getY( 2 ), 2.0 , 0.0000000001 );
645 
646  BOOST_CHECK_CLOSE( lr.getX( 3 ), 2.0 , 0.0000000001 );
647  BOOST_CHECK_CLOSE( lr.getY( 3 ), 1.0 , 0.0000000001 );
648 
649  BOOST_CHECK_CLOSE( lr.getX( 4 ), 2.0 , 0.0000000001 );
650  BOOST_CHECK_CLOSE( lr.getY( 4 ), 0.0 , 0.0000000001 );
651 
652  BOOST_CHECK_CLOSE( lr.getX( 5 ), 1.0 , 0.0000000001 );
653  BOOST_CHECK_CLOSE( lr.getY( 5 ), 0.0 , 0.0000000001 );
654 
655  BOOST_CHECK_CLOSE( lr.getX( 6 ), 0.0 , 0.0000000001 );
656  BOOST_CHECK_CLOSE( lr.getY( 6 ), 0.0 , 0.0000000001 );
657 
658  BOOST_CHECK_CLOSE( lr.getX( 7 ), 0.0 , 0.0000000001 );
659  BOOST_CHECK_CLOSE( lr.getY( 7 ), 1.0 , 0.0000000001 );
660 
661  BOOST_CHECK_CLOSE( lr.getX( 8 ), 0.0 , 0.0000000001 );
662  BOOST_CHECK_CLOSE( lr.getY( 8 ), 2.0 , 0.0000000001 );
663 
664  BOOST_CHECK_CLOSE( grid.getExtent()->m_llx, 0.0 , 0.0000000001 );
665  BOOST_CHECK_CLOSE( grid.getExtent()->m_lly, 0.0 , 0.0000000001 );
666  BOOST_CHECK_CLOSE( grid.getExtent()->m_urx, 2.0 , 0.0000000001 );
667  BOOST_CHECK_CLOSE( grid.getExtent()->m_ury, 2.0 , 0.0000000001 );
668 }
669 
670 BOOST_AUTO_TEST_CASE(getIndexedDetailedExtent_test)
671 {
672  te::rst::Grid grid( 2u, 2u, new te::gm::Envelope( 0.0, 0.0, 2.0, 2.0 ), 12345 );
673 
675 
676  BOOST_CHECK( te::rp::GetIndexedDetailedExtent( grid, lr ) );
677  BOOST_CHECK( lr.size() == 9 );
678 
679  BOOST_CHECK_CLOSE( lr.getX( 0 ), -0.5 , 0.0000000001 );
680  BOOST_CHECK_CLOSE( lr.getY( 0 ), -0.5 , 0.0000000001 );
681 
682  BOOST_CHECK_CLOSE( lr.getX( 1 ), 0.5 , 0.0000000001 );
683  BOOST_CHECK_CLOSE( lr.getY( 1 ), -0.5 , 0.0000000001 );
684 
685  BOOST_CHECK_CLOSE( lr.getX( 2 ), 1.5 , 0.0000000001 );
686  BOOST_CHECK_CLOSE( lr.getY( 2 ), -0.5 , 0.0000000001 );
687 
688  BOOST_CHECK_CLOSE( lr.getX( 3 ), 1.5 , 0.0000000001 );
689  BOOST_CHECK_CLOSE( lr.getY( 3 ), 0.5 , 0.0000000001 );
690 
691  BOOST_CHECK_CLOSE( lr.getX( 4 ), 1.5 , 0.0000000001 );
692  BOOST_CHECK_CLOSE( lr.getY( 4 ), 1.5 , 0.0000000001 );
693 
694  BOOST_CHECK_CLOSE( lr.getX( 5 ), 0.5 , 0.0000000001 );
695  BOOST_CHECK_CLOSE( lr.getY( 5 ), 1.5 , 0.0000000001 );
696 
697  BOOST_CHECK_CLOSE( lr.getX( 6 ), -0.5 , 0.0000000001 );
698  BOOST_CHECK_CLOSE( lr.getY( 6 ), 1.5 , 0.0000000001 );
699 
700  BOOST_CHECK_CLOSE( lr.getX( 7 ), -0.5 , 0.0000000001 );
701  BOOST_CHECK_CLOSE( lr.getY( 7 ), 0.5 , 0.0000000001 );
702 
703  BOOST_CHECK_CLOSE( lr.getX( 8 ), -0.5 , 0.0000000001 );
704  BOOST_CHECK_CLOSE( lr.getY( 8 ), -0.5 , 0.0000000001 );
705 
706  BOOST_CHECK_CLOSE( grid.getExtent()->m_llx, 0.0 , 0.0000000001 );
707  BOOST_CHECK_CLOSE( grid.getExtent()->m_lly, 0.0 , 0.0000000001 );
708  BOOST_CHECK_CLOSE( grid.getExtent()->m_urx, 2.0 , 0.0000000001 );
709  BOOST_CHECK_CLOSE( grid.getExtent()->m_ury, 2.0 , 0.0000000001 );
710 }
711 
712 BOOST_AUTO_TEST_CASE(waveletAtrous_test)
713 {
714  const unsigned int waveletDecompLevelsNumb = 3;
715 
716  /* Openning input raster */
717 
718  std::map<std::string, std::string> auxRasterInfo;
719 
720  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
721  std::unique_ptr< te::rst::Raster > diskRasterPtr( te::rst::RasterFactory::open(
722  auxRasterInfo ) );
723  BOOST_CHECK( diskRasterPtr.get() );
724 
725  std::vector< te::rst::BandProperty * > waveletRasterBandProps;
726  for( unsigned int bandIdx = 0 ; bandIdx < diskRasterPtr->getNumberOfBands() ;
727  ++bandIdx )
728  {
729  for( unsigned int levelIdx = 0 ; levelIdx < waveletDecompLevelsNumb ;
730  ++levelIdx )
731  {
732  waveletRasterBandProps.push_back( new te::rst::BandProperty(
733  *( diskRasterPtr->getBand( 0 )->getProperty() ) ) );
734  waveletRasterBandProps.back()->m_blkh = diskRasterPtr->getNumberOfRows();
735  waveletRasterBandProps.back()->m_blkw = diskRasterPtr->getNumberOfColumns();
736  waveletRasterBandProps.back()->m_nblocksx = 1;
737  waveletRasterBandProps.back()->m_nblocksy = 1;
738  waveletRasterBandProps.back()->m_type = te::dt::DOUBLE_TYPE;
739 
740  waveletRasterBandProps.push_back( new te::rst::BandProperty(
741  *( diskRasterPtr->getBand( 0 )->getProperty() ) ) );
742  waveletRasterBandProps.back()->m_blkh = diskRasterPtr->getNumberOfRows();
743  waveletRasterBandProps.back()->m_blkw = diskRasterPtr->getNumberOfColumns();
744  waveletRasterBandProps.back()->m_nblocksx = 1;
745  waveletRasterBandProps.back()->m_nblocksy = 1;
746  waveletRasterBandProps.back()->m_type = te::dt::DOUBLE_TYPE;
747  }
748  }
749 
750  std::unique_ptr< te::rst::Raster > waveletRasterPtr( te::rst::RasterFactory::make(
751  "MEM", new te::rst::Grid( *( diskRasterPtr->getGrid() ) ), waveletRasterBandProps,
752  std::map<std::string, std::string>(), 0, 0 ) );
753  BOOST_CHECK( waveletRasterPtr.get() );
754 
755  std::vector< unsigned int > inputRasterBands;
756  inputRasterBands.push_back( 0 );
757  inputRasterBands.push_back( 1 );
758  inputRasterBands.push_back( 2 );
759 
760  boost::numeric::ublas::matrix< double > filter = te::rp::CreateWaveletAtrousFilter(
762 
763  BOOST_CHECK( te::rp::DirectWaveletAtrous(
764  *diskRasterPtr,
765  inputRasterBands,
766  *waveletRasterPtr,
767  waveletDecompLevelsNumb,
768  filter ) );
769 
770  BOOST_CHECK( te::rp::Copy2DiskRaster( *waveletRasterPtr, "terralib_unittest_rp_functions_DirectWaveletAtrous.tif" ) ) ;
771 
772  auxRasterInfo.clear();
773  auxRasterInfo["URI"] = "terralib_unittest_rp_functions_InverseWaveletAtrous.tif";
774 
775  std::vector< te::rst::BandProperty * > bandProps2;
776  bandProps2.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 0 )->getProperty() ) ) );
777  bandProps2.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 1 )->getProperty() ) ) );
778  bandProps2.push_back( new te::rst::BandProperty( *( diskRasterPtr->getBand( 2 )->getProperty() ) ) );
779 
780  std::unique_ptr< te::rst::Raster > outDiskRasterPtr( te::rst::RasterFactory::make(
781  "GDAL", new te::rst::Grid( *( diskRasterPtr->getGrid() ) ), bandProps2,
782  auxRasterInfo, 0, 0 ) );
783  BOOST_CHECK( outDiskRasterPtr.get() );
784 
785  BOOST_CHECK( te::rp::InverseWaveletAtrous(
786  *waveletRasterPtr,
787  waveletDecompLevelsNumb,
788  *outDiskRasterPtr,
789  inputRasterBands ) );
790 }
791 
792 BOOST_AUTO_TEST_SUITE_END()
unsigned int unsigned int std::unique_ptr< te::rst::Raster > & rasterPointer
bool GetMeanValue(const te::rst::Band &band, const unsigned int maxThreads, double &meanValue)
Get the mean of band pixel values.
unsigned int band
Near neighborhood interpolation method.
A feeder from a input rasters vector;.
Definition: FeedersRaster.h:69
A raster band description.
Definition: BandProperty.h:61
bool RasterSlicing(const te::rst::Raster &inputRaster, const unsigned int inputRasterBand, const bool createPaletteRaster, const unsigned int slicesNumber, const bool eqHistogram, const std::map< std::string, std::string > &rasterInfo, const std::string &rasterType, const bool enableProgress, std::vector< te::rst::BandProperty::ColorEntry > const *const palettePtr, std::unique_ptr< te::rst::Raster > &outRasterPtr, std::map< double, double > limits)
Generate all wavelet planes from the given input raster.
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.
double m_urx
Upper right corner x-coordinate.
Red channel color interpretation.
bool ComposeBands(te::rp::FeederConstRaster &feeder, const std::vector< unsigned int > &inputRasterBands, const te::rst::Interpolator::Method &interpMethod, const std::map< std::string, std::string > &outputRasterInfo, const std::string &outputDataSourceType, std::unique_ptr< te::rst::Raster > &outputRasterPtr)
Compose a set of bands into one multi-band raster.
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.
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.
const double & getY(std::size_t i) const
It returns the n-th y coordinate value.
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.
unsigned int line
bool ConvertHLS2RGB(const te::rst::Raster &inputHLSRaster, const unsigned int hueBandIdx, const unsigned int lightBandIdx, const unsigned int saturationBandIdx, const double, const double rgbRangeMax, te::rst::Raster &outputRGBRaster)
HLS to RGB conversion.
unsigned int unsigned int nCols
bool TERPEXPORT Copy2DiskRaster(const te::rst::Raster &inputRaster, const std::string &fileName)
Create a new raster into a GDAL datasource.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
double m_llx
Lower left corner x-coordinate.
An Envelope defines a 2D rectangular region.
const double & getX(std::size_t i) const
It returns the n-th x coordinate value.
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.
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.
This file contains include headers for the TerraLib Raster Processing module.
bool GetDetailedExtent(const te::rst::Grid &grid, te::gm::LinearRing &detailedExtent)
Create a datailed extent from the given grid.
bool ConvertRGB2HLS(const te::rst::Raster &inputRGBRaster, const unsigned int redBandIdx, const unsigned int greenBandIdx, const unsigned int blueBandIdx, const double, const double rgbRangeMax, te::rst::Raster &outputHLSRaster)
RGB to HLS conversion.
double m_lly
Lower left corner y-coordinate.
boost::numeric::ublas::matrix< double > CreateWaveletAtrousFilter(const WaveletAtrousFilterType &filterType)
Create a Wavele Atrous Filter.
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.
double m_ury
Upper right corner y-coordinate.
static Raster * make()
It creates and returns an empty raster with default raster driver.
bool GetIndexedDetailedExtent(const te::rst::Grid &grid, te::gm::LinearRing &indexedDetailedExtent)
Create a indexed (lines,columns) datailed extent from the given grid.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
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.
void CreateFixedStepPalette(const unsigned int paletteSize, const bool randomize, std::vector< te::rst::BandProperty::ColorEntry > &palette)
Create a fixed step sequential color palette.
BOOST_AUTO_TEST_SUITE(functions_tests) BOOST_AUTO_TEST_CASE(createFixedStepPalette_test)
Definition: TsFunctions.cpp:42
BOOST_AUTO_TEST_CASE(rasterSlicing_test)
Definition: TsFunctions.cpp:82
Blue channel color interpretation.
list rasters
Definition: compose.py:3
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
This file contains include headers for the Data Access module of TerraLib.
Green channel color interpretation.
unsigned int col
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.
std::size_t size() const
It returns the number of points (vertexes) in the geometry.
Definition: LineString.h:262
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.