TsMAP.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 TsMAP.cpp
22 
23  \brief A test suit for the SynchronizedRaster Class.
24  */
25 
26 // TerraLib
27 #include <terralib_buildconfig.h>
28 
30 #include <terralib/raster.h>
31 #include "Config.h"
32 
33 // STL
34 #include <memory>
35 #include <vector>
36 
37 // Boost
38 #include <boost/test/unit_test.hpp>
39 
40  /*!
41  \class RasterInputAdaptor
42  \brief Classifiers raster input data adaptor.
43 
44  */
45  template<typename DataType>
46  class RasterInputAdaptor : public te::cl::InputAdaptor< DataType >
47  {
48  public:
49 
51  : m_raster( raster ), m_nRows( raster.getNumberOfRows() ),
52  m_nCols( raster.getNumberOfColumns() )
53  {};
54 
56 
57  // overload
58  void getFeature(const unsigned int& elementIndex, const unsigned int& featureIndex,
59  DataType& featureValue ) const
60  {
61  m_raster.getValue( elementIndex % m_nCols, elementIndex / m_nCols, featureValue, featureIndex );
62  };
63 
64  // overload
65  unsigned int getElementsCount() const
66  {
67  return ( m_nRows * m_nCols );
68  };
69 
70  // overload
71  unsigned int getFeaturesCount() const
72  {
73  return m_raster.getNumberOfBands();
74  };
75 
76  protected :
77 
79  unsigned int m_nRows;
80  unsigned int m_nCols;
81  };
82 
83  /*!
84  \class RasterOutputAdaptor
85  \brief Classifiers raster output data adaptor.
86 
87  */
88  template<typename DataType>
89  class RasterOutputAdaptor : public te::cl::OutputAdaptor< DataType >
90  {
91  public:
92 
94  : m_raster( raster ), m_nRows( raster.getNumberOfRows() ),
95  m_nCols( raster.getNumberOfColumns() )
96  {};
97 
99 
100  // overload
101  void setFeature(const unsigned int& elementIndex,
102  const unsigned int& featureIndex, const DataType& value )
103  {
104  m_raster.setValue( elementIndex % m_nCols, elementIndex / m_nCols, (double)value,
105  featureIndex );
106  };
107 
108  // overload
109  unsigned int getElementsCount() const
110  {
111  return ( m_nRows * m_nCols );
112  };
113 
114  // overload
115  unsigned int getFeaturesCount() const
116  {
117  return m_raster.getNumberOfBands();
118  };
119 
120  protected :
121 
123  unsigned int m_nRows;
124  unsigned int m_nCols;
125  double m_value;
126  };
127 
128 BOOST_AUTO_TEST_SUITE ( classification_map_tests )
129 
130 BOOST_AUTO_TEST_CASE (map_test_1)
131 {
132  // creating samples
133 
134  std::vector< std::vector< double > > samples;
135  std::vector<unsigned int> samplesAttributesIndices;
136  std::vector<unsigned int> samplesLabels;
137  {
138  samplesAttributesIndices.push_back( 0 );
139  samplesAttributesIndices.push_back( 1 );
140  samplesAttributesIndices.push_back( 2 );
141 
142  std::vector< double > sample;
143 
144  // vegetation
145 
146  sample.clear();
147  sample.push_back( 41 );
148  sample.push_back( 212 );
149  sample.push_back( 81 );
150  samples.push_back( sample );
151  samplesLabels.push_back( 1 );
152 
153  sample.clear();
154  sample.push_back( 38 );
155  sample.push_back( 213 );
156  sample.push_back( 76 );
157  samples.push_back( sample );
158  samplesLabels.push_back( 1 );
159 
160  sample.clear();
161  sample.push_back( 42 );
162  sample.push_back( 128 );
163  sample.push_back( 70 );
164  samples.push_back( sample );
165  samplesLabels.push_back( 1 );
166 
167  sample.clear();
168  sample.push_back( 31 );
169  sample.push_back( 159 );
170  sample.push_back( 59 );
171  samples.push_back( sample );
172  samplesLabels.push_back( 1 );
173 
174  // water
175 
176  sample.clear();
177  sample.push_back( 39 );
178  sample.push_back( 36 );
179  sample.push_back( 81 );
180  samples.push_back( sample );
181  samplesLabels.push_back( 20 );
182 
183  sample.clear();
184  sample.push_back( 35 );
185  sample.push_back( 41 );
186  sample.push_back( 72 );
187  samples.push_back( sample );
188  samplesLabels.push_back( 20 );
189 
190  sample.clear();
191  sample.push_back( 39 );
192  sample.push_back( 42 );
193  sample.push_back( 76 );
194  samples.push_back( sample );
195  samplesLabels.push_back( 20 );
196 
197  sample.clear();
198  sample.push_back( 38 );
199  sample.push_back( 38 );
200  sample.push_back( 77 );
201  samples.push_back( sample );
202  samplesLabels.push_back( 20 );
203  }
204 
205  /* Openning input raster */
206 
207  std::map<std::string, std::string> auxRasterInfo;
208 
209  auxRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers_rgb342_crop1.tif";
210  std::unique_ptr< te::rst::Raster > inputRasterPtrPointer ( te::rst::RasterFactory::open(
211  auxRasterInfo ) );
212  BOOST_CHECK( inputRasterPtrPointer.get() );
213 
214  std::vector< unsigned int > bands;
215  bands.push_back( 0 );
216  bands.push_back( 1 );
217  bands.push_back( 2 );
218 
219  // creating the output raster
220 
221  std::vector< te::rst::BandProperty* > bProps;
222  bProps.push_back( new te::rst::BandProperty( *inputRasterPtrPointer->getBand( 0 )->getProperty() ) );
223 
224  auxRasterInfo["URI"] = "terralib_unittest_map_test_1.tif";
225 
226  std::unique_ptr< te::rst::Raster > outputRasterPtr( te::rst::RasterFactory::make(
227  "GDAL",
228  new te::rst::Grid( *inputRasterPtrPointer->getGrid() ),
229  bProps,
230  auxRasterInfo,
231  0,
232  0 ) );
233 
234  // training
235 
236  te::cl::VectorOfVectorsInputAdaptor< double > samplesAdaptor( samples );
237 
238  te::cl::MAP classifierInstance;
239 
241 
242  BOOST_CHECK( classifierInstance.initialize( params ) );
243 
244  BOOST_CHECK( classifierInstance.train( samplesAdaptor, samplesAttributesIndices,
245  samplesLabels, true ) );
246 
247  RasterInputAdaptor< double > inputRasterAdp( *inputRasterPtrPointer );
248  RasterOutputAdaptor< unsigned int > outputRasterAdp( *outputRasterPtr );
249 
250  std::vector< double > inputNoDataValues( inputRasterPtrPointer->getNumberOfBands(),
251  std::numeric_limits< double >::max() );
252 
253  BOOST_CHECK( classifierInstance.classify( inputRasterAdp, samplesAttributesIndices,
254  inputNoDataValues, outputRasterAdp, 0, 255, true ) );
255 }
256 
257 BOOST_AUTO_TEST_SUITE_END()
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
Classifiers raster output data adaptor.
Definition: TsMAP.cpp:89
This file contains include headers for the TerraLib Classification module.
Classifier Parameters.
Definition: MAP.h:62
unsigned int m_nCols
Definition: TsMAP.cpp:124
A raster band description.
Definition: BandProperty.h:61
bool train(const InputAdaptor< double > &samples, const std::vector< unsigned int > &attributesIndices, const std::vector< unsigned int > &sampleLabels, const bool enableProgressInterface)
Train this classifier instance using the initialization parameters and the suppied train data...
Definition: MAP.cpp:126
void getFeature(const unsigned int &elementIndex, const unsigned int &featureIndex, DataType &featureValue) const
Returns one feature value.
Definition: TsMAP.cpp:58
unsigned int getElementsCount() const
Returns the total elements number.
Definition: TsMAP.cpp:65
unsigned int m_nCols
Definition: TsMAP.cpp:80
bool initialize(const Parameters &params)
Initialize this classifier instance with new parameters.
Definition: MAP.cpp:74
te::rst::Raster & m_raster
Definition: TsMAP.cpp:118
Classifiers raster input data adaptor.
Definition: TsMAP.cpp:46
unsigned int m_nRows
Definition: TsMAP.cpp:79
bool classify(const InputAdaptor< double > &input, const std::vector< unsigned int > &attributesIndices, const std::vector< double > &inputNoDataValues, OutputAdaptor< unsigned int > &output, const unsigned int outputIndex, const double outputNoDataValue, const bool enableProgressInterface)
Classify an input iterated data and save the result on the output iterated data.
Definition: MAP.cpp:346
Classifiers input data adaptor.
Definition: Adaptors.h:45
An abstract class for raster data strucutures.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
Classifiers output data adaptor.
Definition: Adaptors.h:79
MAP strategy for classification.
Definition: MAP.h:54
unsigned int m_nRows
Definition: TsMAP.cpp:123
list bands
Definition: compose.py:2
unsigned int getFeaturesCount() const
Returns the total features per element number.
Definition: TsMAP.cpp:115
const te::rst::Raster & m_raster
Definition: TsMAP.cpp:74
virtual void getValue(unsigned int c, unsigned int r, double &value, std::size_t b=0) const
Returns the attribute value of a band of a cell.
void setFeature(const unsigned int &elementIndex, const unsigned int &featureIndex, const DataType &value)
Set one feature value.
Definition: TsMAP.cpp:101
BOOST_AUTO_TEST_SUITE(classification_map_tests) BOOST_AUTO_TEST_CASE(map_test_1)
Definition: TsMAP.cpp:128
static Raster * make()
It creates and returns an empty raster with default raster driver.
BOOST_AUTO_TEST_CASE(encoding_test_utf8_latin1)
RasterOutputAdaptor(te::rst::Raster &raster)
Definition: TsMAP.cpp:93
unsigned int getFeaturesCount() const
Returns the total features per element number.
Definition: TsMAP.cpp:71
RasterInputAdaptor(const te::rst::Raster &raster)
Definition: TsMAP.cpp:50
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
unsigned int getElementsCount() const
Returns the total elements number.
Definition: TsMAP.cpp:109
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.