All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ClassifierWizardPage.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 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/ClassifierWizardPage.cpp
22 
23  \brief This file defines a class for a Classifier Wizard page.
24 */
25 
26 // TerraLib
27 #include "../../../common/StringUtils.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/utils/Utils.h"
30 #include "../../../raster/Raster.h"
31 #include "../../../rp/ClassifierISOSegStrategy.h"
32 #include "../../widgets/canvas/Canvas.h"
33 #include "../../widgets/canvas/MapDisplay.h"
34 #include "ClassifierWizardPage.h"
35 #include "ui_ClassifierWizardPageForm.h"
36 
37 // Qt
38 #include <QGridLayout>
39 #include <QtGui/QMessageBox>
40 
41 // Boost
42 #include <boost/uuid/random_generator.hpp>
43 #include <boost/uuid/uuid_io.hpp>
44 
45 
46 // stl
47 #include <memory>
48 
50  : QWizardPage(parent),
51  m_ui(new Ui::ClassifierWizardPageForm),
52  m_countSamples(0),
53  m_layer(0)
54 {
55 // setup controls
56  m_ui->setupUi(this);
57 
59 
60 //connects
61  connect(m_ui->m_tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(onItemChanged(QTableWidgetItem*)));
62  connect(m_ui->m_removeToolButton, SIGNAL(clicked()), this, SLOT(onRemoveToolButtonClicked()));
63 
64 
65 //configure page
66  this->setTitle(tr("Classifier"));
67  this->setSubTitle(tr("Select the type of classifier and set their specific parameters."));
68 
69  m_ui->m_removeToolButton->setIcon(QIcon::fromTheme("list-remove"));
70 }
71 
73 {
74  std::map<std::string, ClassifierSamples>::iterator it = m_samples.begin();
75 
76  while(it != m_samples.end())
77  {
78  delete it->second.m_poly;
79 
80  ++it;
81  }
82 
83  m_samples.clear();
84 }
85 
87 {
88  m_layer = layer;
89 
90  listBands();
91 }
92 
94 {
95  int idx = m_ui->m_classifierTypeComboBox->currentIndex();
96  int type = m_ui->m_classifierTypeComboBox->itemData(idx).toInt();
97 
99 
100  if(type == CLASSIFIER_ISOSEG)
101  {
102  algoInputParams.m_strategyName = "isoseg";
103 
105  classifierparameters.m_acceptanceThreshold = m_ui->m_acceptanceThresholdComboBox->currentText().toDouble();
106 
107  algoInputParams.setClassifierStrategyParams(classifierparameters);
108  }
109 
110  //get bands
111  QList<QListWidgetItem*> selectedBands = m_ui->m_inputRasterBandsListWidget->selectedItems();
112 
113  QList<QListWidgetItem*>::const_iterator it = selectedBands.begin();
114  QList<QListWidgetItem*>::const_iterator itend = selectedBands.end();
115 
116  while(it != itend)
117  {
118  algoInputParams.m_inputRasterBands.push_back((*it)->text().toUInt());
119 
120  ++it;
121  }
122 
123  //get polygons
124  std::map<std::string, ClassifierSamples>::iterator itSamples = m_samples.begin();
125 
126  std::vector<te::gm::Polygon*> polyVec;
127 
128  while(itSamples != m_samples.end())
129  {
130  polyVec.push_back(itSamples->second.m_poly);
131 
132  ++itSamples;
133  }
134 
135  algoInputParams.m_inputPolygons = polyVec;
136 
137  return algoInputParams;
138 }
139 
141 {
142  te::rp::Classifier::OutputParameters algoOutputParams;
143 
144  return algoOutputParams;
145 }
146 
148 {
149  m_ui->m_classifierTypeComboBox->clear();
150 
151  m_ui->m_classifierTypeComboBox->addItem(tr("ISOSeg"), CLASSIFIER_ISOSEG);
152 }
153 
155 {
156  m_ui->m_inputRasterBandsListWidget->clear();
157 
158  assert(m_layer.get());
159 
160  //get input raster
161  std::auto_ptr<te::da::DataSet> ds = m_layer->getData();
162 
163  if(ds.get())
164  {
165  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
166 
167  std::auto_ptr<te::rst::Raster> inputRst = ds->getRaster(rpos);
168 
169  if(inputRst.get())
170  {
171  for(unsigned int i = 0; i < inputRst->getNumberOfBands(); ++i)
172  {
173  m_ui->m_inputRasterBandsListWidget->addItem(QString::number(i));
174  }
175  }
176  }
177 }
178 
180 {
181  assert(m_display);
182 
183  m_display->getDraftPixmap()->fill(QColor(0, 0, 0, 0));
184 
185  const te::gm::Envelope& mapExt = m_display->getExtent();
186 
187  te::qt::widgets::Canvas canvasInstance(m_display->getDraftPixmap());
188  canvasInstance.setWindow(mapExt.m_llx, mapExt.m_lly, mapExt.m_urx, mapExt.m_ury);
189 
190  canvasInstance.setPolygonContourColor(te::color::RGBAColor(0,0,0, TE_OPAQUE));
191  canvasInstance.setPolygonContourWidth(1);
192  canvasInstance.setPolygonFillColor(te::color::RGBAColor(255,0,0, TE_OPAQUE));
193 
194  std::map<std::string, ClassifierSamples>::iterator it = m_samples.begin();
195 
196  while(it != m_samples.end())
197  {
198  te::gm::Polygon* poly = it->second.m_poly;
199 
200  canvasInstance.draw(poly);
201 
202  ++it;
203  }
204 
205  m_display->repaint();
206 }
207 
209 {
210  m_ui->m_tableWidget->setRowCount(0);
211 
212  std::map<std::string, ClassifierSamples>::iterator it = m_samples.begin();
213 
214  while(it != m_samples.end())
215  {
216  int newrow = m_ui->m_tableWidget->rowCount();
217  m_ui->m_tableWidget->insertRow(newrow);
218 
219  //name
220  QTableWidgetItem* itemName = new QTableWidgetItem(QString::fromStdString(it->second.m_name));
221  itemName->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
222  itemName->setData(Qt::UserRole, QVariant(it->second.m_id.c_str()));
223  m_ui->m_tableWidget->setItem(newrow, 0, itemName);
224 
225  ++it;
226  }
227 
228  m_ui->m_tableWidget->sortByColumn(0, Qt::AscendingOrder);
229 
230  drawSamples();
231 }
232 
234 {
235  if(m_samples.empty() == false)
236  drawSamples();
237 }
238 
240 {
241  assert(m_layer.get());
242 
243  m_display = map;
244 
245  //component id
246  static boost::uuids::basic_random_generator<boost::mt19937> gen;
247  boost::uuids::uuid u = gen();
248  std::string id = boost::uuids::to_string(u);
249 
250  //component name
251  QString className = QString(tr("Sample ") + QString::number(m_countSamples++));
252 
254  cs.m_id = id;
255  cs.m_name = className.toStdString();
256  cs.m_poly = poly;
257 
258  m_samples.insert(std::map<std::string, ClassifierSamples >::value_type(id, cs));
259 
260  updateSamples();
261 }
262 
264 {
265  std::string id = item->data(Qt::UserRole).toString().toStdString();
266 
267  std::string name = item->text().toStdString();
268 
269  std::map<std::string, ClassifierSamples >::iterator it = m_samples.find(id);
270 
271  bool update = false;
272 
273  if(it != m_samples.end())
274  {
275  if(it->second.m_name != name)
276  {
277  it->second.m_name = name;
278  update = true;
279  }
280  }
281 
282  if(update)
283  updateSamples();
284 }
285 
287 {
288  if(m_ui->m_tableWidget->currentRow() == -1)
289  return;
290 
291  std::string id = m_ui->m_tableWidget->item(m_ui->m_tableWidget->currentRow(), 0)->data(Qt::UserRole).toString().toStdString();
292 
293  std::map<std::string, ClassifierSamples >::iterator it = m_samples.find(id);
294 
295  if(it != m_samples.end())
296  {
297  m_samples.erase(it);
298 
299  if(m_samples.empty())
300  m_countSamples = 0;
301 
302  updateSamples();
303  }
304 }
305 
std::vector< te::gm::Polygon * > m_inputPolygons
The polygons to be classified when using object-based image analysis (OBIA).
Definition: Classifier.h:115
double m_acceptanceThreshold
The acceptance threshold (the closer to 100%, few clusters are created).
std::auto_ptr< Ui::ClassifierWizardPageForm > m_ui
te::rp::Classifier::OutputParameters getOutputParams()
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
te::rp::Classifier::InputParameters getInputParams()
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: Classifier.h:114
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
Classifier output parameters.
Definition: Classifier.h:127
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
void onItemChanged(QTableWidgetItem *item)
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
A canvas built on top of Qt.
Definition: Canvas.h:54
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:428
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
A widget to control the display of a set of layers.
Definition: MapDisplay.h:65
This file defines a class for a Classifier Wizard page.
void set(te::map::AbstractLayerPtr layer)
This method is used to set the selected layer for classifier operation.
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
std::string m_strategyName
The classifier strategy name see each te::rp::ClassifierStrategyFactory inherited classes documentati...
Definition: Classifier.h:116
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void setWindow(const double &llx, const double &lly, const double &urx, const double &ury)
It sets the world (or window) coordinates area (supposing a cartesian reference system).
Definition: Canvas.cpp:147
Classifier input parameters.
Definition: Classifier.h:74
void onGeomAquired(te::gm::Polygon *poly, te::qt::widgets::MapDisplay *map)
void setClassifierStrategyParams(const StrategyParameters &p)
Set specific classifier strategy parameters.
Definition: Classifier.cpp:60