src/terralib/rp/Classifier.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/rp/Classifier.cpp
22 
23  \brief Raster classification.
24  */
25 
26 // TerraLib
27 #include "../geometry/Envelope.h"
28 #include "../geometry/Polygon.h"
29 #include "../raster/Raster.h"
30 #include "../raster/RasterFactory.h"
31 #include "../raster/Band.h"
32 #include "../raster/BandProperty.h"
33 #include "../raster/Grid.h"
34 #include "Algorithm.h"
35 #include "Classifier.h"
36 #include "ClassifierStrategy.h"
38 #include "Config.h"
39 #include "Matrix.h"
40 
42  : m_classifierStrategyParamsPtr(nullptr)
43 {
44  reset();
45 }
46 
49 {
50  reset();
51 
52  operator=(rhs);
53 }
54 
56 {
57  reset();
58 }
59 
61 {
63  {
66  }
67 
69 }
70 
72 {
74 }
75 
77 {
78  m_inputRasterPtr = nullptr;
80  m_enableRasterCache = true;
81  m_enableMultiThread = true;
82  m_createRasterPalette = true;
83  m_inputRasterBands.clear();
84  m_inputPolygons.clear();
85  m_strategyName.clear();
86 
88  {
91  }
92 
93  m_userOutputPalette.clear();
94 }
95 
97 {
98  reset();
99 
108 
110 
112 
113  return *this;
114 }
115 
117 {
118  return new InputParameters(*this);
119 }
120 
122 {
123  reset();
124 }
125 
127 {
128  reset();
129 
130  operator=(rhs);
131 }
132 
134 {
135  reset();
136 }
137 
139 {
140  m_rType.clear();
141  m_rInfo.clear();
142  m_outputRasterPtr.reset();
143 }
144 
146 {
147  reset();
148 
149  m_rType = params.m_rType;
150  m_rInfo = params.m_rInfo;
151  m_outputRasterPtr.reset( params.m_outputRasterPtr.release() );
152 
153  return *this;
154 }
155 
157 {
158  return new OutputParameters(*this);
159 }
160 
162  : m_instanceInitialized(false)
163 {
164 }
165 
167 
168 bool te::rp::Classifier::execute(AlgorithmOutputParameters& outputParams) throw(te::rp::Exception)
169 {
171  return false;
172 
173  // executing the classification strategy
174 
175  boost::shared_ptr<te::rp::ClassifierStrategy> strategyPtr(ClassifierStrategyFactory::make(m_inputParameters.m_strategyName));
176 
177  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(strategyPtr.get(), "Unable to create the classifier strategy");
178 
179  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(strategyPtr->initialize(
181  "Unable to initialize the classification strategy - "
182  + strategyPtr->getErrorMessage() );
183 
184  strategyPtr->setInputRaster( *m_inputParameters.m_inputRasterPtr );
185  strategyPtr->setInputRasterBands( m_inputParameters.m_inputRasterBands );
186  strategyPtr->setInputPolygons( m_inputParameters.m_inputPolygons );
187  strategyPtr->enableProgressInterface( m_inputParameters.m_enableProgressInterface );
188  strategyPtr->enableRasterCache( m_inputParameters.m_enableRasterCache );
189  strategyPtr->enableOutputPalette( m_inputParameters.m_createRasterPalette );
190  strategyPtr->setUserOutputPalette( m_inputParameters.m_userOutputPalette );
191  strategyPtr->enableMultiThread( m_inputParameters.m_enableMultiThread );
192 
193  TERP_INSTANCE_TRUE_OR_RETURN_FALSE( strategyPtr->execute(),
194  "Unable to execute the classification strategy - " +
195  strategyPtr->getErrorMessage() );
196 
197  // Creating the output raster
198 
199  std::unique_ptr< te::rst::Raster > memOutRasterPtr = strategyPtr->releaseOutputRaster();
200  assert( memOutRasterPtr.get() != (te::rst::Raster*)nullptr );
201 
202  std::vector<te::rst::BandProperty*> bandsProperties;
203 
204  for(unsigned int memBandIdx=0; memBandIdx < memOutRasterPtr->getNumberOfBands(); memBandIdx++)
205  {
207  *( memOutRasterPtr->getBand( memBandIdx )->getProperty() ) );
208 
209  // Change the first band data type to 8-bits
210  // if there is enought room for the current pallete there.
211  if(
212  ( memBandIdx == 0 )
213  &&
214  (
215  memOutRasterPtr->getBand( 0 )->getProperty()->m_colorInterp
216  ==
218  )
219  &&
220  (
221  memOutRasterPtr->getBand( 0 )->getProperty()->m_palette.size()
222  <
223  257
224  )
225  )
226  {
227  newbprop->m_type = te::dt::UCHAR_TYPE;
228  }
229 
230  bandsProperties.push_back(newbprop);
231  }
232 
233  te::rp::Classifier::OutputParameters* outputParamsPtr = dynamic_cast<te::rp::Classifier::OutputParameters*>(&outputParams);
234  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(outputParamsPtr, "Invalid parameters");
235 
236  te::rst::Grid* newgrid = new te::rst::Grid(*(memOutRasterPtr->getGrid()));
237 
238  outputParamsPtr->m_outputRasterPtr.reset(
239  te::rst::RasterFactory::make(outputParamsPtr->m_rType, newgrid, bandsProperties,
240  outputParamsPtr->m_rInfo, nullptr, nullptr));
242  "Output raster creation error");
243 
244  // Output raster data copy
245 
246  const unsigned int nBands = static_cast<unsigned int>(memOutRasterPtr->getNumberOfBands());
247  const unsigned int nCols = memOutRasterPtr->getNumberOfColumns();
248  const unsigned int nRows = memOutRasterPtr->getNumberOfRows();
249  unsigned int row = 0;
250  unsigned int col = 0;
251  std::complex< double > value;
252 
253  for( unsigned int bandIdx = 0 ; bandIdx < nBands ; ++bandIdx )
254  {
255  const te::rst::Band& inBand = *memOutRasterPtr->getBand( bandIdx );
256  te::rst::Band& outBand = *outputParamsPtr->m_outputRasterPtr->getBand( bandIdx );
257 
258  for( row = 0 ; row < nRows ; ++row )
259  {
260  for( col = 0 ; col < nCols ; ++col )
261  {
262  inBand.getValue( col, row, value );
263  outBand.setValue( col, row, value );
264  }
265  }
266  }
267 
268  strategyPtr->getMetaData( outputParamsPtr->m_metadata );
269 
270  return true;
271 }
272 
274 {
276 
277  m_instanceInitialized = false;
278 
280 }
281 
282 bool te::rp::Classifier::initialize(const AlgorithmInputParameters& inputParams) throw(te::rp::Exception)
283 {
284  reset();
285 
286  te::rp::Classifier::InputParameters const* inputParamsPtr = dynamic_cast<te::rp::Classifier::InputParameters const* >(&inputParams);
287 
288 // check if input raster is ok
289  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(inputParamsPtr, "Invalid parameters");
290 
291  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterPtr, "Invalid raster pointer");
292 
294  te::common::RAccess, "Invalid raster");
295 
296 // check if input raster and bands fit
297  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterBands.size() > 0,
298  "Invalid raster bands number");
299 
300  for(unsigned int i = 0; i < inputParamsPtr->m_inputRasterBands.size(); i++)
302  inputParamsPtr->m_inputRasterPtr->getNumberOfBands(),
303  "Invalid raster bands" );
304 
305 // check if input raster and geometries fit
306 
307  const std::vector< te::gm::Polygon* >& inputPolygons =
308  inputParamsPtr->m_inputPolygons;
309  const te::rst::Raster& inputRaster = *inputParamsPtr->m_inputRasterPtr;
310 
311  const std::size_t inputPolygonsSize = inputPolygons.size();
312 
313  for (std::size_t inputPolygonsIdx = 0; inputPolygonsIdx > inputPolygonsSize;
314  inputPolygonsIdx++)
315  {
316  TERP_INSTANCE_TRUE_OR_RETURN_FALSE( inputPolygons[ inputPolygonsIdx ],
317  "Invalid input polygon pointer");
319  inputPolygons[ inputPolygonsIdx ]->getSRID() ==
320  inputRaster.getSRID(), "Invalid input polygon SRID" );
322  inputPolygons[ inputPolygonsIdx ]->getMBR()->within(*inputRaster.getExtent()),
323  "Raster and Polygons does not fit");
324  }
325 
326 // everything is ok
327  m_instanceInitialized = true;
328 
329  m_inputParameters = *inputParamsPtr;
330 
331  return true;
332 }
333 
335 {
336  return m_instanceInitialized;
337 }
const Classifier::InputParameters & operator=(const Classifier::InputParameters &params)
std::vector< te::gm::Polygon * > m_inputPolygons
The polygons to be classified when using object-based image analysis (OBIA).
Definition: Classifier.h:120
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
Palette indexes color interpretation.
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Classifier.h:114
bool m_enableProgressInterface
Enable (true) or disable (false) the use of a progress interface (default: disabled).
Definition: Classifier.h:115
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
A raster band description.
Definition: BandProperty.h:61
std::string m_strategyName
The classifier strategy name see each te::rp::ClassifierStrategyFactory inherited classes documentati...
Definition: Classifier.h:121
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
Base exception class for plugin module.
Raster classification.
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer to the generated output raster (label image).
Definition: Classifier.h:162
Raster Processing algorithm output parameters base interface.
std::vector< te::rst::BandProperty::ColorEntry > m_userOutputPalette
User output raster palette (it must be large enough to accomodate all classifyier generated classes o...
Definition: Classifier.h:123
int m_type
The data type of the elements in the band ( See te::dt namespace basic data types for reference )...
Definition: BandProperty.h:133
Raster Processing algorithm base interface class.
Classifier output parameters.
Definition: Classifier.h:133
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
unsigned int unsigned int nCols
bool m_enableRasterCache
Enable (true) or disable (false) the use of memory raster data cache (default:true).
Definition: Classifier.h:117
const Classifier::OutputParameters & operator=(const Classifier::OutputParameters &params)
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.
URI C++ Library.
Definition: Attributes.h:37
Classifier::InputParameters m_inputParameters
Classifier execution parameters.
Definition: Classifier.h:184
te::gm::Polygon * p
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: Classifier.h:119
A raster band description.
Generic template matrix.
static te::rp::ClassifierStrategy * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
Raster classifier strategy factory base class.
ClassifierStrategyParameters const * getClassifierStrategyParams() const
Returns a pointer to the internal specific classifier strategy parameters, or null if no parameters a...
Abstract parameters base interface.
int getSRID() const
Returns the raster spatial reference system identifier.
bool m_instanceInitialized
Is this instance already initialized?
Definition: Classifier.h:183
void setClassifierStrategyParams(const ClassifierStrategyParameters &p)
Set specific classifier strategy parameters.
te::common::AbstractParameters * clone() const
Create a clone copy of this instance.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
te::common::AbstractParameters * clone() const
Create a clone copy of this instance.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
static Raster * make()
It creates and returns an empty raster with default raster driver.
Raster Processing algorithm input parameters base interface.
Raster classifier strategy base class.
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
virtual void reset() _NOEXCEPT_OP(false)
Clear all internal allocated objects and reset the algorithm to its initial state.
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: Classifier.h:161
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Classifier.h:160
Configuration flags for the Raster Processing module of TerraLib.
#define TERP_INSTANCE_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged, the current instance erro...
Definition: Macros.h:200
ClassifierStrategyParameters * m_classifierStrategyParamsPtr
Internal specific classifier strategy parameters.
Definition: Classifier.h:122
bool m_createRasterPalette
Enable (true) or disable (false) the creation of a paletted output raster (default:true).
Definition: Classifier.h:116
unsigned int col
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
Classifier input parameters.
Definition: Classifier.h:75
virtual AbstractParameters * clone() const =0
Create a clone copy of this instance.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
std::map< std::string, std::string > m_metadata
Extra strategy-dependent generated metadata.
Definition: Classifier.h:163
bool m_enableMultiThread
Enable (true) or disable (false) the use multiple threads (default:true).
Definition: Classifier.h:118