src/terralib/rp/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 
45 {
46  reset();
47 
48  operator=(rhs);
49 }
50 
52 {
53  reset();
54 }
55 
57 {
59  {
62  }
63 
65 }
66 
68 {
70 }
71 
73 {
74  m_inputRasterPtr = 0;
75  m_inputRasterBands.clear();
76  m_inputSensorBands.clear();
77  m_components.clear();
78  m_strategyName.clear();
79 
81  {
84  }
85 
86  m_transfMatrix.clear();
87 }
88 
90 {
91  reset();
92 
96  m_components = params.m_components;
98 
100 
102 
104 
105  return *this;
106 }
107 
109 {
110  return new InputParameters(*this);
111 }
112 
114 {
115  reset();
116 }
117 
119 {
120  reset();
121 
122  operator=(rhs);
123 }
124 
126 {
127  reset();
128 }
129 
131 {
132  m_rType.clear();
133  m_rInfo.clear();
134  m_rInfoError.clear();
135  m_outputRasterPtr.reset();
136  m_errorRasterPtr.reset();
137  m_normalizeOutput = false;
138  m_normalMin = 0.;
139  m_normalMax = 1.;
140  m_createErrorRaster = false;
141  m_min.clear();
142  m_max.clear();
143 }
144 
146 {
147  reset();
148 
149  m_rType = params.m_rType;
150  m_rInfo = params.m_rInfo;
151  m_outputRasterPtr = std::move(params.m_outputRasterPtr);
152  m_errorRasterPtr = std::move(params.m_errorRasterPtr);
153  m_normalizeOutput = params.m_normalizeOutput;
154  m_normalMin = params.m_normalMin;
155  m_normalMax = params.m_normalMax;
156  m_createErrorRaster = params.m_createErrorRaster;
157  m_min = params.m_min;
158  m_max = params.m_max;
159 
160  return *this;
161 }
162 
164 {
165  return new OutputParameters(*this);
166 }
167 
169  : m_instanceInitialized(false)
170 {
171 }
172 
174 {
175 }
176 
177 bool te::rp::MixtureModel::execute(AlgorithmOutputParameters& outputParams) throw(te::rp::Exception)
178 {
180  return false;
181 
182 // creating the output raster
183  te::rp::MixtureModel::OutputParameters* outputParamsPtr = dynamic_cast<te::rp::MixtureModel::OutputParameters*>(&outputParams);
184  TERP_TRUE_OR_RETURN_FALSE(outputParamsPtr, "Invalid parameters");
185 
186  std::vector<te::rst::Raster*> outputRaster;
187 
188  std::vector<te::rst::BandProperty*> bandsProperties;
189  std::map<std::string, std::vector<double> >::iterator it;
190  for (it = m_inputParameters.m_components.begin(); it != m_inputParameters.m_components.end(); it++)
191  {
195  if (outputParamsPtr->m_normalizeOutput)
196  newbprop->m_type = te::dt::UCHAR_TYPE;
197  else
198  newbprop->m_type = newbprop->getType();
201  else
202  newbprop->m_noDataValue = newbprop->m_noDataValue;
203 
204  newbprop->m_description = "Mixture model band for class " + it->first;
205 
206  bandsProperties.push_back(newbprop);
207  }
208 
210 
211  outputParamsPtr->m_outputRasterPtr.reset(
212  te::rst::RasterFactory::make(outputParamsPtr->m_rType, newgrid, bandsProperties, outputParamsPtr->m_rInfo, 0, 0));
213  TERP_TRUE_OR_RETURN_FALSE(outputParamsPtr->m_outputRasterPtr.get(),
214  "Output raster creation error");
215 
216  outputRaster.push_back(outputParamsPtr->m_outputRasterPtr.get());
217 
218  std::vector<te::rst::BandProperty*> bandsPropertiesError;
219  if (outputParamsPtr->m_createErrorRaster)
220  {
221  for (it = m_inputParameters.m_components.begin(); it != m_inputParameters.m_components.end(); it++)
222  {
226  if (m_inputParameters.m_strategyName == "linear")
227  newbprop->m_type = te::dt::DOUBLE_TYPE;
228  else
229  newbprop->m_type = newbprop->getType();
230  newbprop->m_description = "Error raster for class " + it->first;
231 
232  bandsPropertiesError.push_back(newbprop);
233  }
235 
236  outputParamsPtr->m_errorRasterPtr.reset(
237  te::rst::RasterFactory::make(outputParamsPtr->m_rType, newgriderror, bandsPropertiesError, outputParamsPtr->m_rInfoError, 0, 0));
238  TERP_TRUE_OR_RETURN_FALSE(outputParamsPtr->m_errorRasterPtr.get(),
239  "Error raster creation error");
240  outputRaster.push_back(outputParamsPtr->m_errorRasterPtr.get());
241  }
242 
243  if (m_inputParameters.m_inputSensorBands.size() == 0)
244  for (unsigned b = 0; b < m_inputParameters.m_inputRasterBands.size(); b++)
245  m_inputParameters.m_inputSensorBands.push_back("default_sensor");
246 
247 // instantiating the segmentation strategy
248  boost::shared_ptr<te::rp::MixtureModelStrategy> strategyPtr(MixtureModelStrategyFactory::make(m_inputParameters.m_strategyName));
249  TERP_TRUE_OR_RETURN_FALSE(strategyPtr.get(), "Unable to create the MixtureModel strategy");
251  "Unable to initialize the mixture model strategy");
252  strategyPtr->setTransformMatrix(m_inputParameters.m_transfMatrix);
255  outputRaster, outputParamsPtr->m_normalizeOutput, true),
256  "Unable to execute the mixture model strategy");
257 
258  strategyPtr->getMinMax(outputParamsPtr->m_min, outputParamsPtr->m_max);
259  strategyPtr->getMinMaxError(outputParamsPtr->m_minerror, outputParamsPtr->m_maxerror);
260 
261  return true;
262 }
263 
265 {
266  m_instanceInitialized = false;
267 
269 }
270 
271 bool te::rp::MixtureModel::initialize(const AlgorithmInputParameters& inputParams) throw(te::rp::Exception)
272 {
273  reset();
274 
275  te::rp::MixtureModel::InputParameters const* inputParamsPtr = dynamic_cast<te::rp::MixtureModel::InputParameters const* >(&inputParams);
276 
277 // check if input raster is ok
278  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr, "Invalid parameters");
279 
280  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterPtr, "Invalid raster pointer");
281 
283  te::common::RAccess, "Invalid raster");
284 
285 // check if input raster and bands fit
286  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterBands.size() > 0,
287  "Invalid raster bands number");
288 
289 // check if input sensor/bands information fits
290  if (inputParamsPtr->m_inputSensorBands.size() == 0)
291  {
292  TERP_LOGWARN("No information about sensors were provided, using defaults.");
293  }
294  else
295  {
296  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputSensorBands.size() == inputParamsPtr->m_inputRasterBands.size(),
297  "Invalid raster bands number");
298  }
299 
300  for(unsigned int i = 0; i < inputParamsPtr->m_inputRasterBands.size(); i++)
301  {
302  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_inputRasterBands[i] <
303  inputParamsPtr->m_inputRasterPtr->getNumberOfBands(),
304  "Invalid raster bands" );
305  }
306 
307  std::map<std::string, std::vector<double> >::const_iterator it;
308  for (it = inputParamsPtr->m_components.begin(); it != inputParamsPtr->m_components.end(); ++it)
309  {
310  TERP_TRUE_OR_RETURN_FALSE(it->second.size() <= inputParamsPtr->m_inputRasterBands.size(),
311  "Endmember's number of channels is bigger from input raster bands number");
312  }
313 
314  TERP_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_components.size() == inputParamsPtr->m_inputRasterBands.size(),
315  "Number of components must be equal to the number of selected bands");
316 
317 // everything is ok
318  m_instanceInitialized = true;
319 
320  m_inputParameters = *inputParamsPtr;
321 
322  return true;
323 }
324 
326 {
327  return m_instanceInitialized;
328 }
329 
330 bool te::rp::MixtureModel::generateTransformMatrix(boost::numeric::ublas::matrix<double>& transfMatrix)
331 {
332  // instantiating the segmentation strategy
333  boost::shared_ptr<te::rp::MixtureModelStrategy> strategyPtr(MixtureModelStrategyFactory::make(m_inputParameters.m_strategyName));
334  TERP_TRUE_OR_RETURN_FALSE(strategyPtr.get(), "Unable to create the MixtureModel strategy");
336  "Unable to initialize the mixture model strategy");
338  "Unable to generate transform matrix ");
339  strategyPtr->getTransformMatrix(transfMatrix);
340 
341  return true;
342 }
std::vector< double > m_min
Minimun value calculeted in output raster.
Definition: MixtureModel.h:163
Raster decomposition using mixture model.
Index into a lookup table.
std::map< std::string, std::string > m_rInfoError
The necessary information to create the error raster (as described in te::raster::RasterFactory).
Definition: MixtureModel.h:158
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: MixtureModel.h:154
A raster band description.
Definition: BandProperty.h:61
Base exception class for plugin module.
#define TERP_LOGWARN(message)
Logs a warning message.
Definition: Macros.h:128
std::unique_ptr< te::rst::Raster > m_errorRasterPtr
A pointer to the generated output error raster, one band per component plus one error band per compon...
Definition: MixtureModel.h:157
Raster Processing algorithm output parameters base interface.
bool m_instanceInitialized
Is this instance already initialized?
Definition: MixtureModel.h:194
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:113
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
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::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:115
Raster mixture model strategy factory base class.
double m_normalMax
Upper limit to normalized.
Definition: MixtureModel.h:161
double m_normalMin
Lower limit to normalized.
Definition: MixtureModel.h:160
#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:185
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
std::map< std::string, std::string > m_rInfo
The necessary information to create the output raster (as described in te::raster::RasterFactory).
Definition: MixtureModel.h:155
MixtureModel input parameters.
Definition: MixtureModel.h:72
int b
Definition: TsRtree.cpp:32
std::vector< double > m_max
Maximum value calculeted in output raster.
Definition: MixtureModel.h:164
MixtureModel::InputParameters m_inputParameters
Mixture model execution parameters.
Definition: MixtureModel.h:195
bool m_normalizeOutput
A flag to indicate that output raster will be normalized , default [0, 1].
Definition: MixtureModel.h:159
std::vector< double > m_minerror
Minimun value calculeted in output raster.
Definition: MixtureModel.h:165
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.
BandProperty * getProperty()
Returns the band property.
URI C++ Library.
Definition: Attributes.h:37
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::vector< std::complex< double > > m_inputRasterNoDataValues
A vector of values to be used as input raster no-data values or an empty vector indicating to use the...
Definition: MixtureModel.h:118
bool generateTransformMatrix(boost::numeric::ublas::matrix< double > &matrix)
Generates a Transform Matrix.
te::gm::Polygon * p
std::vector< double > m_maxerror
Maximum value calculeted in output raster.
Definition: MixtureModel.h:166
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
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:114
MixtureModel output parameters.
Definition: MixtureModel.h:127
te::common::AbstractParameters * clone() const
Copy paramters.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the mixing model using the parameters defined in inputParams and outputParams.
std::unique_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:156
bool isInitialized() const
Model already initialized?
static Raster * make()
It creates and returns an empty raster with default raster driver.
const MixtureModel::OutputParameters & operator=(const MixtureModel::OutputParameters &params)
assigment operator
Raster mixture model strategy base class.
void setMixtureModelStrategyParams(const StrategyParameters &p)
Set specific mixture model strategy parameters.
Raster Processing algorithm input parameters base interface.
boost::numeric::ublas::matrix< double > m_transfMatrix
Used transformation matrix (when applicable).
Definition: MixtureModel.h:117
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: MixtureModel.h:111
Raster Processing functions.
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
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:162
bool initialize(const AlgorithmInputParameters &inputParams)
Initializes model with paramters defined in inputParams.
A rectified grid is the spatial support for raster data.
Definition: raster/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:116
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: MixtureModel.h:112
void reset()
Resets all variables.
virtual AbstractParameters * clone() const =0
Create a clone copy of this instance.