All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MixtureModel.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/MixtureModel.cpp
22 
23  \brief Raster decomposition using mixture model.
24  */
25 
26 // TerraLib
27 #include "../raster/Band.h"
28 #include "../raster/BandProperty.h"
29 #include "../raster/Grid.h"
30 #include "../raster/Raster.h"
31 #include "../raster/RasterFactory.h"
32 #include "MixtureModel.h"
33 #include "MixtureModelStrategy.h"
35 #include "Functions.h"
36 
38  : m_mixtureModelStrategyParamsPtr(0)
39 {
40  reset();
41 }
42 
44  : m_mixtureModelStrategyParamsPtr(0)
45 {
46  reset();
47 
48  operator=(rhs);
49 }
50 
52 {
53  reset();
54 }
55 
57 {
58  if(m_mixtureModelStrategyParamsPtr)
59  {
60  delete m_mixtureModelStrategyParamsPtr;
61  m_mixtureModelStrategyParamsPtr = 0;
62  }
63 
64  m_mixtureModelStrategyParamsPtr = (StrategyParameters*)p.clone();
65 }
66 
68 {
69  return m_mixtureModelStrategyParamsPtr;
70 }
71 
72 void te::rp::MixtureModel::InputParameters::reset() throw(te::rp::Exception)
73 {
74  m_inputRasterPtr = 0;
75  m_inputRasterBands.clear();
76  m_inputSensorBands.clear();
77  m_components.clear();
78  m_strategyName.clear();
79 
80  if(m_mixtureModelStrategyParamsPtr)
81  {
82  delete m_mixtureModelStrategyParamsPtr;
83  m_mixtureModelStrategyParamsPtr = 0;
84  }
85 }
86 
88 {
89  reset();
90 
91  m_inputRasterPtr = params.m_inputRasterPtr;
92  m_inputRasterBands = params.m_inputRasterBands;
93  m_inputSensorBands = params.m_inputSensorBands;
94  m_components = params.m_components;
95  m_strategyName = params.m_strategyName;
96 
97  setMixtureModelStrategyParams(*params.m_mixtureModelStrategyParamsPtr);
98 
99  return *this;
100 }
101 
103 {
104  return new InputParameters(*this);
105 }
106 
108 {
109  reset();
110 }
111 
113 {
114  reset();
115 
116  operator=(rhs);
117 }
118 
120 {
121  reset();
122 }
123 
124 void te::rp::MixtureModel::OutputParameters::reset() throw(te::rp::Exception)
125 {
126  m_rType.clear();
127  m_rInfo.clear();
128  m_outputRasterPtr.reset();
129  m_normalizeOutput = false;
130  m_createErrorRaster = false;
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  m_normalizeOutput = params.m_normalizeOutput;
141  m_createErrorRaster = params.m_createErrorRaster;
142 
143  return *this;
144 }
145 
147 {
148  return new OutputParameters(*this);
149 }
150 
152  : m_instanceInitialized(false)
153 {
154 }
155 
157 {
158 }
159 
160 bool te::rp::MixtureModel::execute(AlgorithmOutputParameters& outputParams) throw(te::rp::Exception)
161 {
162  if(!m_instanceInitialized)
163  return false;
164 
165 // creating the output raster
166  te::rp::MixtureModel::OutputParameters* outputParamsPtr = dynamic_cast<te::rp::MixtureModel::OutputParameters*>(&outputParams);
167  TERP_TRUE_OR_RETURN_FALSE(outputParamsPtr, "Invalid parameters");
168 
169  std::vector<te::rst::BandProperty*> bandsProperties;
170  std::map<std::string, std::vector<double> >::iterator it;
171  for (it = m_inputParameters.m_components.begin(); it != m_inputParameters.m_components.end(); it++)
172  {
173  te::rst::BandProperty* newbprop = new te::rst::BandProperty(*(m_inputParameters.m_inputRasterPtr->getBand(
174  m_inputParameters.m_inputRasterBands[0])->getProperty()));
176  newbprop->m_type = te::dt::DOUBLE_TYPE;
177  newbprop->m_noDataValue = -1;
178  newbprop->m_description = "Mixture model band for class " + it->first;
179 
180  bandsProperties.push_back(newbprop);
181  }
182 
183  if (outputParamsPtr->m_createErrorRaster)
184  {
185  for (it = m_inputParameters.m_components.begin(); it != m_inputParameters.m_components.end(); it++)
186  {
187  te::rst::BandProperty* newbprop = new te::rst::BandProperty(*(m_inputParameters.m_inputRasterPtr->getBand(
188  m_inputParameters.m_inputRasterBands[0])->getProperty()));
190  newbprop->m_type = te::dt::DOUBLE_TYPE;
191  newbprop->m_noDataValue = -1;
192  newbprop->m_description = "Error raster for class " + it->first;
193 
194  bandsProperties.push_back(newbprop);
195  }
196  }
197 
198  te::rst::Grid* newgrid = new te::rst::Grid(*(m_inputParameters.m_inputRasterPtr->getGrid()));
199 
200  outputParamsPtr->m_outputRasterPtr.reset(
201  te::rst::RasterFactory::make(outputParamsPtr->m_rType, newgrid, bandsProperties, outputParamsPtr->m_rInfo, 0, 0));
202  TERP_TRUE_OR_RETURN_FALSE(outputParamsPtr->m_outputRasterPtr.get(),
203  "Output raster creation error");
204 
205  if (m_inputParameters.m_inputSensorBands.size() == 0)
206  for (unsigned b = 0; b < m_inputParameters.m_inputRasterBands.size(); b++)
207  m_inputParameters.m_inputSensorBands.push_back("default_sensor");
208 
209 // instantiating the segmentation strategy
210  boost::shared_ptr<te::rp::MixtureModelStrategy> strategyPtr(MixtureModelStrategyFactory::make(m_inputParameters.m_strategyName));
211  TERP_TRUE_OR_RETURN_FALSE(strategyPtr.get(), "Unable to create the MixtureModel strategy");
212  TERP_TRUE_OR_RETURN_FALSE(strategyPtr->initialize(m_inputParameters.getMixtureModelStrategyParams()),
213  "Unable to initialize the mixture model strategy");
214  TERP_TRUE_OR_RETURN_FALSE(strategyPtr->execute(*m_inputParameters.m_inputRasterPtr, m_inputParameters.m_inputRasterBands,
215  m_inputParameters.m_inputSensorBands, m_inputParameters.m_components,
216  *outputParamsPtr->m_outputRasterPtr, true),
217  "Unable to execute the mixture model strategy");
218 
219  if (outputParamsPtr->m_normalizeOutput)
220  NormalizeRaster(*outputParamsPtr->m_outputRasterPtr);
221 
222  return true;
223 }
224 
225 void te::rp::MixtureModel::reset() throw(te::rp::Exception)
226 {
227  m_instanceInitialized = false;
228 
229  m_inputParameters.reset();
230 }
231 
232 bool te::rp::MixtureModel::initialize(const AlgorithmInputParameters& inputParams) throw(te::rp::Exception)
233 {
234  reset();
235 
236  te::rp::MixtureModel::InputParameters const* inputParamsPtr = dynamic_cast<te::rp::MixtureModel::InputParameters const* >(&inputParams);
237 
238 // check if input raster is ok
239  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr, "Invalid parameters");
240 
241  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterPtr, "Invalid raster pointer");
242 
244  te::common::RAccess, "Invalid raster");
245 
246 // check if input raster and bands fit
247  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterBands.size() > 0,
248  "Invalid raster bands number");
249 
250 // check if input sensor/bands information fits
251  if (inputParamsPtr->m_inputSensorBands.size() == 0)
252  {
253  TERP_LOGWARN("No information about sensors were provided, using defaults.");
254  }
255  else
256  {
257  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputSensorBands.size() == inputParamsPtr->m_inputRasterBands.size(),
258  "Invalid raster bands number");
259  }
260 
261  for(unsigned int i = 0; i < inputParamsPtr->m_inputRasterBands.size(); i++)
262  {
263  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterBands[i] <
264  inputParamsPtr->m_inputRasterPtr->getNumberOfBands(),
265  "Invalid raster bands" );
266  }
267 
268  std::map<std::string, std::vector<double> >::const_iterator it;
269  for (it = inputParamsPtr->m_components.begin(); it != inputParamsPtr->m_components.end(); ++it)
270  {
271  TERP_TRUE_OR_RETURN_FALSE(it->second.size() <= inputParamsPtr->m_inputRasterBands.size(),
272  "Endmember's number of channels is bigger from input raster bands number");
273  }
274 
275  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_components.size() == inputParamsPtr->m_inputRasterBands.size(),
276  "Number of components must be equal to the number of selected bands");
277 
278 // everything is ok
279  m_instanceInitialized = true;
280 
281  m_inputParameters = *inputParamsPtr;
282 
283  return true;
284 }
285 
287 {
288  return m_instanceInitialized;
289 }
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
virtual AbstractParameters * clone() const =0
Create a clone copy of this instance.
Raster decomposition using mixture model.
Index into a lookup table.
Definition: Enums.h:57
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: MixtureModel.h:150
A raster band description.
Definition: BandProperty.h:61
#define TERP_LOGWARN(message)
Logs a warning message.
Definition: Macros.h:127
Raster Processing algorithm output parameters base interface.
bool m_instanceInitialized
Is this instance already initialized?
Definition: MixtureModel.h:175
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
std::vector< std::string > m_inputSensorBands
The names of the sensor/bands.
Definition: MixtureModel.h:110
int m_type
The data type of the elements in the band.
Definition: BandProperty.h:133
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
Definition: BandProperty.h:136
std::string m_strategyName
The mixture model strategy name see each te::rp::MixtureModelStrategyFactory inherited classes docume...
Definition: MixtureModel.h:112
Raster mixture model strategy factory base class.
Raster Processing functions.
#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
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: MixtureModel.h:151
MixtureModel input parameters.
Definition: MixtureModel.h:69
bool m_normalizeOutput
A flag to indicate that output raster will be normalized, by default [0, 255].
Definition: MixtureModel.h:153
bool NormalizeRaster(te::rst::Raster &inraster, double nmin, double nmax)
Normalizes one raster in a given interval.
Definition: Functions.cpp:701
const MixtureModel::InputParameters & operator=(const MixtureModel::InputParameters &params)
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.
StrategyParameters const * getMixtureModelStrategyParams() const
Returns a pointer to the internal specific mixture model strategy parameters, or null if no parameter...
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer to the generated output raster, one band per component plus one error band per component (w...
Definition: MixtureModel.h:152
static te::rp::MixtureModelStrategy * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
Abstract parameters base interface.
std::map< std::string, std::vector< double > > m_components
A set of endmembers and its radiances.
Definition: MixtureModel.h:111
MixtureModel output parameters.
Definition: MixtureModel.h:123
te::common::AbstractParameters * clone() const
Create a clone copy of this instance.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
static Raster * make()
It creates and returns an empty raster with default raster driver.
const MixtureModel::OutputParameters & operator=(const MixtureModel::OutputParameters &params)
Raster mixture model strategy base class.
void setMixtureModelStrategyParams(const StrategyParameters &p)
Set specific mixture model strategy parameters.
Raster Processing algorithm input parameters base interface.
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: MixtureModel.h:108
InputParameters()
Default constructor.
std::string m_description
A description.
Definition: BandProperty.h:134
bool m_createErrorRaster
A flag to indicate that output raster will include the error bands.
Definition: MixtureModel.h:154
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
ColorInterp m_colorInterp
The color interpretation.
Definition: BandProperty.h:140
te::common::AbstractParameters * clone() const
Create a clone copy of this instance.
StrategyParameters * m_mixtureModelStrategyParamsPtr
Internal specific mixture model strategy parameters.
Definition: MixtureModel.h:113
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: MixtureModel.h:109
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
OutputParameters()
Default constructor.