TsRasterIterator.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/raster/TsRasterIterator.cpp
22 
23  \brief A test suit for the Raster iterator class.
24  */
25 
26 // TerraLib
27 #include <terralib_buildconfig.h>
28 
29 #include <terralib/raster.h>
30 #include <terralib/dataaccess.h>
31 #include <terralib/geometry.h>
32 #include <terralib/memory.h>
33 #include <terralib/rp.h>
34 #include "../Config.h"
35 
36 // STL
37 #include <memory>
38 
39 // Boost
40 #include <boost/test/unit_test.hpp>
41 #include <boost/shared_ptr.hpp>
42 
43 BOOST_AUTO_TEST_SUITE ( rasterIterator_tests )
44 
45 void drawPolygonInRaster( const te::gm::Polygon& polygon,
46  const unsigned char polValue,
47  const te::rst::Grid& rasterGrid, const std::string& rasterFileName )
48 {
49  std::unique_ptr< te::rst::Raster > rasterPointer;
50  {
51  std::vector< te::rst::BandProperty * > bandsProps;
52  bandsProps.push_back( new te::rst::BandProperty( 0,
54  bandsProps[ 0 ]->m_noDataValue = 255;
55 
56  std::map< std::string, std::string > rasterInfo;
57  rasterInfo[ "URI" ] = rasterFileName;
58 
59  rasterPointer.reset( te::rst::RasterFactory::make( "GDAL",
60  new te::rst::Grid( rasterGrid ), bandsProps,
61  rasterInfo, 0, 0 ) );
62 
63  const unsigned int nLines = rasterPointer->getNumberOfRows();
64  const unsigned nCols = rasterPointer->getNumberOfColumns();
65  unsigned int band = 0;
66  unsigned int line = 0;
67  unsigned int col = 0;
68 
69  for( line = 0 ; line < nLines ; ++line )
70  {
71  for( col = 0 ; col < nCols ; ++col )
72  {
73  rasterPointer->setValue( col, line, 0, band );
74  }
75  }
76  }
77 
80  &polygon);
82  te::rst::PolygonIterator< double >::end(rasterPointer.get(),
83  &polygon);
84 
85  int col = 0;
86  int row = 0;
87  while (it != itEnd)
88  {
89  rasterPointer->setValue(it.getColumn(), it.getRow(), (double)polValue, 0);
90  ++it;
91  }
92 }
93 
94 void CreateTestRaster( unsigned int nBands, unsigned int nLines,
95  unsigned int nCols, boost::shared_ptr< te::rst::Raster >& rasterPointer, bool zero )
96 {
97  std::vector< te::rst::BandProperty * > bandsProps;
98  for( unsigned int bandsPropsIdx = 0 ; bandsPropsIdx < nBands ; ++bandsPropsIdx )
99  {
100  bandsProps.push_back( new te::rst::BandProperty( bandsPropsIdx,
102  bandsProps[ bandsPropsIdx ]->m_noDataValue = 0;
103  }
104 
105  rasterPointer.reset( te::rst::RasterFactory::make( "MEM",
106  new te::rst::Grid( nCols, nLines ), bandsProps,
107  std::map< std::string, std::string >(), 0, 0 ) );
108 
109  unsigned int band = 0;
110  unsigned int line = 0;
111  unsigned int col = 0;
112  double pixelValue = 0;
113 
114  for( band = 0 ; band < nBands ; ++band )
115  for( line = 0 ; line < nLines ; ++line )
116  for( col = 0 ; col < nCols ; ++col )
117  {
118  rasterPointer->setValue( col, line, pixelValue, band );
119  if (!zero)
120  ++pixelValue;
121  }
122 }
123 
124 void Copy2DiskShp(std::vector<te::gm::Polygon*> polygons, std::string shpName)
125 {
126  std::unique_ptr< te::mem::DataSet> memDataSetPtr;
127 
128  std::unique_ptr<te::da::DataSetType> dataSetTypePtr(new te::da::DataSetType(shpName));
129 
130  te::gm::GeometryProperty* propPtr = new te::gm::GeometryProperty("polygon", 0,
131  te::gm::PolygonType, true);
132  propPtr->setSRID(polygons[0]->getSRID());
133  dataSetTypePtr->add(propPtr);
134 
135  memDataSetPtr.reset(new te::mem::DataSet(dataSetTypePtr.get()));
136 
137  for (unsigned int polygonsIdx = 0; polygonsIdx < polygons.size(); polygonsIdx++)
138  {
139  te::mem::DataSetItem* dsItemPtr = new te::mem::DataSetItem(memDataSetPtr.get());
140  dsItemPtr->setGeometry(0, (te::gm::Geometry*)polygons[polygonsIdx]->clone());
141 
142  memDataSetPtr->add(dsItemPtr);
143  }
144 
145  std::string connInfo("file://" + shpName);
146 
147  std::unique_ptr<te::da::DataSource> dsOGR(te::da::DataSourceFactory::make("OGR", connInfo));
148  dsOGR->open();
149 
150  memDataSetPtr->moveBeforeFirst();
151 
152  te::da::Create(dsOGR.get(), dataSetTypePtr.get(), memDataSetPtr.get());
153 
154  dsOGR->close();
155 }
156 
157 bool Copy2DiskRaster( const te::rst::Raster& inputRaster,
158  const std::string& fileName )
159 {
160  if( !(inputRaster.getAccessPolicy() & te::common::RAccess ) )
161  {
162  return false;
163  };
164 
165  const unsigned int nBands = inputRaster.getNumberOfBands();
166  const unsigned int nCols = inputRaster.getNumberOfColumns();
167  const unsigned int nRows = inputRaster.getNumberOfRows();
168  unsigned int bandIdx =0;
169 
170  std::vector< te::rst::BandProperty* > bandsProperties;
171  for( bandIdx = 0 ; bandIdx < nBands ; ++bandIdx )
172  {
173  bandsProperties.push_back( new te::rst::BandProperty(
174  *( inputRaster.getBand( bandIdx )->getProperty() ) ) );
175  }
176 
177  std::unique_ptr< te::rst::Raster > outRasterPtr;
178 
179  std::map< std::string, std::string > rInfo;
180  rInfo[ "URI" ] = fileName;
181 
182  outRasterPtr.reset( te::rst::RasterFactory::make(
183  "GDAL",
184  new te::rst::Grid( *inputRaster.getGrid() ),
185  bandsProperties,
186  rInfo,
187  0,
188  0 ) );
189 
190  double value = 0;
191  unsigned int col = 0;
192  unsigned int row = 0;
193 
194  for( bandIdx = 0 ; bandIdx < nBands ; ++bandIdx )
195  {
196  const te::rst::Band& inBand = *inputRaster.getBand( bandIdx );
197  te::rst::Band& outBand = *outRasterPtr->getBand( bandIdx );
198 
199  for( row = 0 ; row < nRows ; ++row )
200  {
201  for( col = 0 ; col < nCols ; ++col )
202  {
203  inBand.getValue( col, row, value );
204  outBand.setValue( col, row, value );
205  }
206  }
207  }
208 
209  return true;
210 }
211 
212 te::gm::LinearRing* createSquare(const double& xCenter, const double& yCenter,
213  const double& lateralSize, int srid)
214 {
216  double halfSize = lateralSize * 0.5;
217  s->setPoint(0, xCenter - halfSize, yCenter - halfSize); // lower left
218  s->setPoint(1, xCenter - halfSize, yCenter + halfSize); // upper left
219  s->setPoint(2, xCenter + halfSize, yCenter + halfSize); // upper rigth
220  s->setPoint(3, xCenter + halfSize, yCenter - halfSize); // lower rigth
221  s->setPoint(4, xCenter - halfSize, yCenter - halfSize); // closing
222  return s;
223 }
224 
225 te::gm::LinearRing* createRotatedSquare(const double& xCenter, const double& yCenter,
226  const double& diagonalSize, int srid)
227 {
229  double halfSize = diagonalSize * 0.5;
230  s->setPoint(0, xCenter - halfSize, yCenter);
231  s->setPoint(1, xCenter, yCenter + halfSize);
232  s->setPoint(2, xCenter + halfSize, yCenter);
233  s->setPoint(3, xCenter, yCenter - halfSize);
234  s->setPoint(4, xCenter - halfSize, yCenter);
235  return s;
236 }
237 
238 BOOST_AUTO_TEST_CASE (rasterIteratorConstructor1_test)
239 {
240 }
241 
242 BOOST_AUTO_TEST_CASE (rasterIteratorConstructor2_test)
243 {
244 }
245 
246 BOOST_AUTO_TEST_CASE (rasterIteratorConstructor3_test)
247 {
248 }
249 
250 BOOST_AUTO_TEST_CASE (rasterIteratorGetRow_test)
251 {
252 }
253 
254 BOOST_AUTO_TEST_CASE (rasterIteratorGetCol_test)
255 {
256 }
257 
258 BOOST_AUTO_TEST_CASE (polygonIterator1_test)
259 {
260  boost::shared_ptr< te::rst::Raster > rasterPointer;
261  CreateTestRaster( 3, 3, 3, rasterPointer, false );
262  Copy2DiskRaster( *rasterPointer, "polygonIterator1_test.tif" );
263 
264  std::unique_ptr< te::gm::Polygon > polPtr( (te::gm::Polygon*)
265  te::gm::GetGeomFromEnvelope( rasterPointer->getGrid()->getExtent(),
266  rasterPointer->getGrid()->getSRID() ) );
267 
268  double pixelValueCounter = 0;
269 
270  for( unsigned int band = 0 ; band < rasterPointer->getNumberOfBands() ;
271  ++band )
272  {
274  te::rst::PolygonIterator< double >::begin( rasterPointer.get(),
275  polPtr.get() );
277  te::rst::PolygonIterator< double >::end( rasterPointer.get(),
278  polPtr.get() );
279 
280  // Testing AbstractPositionIterator interface
282  dynamic_cast< te::rst::AbstractPositionIterator< double >* >( &it );
284  dynamic_cast< te::rst::AbstractPositionIterator< double > const* >( &itEnd );
285 
286  int col = 0;
287  int row = 0;
288  int count = 0;
289  while ( (*itPtr) != (*itEndPtr) )
290  {
291  BOOST_CHECK_EQUAL((double)pixelValueCounter, itPtr->operator[]( band ) );
292  BOOST_CHECK_EQUAL(row, (int)itPtr->getRow());
293  BOOST_CHECK_EQUAL(col, (int)itPtr->getColumn());
294 
295  ++pixelValueCounter;
296  itPtr->operator++();
297  count++;
298 
299  if (col < rasterPointer->getNumberOfColumns() - 1)
300  col++;
301  else {
302  row++;
303  col = 0;
304  }
305  }
306 
307  BOOST_CHECK_EQUAL(count, 9);
308  }
309 }
310 
311 BOOST_AUTO_TEST_CASE (polygonIterator2_test)
312 {
313  boost::shared_ptr< te::rst::Raster > rasterPointer;
314  {
315  const unsigned int nLines = 30;
316  const unsigned int nCols = 30;
317 
318  std::vector< te::rst::BandProperty * > bandsProps;
319  bandsProps.push_back( new te::rst::BandProperty( 0,
320  te::dt::UCHAR_TYPE ) );
321  bandsProps[ 0 ]->m_noDataValue = 0;
322 
323  const te::gm::Coord2D gridULCoord( 0, 0 );
324 
325  te::rst::Grid* gridPtr = new te::rst::Grid( nCols, nLines, 1, 1, &gridULCoord, 4326 );
326 
327  rasterPointer.reset( te::rst::RasterFactory::make( "MEM",
328  gridPtr, bandsProps,
329  std::map< std::string, std::string >(), 0, 0 ) );
330 
331  unsigned int band = 0;
332  unsigned int line = 0;
333  unsigned int col = 0;
334 
335  for( line = 0 ; line < nLines ; ++line )
336  {
337  for( col = 0 ; col < nCols ; ++col )
338  {
339  rasterPointer->setValue( col, line, 0, band );
340  }
341  }
342  }
343 
344  std::unique_ptr< te::gm::Polygon > internalPol_1( new te::gm::Polygon(0, te::gm::PolygonType,
345  rasterPointer->getSRID()) );
346  te::gm::LinearRing* l1 = createSquare(2, -2, 1, rasterPointer->getSRID());
347  internalPol_1->add(l1);
348 
349  std::unique_ptr< te::gm::Polygon > internalPol_2( new te::gm::Polygon(0, te::gm::PolygonType,
350  rasterPointer->getSRID()) );
351  te::gm::LinearRing* l2 = createSquare(3, -5.5, 3, rasterPointer->getSRID());
352  internalPol_2->add(l2);
353 
354  std::unique_ptr< te::gm::Polygon > internalPol_3( new te::gm::Polygon(0, te::gm::PolygonType,
355  rasterPointer->getSRID()) );
356  te::gm::LinearRing* l3 = createSquare(7, -6, 2.5, rasterPointer->getSRID());
357  internalPol_3->add(l3);
358 
359  std::unique_ptr< te::gm::Polygon > internalPol_4( new te::gm::Polygon(0, te::gm::PolygonType,
360  rasterPointer->getSRID()) );
361  te::gm::LinearRing* l4 = new te::gm::LinearRing(6, te::gm::LineStringType, rasterPointer->getSRID());
362  l4->setPoint(0, 4.5, -2.5); // starting
363  l4->setPoint(1, 6.5, -3.25);
364  l4->setPoint(2, 7.5, -2.75);
365  l4->setPoint(3, 7.5, -1.5);
366  l4->setPoint(4, 5.5, -1.5);
367  l4->setPoint(5, 4.5, -2.5); // closing
368  internalPol_4->add(l4);
369 
370  std::unique_ptr< te::gm::Polygon > internalPol_5( new te::gm::Polygon(0, te::gm::PolygonType,
371  rasterPointer->getSRID()) );
372  te::gm::LinearRing* l5 = new te::gm::LinearRing(13, te::gm::LineStringType, rasterPointer->getSRID());
373  l5->setPoint(0, 9, -1);
374  l5->setPoint(1, 10, -1);
375  l5->setPoint(2, 10, -2);
376  l5->setPoint(3, 12, -2);
377  l5->setPoint(4, 12, -1);
378  l5->setPoint(5, 16, -1);
379  l5->setPoint(6, 16, -3);
380  l5->setPoint(7, 15, -3);
381  l5->setPoint(8, 15, -2);
382  l5->setPoint(9, 13, -2);
383  l5->setPoint(10, 13, -3);
384  l5->setPoint(11, 9, -3);
385  l5->setPoint(12, 9, -1);
386  internalPol_5->add(l5);
387 
388  std::unique_ptr< te::gm::Polygon > internalPol_6( new te::gm::Polygon(0, te::gm::PolygonType,
389  rasterPointer->getSRID()) );
390  te::gm::LinearRing* l6 = new te::gm::LinearRing(5, te::gm::LineStringType, rasterPointer->getSRID());
391  l6->setPoint(0, 9, -5);
392  l6->setPoint(1, 10, -5);
393  l6->setPoint(2, 10, -6);
394  l6->setPoint(3, 9, -6);
395  l6->setPoint(4, 9, -5);
396  internalPol_6->add(l6);
397 
398  std::unique_ptr< te::gm::Polygon > internalPol_7( new te::gm::Polygon(0, te::gm::PolygonType,
399  rasterPointer->getSRID()) );
400  te::gm::LinearRing* l7 = createSquare(14, -6.5, 4, rasterPointer->getSRID());
401  internalPol_7->add( l7 );
402 
403  std::unique_ptr< te::gm::Polygon > internalPol_8( new te::gm::Polygon(0, te::gm::PolygonType,
404  rasterPointer->getSRID()) );
405  te::gm::LinearRing* l8 = createRotatedSquare(4, -11, 4, rasterPointer->getSRID());
406  internalPol_8->add( l8 );
407 
408  std::unique_ptr< te::gm::Polygon > internalPol_9( new te::gm::Polygon(0, te::gm::PolygonType,
409  rasterPointer->getSRID()) );
410  te::gm::LinearRing* l9 = createRotatedSquare(9.5, -11.5, 4, rasterPointer->getSRID());
411  internalPol_9->add( l9 );
412 
413  std::unique_ptr< te::gm::Polygon > internalPol_10( new te::gm::Polygon(0, te::gm::PolygonType,
414  rasterPointer->getSRID()) );
415  te::gm::LinearRing* l10 = createRotatedSquare(4, -17, 5, rasterPointer->getSRID());
416  internalPol_10->add( l10 );
417 
418  std::unique_ptr< te::gm::Polygon > internalPol_11( new te::gm::Polygon(0, te::gm::PolygonType,
419  rasterPointer->getSRID()) );
420  te::gm::LinearRing* l11 = createRotatedSquare(11.5, -17.5, 5, rasterPointer->getSRID());
421  internalPol_11->add( l11 );
422 
423  std::unique_ptr< te::gm::Polygon > externalPol( (te::gm::Polygon*) te::gm::GetGeomFromEnvelope(
424  rasterPointer->getGrid()->getExtent(), rasterPointer->getGrid()->getSRID()) );
425  externalPol->add((te::gm::LinearRing*)l1->clone());
426  externalPol->add((te::gm::LinearRing*)l2->clone());
427  externalPol->add((te::gm::LinearRing*)l3->clone());
428  externalPol->add((te::gm::LinearRing*)l4->clone());
429  externalPol->add((te::gm::LinearRing*)l5->clone());
430  externalPol->add((te::gm::LinearRing*)l6->clone());
431  externalPol->add((te::gm::LinearRing*)l7->clone());
432  externalPol->add((te::gm::LinearRing*)l8->clone());
433  externalPol->add((te::gm::LinearRing*)l9->clone());
434  externalPol->add((te::gm::LinearRing*)l10->clone());
435  externalPol->add((te::gm::LinearRing*)l11->clone());
436 
437  // Polygons vector
438 
439  std::vector<te::gm::Polygon* > polVector;
440  polVector.push_back(internalPol_1.get());
441  polVector.push_back(internalPol_2.get());
442  polVector.push_back(internalPol_3.get());
443  polVector.push_back(internalPol_4.get());
444  polVector.push_back(internalPol_5.get());
445  polVector.push_back(internalPol_6.get());
446  polVector.push_back(internalPol_7.get());
447  polVector.push_back(internalPol_8.get());
448  polVector.push_back(internalPol_9.get());
449  polVector.push_back(internalPol_10.get());
450  polVector.push_back(internalPol_11.get());
451  polVector.push_back(externalPol.get());
452 
453  Copy2DiskShp( polVector, "polygonIterator2_test.shp" );
454 
455  double pixelValueCounter = 0;
456 
457  for (int countPol = 0; countPol < polVector.size(); countPol++)
458  {
460  te::rst::PolygonIterator< double >::begin(rasterPointer.get(),
461  polVector[countPol]);
463  te::rst::PolygonIterator< double >::end(rasterPointer.get(),
464  polVector[countPol]);
465 
466  int col = 0;
467  int row = 0;
468  while (it != itEnd)
469  {
470  if (it[0] == 0)
471  rasterPointer->setValue(it.getColumn(), it.getRow(), (countPol + 1), 0);
472  else
473  rasterPointer->setValue(it.getColumn(), it.getRow(), 15, 0);
474 
475  ++it;
476  }
477  }
478 
479  Copy2DiskRaster( *rasterPointer, "polygonIterator2_test.tif" );
480 }
481 
482 BOOST_AUTO_TEST_CASE (polygonIterator3_outsidepol_test)
483 {
484  const te::gm::Coord2D gridULCoord( 0, 0 );
485  te::rst::Grid rasterGrid( 3, 3 , 1, 1, &gridULCoord, 4326);
486  std::vector<te::gm::Polygon*> polVector( 1 );
487 
488  {
489  std::unique_ptr< te::gm::Polygon > polygonPtr( new te::gm::Polygon(0, te::gm::PolygonType,
490  0) );
491  te::gm::LinearRing* linearRingPtr = createSquare(0, 0, 2.0, 0);
492  polygonPtr->add(linearRingPtr);
493  polygonPtr->setSRID( rasterGrid.getSRID() );
494  polVector[ 0 ] = polygonPtr.get();
495  Copy2DiskShp( polVector, "polygonIterator3_outsidepol_test1.shp" );
496  drawPolygonInRaster( *polygonPtr, 1, rasterGrid,
497  "polygonIterator3_outsidepol_test1.tif" );
498  }
499 
500  {
501  std::unique_ptr< te::gm::Polygon > polygonPtr( new te::gm::Polygon(0, te::gm::PolygonType,
502  0) );
503  te::gm::LinearRing* linearRingPtr = createSquare(
504  rasterGrid.getExtent()->m_urx,
505  rasterGrid.getExtent()->m_lly,
506  2.0, 0);
507  polygonPtr->add(linearRingPtr);
508  polygonPtr->setSRID( rasterGrid.getSRID() );
509  polVector[ 0 ] = polygonPtr.get();
510  Copy2DiskShp( polVector, "polygonIterator3_outsidepol_test2.shp" );
511  drawPolygonInRaster( *polygonPtr, 1, rasterGrid,
512  "polygonIterator3_outsidepol_test2.tif" );
513  }
514 
515  {
516  std::unique_ptr< te::gm::Polygon > polygonPtr( new te::gm::Polygon(0, te::gm::PolygonType,
517  0) );
518  te::gm::LinearRing* linearRingPtr = createSquare(
519  rasterGrid.getExtent()->getCenter().x,
520  rasterGrid.getExtent()->getCenter().y,
521  4.0, 0);
522  polygonPtr->add(linearRingPtr);
523  polygonPtr->setSRID( rasterGrid.getSRID() );
524  polVector[ 0 ] = polygonPtr.get();
525  Copy2DiskShp( polVector, "polygonIterator3_outsidepol_test3.shp" );
526  drawPolygonInRaster( *polygonPtr, 1, rasterGrid,
527  "polygonIterator3_outsidepol_test3.tif" );
528  }
529 
530  {
531  std::unique_ptr< te::gm::Polygon > polygonPtr( new te::gm::Polygon(0, te::gm::PolygonType,
532  0) );
533  te::gm::LinearRing* linearRingPtr = createSquare(
534  rasterGrid.getExtent()->m_urx +
535  rasterGrid.getResolutionX(),
536  rasterGrid.getExtent()->m_ury +
537  rasterGrid.getResolutionY(),
538  2.0, 0);
539  polygonPtr->add(linearRingPtr);
540  polygonPtr->setSRID( rasterGrid.getSRID() );
541  polVector[ 0 ] = polygonPtr.get();
542  Copy2DiskShp( polVector, "polygonIterator3_outsidepol_test4.shp" );
543  drawPolygonInRaster( *polygonPtr, 1, rasterGrid,
544  "polygonIterator3_outsidepol_test4.tif" );
545  }
546 
547  {
548  std::unique_ptr< te::gm::Polygon > polygonPtr( new te::gm::Polygon(0, te::gm::PolygonType,
549  0) );
550  te::gm::LinearRing* linearRingPtr = createSquare(
551  rasterGrid.getExtent()->m_urx +
552  ( 2 * rasterGrid.getResolutionX() ),
553  rasterGrid.getExtent()->getCenter().y,
554  2.0, 0);
555  polygonPtr->add(linearRingPtr);
556  polygonPtr->setSRID( rasterGrid.getSRID() );
557  polVector[ 0 ] = polygonPtr.get();
558  Copy2DiskShp( polVector, "polygonIterator3_outsidepol_test5.shp" );
559  drawPolygonInRaster( *polygonPtr, 1, rasterGrid,
560  "polygonIterator3_outsidepol_test5.tif" );
561  }
562 
563  {
564  std::unique_ptr< te::gm::Polygon > polygonPtr( new te::gm::Polygon(0, te::gm::PolygonType,
565  0) );
567  linearRingPtr->setPoint(0, -1, 1);
568  linearRingPtr->setPoint(1, 0, 1);
569  linearRingPtr->setPoint(2, 0, -3);
570  linearRingPtr->setPoint(3, 3, -3);
571  linearRingPtr->setPoint(4, 3, 1);
572  linearRingPtr->setPoint(5, 4, 1);
573  linearRingPtr->setPoint(6, 4, -4);
574  linearRingPtr->setPoint(7, -1, -4);
575  linearRingPtr->setPoint(8, -1, 1);
576  polygonPtr->add(linearRingPtr);
577  polygonPtr->setSRID( rasterGrid.getSRID() );
578  polVector[ 0 ] = polygonPtr.get();
579  Copy2DiskShp( polVector, "polygonIterator3_outsidepol_test6.shp" );
580  drawPolygonInRaster( *polygonPtr, 1, rasterGrid,
581  "polygonIterator3_outsidepol_test6.tif" );
582  }
583 }
584 
585 BOOST_AUTO_TEST_CASE (polygonIterator_NDisjointBBOXIterationT_test1)
586 {
587  boost::shared_ptr< te::rst::Raster > rasterPointer;
588  CreateTestRaster( 3, 3, 3, rasterPointer, false );
589  Copy2DiskRaster( *rasterPointer, "polygonIterator_NDisjointBBOXIterationT_test1.tif" );
590 
591  std::unique_ptr< te::gm::Polygon > polPtr( (te::gm::Polygon*)
592  te::gm::GetGeomFromEnvelope( rasterPointer->getGrid()->getExtent(),
593  rasterPointer->getGrid()->getSRID() ) );
594 
595  double pixelValueCounter = 0;
596 
597  for( unsigned int band = 0 ; band < rasterPointer->getNumberOfBands() ;
598  ++band )
599  {
601  te::rst::PolygonIterator< double >::begin( rasterPointer.get(),
604  te::rst::PolygonIterator< double >::end( rasterPointer.get(),
606 
607  int col = 0;
608  int row = 0;
609  int count = 0;
610  while ( it != itEnd )
611  {
612  BOOST_CHECK_EQUAL((double)pixelValueCounter, it( band ) );
613  BOOST_CHECK_EQUAL(row, (int)it.getRow());
614  BOOST_CHECK_EQUAL(col, (int)it.getColumn());
615 
616  ++pixelValueCounter;
617  ++it;
618  count++;
619 
620  if (col < rasterPointer->getNumberOfColumns() - 1)
621  col++;
622  else {
623  row++;
624  col = 0;
625  }
626  }
627 
628  BOOST_CHECK_EQUAL(count, 9);
629  }
630 }
631 
632 BOOST_AUTO_TEST_CASE (polygonIterator_NDisjointBBOXIterationT_test2)
633 {
634  boost::shared_ptr< te::rst::Raster > rasterPointer;
635  {
636  const unsigned int nLines = 30;
637  const unsigned int nCols = 30;
638 
639  std::vector< te::rst::BandProperty * > bandsProps;
640  bandsProps.push_back( new te::rst::BandProperty( 0,
641  te::dt::UCHAR_TYPE ) );
642  bandsProps[ 0 ]->m_noDataValue = 0;
643 
644  const te::gm::Coord2D gridULCoord( 0, 0 );
645 
646  te::rst::Grid* gridPtr = new te::rst::Grid( nCols, nLines, 1, 1, &gridULCoord, 4326 );
647 
648  rasterPointer.reset( te::rst::RasterFactory::make( "MEM",
649  gridPtr, bandsProps,
650  std::map< std::string, std::string >(), 0, 0 ) );
651 
652  unsigned int band = 0;
653  unsigned int line = 0;
654  unsigned int col = 0;
655 
656  for( line = 0 ; line < nLines ; ++line )
657  {
658  for( col = 0 ; col < nCols ; ++col )
659  {
660  rasterPointer->setValue( col, line, 0, band );
661  }
662  }
663  }
664 
665  std::unique_ptr< te::gm::Polygon > internalPol_1( new te::gm::Polygon(0, te::gm::PolygonType,
666  rasterPointer->getSRID()) );
667  te::gm::LinearRing* l1 = createSquare(2, -2, 1, rasterPointer->getSRID());
668  internalPol_1->add(l1);
669 
670  std::unique_ptr< te::gm::Polygon > internalPol_2( new te::gm::Polygon(0, te::gm::PolygonType,
671  rasterPointer->getSRID()) );
672  te::gm::LinearRing* l2 = createSquare(3, -5.5, 3, rasterPointer->getSRID());
673  internalPol_2->add(l2);
674 
675  std::unique_ptr< te::gm::Polygon > internalPol_3( new te::gm::Polygon(0, te::gm::PolygonType,
676  rasterPointer->getSRID()) );
677  te::gm::LinearRing* l3 = createSquare(7, -6, 2.5, rasterPointer->getSRID());
678  internalPol_3->add(l3);
679 
680  std::unique_ptr< te::gm::Polygon > internalPol_4( new te::gm::Polygon(0, te::gm::PolygonType,
681  rasterPointer->getSRID()) );
682  te::gm::LinearRing* l4 = new te::gm::LinearRing(6, te::gm::LineStringType, rasterPointer->getSRID());
683  l4->setPoint(0, 4.5, -2.5); // starting
684  l4->setPoint(1, 6.5, -3.25);
685  l4->setPoint(2, 7.5, -2.75);
686  l4->setPoint(3, 7.5, -1.5);
687  l4->setPoint(4, 5.5, -1.5);
688  l4->setPoint(5, 4.5, -2.5); // closing
689  internalPol_4->add(l4);
690 
691  std::unique_ptr< te::gm::Polygon > internalPol_5( new te::gm::Polygon(0, te::gm::PolygonType,
692  rasterPointer->getSRID()) );
693  te::gm::LinearRing* l5 = new te::gm::LinearRing(13, te::gm::LineStringType, rasterPointer->getSRID());
694  l5->setPoint(0, 9, -1);
695  l5->setPoint(1, 10, -1);
696  l5->setPoint(2, 10, -2);
697  l5->setPoint(3, 12, -2);
698  l5->setPoint(4, 12, -1);
699  l5->setPoint(5, 16, -1);
700  l5->setPoint(6, 16, -3);
701  l5->setPoint(7, 15, -3);
702  l5->setPoint(8, 15, -2);
703  l5->setPoint(9, 13, -2);
704  l5->setPoint(10, 13, -3);
705  l5->setPoint(11, 9, -3);
706  l5->setPoint(12, 9, -1);
707  internalPol_5->add(l5);
708 
709  std::unique_ptr< te::gm::Polygon > internalPol_6( new te::gm::Polygon(0, te::gm::PolygonType,
710  rasterPointer->getSRID()) );
711  te::gm::LinearRing* l6 = new te::gm::LinearRing(5, te::gm::LineStringType, rasterPointer->getSRID());
712  l6->setPoint(0, 9, -5);
713  l6->setPoint(1, 10, -5);
714  l6->setPoint(2, 10, -6);
715  l6->setPoint(3, 9, -6);
716  l6->setPoint(4, 9, -5);
717  internalPol_6->add(l6);
718 
719  std::unique_ptr< te::gm::Polygon > internalPol_7( new te::gm::Polygon(0, te::gm::PolygonType,
720  rasterPointer->getSRID()) );
721  te::gm::LinearRing* l7 = createSquare(14, -6.5, 4, rasterPointer->getSRID());
722  internalPol_7->add( l7 );
723 
724  std::unique_ptr< te::gm::Polygon > internalPol_8( new te::gm::Polygon(0, te::gm::PolygonType,
725  rasterPointer->getSRID()) );
726  te::gm::LinearRing* l8 = createRotatedSquare(4, -11, 4, rasterPointer->getSRID());
727  internalPol_8->add( l8 );
728 
729  std::unique_ptr< te::gm::Polygon > internalPol_9( new te::gm::Polygon(0, te::gm::PolygonType,
730  rasterPointer->getSRID()) );
731  te::gm::LinearRing* l9 = createRotatedSquare(9.5, -11.5, 4, rasterPointer->getSRID());
732  internalPol_9->add( l9 );
733 
734  std::unique_ptr< te::gm::Polygon > internalPol_10( new te::gm::Polygon(0, te::gm::PolygonType,
735  rasterPointer->getSRID()) );
736  te::gm::LinearRing* l10 = createRotatedSquare(4, -17, 5, rasterPointer->getSRID());
737  internalPol_10->add( l10 );
738 
739  std::unique_ptr< te::gm::Polygon > internalPol_11( new te::gm::Polygon(0, te::gm::PolygonType,
740  rasterPointer->getSRID()) );
741  te::gm::LinearRing* l11 = createRotatedSquare(11.5, -17.5, 5, rasterPointer->getSRID());
742  internalPol_11->add( l11 );
743 
744  std::unique_ptr< te::gm::Polygon > externalPol( (te::gm::Polygon*) te::gm::GetGeomFromEnvelope(
745  rasterPointer->getGrid()->getExtent(), rasterPointer->getGrid()->getSRID()) );
746  externalPol->add((te::gm::LinearRing*)l1->clone());
747  externalPol->add((te::gm::LinearRing*)l2->clone());
748  externalPol->add((te::gm::LinearRing*)l3->clone());
749  externalPol->add((te::gm::LinearRing*)l4->clone());
750  externalPol->add((te::gm::LinearRing*)l5->clone());
751  externalPol->add((te::gm::LinearRing*)l6->clone());
752  externalPol->add((te::gm::LinearRing*)l7->clone());
753  externalPol->add((te::gm::LinearRing*)l8->clone());
754  externalPol->add((te::gm::LinearRing*)l9->clone());
755  externalPol->add((te::gm::LinearRing*)l10->clone());
756  externalPol->add((te::gm::LinearRing*)l11->clone());
757 
758  // Polygons vector
759 
760  std::vector<te::gm::Polygon* > polVector;
761  polVector.push_back(internalPol_1.get());
762  polVector.push_back(internalPol_2.get());
763  polVector.push_back(internalPol_3.get());
764  polVector.push_back(internalPol_4.get());
765  polVector.push_back(internalPol_5.get());
766  polVector.push_back(internalPol_6.get());
767  polVector.push_back(internalPol_7.get());
768  polVector.push_back(internalPol_8.get());
769  polVector.push_back(internalPol_9.get());
770  polVector.push_back(internalPol_10.get());
771  polVector.push_back(internalPol_11.get());
772  polVector.push_back(externalPol.get());
773 
774  Copy2DiskShp( polVector, "polygonIterator_NDisjointBBOXIterationT_test2.shp" );
775 
776  double pixelValueCounter = 0;
777 
778  for (int countPol = 0; countPol < polVector.size(); countPol++)
779  {
781  te::rst::PolygonIterator< double >::begin(rasterPointer.get(),
784  te::rst::PolygonIterator< double >::end(rasterPointer.get(),
786 
787  int col = 0;
788  int row = 0;
789  while (it != itEnd)
790  {
791  if (it[0] == 0)
792  rasterPointer->setValue(it.getColumn(), it.getRow(), (countPol + 1), 0);
793  else
794  rasterPointer->setValue(it.getColumn(), it.getRow(), 15, 0);
795 
796  ++it;
797  }
798  }
799 
800  Copy2DiskRaster( *rasterPointer, "polygonIterator_NDisjointBBOXIterationT_test2.tif" );
801 }
802 
803 BOOST_AUTO_TEST_SUITE_END ()
unsigned int unsigned int std::unique_ptr< te::rst::Raster > & rasterPointer
unsigned int band
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
This file contains include headers for the memory data source of TerraLib.
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
double y
y-coordinate.
Definition: Coord2D.h:114
te::gm::LinearRing * createSquare(const double &xCenter, const double &yCenter, const double &lateralSize, int srid)
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.
A class that models the description of a dataset.
Definition: DataSetType.h:72
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
te::gm::LineString * l3
double m_urx
Upper right corner x-coordinate.
double pixelValue
This class is the base for implementing ways to navigate over the band with spatial restriction...
unsigned int getRow() const
Returns the current row in iterator.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
void CreateTestRaster(unsigned int nBands, unsigned int nLines, unsigned int nCols, boost::shared_ptr< te::rst::Raster > &rasterPointer, bool zero)
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
unsigned int line
te::dt::AbstractData * clone() const
It clones the linear ring.
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
unsigned int unsigned int nCols
bool Copy2DiskRaster(const te::rst::Raster &inputRaster, const std::string &fileName)
Coord2D getCenter() const
It returns the rectangle&#39;s center coordinate.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
void Copy2DiskShp(std::vector< te::gm::Polygon * > polygons, std::string shpName)
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An abstract class for raster data strucutures.
TEDATAACCESSEXPORT void Create(DataSource *ds, DataSetType *dt, DataSet *d, std::size_t limit=0)
It creates the dataset definition in a data source and then fill it with data from the input dataset...
unsigned int getNumberOfRows() const
Returns the raster number of rows.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
BandProperty * getProperty()
Returns the band property.
URI C++ Library.
Definition: Attributes.h:37
double getResolutionX() const
Returns the grid horizontal (x-axis) resolution.
This file contains include headers for the TerraLib Raster Processing module.
te::gm::LineString * l2
A raster band description.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
double m_lly
Lower left corner y-coordinate.
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p, const IterationType iterationType)
Returns an iterator referring to the first value of the band.
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
double m_ury
Upper right corner y-coordinate.
static Raster * make()
It creates and returns an empty raster with default raster driver.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
void setSRID(int srid)
It sets the Spatial Reference System ID of the linestring.
te::gm::LineString * l1
BOOST_AUTO_TEST_SUITE(rasterIterator_tests) void drawPolygonInRaster(const te
This file contains include headers for the Vector Geometry model of TerraLib.
te::gm::LinearRing * createRotatedSquare(const double &xCenter, const double &yCenter, const double &diagonalSize, int srid)
unsigned int getColumn() const
Returns the current column in iterator.
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.
unsigned int nLines
unsigned int col
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
BOOST_AUTO_TEST_CASE(rasterIteratorConstructor1_test)