All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MixtureModelWizardPage.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/MixtureModelWizardPage.cpp
22 
23  \brief This file defines a class for a Mixture Model Wizard page.
24 */
25 
26 // TerraLib
27 #include "../../../common/STLUtils.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/utils/Utils.h"
30 #include "../../../geometry/Point.h"
31 #include "../../../maptools/MarkRendererManager.h"
32 #include "../../../raster/Raster.h"
33 #include "../../../rp/MixtureModel.h"
34 #include "../../../rp/MixtureModelLinearStrategy.h"
35 #include "../../../rp/MixtureModelPCAStrategy.h"
36 #include "../../../rp/Functions.h"
37 #include "../../../se/Fill.h"
38 #include "../../../se/Mark.h"
39 #include "../../../se/Stroke.h"
40 #include "../../../se/Utils.h"
41 #include "../../widgets/canvas/Canvas.h"
42 #include "../../widgets/canvas/MapDisplay.h"
43 #include "MixtureModelWizardPage.h"
44 #include "MapDisplay.h"
45 #include "RasterNavigatorWidget.h"
46 #include "RasterNavigatorDialog.h"
47 #include "ui_MixtureModelWizardPageForm.h"
48 
49 // Qt
50 #include <QtGui/QGridLayout>
51 
52 // Boost
53 #include <boost/uuid/random_generator.hpp>
54 #include <boost/uuid/uuid_io.hpp>
55 
56 //STL
57 #include <memory>
58 
59 #define PATTERN_SIZE 12
60 
62  : QWizardPage(parent),
63  m_ui(new Ui::MixtureModelWizardPageForm),
64  m_countComponents(0),
65  m_layer(0)
66 {
67 // setup controls
68  m_ui->setupUi(this);
69 
71 
72 //define mark
73  te::se::Stroke* stroke = te::se::CreateStroke("#000000", "1");
74  te::se::Fill* fill = te::se::CreateFill("#FF0000", "1.0");
75  m_mark = te::se::CreateMark("cross", stroke, fill);
76 
78 
79 //connects
80  connect(m_ui->m_tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(onItemChanged(QTableWidgetItem*)));
81  connect(m_ui->m_removeToolButton, SIGNAL(clicked()), this, SLOT(onRemoveToolButtonClicked()));
82  connect(m_ui->m_acquireToolButton, SIGNAL(toggled(bool)), this, SLOT(showNavigator(bool)));
83 
84 
85  //configure raster navigator
86  m_navigatorDlg.reset(new te::qt::widgets::RasterNavigatorDialog(this, Qt::Tool));
87  m_navigatorDlg->setWindowTitle(tr("Display"));
88  m_navigatorDlg->setMinimumSize(550, 400);
89  m_navigatorDlg->getWidget()->hideGeomTool(true);
90  m_navigatorDlg->getWidget()->hideInfoTool(true);
91 
92  //connects
93  connect(m_navigatorDlg.get(), SIGNAL(navigatorClosed()), this, SLOT(onNavigatorClosed()));
94  connect(m_navigatorDlg->getWidget(), SIGNAL(mapDisplayExtentChanged()), this, SLOT(onMapDisplayExtentChanged()));
95  connect(m_navigatorDlg->getWidget(), SIGNAL(pointPicked(double, double, te::qt::widgets::MapDisplay*)),
96  this, SLOT(onPointPicked(double, double, te::qt::widgets::MapDisplay*)));
97 
98 // configure page
99  this->setTitle(tr("Mixture Model"));
100  this->setSubTitle(tr("Select the type of mixture model and set their specific parameters."));
101 
102  m_ui->m_removeToolButton->setIcon(QIcon::fromTheme("list-remove"));
103  m_ui->m_acquireToolButton->setIcon(QIcon::fromTheme("wand"));
104 }
105 
107 {
108  m_components.clear();
109 
110  te::common::Free(m_rgbaMark, PATTERN_SIZE);
111 
112  delete m_mark;
113 }
114 
116 {
117  if(m_ui->m_tableWidget->rowCount() == 0)
118  return false;
119 
120  int nBands = m_ui->m_bandTableWidget->rowCount();
121 
122  bool isChecked = false;
123  for(int i = 0; i < nBands; ++i)
124  {
125  QCheckBox* checkBox = (QCheckBox*)m_ui->m_bandTableWidget->cellWidget(i, 0);
126 
127  if(checkBox->isChecked())
128  {
129  isChecked = true;
130  break;
131  }
132  }
133 
134  if(!isChecked)
135  return false;
136 
137  return true;
138 }
139 
141 {
142  m_layer = layer;
143 
144  m_navigatorDlg->set(m_layer);
145 
146  listBands();
147 }
148 
150 {
151  return m_layer;
152 }
153 
155 {
156  int idx = m_ui->m_typeComboBox->currentIndex();
157  int type = m_ui->m_typeComboBox->itemData(idx).toInt();
158 
160 
161  if(type == MIXMODEL_LINEAR)
162  {
163  algoInputParams.m_strategyName = "linear";
164 
166  algoInputParams.setMixtureModelStrategyParams(specificParameters);
167  }
168  else if(type == MIXMODEL_PCA)
169  {
170  algoInputParams.m_strategyName = "pca";
171 
173  algoInputParams.setMixtureModelStrategyParams(specificParameters);
174  }
175 
176 // insert selected bands
177  unsigned selectedBands = 0;
178  std::vector<bool> selectedBandsVector;
179 
180  for (int i = 0; i < m_ui->m_bandTableWidget->rowCount(); ++i)
181  {
182  QCheckBox* bandCheckBox = (QCheckBox*) m_ui->m_bandTableWidget->cellWidget(i, 0);
183 
184  if (bandCheckBox->isChecked())
185  {
186  selectedBandsVector.push_back(true);
187  selectedBands++;
188 
189  algoInputParams.m_inputRasterBands.push_back(i);
190 
191  QComboBox *sensorComboBox = (QComboBox*) m_ui->m_bandTableWidget->cellWidget(i, 1);
192  algoInputParams.m_inputSensorBands.push_back(std::string(sensorComboBox->currentText().toStdString()));
193  }
194  else
195  selectedBandsVector.push_back(false);
196  }
197 
198 // insert mixture components
199  std::map<std::string, MixModelComponent>::iterator it = m_components.begin();
200  std::vector<double> components;
201 
202  while(it != m_components.end())
203  {
204  components.clear();
205 
206  for (unsigned int i = 0; i < selectedBandsVector.size(); i++)
207  if (selectedBandsVector[i])
208  components.push_back(it->second.m_values[i]);
209 
210  algoInputParams.m_components[it->second.m_name] = components;
211 
212  ++it;
213  }
214 
215 
216  return algoInputParams;
217 }
218 
220 {
222 
223  algoOutputParams.m_normalizeOutput = m_ui->m_normalizeOutputCheckBox->isChecked();
224  algoOutputParams.m_createErrorRaster = m_ui->m_createErrorRasterCheckBox->isChecked();
225 
226  return algoOutputParams;
227 }
228 
230 {
231  if(m_components.empty() == false)
232  drawMarks();
233 }
234 
236 {
237  assert(m_layer.get());
238 
239  m_display = map;
240 
241  //get input raster
242  std::auto_ptr<te::da::DataSet> ds = m_layer->getData();
243 
244  if(ds.get())
245  {
246  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
247  std::auto_ptr<te::rst::Raster> inputRst = ds->getRaster(rpos);
248 
249  if(inputRst.get())
250  {
251  te::gm::Coord2D pixelLocation = inputRst->getGrid()->geoToGrid(x, y);
252 
253  int currentColumn = pixelLocation.x;
254  int currentRow = pixelLocation.y;
255 
256  if (currentColumn < 0 || currentColumn >= (int) inputRst->getNumberOfColumns())
257  return;
258  if (currentRow < 0 || currentRow >= (int) inputRst->getNumberOfRows())
259  return;
260 
261  //component id
262  static boost::uuids::basic_random_generator<boost::mt19937> gen;
263  boost::uuids::uuid u = gen();
264  std::string id = boost::uuids::to_string(u);
265 
266  //component name
267  QString className = QString(tr("Component ") + QString::number(m_countComponents++));
268 
269  //component values
270  std::vector<double> componentsVector;
271 
272  double value;
273  for(unsigned b = 0 ; b < inputRst->getNumberOfBands(); b++)
274  {
275  inputRst->getValue(currentColumn, currentRow, value, b);
276 
277  componentsVector.push_back(value);
278  }
279 
280  //component coordinate
281  te::gm::Coord2D coordinateGrid(currentColumn, currentRow);
282  te::gm::Coord2D coordinateGeo(x, y);
283 
284  MixModelComponent mmc;
285  mmc.m_id = id;
286  mmc.m_name = className.toStdString();
287  mmc.m_values = componentsVector;
288  mmc.m_coordGrid = coordinateGrid;
289  mmc.m_coordGeo = coordinateGeo;
290 
291  m_components.insert(std::map<std::string, MixModelComponent >::value_type(id, mmc));
292 
293  updateComponents();
294  }
295  }
296 }
297 
299 {
300  std::string id = item->data(Qt::UserRole).toString().toStdString();
301 
302  std::string name = item->text().toStdString();
303 
304  std::map<std::string, MixModelComponent >::iterator it = m_components.find(id);
305 
306  bool update = false;
307 
308  if(it != m_components.end())
309  {
310  if(it->second.m_name != name)
311  {
312  it->second.m_name = name;
313  update = true;
314  }
315  }
316 
317  if(update)
318  updateComponents();
319 }
320 
322 {
323  if(m_ui->m_tableWidget->currentRow() == -1)
324  return;
325 
326  std::string id = m_ui->m_tableWidget->item(m_ui->m_tableWidget->currentRow(), 0)->data(Qt::UserRole).toString().toStdString();
327 
328  std::map<std::string, MixModelComponent >::iterator it = m_components.find(id);
329 
330  if(it != m_components.end())
331  {
332  m_components.erase(it);
333 
334  if(m_components.empty())
335  m_countComponents = 0;
336 
337  updateComponents();
338  }
339 }
340 
342 {
343  if(show)
344  m_navigatorDlg->show();
345  else
346  m_navigatorDlg->hide();
347 }
348 
350 {
351  m_ui->m_acquireToolButton->setChecked(false);
352 }
353 
355 {
356  m_ui->m_typeComboBox->clear();
357 
358  m_ui->m_typeComboBox->addItem(tr("Linear"), MIXMODEL_LINEAR);
359  m_ui->m_typeComboBox->addItem(tr("PCA - Principal Component Analysis"), MIXMODEL_PCA);
360 }
361 
363 {
364  assert(m_layer.get());
365 
366  //get input raster
367  std::auto_ptr<te::da::DataSet> ds = m_layer->getData();
368 
369  if(ds.get())
370  {
371  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
372  std::auto_ptr<te::rst::Raster> inputRst = ds->getRaster(rpos);
373 
374  if(inputRst.get())
375  {
376  // define sensor information
377  QStringList sensorsDescriptions;
378  std::vector<std::string> bandNames = te::rp::GetBandNames();
379 
380  for(unsigned int i = 0; i < bandNames.size(); i++)
381  sensorsDescriptions.append(bandNames[i].c_str());
382 
383  m_ui->m_bandTableWidget->setRowCount(0);
384 
385  // initializing the list of bands
386  for(unsigned b = 0 ; b < inputRst->getNumberOfBands(); b++)
387  {
388  int newrow = m_ui->m_bandTableWidget->rowCount();
389  m_ui->m_bandTableWidget->insertRow(newrow);
390 
391  QString bName(tr("Band "));
392  bName.append(QString::number(b));
393 
394  QCheckBox* bandCheckBox = new QCheckBox(bName, this);
395 
396  //if(inputRst->getNumberOfBands() == 1)
397  bandCheckBox->setChecked(true);
398 
399  QComboBox* sensorDescriptionComboBox = new QComboBox(this);
400  sensorDescriptionComboBox->addItems(sensorsDescriptions);
401 
402  m_ui->m_bandTableWidget->setCellWidget(newrow, 0, bandCheckBox);
403  m_ui->m_bandTableWidget->setCellWidget(newrow, 1, sensorDescriptionComboBox);
404  }
405 
406  m_ui->m_bandTableWidget->resizeColumnToContents(0);
407  }
408  }
409 }
410 
412 {
413  assert(m_display);
414 
415  m_display->getDraftPixmap()->fill(QColor(0, 0, 0, 0));
416 
417  const te::gm::Envelope& mapExt = m_display->getExtent();
418 
419  te::qt::widgets::Canvas canvasInstance(m_display->getDraftPixmap());
420  canvasInstance.setWindow(mapExt.m_llx, mapExt.m_lly, mapExt.m_urx, mapExt.m_ury);
421 
422  canvasInstance.setPointPattern(m_rgbaMark, PATTERN_SIZE, PATTERN_SIZE);
423 
424  std::map<std::string, MixModelComponent>::iterator it = m_components.begin();
425 
426  while(it != m_components.end())
427  {
428  te::gm::Coord2D cGeo = it->second.m_coordGeo;
429 
430  te::gm::Point point;
431  point.setX(cGeo.x);
432  point.setY(cGeo.y);
433 
434  canvasInstance.draw(&point);
435 
436  ++it;
437  }
438 
439  m_display->repaint();
440 }
441 
443 {
444  m_ui->m_tableWidget->setRowCount(0);
445 
446  std::map<std::string, MixModelComponent>::iterator it = m_components.begin();
447 
448  while(it != m_components.end())
449  {
450  int newrow = m_ui->m_tableWidget->rowCount();
451  m_ui->m_tableWidget->insertRow(newrow);
452 
453  //name
454  QTableWidgetItem* itemName = new QTableWidgetItem(QString::fromStdString(it->second.m_name));
455  itemName->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
456  itemName->setData(Qt::UserRole, QVariant(it->second.m_id.c_str()));
457  m_ui->m_tableWidget->setItem(newrow, 0, itemName);
458 
459  //values
460  QComboBox* combo = new QComboBox(m_ui->m_tableWidget);
461 
462  for(size_t t = 0; t < it->second.m_values.size(); ++t)
463  {
464  combo->addItem(QString::number(it->second.m_values[t]));
465  }
466 
467  m_ui->m_tableWidget->setCellWidget(newrow, 1, combo);
468 
469  ////coord x
470  //QTableWidgetItem* itemCoordX = new QTableWidgetItem(QString::number(it->second.m_coordGeo.x));
471  //itemCoordX->setFlags(Qt::ItemIsEnabled);
472  //m_ui->m_tableWidget->setItem(newrow, 2, itemCoordX);
473 
474  ////coord y
475  //QTableWidgetItem* itemCoordY = new QTableWidgetItem(QString::number(it->second.m_coordGeo.y));
476  //itemCoordY->setFlags(Qt::ItemIsEnabled);
477  //m_ui->m_tableWidget->setItem(newrow, 3, itemCoordY);
478 
479  ////coord x
480  //QTableWidgetItem* itemCoordC = new QTableWidgetItem(QString::number(it->second.m_coordGrid.x));
481  //itemCoordC->setFlags(Qt::ItemIsEnabled);
482  //m_ui->m_tableWidget->setItem(newrow, 4, itemCoordC);
483 
484  ////coord y
485  //QTableWidgetItem* itemCoordL = new QTableWidgetItem(QString::number(it->second.m_coordGrid.y));
486  //itemCoordL->setFlags(Qt::ItemIsEnabled);
487  //m_ui->m_tableWidget->setItem(newrow, 5, itemCoordL);
488 
489  ++it;
490  }
491 
492  m_ui->m_tableWidget->sortByColumn(0, Qt::AscendingOrder);
493 
494  drawMarks();
495 
496  emit completeChanged();
497 }
std::map< std::string, std::vector< double > > m_components
A set of endmembers and its radiances.
Definition: MixtureModel.h:111
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.
Definition: Utils.cpp:107
void setX(const double &x)
It sets the Point x-coordinate value.
Definition: Point.h:143
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
This file has the RasterNavigatorWidget class.
bool m_createErrorRaster
A flag to indicate that output raster will include the error bands.
Definition: MixtureModel.h:154
void setY(const double &y)
It sets the Point y-coordinate value.
Definition: Point.h:157
double y
y-coordinate.
Definition: Coord2D.h:87
A Qt4 widget to control the display of a set of layers.
te::rp::MixtureModel::InputParameters getInputParams()
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
Definition: Utils.cpp:128
std::vector< std::string > m_inputSensorBands
The names of the sensor/bands.
Definition: MixtureModel.h:110
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
std::auto_ptr< Ui::MixtureModelWizardPageForm > m_ui
bool m_normalizeOutput
A flag to indicate that output raster will be normalized, by default [0, 255].
Definition: MixtureModel.h:153
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
Definition: Utils.cpp:52
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
std::string m_strategyName
The mixture model strategy name see each te::rp::MixtureModelStrategyFactory inherited classes docume...
Definition: MixtureModel.h:112
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
void setMixtureModelStrategyParams(const StrategyParameters &p)
Set specific mixture model strategy parameters.
A canvas built on top of Qt.
Definition: Canvas.h:54
std::vector< std::string > GetBandNames()
Returns a vector os with band&#39;s names.
Definition: Functions.cpp:511
A point with x and y coordinate values.
Definition: Point.h:50
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:428
#define PATTERN_SIZE
This file defines a class for a MixtureModel Wizard page.
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 Raster Navigator Dialog.
MixtureModel output parameters.
Definition: MixtureModel.h:123
This class is a dialog for the RasterNavigator widget.
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:131
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
MixtureModel input parameters.
Definition: MixtureModel.h:69
te::rp::MixtureModel::OutputParameters getOutputParams()
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void onPointPicked(double x, double y, te::qt::widgets::MapDisplay *map)
static MarkRendererManager & getInstance()
It returns a reference to the singleton instance.
double x
x-coordinate.
Definition: Coord2D.h:86
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
std::auto_ptr< te::qt::widgets::RasterNavigatorDialog > m_navigatorDlg
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: MixtureModel.h:109
void set(te::map::AbstractLayerPtr layer)
This method is used to set the selected layer for mixture model operation.