ClassifierWizard.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/qt/widgets/rp/ClassifierWizard.cpp
22 
23  \brief A Qt dialog that allows users to run a classifier operation defined by RP module.
24 */
25 
26 // TerraLib
27 #include "../../../common/progress/ProgressManager.h"
28 #include "../../../color/ColorBar.h"
29 #include "../../../color/ColorScheme.h"
30 #include "../../../color/ColorSchemeCatalog.h"
31 #include "../../../color/ColorSchemeCatalogManager.h"
32 #include "../../../color/ColorSchemeGroup.h"
33 #include "../../../color/RGBAColor.h"
34 #include "../../../dataaccess/dataset/DataSet.h"
35 #include "../../../dataaccess/utils/Utils.h"
36 #include "../../../raster/Grid.h"
37 #include "../../../raster/Raster.h"
38 #include "../../../raster/RasterSummary.h"
39 #include "../../../raster/RasterSummaryManager.h"
40 #include "../../../rp/Classifier.h"
41 #include "../../../rp/Module.h"
42 #include "../../../se/Categorize.h"
43 #include "../../../se/ColorMap.h"
44 #include "../../../se/ParameterValue.h"
45 #include "../../../se/RasterSymbolizer.h"
46 #include "../../../se/Rule.h"
47 #include "../../../se/Utils.h"
48 #include "../../../maptools/Utils.h"
49 #include "../../../maptools/GroupingAlgorithms.h"
50 #include "../help/HelpPushButton.h"
51 #include "../layer/search/LayerSearchWidget.h"
52 #include "../layer/search/LayerSearchWizardPage.h"
53 #include "../progress/ProgressViewerDialog.h"
54 #include "ClassifierWizard.h"
55 #include "ClassifierWizardPage.h"
56 #include "../raster/RasterInfoWidget.h"
57 #include "RasterInfoWizardPage.h"
58 #include "Utils.h"
59 
60 // STL
61 #include <cassert>
62 
63 // Qt
64 #include <QMessageBox>
65 #include <QApplication>
66 #include <QActionGroup>
67 
69  : QWizard(parent)
70 {
71  //configure the wizard
72  this->setWizardStyle(QWizard::ModernStyle);
73  this->setWindowTitle(tr("Classifier"));
74  //this->setFixedSize(640, 480);
75 
76  this->setOption(QWizard::HaveHelpButton, true);
77  this->setOption(QWizard::HelpButtonOnRight, false);
78 
80 
81  this->setButton(QWizard::HelpButton, helpButton);
82 
83  helpButton->setPageReference("plugins/rp/rp_classifier.html");
84 
85  connect(this, SIGNAL(currentIdChanged(int)), SLOT(onPageChanged(int)));
86 
87  addPages();
88 }
89 
91 {
92  m_classifierPage->clearCanvas();
93 }
94 
96 {
97  if(currentPage() == m_layerSearchPage.get())
98  {
99  std::list<te::map::AbstractLayerPtr> list = m_layerSearchPage->getSearchWidget()->getSelecteds();
100 
101  if(list.empty() == false)
102  {
103  te::map::AbstractLayerPtr l = *list.begin();
104 
105  m_classifierPage->set(l);
106  }
107 
108  return m_layerSearchPage->isComplete();
109  }
110  else if(currentPage() == m_classifierPage.get())
111  {
112  return m_classifierPage->isComplete();
113  }
114  else if(currentPage() == m_rasterInfoPage.get())
115  {
116  return execute();
117  }
118 
119  return true;
120 }
121 
122 void te::qt::widgets::ClassifierWizard::setList(std::list<te::map::AbstractLayerPtr>& layerList)
123 {
124  m_layerSearchPage->getSearchWidget()->setList(layerList);
125  m_layerSearchPage->getSearchWidget()->filterOnlyByRaster();
126 
127  m_classifierPage->setList(layerList);
128 }
129 
131 {
132  m_classifierPage->setMapDisplay(mapDisplay);
133 }
134 
136 {
137  m_classifierPage->setActionGroup(actionGroup);
138 }
139 
141 {
142  return m_outputLayer;
143 }
144 
146 {
147  m_classifierPage->onMapDisplayExtentChanged();
148 }
149 
151 {
155 
156  addPage(m_layerSearchPage.get());
157  addPage(m_classifierPage.get());
158  addPage(m_rasterInfoPage.get());
159 
160  //for contrast only one layer can be selected
161  m_layerSearchPage->getSearchWidget()->enableMultiSelection(false);
162 }
163 
165 {
166  if(m_rasterInfoPage->getWidget()->fileExists())
167  {
168  QMessageBox::warning(this, tr("Classifier"), tr("File already exists."));
169  return false;
170  }
171 
172  //get layer
173  std::list<te::map::AbstractLayerPtr> list = m_layerSearchPage->getSearchWidget()->getSelecteds();
174  te::map::AbstractLayerPtr l = *list.begin();
175 
176  std::unique_ptr<te::rst::Raster> inputRst(te::map::GetRaster(l.get()));
177 
178  //run contrast
179  te::rp::Classifier algorithmInstance;
180 
181  te::rp::Classifier::InputParameters algoInputParams = m_classifierPage->getInputParams();
182  algoInputParams.m_inputRasterPtr = inputRst.get();
183 
184  if(
185  ( m_classifierPage->getROISet() != 0 )
186  &&
187  ( ! m_classifierPage->getROISet()->getROISet().empty() )
188  )
189  {
190  algoInputParams.m_createRasterPalette = false;
191  }
192  else
193  {
194  algoInputParams.m_createRasterPalette = m_classifierPage->getPallete();
195  }
196 
197  te::rp::Classifier::OutputParameters algoOutputParams = m_classifierPage->getOutputParams();
198  algoOutputParams.m_rInfo = m_rasterInfoPage->getWidget()->getInfo();
199  algoOutputParams.m_rType = m_rasterInfoPage->getWidget()->getType();
200 
201  //progress
203 
204  QApplication::setOverrideCursor(Qt::WaitCursor);
205 
206  if(algorithmInstance.initialize(algoInputParams))
207  {
208  if(algorithmInstance.execute(algoOutputParams))
209  {
210  algoOutputParams.reset();
211 
212  //set output layer
214  m_rasterInfoPage->getWidget()->getInfo());
215 
216  QMessageBox::information(this, tr("Classifier"), tr("Classifier ended sucessfully."));
217  }
218  else
219  {
220  QMessageBox::critical(this, tr("Classifier"), tr("Classifier execution error.") +
221  ( " " + algorithmInstance.getErrorMessage() ).c_str());
222 
223  QApplication::restoreOverrideCursor();
224 
225  return false;
226  }
227  }
228  else
229  {
230  QMessageBox::critical(this, tr("Classifier"), tr("Classifier initialization error.") +
231  ( " " + algorithmInstance.getErrorMessage() ).c_str() );
232 
233  QApplication::restoreOverrideCursor();
234 
235  return false;
236  }
237 
238  //create legend
239  te::cl::ROISet* rs = m_classifierPage->getROISet();
240 
241  if(rs && rs->getROISet().empty() == false)
242  {
244 
246 
247  c->setFallbackValue("#000000");
248  c->setLookupValue(new te::se::ParameterValue("Rasterdata"));
249 
250  QColor cWhite(Qt::white);
251  std::string colorWhiteStr = cWhite.name().toUtf8().data();
252 
253  //added dummy color for values < than min values...
254  c->addValue(new te::se::ParameterValue(colorWhiteStr));
255 
256  std::map<std::string, te::cl::ROI*> roiSetMap = rs->getROISet();
257  std::map<std::string, te::cl::ROI*>::iterator it = roiSetMap.begin();
258 
259  unsigned int count = 1;
260 
261  while(it != roiSetMap.end())
262  {
263  std::string color = it->second->getColor();
264  std::string range = QString::number(count).toUtf8().data();
265 
266 
267  c->addThreshold(new te::se::ParameterValue(range));
268  c->addValue(new te::se::ParameterValue(color));
269 
270  if(count == roiSetMap.size())
271  {
272  std::string rangeNext = QString::number(count + 1).toUtf8().data();
273  c->addThreshold(new te::se::ParameterValue(rangeNext));
274  }
275 
276  ++count;
277  ++it;
278  }
279 
280  //added dummy color for values > than max values...
281  c->addValue(new te::se::ParameterValue(colorWhiteStr));
282 
284  cm->setCategorize(c);
285 
287  rasterSymb->setColorMap(cm);
288  }
289 // else
290 // {
291 // buildLegend();
292 // }
293 
294  QApplication::restoreOverrideCursor();
295 
297 
298  emit addLayer(m_outputLayer);
299 
300  return true;
301 }
302 
304 {
305  //Calc min and max
308  const std::complex<double>* cmin = rsMin->at(0).m_minVal;
309  const std::complex<double>* cmax = rsMax->at(0).m_maxVal;
310  double min = cmin->real();
311  double max = cmax->real();
312 
313  std::vector<double> vec;
314  vec.push_back(min);
315  vec.push_back(max);
316 
317  //Get color
318  std::vector<te::color::RGBAColor> colorVec;
319 
321 
322  if(!csc)
323  return;
324 
325  std::vector<te::color::ColorSchemeGroup*> csgVec = csc->getColorSchemeGroups();
326 
327  for(std::size_t t = 0; t < csgVec.size(); ++t)
328  {
329  te::color::ColorSchemeGroup* csg = csgVec[t];
330 
331  if(csg->getName() == "Basic")
332  {
333  std::vector<te::color::ColorScheme*> csVec = csg->getColorSchemes();
334 
335  for(std::size_t p = 0; p < csVec.size(); ++p)
336  {
337  te::color::ColorScheme* cs = csVec[p];
338 
339  if(cs->getName() == "Red")
340  {
341  std::vector<te::color::RGBAColor>* colors = cs->getColors()[0]; //WARNING... Always getting the first position
342 
343  std::vector<te::color::RGBAColor>::iterator it = colors->begin();
344 
345  // create color bar
346  te::color::ColorBar* cb = new te::color::ColorBar(*(colors->begin()), *(colors->end() - 1), 256);
347 
348  int count = 0;
349 
350  //fill color bar
351  while(it != colors->end())
352  {
353  if(count != 0 && count != static_cast<int>(colors->size()) - 1)
354  {
355  double pos = (1. / (colors->size() - 1)) * count;
356 
357  cb->addColor(*it, pos);
358  }
359 
360  ++count;
361  ++it;
362  }
363  colorVec = cb->getSlices(2);
364  }
365  }
366  }
367  }
368 
369  //Get legend
370  std::vector<te::se::Rule*> legVec;
371 
372  te::map::GroupingByEqualSteps("raster",vec.begin(), vec.end(), 2, legVec);
373 
375 
376  c->setFallbackValue("#000000");
377  c->setLookupValue(new te::se::ParameterValue("Rasterdata"));
378 
379  QColor cWhite(Qt::white);
380  std::string colorWhiteStr = cWhite.name().toUtf8().data();
381 
382  //added dummy color for values < than min values...
383  c->addValue(new te::se::ParameterValue(colorWhiteStr));
384 
385  for(std::size_t i = 0; i < legVec.size(); ++i)
386  {
387  std::string rangeStrLower;
388  std::string rangeStrUpper;
389 
390  te::fe::GetFilterStepValues(legVec[i]->getFilter(), rangeStrLower, rangeStrUpper);
391 
392  QColor color(colorVec[i].getRed(), colorVec[i].getGreen(), colorVec[i].getBlue(), colorVec[i].getAlpha());
393 
394  std::string colorStr = color.name().toUtf8().data();
395 
396  c->addThreshold(new te::se::ParameterValue(rangeStrLower));
397  c->addValue(new te::se::ParameterValue(colorStr));
398 
399  if(legVec[i] == legVec.back())
400  {
401  c->addThreshold(new te::se::ParameterValue(rangeStrUpper));
402  }
403  }
404 
405  c->addValue(new te::se::ParameterValue(colorWhiteStr));
406 
407  c->setThresholdsBelongTo(te::se::Categorize::SUCCEEDING);
408 
410 
411  cm->setCategorize(c);
412  cm->setInterpolate(nullptr);
413  cm->setRecode(nullptr);
414 
416  rasterSymb->setColorMap(cm);
417 }
418 
420 {
421  this->adjustSize();
422 }
const std::string & getErrorMessage() const
Return the current error message if there is any.
void addLayer(te::map::AbstractLayerPtr layer)
Raster classification.
Definition: Classifier.h:65
The transformation of continuous values to distinct values (Categorize function). ...
Definition: Categorize.h:90
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Classifier.h:114
const std::vector< ColorScheme * > & getColorSchemes() const
It returns a reference to the list of color schemes belonging to this group.
const std::vector< ColorSchemeGroup * > & getColorSchemeGroups() const
It returns the list of color scheme groups in the catalog.
A Qt dialog that allows users to run a classifier operation defined by RP module. ...
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
void setPageReference(const QString &ref)
Sets the documentation page reference.
const std::string & getName() const
It returns the group name.
TESEEXPORT RasterSymbolizer * GetRasterSymbolizer(Style *s)
Try to get raster symbolizer from a style.
It models the concept of color scheme.
Definition: ColorScheme.h:58
te::map::AbstractLayerPtr getOutputLayer()
void setLookupValue(ParameterValue *v)
Definition: Categorize.cpp:81
const std::vector< std::vector< RGBAColor > * > & getColors() const
It returns all color lists.
Definition: ColorScheme.cpp:68
void setCategorize(Categorize *c)
Definition: ColorMap.cpp:67
A widget to control the display of a set of layers.
void GroupingByEqualSteps(std::string attrName, iterator begin, iterator end, int nSteps, std::vector< te::se::Rule * > &rules, int precision=0)
It groups the values defined by a range of iterators using the equal steps algorithm.
Classifier output parameters.
Definition: Classifier.h:133
The "ParameterValueType" uses WFS-Filter expressions to give values for SE graphic parameters...
A ROISet is a set of ROI&#39;s.
Definition: ROISet.h:53
This file defines a class for a Raster Info Wizard page.
std::unique_ptr< te::qt::widgets::ClassifierWizardPage > m_classifierPage
void setMapDisplay(te::qt::widgets::MapDisplay *mapDisplay)
te::map::AbstractLayerPtr m_outputLayer
TEFEEXPORT void GetFilterStepValues(const te::fe::Filter *filter, std::string &valueMin, std::string &valueMax)
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
This file defines a class for a Classifier Wizard page.
This class is GUI used to define the raster info parameters for raster factory.
This class is GUI used to define the classifier parameters for the RP constast operation.
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
te::gm::Polygon * p
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
const std::string & getName() const
It returns the color schema name.
Definition: ColorScheme.cpp:42
Utility functions for the data access module.
std::unique_ptr< te::qt::widgets::LayerSearchWizardPage > m_layerSearchPage
void setRecode(Recode *i)
Definition: ColorMap.cpp:89
const std::vector< te::color::RGBAColor > & getSlices(const int &n)
It generates color bar.
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
void addColor(const RGBAColor &color, const double &pos)
It adds a color in the color bar.
This class represents a group of color schemes.
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const te::core::URI &connInfo)
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
A catalog for color schemes.
void setColorMap(ColorMap *c)
It models the concept of color bar.
std::unique_ptr< te::qt::widgets::RasterInfoWizardPage > m_rasterInfoPage
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
Calculate the min value.
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Classifier.h:160
Calculate the max value.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void setActionGroup(QActionGroup *actionGroup)
void setThresholdsBelongTo(ThresholdsBelongToType t)
Definition: Categorize.cpp:104
void setInterpolate(Interpolate *i)
Definition: ColorMap.cpp:78
void addThreshold(ParameterValue *v)
Definition: Categorize.cpp:94
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
Definition: ColorMap.h:61
void addValue(ParameterValue *v)
Definition: Categorize.cpp:99
bool m_createRasterPalette
Enable (true) or disable (false) the creation of a paletted output raster (default:true).
Definition: Classifier.h:116
std::map< std::string, te::cl::ROI * > & getROISet()
Get the roi set map.
Definition: ROISet.cpp:77
TEQTWIDGETSEXPORT void applyRasterMultiResolution(const QString &toolName, te::rst::Raster *raster)
Classifier input parameters.
Definition: Classifier.h:75
void setFallbackValue(const std::string &v)
Definition: se/Function.cpp:33
void setList(std::list< te::map::AbstractLayerPtr > &layerList)