TsSynchronizedRaster.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 TsSynchronizedRaster.cpp
22 
23  \brief A test suit for the SynchronizedRaster Class.
24  */
25 
26 // TerraLib
27 #include <terralib_buildconfig.h>
28 
29 #include <terralib/raster.h>
30 #include "../Config.h"
31 
32 // STL
33 #include <memory>
34 
35 // Boost
36 #include <boost/test/unit_test.hpp>
37 #include <boost/shared_ptr.hpp>
38 #include <boost/thread.hpp>
39 
40 BOOST_AUTO_TEST_SUITE ( synchronizedRaster_tests )
41 
42 void CreateTestRaster( unsigned int nBands, unsigned int nLines,
43  unsigned int nCols, boost::shared_ptr< te::rst::Raster >& rasterPointer )
44 {
45  std::vector< te::rst::BandProperty * > bandsProps;
46  for( unsigned int bandsPropsIdx = 0 ; bandsPropsIdx < nBands ; ++bandsPropsIdx )
47  {
48  bandsProps.push_back( new te::rst::BandProperty( bandsPropsIdx,
50  }
51 
52  rasterPointer.reset( te::rst::RasterFactory::make( "MEM",
53  new te::rst::Grid( nCols, nLines ), bandsProps,
54  std::map< std::string, std::string >(), 0, 0 ) );
55 
56  unsigned int band = 0;
57  unsigned int line = 0;
58  unsigned int col = 0;
59  double pixelValue = 0;
60 
61  for( band = 0 ; band < nBands ; ++band )
62  for( line = 0 ; line < nLines ; ++line )
63  for( col = 0 ; col < nCols ; ++col )
64  {
65  rasterPointer->setValue( col, line, pixelValue, band );
66  ++pixelValue;
67  }
68 }
69 
71 {
72  std::unique_ptr< te::rst::SynchronizedRaster > syncRasterPtr(
73  new te::rst::SynchronizedRaster( 2, *syncPtr ) );
74 
75  const unsigned int nBands = syncRasterPtr->getNumberOfBands();
76  const unsigned int nLines = syncRasterPtr->getNumberOfRows();
77  const unsigned int nCols = syncRasterPtr->getNumberOfColumns();
78  unsigned int band = 0;
79  unsigned int line = 0;
80  unsigned int col = 0;
81  double pixelValue = 0;
82 
83  for( band = 0 ; band < nBands ; ++band )
84  {
85  boost::this_thread::sleep( boost::posix_time::milliseconds(100) );
86 
87  for( line = 0 ; line < nLines ; ++line )
88  {
89  for( col = 0 ; col < nCols ; ++col )
90  {
91  syncRasterPtr->getValue( col, line, pixelValue, band );
92  syncRasterPtr->setValue( col, line, pixelValue + 10.0, band );
93  }
94  }
95  }
96 }
97 
98 BOOST_AUTO_TEST_CASE (singleThread_test)
99 {
100  /* Create the input test raster */
101 
102  const unsigned int nBands = 10;
103  const unsigned int nLines = 10;
104  const unsigned int nCols = 10;
105 
106  boost::shared_ptr< te::rst::Raster > inputRasterPointer;
107  CreateTestRaster( nBands, nLines, nCols, inputRasterPointer );
108 
109  /* Using the synchronized raster adaptor. */
110 
111  {
112  std::unique_ptr< te::rst::RasterSynchronizer > syncPtr(
113  new te::rst::RasterSynchronizer( *inputRasterPointer, te::common::RWAccess ) );
114  std::unique_ptr< te::rst::SynchronizedRaster > syncRasterPtr(
115  new te::rst::SynchronizedRaster( 2, *syncPtr ) );
116 
117  unsigned int band = 0;
118  unsigned int line = 0;
119  unsigned int col = 0;
120  double pixelValue = 0;
121 
122  for( band = 0 ; band < nBands ; ++band )
123  {
124  for( line = 0 ; line < nLines ; ++line )
125  {
126  for( col = 0 ; col < nCols ; ++col )
127  {
128  syncRasterPtr->getValue( col, line, pixelValue, band );
129  syncRasterPtr->setValue( col, line, pixelValue + 10.0, band );
130  }
131  }
132  }
133 
134  syncRasterPtr.reset();
135  syncPtr.reset();
136  }
137 
138  /* Verifying the value */
139 
140  {
141  unsigned int band = 0;
142  unsigned int line = 0;
143  unsigned int col = 0;
144  double pixelValue = 0;
145  double readPixelValue = 0;
146 
147  for( band = 0 ; band < nBands ; ++band )
148  for( line = 0 ; line < nLines ; ++line )
149  for( col = 0 ; col < nCols ; ++col )
150  {
151  inputRasterPointer->getValue( col, line, readPixelValue, band );
152  BOOST_CHECK_CLOSE( pixelValue + 10.0, readPixelValue, 0.0000001 );
153  ++pixelValue;
154  }
155  }
156 }
157 
158 BOOST_AUTO_TEST_CASE (multiThread_test)
159 {
160  /* Create the input test raster */
161 
162  const unsigned int nBands = 10;
163  const unsigned int nLines = 100;
164  const unsigned int nCols = 100;
165 
166  boost::shared_ptr< te::rst::Raster > inputRasterPointer;
167  CreateTestRaster( nBands, nLines, nCols, inputRasterPointer );
168 
169  /* Using the synchronized raster adaptor. */
170 
171  {
172  std::unique_ptr< te::rst::RasterSynchronizer > syncPtr(
173  new te::rst::RasterSynchronizer( *inputRasterPointer, te::common::RWAccess ) );
174 
175  boost::thread_group threads;
176 
177  for( unsigned int threadIdx = 0 ; threadIdx < nBands ;
178  ++threadIdx )
179  {
180  threads.add_thread( new boost::thread( threadEntry, syncPtr.get() ) );
181  };
182 
183  threads.join_all();
184 
185  syncPtr.reset();
186  }
187 
188  /* Verifying the values */
189 
190  {
191  unsigned int band = 0;
192  unsigned int line = 0;
193  unsigned int col = 0;
194  double pixelValue = 0;
195  double readPixelValue = 0;
196 
197  for( band = 0 ; band < nBands ; ++band )
198  for( line = 0 ; line < nLines ; ++line )
199  for( col = 0 ; col < nCols ; ++col )
200  {
201  inputRasterPointer->getValue( col, line, readPixelValue, band );
202  BOOST_CHECK_CLOSE( pixelValue + 100.0, readPixelValue, 0.0000001 );
203  ++pixelValue;
204  }
205  }
206 }
207 
208 BOOST_AUTO_TEST_SUITE_END()
An adapter class to allow concurrent access to raster data by multiple threads.
A raster band description.
Definition: BandProperty.h:61
unsigned int line
An access synchronizer to be used in SynchronizedRaster raster instances.
unsigned int col
void CreateTestRaster(unsigned int nBands, unsigned int nLines, unsigned int nCols, boost::shared_ptr< te::rst::Raster > &rasterPointer, bool zero)
BOOST_AUTO_TEST_CASE(singleThread_test)
URI C++ Library.
Definition: Attributes.h:37
unsigned int unsigned int nCols
unsigned int unsigned int boost::shared_ptr< te::rst::Raster > & rasterPointer
void threadEntry(te::rst::RasterSynchronizer *syncPtr)
static Raster * make()
It creates and returns an empty raster with default raster driver.
unsigned int nLines
BOOST_AUTO_TEST_SUITE(synchronizedRaster_tests) void CreateTestRaster(unsigned int nBands
unsigned int band
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
double pixelValue