TsRaster.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/TsRaster.cpp
22 
23  \brief A test suit for the Cached Raster class.
24  */
25 
26 // TerraLib
27 #include <terralib_buildconfig.h>
28 
29 #include <terralib/dataaccess.h>
30 #include <terralib/datatype.h>
31 #include <terralib/raster.h>
32 #include <terralib/geometry.h>
34 #include "../Config.h"
35 
36 // Boost
37 #include <boost/test/unit_test.hpp>
38 #include <boost/shared_ptr.hpp>
39 
40 BOOST_AUTO_TEST_SUITE ( raster_tests )
41 
42 bool loadSHPFile( const std::string& shpFileName,
43  std::vector< te::gm::Geometry* >& geomPtrs )
44 {
45  std::string connInfo("file://");
46  connInfo += shpFileName;
47  std::unique_ptr< te::da::DataSource > ogrDataSourcePtr(
48  te::da::DataSourceFactory::make("OGR", connInfo) );
49  BOOST_CHECK( ogrDataSourcePtr.get() != 0 );
50 
51  ogrDataSourcePtr->open();
52 
53  BOOST_CHECK( ogrDataSourcePtr->isOpened() );
54 
55  std::vector<std::string> dataSetNames = ogrDataSourcePtr->getDataSetNames();
56  std::unique_ptr< te::da::DataSourceTransactor > transactorPtr =
57  ogrDataSourcePtr->getTransactor();
58 
59  for( unsigned int dataSetNamesIdx = 0 ; dataSetNamesIdx < dataSetNames.size() ;
60  ++dataSetNamesIdx )
61  {
62  std::unique_ptr<te::da::DataSet> datasetPtr = transactorPtr->getDataSet(
63  dataSetNames[ dataSetNamesIdx ] );
64 
65  const std::size_t nProperties = datasetPtr->getNumProperties();
66 
67  BOOST_CHECK( datasetPtr->moveBeforeFirst() );
68 
69  while( datasetPtr->moveNext() )
70  {
71  for( std::size_t propIdx = 0 ; propIdx < nProperties ; ++propIdx )
72  {
73  if( datasetPtr->getPropertyDataType( propIdx ) == te::dt::GEOMETRY_TYPE )
74  {
75  std::unique_ptr< te::gm::Geometry > geomPtr = datasetPtr->getGeometry(
76  propIdx );
77 
78  if( geomPtr->getGeomTypeId() == te::gm::PolygonType )
79  {
80  geomPtrs.push_back( geomPtr.release() );
81  }
82  else if( geomPtr->getGeomTypeId() == te::gm::MultiPolygonType )
83  {
84  te::gm::MultiPolygon const * const mPolPtr = (te::gm::MultiPolygon const *)
85  geomPtr.get();
86 
87  for( std::size_t gIdx = 0 ; gIdx < mPolPtr->getNumGeometries() ;
88  ++gIdx )
89  {
90  geomPtrs.push_back( new te::gm::Polygon( *( (te::gm::Polygon*)
91  mPolPtr->getGeometryN( gIdx ) ) ) );
92  }
93  }
94  }
95  }
96  }
97  }
98 
99  return true;
100 }
101 
102 BOOST_AUTO_TEST_CASE (rasterConstructor_test)
103 {
104 }
105 
106 BOOST_AUTO_TEST_CASE (rasterGrid_test)
107 {
108 }
109 
110 BOOST_AUTO_TEST_CASE (rasterCopyConstructor_test)
111 {
112 }
113 
114 BOOST_AUTO_TEST_CASE (metadata_persistence_test)
115 {
116  std::map<std::string, std::string> dsinfo;
117  dsinfo["URI"] = "raster_metadata_persistence_test.tif";
118 
119  // Creating the output raster
120 
121  {
122  std::vector<te::rst::BandProperty*> vecBandProp;
123  vecBandProp.push_back( new te::rst::BandProperty( 0, te::dt::UCHAR_TYPE ) );
124  vecBandProp[ 0 ]->m_blkh = 100;
125  vecBandProp[ 0 ]->m_blkw = 100;
126  vecBandProp[ 0 ]->m_nblocksx = 1;
127  vecBandProp[ 0 ]->m_nblocksy = 1;
128  vecBandProp[ 0 ]->m_noDataValue = 0;
129  vecBandProp[ 0 ]->m_type = te::dt::UCHAR_TYPE;
130  vecBandProp[ 0 ]->m_categoryNames.push_back( "category_0" );
131  vecBandProp[ 0 ]->m_categoryNames.push_back( "category_1" );
132  vecBandProp[ 0 ]->m_description = "description0";
133  vecBandProp[ 0 ]->m_metadata.push_back(
134  std::pair<std::string, std::string>( "meta0name", "meta0value" ) );
135  vecBandProp[ 0 ]->m_metadata.push_back(
136  std::pair<std::string, std::string>( "meta1name", "meta1value" ) );
137 
138  const te::gm::Coord2D ulc( 0, 0 );
139  te::rst::Grid* grid = new te::rst::Grid( 100, 100, 1.0, 1.0, &ulc, 0 );
140 
141  std::unique_ptr<te::rst::Raster> rst(te::rst::RasterFactory::make("GDAL",
142  grid, vecBandProp, dsinfo));
143 
144  BOOST_REQUIRE( rst.get() );
145  BOOST_CHECK( rst->getBand( 0 )->getProperty()->m_description
146  == "description0" );
147  BOOST_REQUIRE( rst->getBand( 0 )->getProperty()->m_metadata.size()
148  == 2 );
149  BOOST_CHECK( rst->getBand( 0 )->getProperty()->m_metadata[ 0 ].first
150  == "meta0name" );
151  BOOST_CHECK( rst->getBand( 0 )->getProperty()->m_metadata[ 0 ].second
152  == "meta0value" );
153  BOOST_CHECK( rst->getBand( 0 )->getProperty()->m_metadata[ 1 ].first
154  == "meta1name" );
155  BOOST_CHECK( rst->getBand( 0 )->getProperty()->m_metadata[ 1 ].second
156  == "meta1value" );
157  BOOST_REQUIRE( rst->getBand( 0 )->getProperty()->m_categoryNames.size()
158  == 2 );
159  BOOST_CHECK( rst->getBand( 0 )->getProperty()->m_categoryNames[ 0 ]
160  == "category_0" );
161  BOOST_CHECK( rst->getBand( 0 )->getProperty()->m_categoryNames[ 1 ]
162  == "category_1" );
163  }
164 
165  // Reopen raster
166 
167  {
168  boost::shared_ptr< te::rst::Raster > inputRasterPtr (
169  te::rst::RasterFactory::open( dsinfo ) );
170 
171  BOOST_REQUIRE( inputRasterPtr.get() );
172  BOOST_CHECK( inputRasterPtr->getBand( 0 )->getProperty()->m_description
173  == "description0" );
174  BOOST_REQUIRE( inputRasterPtr->getBand( 0 )->getProperty()->m_metadata.size()
175  == 2 );
176  BOOST_CHECK( inputRasterPtr->getBand( 0 )->getProperty()->m_metadata[ 0 ].first
177  == "meta0name" );
178  BOOST_CHECK( inputRasterPtr->getBand( 0 )->getProperty()->m_metadata[ 0 ].second
179  == "meta0value" );
180  BOOST_CHECK( inputRasterPtr->getBand( 0 )->getProperty()->m_metadata[ 1 ].first
181  == "meta1name" );
182  BOOST_CHECK( inputRasterPtr->getBand( 0 )->getProperty()->m_metadata[ 1 ].second
183  == "meta1value" );
184  BOOST_REQUIRE( inputRasterPtr->getBand( 0 )->getProperty()->m_categoryNames.size()
185  == 2 );
186  BOOST_CHECK( inputRasterPtr->getBand( 0 )->getProperty()->m_categoryNames[ 0 ]
187  == "category_0" );
188  BOOST_CHECK( inputRasterPtr->getBand( 0 )->getProperty()->m_categoryNames[ 1 ]
189  == "category_1" );
190  }
191 }
192 
193 BOOST_AUTO_TEST_CASE (rasterize_test)
194 {
195  std::vector< te::gm::Geometry* > geomPtrs;
196  BOOST_CHECK( loadSHPFile( TERRALIB_DATA_DIR "/shape/poligono_unico.shp",
197  geomPtrs ) );
198 
199  std::vector<te::rst::BandProperty*> vecBandProp;
200  vecBandProp.push_back( new te::rst::BandProperty( 0, te::dt::UCHAR_TYPE ) );
201  vecBandProp[ 0 ]->m_blkh = 100;
202  vecBandProp[ 0 ]->m_blkw = 100;
203  vecBandProp[ 0 ]->m_nblocksx = 1;
204  vecBandProp[ 0 ]->m_nblocksy = 1;
205  vecBandProp[ 0 ]->m_noDataValue = 0;
206  vecBandProp[ 0 ]->m_type = te::dt::UCHAR_TYPE;
207 
208  std::map<std::string, std::string> dsinfo;
209  dsinfo["URI"] = "TsRastertcRasterize.tif";
210 
211  te::rst::Grid* grid = new te::rst::Grid( (unsigned int)
212  vecBandProp[ 0 ]->m_blkw, (unsigned int)vecBandProp[ 0 ]->m_blkh,
213  new te::gm::Envelope( *geomPtrs[ 0 ]->getMBR() ), geomPtrs[ 0 ]->getSRID() );
214 
215  std::unique_ptr<te::rst::Raster> rst(te::rst::RasterFactory::make("GDAL", grid,
216  vecBandProp, dsinfo));
217 
218  std::vector< double > values;
219  values.push_back( 1.0 );
220 
221  rst->rasterize( geomPtrs, values, 0 );
222 }
223 
224 BOOST_AUTO_TEST_CASE (raste_clip_test)
225 {
226  /* Openning input raster */
227 
228  std::map<std::string, std::string> auxRasterInfo;
229 
230  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers_rgb342_crop3_EPSG_22522.tif";
231  boost::shared_ptr< te::rst::Raster > inputRasterPtr ( te::rst::RasterFactory::open(
232  auxRasterInfo ) );
233  BOOST_CHECK( inputRasterPtr.get() );
234 
235  // Creating polygon
236 
237  unsigned int nCols = inputRasterPtr->getNumberOfColumns();
238  unsigned int nRows = inputRasterPtr->getNumberOfRows();
239 
240  double x = 0;
241  double y = 0;
242 
243  std::unique_ptr< te::gm::LinearRing > linearRingPtr( new te::gm::LinearRing(4,
244  te::gm::LineStringType, inputRasterPtr->getSRID() ) );
245  inputRasterPtr->getGrid()->gridToGeo( -0.5, -0.5, x, y );
246  linearRingPtr->setPoint(0, x, y );
247  inputRasterPtr->getGrid()->gridToGeo( (double)( nCols / 2 ) , -0.5, x, y );
248  linearRingPtr->setPoint(1, x, y );
249  inputRasterPtr->getGrid()->gridToGeo( -0.5 , (double)( nRows / 2 ), x, y );
250  linearRingPtr->setPoint(2, x, y );
251  inputRasterPtr->getGrid()->gridToGeo( -0.5, -0.5, x, y );
252  linearRingPtr->setPoint(3, x, y );
253 
254  std::unique_ptr< te::gm::Polygon > polygonPtr( new te::gm::Polygon(0, te::gm::PolygonType,
255  inputRasterPtr->getSRID() ) );
256  polygonPtr->add( linearRingPtr.release() );
257 
258  /* Clipping */
259 
260  std::vector< te::gm::Geometry const *> geometries;
261  geometries.push_back( polygonPtr.get() );
262 
263  auxRasterInfo["URI"] = "TsRaster_clipping.tif";
264  boost::shared_ptr< te::rst::Raster > outputRasterPtr(
265  inputRasterPtr->clip( geometries, auxRasterInfo, "GDAL" ) );
266  BOOST_CHECK( outputRasterPtr.get() );
267 }
268 
269 BOOST_AUTO_TEST_CASE (raster_with_multirest_and_palette_test)
270 {
271  /* Openning input raster */
272 
273  std::map<std::string, std::string> auxRasterInfo;
274 
275  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop_with_color_table_and_multires.tif";
276  boost::shared_ptr< te::rst::Raster > inputRasterPtr ( te::rst::RasterFactory::open(
277  auxRasterInfo ) );
278  BOOST_CHECK( inputRasterPtr.get() );
279 
280  BOOST_CHECK( inputRasterPtr->getNumberOfBands() == 1 );
281  BOOST_CHECK( inputRasterPtr->getBand( 0 )->getProperty()->m_colorInterp == te::rst::PaletteIdxCInt );
282  BOOST_CHECK( inputRasterPtr->getMultiResLevelsCount() == 1 );
283 
284  boost::shared_ptr< te::rst::Raster > inputRasterMultiRestLevel0Ptr( inputRasterPtr->getMultiResLevel( 0 ) );
285  BOOST_CHECK( inputRasterMultiRestLevel0Ptr.get() );
286 
287  BOOST_CHECK( inputRasterMultiRestLevel0Ptr->getNumberOfBands() == 1 );
288  BOOST_CHECK( inputRasterMultiRestLevel0Ptr->getBand( 0 )->getProperty()->m_colorInterp == te::rst::PaletteIdxCInt );
289 
290  const std::size_t palSize = inputRasterPtr->getBand( 0 )->getProperty()->m_palette.size();
291  BOOST_CHECK( palSize == 256 );
292  const std::vector< te::rst::BandProperty::ColorEntry >& pal1 =
293  inputRasterMultiRestLevel0Ptr->getBand( 0 )->getProperty()->m_palette;
294  const std::vector< te::rst::BandProperty::ColorEntry >& pal2 =
295  inputRasterPtr->getBand( 0 )->getProperty()->m_palette;
296 
297  for( std::size_t cIdx = 0 ; cIdx < palSize ; ++cIdx )
298  {
299  BOOST_CHECK( pal1[ cIdx ].c1 == pal2[ cIdx ].c1 );
300  BOOST_CHECK( pal1[ cIdx ].c2 == pal2[ cIdx ].c2 );
301  BOOST_CHECK( pal1[ cIdx ].c3 == pal2[ cIdx ].c3 );
302  BOOST_CHECK( pal1[ cIdx ].c4 == pal2[ cIdx ].c4 );
303  }
304 
305 }
306 
307 BOOST_AUTO_TEST_CASE (statistics_persistence_test)
308 {
309  std::map<std::string, std::string> dsinfo;
310  dsinfo["URI"] = "raster_statistics_persistence_test.tif";
311 
312  // Creating the output raster
313 
314  {
315  std::vector<te::rst::BandProperty*> vecBandProp;
316  vecBandProp.push_back( new te::rst::BandProperty( 0, te::dt::UCHAR_TYPE ) );
317  vecBandProp[ 0 ]->m_blkh = 100;
318  vecBandProp[ 0 ]->m_blkw = 100;
319  vecBandProp[ 0 ]->m_nblocksx = 1;
320  vecBandProp[ 0 ]->m_nblocksy = 1;
321  vecBandProp[ 0 ]->m_noDataValue = 0;
322  vecBandProp[ 0 ]->m_type = te::dt::UCHAR_TYPE;
323  vecBandProp[ 0 ]->m_min = 1;
324  vecBandProp[ 0 ]->m_max = 1;
325  vecBandProp[ 0 ]->m_mean = 1;
326  vecBandProp[ 0 ]->m_stdDev = 1;
327 
328  const te::gm::Coord2D ulc( 0, 0 );
329  te::rst::Grid* grid = new te::rst::Grid( 100, 100, 1.0, 1.0, &ulc, 0 );
330 
331  std::unique_ptr<te::rst::Raster> rst(te::rst::RasterFactory::make("GDAL",
332  grid, vecBandProp, dsinfo));
333 
334  BOOST_REQUIRE( rst.get() );
335  BOOST_REQUIRE( rst->getBand( 0 )->getProperty()->m_min == 1 );
336  BOOST_REQUIRE( rst->getBand( 0 )->getProperty()->m_max == 1 );
337  BOOST_REQUIRE( rst->getBand( 0 )->getProperty()->m_mean == 1 );
338  BOOST_REQUIRE( rst->getBand( 0 )->getProperty()->m_stdDev == 1 );
339  }
340 
341  // Reopen raster
342 
343  {
344  boost::shared_ptr< te::rst::Raster > inputRasterPtr (
345  te::rst::RasterFactory::open( dsinfo ) );
346 
347  BOOST_REQUIRE( inputRasterPtr.get() );
348  BOOST_REQUIRE( inputRasterPtr->getBand( 0 )->getProperty()->m_min == 1 );
349  BOOST_REQUIRE( inputRasterPtr->getBand( 0 )->getProperty()->m_max == 1 );
350  BOOST_REQUIRE( inputRasterPtr->getBand( 0 )->getProperty()->m_mean == 1 );
351  BOOST_REQUIRE( inputRasterPtr->getBand( 0 )->getProperty()->m_stdDev == 1 );
352  }
353 }
354 
355 BOOST_AUTO_TEST_SUITE_END ()
This file contains include headers for the Data Type module of TerraLib.
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
Palette indexes color interpretation.
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
A raster band description.
Definition: BandProperty.h:61
A RAM cache adaptor to an external existent raster that must always be avaliable. ...
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
unsigned int unsigned int nCols
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
An Envelope defines a 2D rectangular region.
URI C++ Library.
Definition: Attributes.h:37
BOOST_AUTO_TEST_SUITE(raster_tests) bool loadSHPFile(const std
Definition: TsRaster.cpp:40
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
static Raster * make()
It creates and returns an empty raster with default raster driver.
BOOST_AUTO_TEST_CASE(rasterConstructor_test)
Definition: TsRaster.cpp:102
This file contains include headers for the Vector Geometry model of TerraLib.
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.
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.