All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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(0)
43 {
44  reset();
45 }
46 
48  : m_classifierStrategyParamsPtr(0)
49 {
50  reset();
51 
52  operator=(rhs);
53 }
54 
56 {
57  reset();
58 }
59 
61 {
62  if(m_classifierStrategyParamsPtr)
63  {
64  delete m_classifierStrategyParamsPtr;
65  m_classifierStrategyParamsPtr = 0;
66  }
67 
68  m_classifierStrategyParamsPtr = (StrategyParameters*)p.clone();
69 }
70 
72 {
73  return m_classifierStrategyParamsPtr;
74 }
75 
76 void te::rp::Classifier::InputParameters::reset() throw(te::rp::Exception)
77 {
78  m_inputRasterPtr = 0;
79  m_inputRasterBands.clear();
80  m_inputPolygons.clear();
81  m_strategyName.clear();
82 
83  if(m_classifierStrategyParamsPtr)
84  {
85  delete m_classifierStrategyParamsPtr;
86  m_classifierStrategyParamsPtr = 0;
87  }
88 }
89 
91 {
92  reset();
93 
94  m_inputRasterPtr = params.m_inputRasterPtr;
95  m_inputRasterBands = params.m_inputRasterBands;
96  m_inputPolygons = params.m_inputPolygons;
97  m_strategyName = params.m_strategyName;
98 
99  setClassifierStrategyParams(*params.m_classifierStrategyParamsPtr);
100 
101  return *this;
102 }
103 
105 {
106  return new InputParameters(*this);
107 }
108 
110 {
111  reset();
112 }
113 
115 {
116  reset();
117 
118  operator=(rhs);
119 }
120 
122 {
123  reset();
124 }
125 
126 void te::rp::Classifier::OutputParameters::reset() throw(te::rp::Exception)
127 {
128  m_rType.clear();
129  m_rInfo.clear();
130  m_outputRasterPtr.reset();
131 }
132 
134 {
135  reset();
136 
137  m_rType = params.m_rType;
138  m_rInfo = params.m_rInfo;
139  m_outputRasterPtr = params.m_outputRasterPtr;
140 
141  return *this;
142 }
143 
145 {
146  return new OutputParameters(*this);
147 }
148 
150  : m_instanceInitialized(false)
151 {
152 }
153 
155 {
156 }
157 
158 bool te::rp::Classifier::execute(AlgorithmOutputParameters& outputParams) throw(te::rp::Exception)
159 {
160  if(!m_instanceInitialized)
161  return false;
162 
163 // creating the output raster
164  te::rp::Classifier::OutputParameters* outputParamsPtr = dynamic_cast<te::rp::Classifier::OutputParameters*>(&outputParams);
165  TERP_TRUE_OR_RETURN_FALSE(outputParamsPtr, "Invalid parameters");
166 
167  std::vector<te::rst::BandProperty*> bandsProperties;
168  bandsProperties.push_back(new te::rst::BandProperty(*(m_inputParameters.m_inputRasterPtr->getBand(
169  m_inputParameters.m_inputRasterBands[0])->getProperty())));
170  bandsProperties[0]->m_colorInterp = te::rst::GrayIdxCInt;
171  bandsProperties[0]->m_type = te::dt::UINT32_TYPE;
172  bandsProperties[0]->m_noDataValue = 0;
173 
174  te::rst::Grid* newgrid = new te::rst::Grid(*(m_inputParameters.m_inputRasterPtr->getGrid()));
175 
176  outputParamsPtr->m_outputRasterPtr.reset(
177  te::rst::RasterFactory::make(outputParamsPtr->m_rType, newgrid, bandsProperties, outputParamsPtr->m_rInfo, 0, 0));
178  TERP_TRUE_OR_RETURN_FALSE(outputParamsPtr->m_outputRasterPtr.get(),
179  "Output raster creation error");
180 
181 // instantiating the classification strategy
182  boost::shared_ptr<te::rp::ClassifierStrategy> strategyPtr(ClassifierStrategyFactory::make(m_inputParameters.m_strategyName));
183  TERP_TRUE_OR_RETURN_FALSE(strategyPtr.get(), "Unable to create the classifier strategy");
184  TERP_TRUE_OR_RETURN_FALSE(strategyPtr->initialize(m_inputParameters.getClassifierStrategyParams()),
185  "Unable to initialize the classification strategy");
186  TERP_TRUE_OR_RETURN_FALSE(strategyPtr->execute(*m_inputParameters.m_inputRasterPtr, m_inputParameters.m_inputRasterBands,
187  m_inputParameters.m_inputPolygons, *outputParamsPtr->m_outputRasterPtr, 0, true),
188  "Unable to execute the classification strategy");
189 
190  return true;
191 }
192 
193 void te::rp::Classifier::reset() throw(te::rp::Exception)
194 {
195  m_instanceInitialized = false;
196 
197  m_inputParameters.reset();
198 }
199 
200 bool te::rp::Classifier::initialize(const AlgorithmInputParameters& inputParams) throw(te::rp::Exception)
201 {
202  reset();
203 
204  te::rp::Classifier::InputParameters const* inputParamsPtr = dynamic_cast<te::rp::Classifier::InputParameters const* >(&inputParams);
205 
206 // check if input raster is ok
207  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr, "Invalid parameters");
208 
209  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterPtr, "Invalid raster pointer");
210 
212  te::common::RAccess, "Invalid raster");
213 
214 // check if input raster and bands fit
215  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterBands.size() > 0,
216  "Invalid raster bands number");
217 
218  for(unsigned int i = 0; i < inputParamsPtr->m_inputRasterBands.size(); i++)
219  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterBands[i] <
220  inputParamsPtr->m_inputRasterPtr->getNumberOfBands(),
221  "Invalid raster bands" );
222 
223 // check if input raster and geometries fit
224  for (unsigned i = 0; i > inputParamsPtr->m_inputPolygons.size(); i++)
225  {
226  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputPolygons[i]->getMBR()->within(*inputParamsPtr->m_inputRasterPtr->getExtent()),
227  "Raster and Polygons does not fit");
228  }
229 
230 // everything is ok
231  m_instanceInitialized = true;
232 
233  m_inputParameters = *inputParamsPtr;
234 
235  return true;
236 }
237 
239 {
240  return m_instanceInitialized;
241 }
const Classifier::InputParameters & operator=(const Classifier::InputParameters &params)
Definition: Classifier.cpp:90
std::vector< te::gm::Polygon * > m_inputPolygons
The polygons to be classified when using object-based image analysis (OBIA).
Definition: Classifier.h:115
virtual AbstractParameters * clone() const =0
Create a clone copy of this instance.
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
Definition: Raster.cpp:104
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Classifier.h:113
OutputParameters()
Default constructor.
Definition: Classifier.cpp:109
Index into a lookup table.
Definition: Enums.h:57
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer to the generated output raster (label image).
Definition: Classifier.h:156
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
Definition: Classifier.cpp:76
InputParameters()
Default constructor.
Definition: Classifier.cpp:41
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:116
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
Definition: Classifier.cpp:158
void setClassifierStrategyParams(const StrategyParameters &p)
Set specific classifier strategy parameters.
Definition: Classifier.cpp:60
Raster classification.
Raster Processing algorithm output parameters base interface.
Classifier output parameters.
Definition: Classifier.h:127
#define TERP_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged and a return of context wi...
Definition: Macros.h:183
const Algorithm & operator=(const Algorithm &)
Definition: Algorithm.cpp:43
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Definition: Raster.cpp:89
StrategyParameters const * getClassifierStrategyParams() const
Returns a pointer to the internal specific classifier strategy parameters, or null if no parameters a...
Definition: Classifier.cpp:71
const Classifier::OutputParameters & operator=(const Classifier::OutputParameters &params)
Definition: Classifier.cpp:133
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
Raster strategy parameters base class.
Raster Processing algorithm base interface class.
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: Classifier.h:114
StrategyParameters * m_classifierStrategyParamsPtr
Internal specific classifier strategy parameters.
Definition: Classifier.h:117
Generic template matrix.
static te::rp::ClassifierStrategy * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
Raster classifier strategy factory base class.
Abstract parameters base interface.
bool m_instanceInitialized
Is this instance already initialized?
Definition: Classifier.h:177
Configuration flags for the Raster Processing module of TerraLib.
te::common::AbstractParameters * clone() const
Create a clone copy of this instance.
Definition: Classifier.cpp:144
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
Definition: Classifier.cpp:126
te::common::AbstractParameters * clone() const
Create a clone copy of this instance.
Definition: Classifier.cpp:104
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
Definition: Classifier.cpp:200
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.
Definition: Classifier.cpp:193
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: Classifier.h:155
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Classifier.h:154
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
Definition: Classifier.cpp:238
Classifier input parameters.
Definition: Classifier.h:74