All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MixtureModelDialog.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/rp/widgets/se/MixtureModelDialog.cpp
22 
23  \brief A dialog used to execute mixture model decomposition.
24 */
25 
26 // TerraLib
27 #include "../canvas/Canvas.h"
28 #include "../canvas/MapDisplay.h"
29 #include "../../../common/StringUtils.h"
30 #include "../../../geometry/Coord2D.h"
31 #include "../../../geometry/Envelope.h"
32 #include "../../../geometry/Point.h"
33 #include "../../../maptools/Utils.h"
34 #include "../../../raster/Grid.h"
35 #include "../../../rp/MixtureModel.h"
36 #include "../../../rp/MixtureModelLinearStrategy.h"
37 #include "../../../rp/MixtureModelPCAStrategy.h"
38 #include "../../../rp/Functions.h"
39 #include "../../widgets/tools/CoordTracking.h"
40 #include "../../widgets/tools/Pan.h"
41 #include "../../widgets/tools/ZoomWheel.h"
42 
43 // GUI
44 #include "MixtureModelDialog.h"
45 #include <ui_MixtureModelForm.h>
46 
47 // QT
48 #include <QtGui/QCheckBox>
49 #include <QtGui/QComboBox>
50 #include <QtGui/QGridLayout>
51 #include <QtGui/QImage>
52 #include <QtGui/QKeyEvent>
53 #include <QtGui/QLineEdit>
54 #include <QtGui/QMessageBox>
55 
56 // defines
57 #define PATTERN_W 11
58 
60  : QObject(parent), m_mDisplay(parent)
61 {
62 }
63 
65 {
66 }
67 
69 {
70  if(event->type() == QEvent::Enter)
71  m_mDisplay->grabKeyboard();
72  else if(event->type() == QEvent::Leave)
73  m_mDisplay->releaseKeyboard();
74  else if(event->type() == QEvent::KeyPress)
75  {
76  if(m_mDisplay->underMouse())
77  emit(keyPressedOverMapDisplay(((QKeyEvent*)event)->key()));
78  }
79 
80  return false;
81 }
82 
83 te::qt::widgets::MixtureModelDialog::MixtureModelDialog(const te::map::DataSetLayerPtr& inputLayerPtr, const std::string& outpuRasterDSType,
84  const std::map<std::string, std::string>& outpuRasterInfo, QWidget* parent, Qt::WindowFlags f)
85  : QDialog(parent, f),
86  m_inputRasterPtr(te::map::GetRaster(inputLayerPtr.get())),
87  m_outpuRasterDSType(outpuRasterDSType),
88  m_outpuRasterInfo(outpuRasterInfo),
89  m_maxComponentsInserted(0)
90 {
91  m_uiPtr = new Ui::MixtureModelForm;
92 
93  m_uiPtr->setupUi(this);
94 
95 // insert image in frame
96  QGridLayout* rasterGridLayout = new QGridLayout(m_uiPtr->m_rasterFrame);
97  m_mapDisplay = new te::qt::widgets::MapDisplay(m_uiPtr->m_rasterFrame->size(), m_uiPtr->m_rasterFrame);
98  m_mapDisplay->setMouseTracking(true);
99  rasterGridLayout->addWidget(m_mapDisplay);
101  std::list<te::map::AbstractLayerPtr> layerList;
102  layerList.push_back(inputLayerPtr);
103  m_mapDisplay->setLayerList(layerList);
105  te::gm::Envelope displayExtent(*(m_inputRasterPtr->getExtent()));
106  m_mapDisplay->setExtent(displayExtent);
107 
108 // image frame events
109  m_panClickEvent = new te::qt::widgets::Pan(m_mapDisplay, Qt::OpenHandCursor, Qt::ClosedHandCursor);
113 
114  m_mapDisplay->installEventFilter(m_coordTracking);
115  m_mapDisplay->installEventFilter(m_keyboardPressTracking);
116  m_mapDisplay->installEventFilter(m_panClickEvent);
117  m_mapDisplay->installEventFilter(m_zoomScroolEvent);
118 
119 // signals & slots
120  connect(m_uiPtr->m_okPushButton, SIGNAL(clicked()), SLOT(on_okPushButton_clicked()));
121  connect(m_coordTracking, SIGNAL(coordTracked(QPointF&)), this, SLOT(on_coordTracked_changed(QPointF&)));
122  connect(m_keyboardPressTracking, SIGNAL(keyPressedOverMapDisplay(int)), this, SLOT(on_keyPressedOverMapDisplay(int)));
123  connect(m_mapDisplay, SIGNAL(extentChanged()), this, SLOT(on_mapDisplay_extentChanged()));
124 
125 // define sensor information
126  QStringList sensorsDescriptions;
127  std::vector<std::string> bandNames = te::rp::GetBandNames();
128  for(unsigned int i = 0; i < bandNames.size(); i++)
129  sensorsDescriptions.append(bandNames[i].c_str());
130 
131 // initializing the list of bands
132  QGridLayout *bandsListLayout = new QGridLayout();
133  for(unsigned b = 0 ; b < m_inputRasterPtr->getNumberOfBands(); b++)
134  {
135  std::string bname = "Band " + te::common::Convert2String(b);
136  QCheckBox* bandCheckBox = new QCheckBox(tr(bname.c_str()));
137 
138  QComboBox* sensorDescriptionComboBox = new QComboBox();
139  sensorDescriptionComboBox->addItems(sensorsDescriptions);
140 
141  bandsListLayout->addWidget(bandCheckBox, b, 0);
142  bandsListLayout->addWidget(sensorDescriptionComboBox, b, 1);
143  }
144  m_uiPtr->m_bandsListGroupBox->setLayout(bandsListLayout);
145 
146 // initializing the list of components
147  QGridLayout *componentsListLayout = new QGridLayout();
148  m_uiPtr->m_componentsListGroupBox->setLayout(componentsListLayout);
149 
150 // creating "x" pattern to plot on map
152 
153  for(unsigned int line = 0; line < PATTERN_W; line++)
154  {
156 
157  for(unsigned int col = 0; col < PATTERN_W; col++)
158  {
159  if((line == col) || (line == (PATTERN_W - col - 1)))
160  m_selectedPointPattern[line][col].setColor(255, 0, 0, TE_OPAQUE);
161  else
162  m_selectedPointPattern[line][col].setColor(0, 0, 0, TE_TRANSPARENT);
163  }
164  }
165 }
166 
168 {
169 // delete "x" patterns
170  for(unsigned int line = 0; line < PATTERN_W; line++)
171  delete[] m_selectedPointPattern[line];
172 
173  delete[] m_selectedPointPattern;
174 
175 // delete interface
176  delete m_uiPtr;
177 }
178 
179 bool te::qt::widgets::MixtureModelDialog::getOutputRaster(boost::shared_ptr<te::rst::Raster>& outputRasterPtr)
180 {
181  if(m_outputRasterPtr.get())
182  {
183  outputRasterPtr = m_outputRasterPtr;
184 
185  return true;
186  }
187 
188  return false;
189 }
190 
192 {
193  m_outputRasterPtr.reset();
194 
195  if (!m_inputRasterPtr)
196  {
197  QMessageBox::critical(this, "", tr("Invalid input raster"));
198 
199  return;
200  }
201 
202 // define classification parameters
203 
204 // input parameters
205  te::rp::MixtureModel::InputParameters algoInputParameters;
206  algoInputParameters.m_strategyName = (m_uiPtr->m_mmTypeComboBox->currentText() == "Linear" ? "linear" : "pca");
207  algoInputParameters.m_inputRasterPtr = m_inputRasterPtr;
208 
209  unsigned selectedBands = 0;
210  std::vector<bool> selectedBandsVector;
211  for (int i = 0; i < (m_uiPtr->m_bandsListGroupBox->layout()->count() - 1); i += 2)
212  {
213  QCheckBox* bandCheckBox = (QCheckBox*) m_uiPtr->m_bandsListGroupBox->layout()->itemAt(i)->widget();
214 
215  if (bandCheckBox->isChecked())
216  {
217  selectedBandsVector.push_back(true);
218  selectedBands++;
219 
220 // insert band number
221  algoInputParameters.m_inputRasterBands.push_back(i / 2);
222 
223 // insert sensor description of band
224  QComboBox *sensorComboBox = (QComboBox*) m_uiPtr->m_bandsListGroupBox->layout()->itemAt(i + 1)->widget();
225  algoInputParameters.m_inputSensorBands.push_back(std::string(sensorComboBox->currentText().toAscii()));
226  }
227  else
228  selectedBandsVector.push_back(false);
229  }
230 
231 // insert mixture components
232  std::map<std::string, std::vector<double> >::const_iterator cit = m_components.begin();
233  std::vector<double> components;
234  while (cit != m_components.end())
235  {
236  components.clear();
237 
238  for (unsigned int i = 0; i < selectedBandsVector.size(); i++)
239  if (selectedBandsVector[i])
240  components.push_back(cit->second[i]);
241 
242  algoInputParameters.m_components[cit->first] = components;
243 
244  ++cit;
245  }
246 
247 // link specific parameters with chosen implementation
248  if (m_uiPtr->m_mmTypeComboBox->currentText() == "Linear")
249  {
251  algoInputParameters.setMixtureModelStrategyParams(specificParameters);
252  }
253  else // "pca"
254  {
256  algoInputParameters.setMixtureModelStrategyParams(specificParameters);
257  }
258 
259 // output parameters
260  te::rp::MixtureModel::OutputParameters algoOutputParameters;
261  algoOutputParameters.m_rInfo = m_outpuRasterInfo;
262  algoOutputParameters.m_rType = m_outpuRasterDSType;
263  algoOutputParameters.m_normalizeOutput = m_uiPtr->m_normalizeOutputCheckBox->isChecked();
264  algoOutputParameters.m_createErrorRaster = m_uiPtr->m_createErrorRasterCheckBox->isChecked();
265 
266 // execute the algorithm
267  te::rp::MixtureModel MixtureModelinstance;
268 
269  if(!MixtureModelinstance.initialize(algoInputParameters))
270  {
271  QMessageBox::critical(this, "", tr("MixtureModel initialization error"));
272 
273  return;
274  }
275 
276  if(!MixtureModelinstance.execute(algoOutputParameters))
277  {
278  QMessageBox::critical(this, "", tr("MixtureModel execution error"));
279 
280  return;
281  }
282 
283  QMessageBox::information(this, "", tr("Mixture model decomposition ended sucessfully"));
284 }
285 
287 {
288 // find the selected pixel location for component
289  te::gm::Coord2D pixelLocation = m_inputRasterPtr->getGrid()->geoToGrid((double) coordinate.rx(), (double)coordinate.ry());
290  currentColumn = pixelLocation.x;
291  currentRow = pixelLocation.y;
292 }
293 
295 {
296 // check range
297  if (currentColumn < 0 || currentColumn >= (int) m_inputRasterPtr->getNumberOfColumns())
298  return;
299  if (currentRow < 0 || currentRow >= (int) m_inputRasterPtr->getNumberOfRows())
300  return;
301 
302  std::vector<double> componentsVector;
303 
304  QString* className = new QString("Change name " + QString::number(m_maxComponentsInserted++));
305 
306  double value;
307  for(unsigned b = 0 ; b < m_inputRasterPtr->getNumberOfBands(); b++)
308  {
309  m_inputRasterPtr->getValue(currentColumn, currentRow, value, b);
310 
311  componentsVector.push_back(value);
312  }
313 
314  m_components[className->toStdString()] = componentsVector;
315 
316  te::gm::Coord2D coordinate(currentColumn, currentRow);
317  m_coordinates[className->toStdString()] = coordinate;
318 
319  updateComponentsGrid();
320 }
321 
323 {
324  QPushButton* sender = (QPushButton*) QObject::sender();
325 
326 // retrieve component class name
327  std::string className = sender->statusTip().toStdString();
328 
329  m_components.erase(className);
330  m_coordinates.erase(className);
331 
332  updateComponentsGrid();
333 }
334 
336 {
337 // get canvas to draw the "x" on selected points
338  const te::gm::Envelope& mapDisplayExtent = m_mapDisplay->getExtent();
339 
340  m_mapDisplay->getDraftPixmap()->fill(QColor(0, 0, 0, 0));
341 
342  te::qt::widgets::Canvas canvasInstance(m_mapDisplay->getDraftPixmap());
343  canvasInstance.setWindow(mapDisplayExtent.m_llx, mapDisplayExtent.m_lly,
344  mapDisplayExtent.m_urx, mapDisplayExtent.m_ury);
345 
346  canvasInstance.setPointPattern(m_selectedPointPattern, PATTERN_W, PATTERN_W);
347 
348  te::gm::Point auxPoint;
349  te::gm::Coord2D auxCoord2D;
350 
351  std::map<std::string, te::gm::Coord2D>::const_iterator cpit = m_coordinates.begin();
352  while (cpit != m_coordinates.end())
353  {
354 // draw "x"
355  m_inputRasterPtr->getGrid()->gridToGeo(cpit->second.x, cpit->second.y,
356  auxCoord2D.x, auxCoord2D.y);
357 
358  auxPoint.setX(auxCoord2D.x);
359  auxPoint.setY(auxCoord2D.y);
360 
361  canvasInstance.draw(&auxPoint);
362 
363  ++cpit;
364  }
365 }
366 
368 {
369  QGridLayout* componentsListLayout = (QGridLayout*) m_uiPtr->m_componentsListGroupBox->layout();
370 
371 // update class names
372  for (int i = 0; i < (componentsListLayout->count() / 5); i++)
373  {
374  std::string className = ((QLineEdit*) componentsListLayout->itemAtPosition(i, 1)->widget())->text().toStdString();
375  std::string originalClassName = ((QLineEdit*) componentsListLayout->itemAtPosition(i, 1)->widget())->toolTip().toStdString();
376 
377 // check if className is already in m_components
378  if (className != originalClassName)
379  {
380  m_components[className] = m_components[originalClassName];
381  m_coordinates[className] = m_coordinates[originalClassName];
382 
383  m_components.erase(originalClassName);
384  m_coordinates.erase(originalClassName);
385  }
386 
387  }
388 
389 // clear layout
390  QLayoutItem* child;
391  while ((child = componentsListLayout->takeAt(0)) != 0)
392  {
393  delete child->widget();
394  delete child;
395  }
396 
397 // clear map display to redraw "x" points
398  te::gm::Envelope auxEnvelope(m_mapDisplay->getExtent());
399  m_mapDisplay->setExtent(auxEnvelope);
400 
401 // add lines
402  std::map<std::string, std::vector<double> >::const_iterator cit = m_components.begin();
403  unsigned int totalComponents = 0;
404  while (cit != m_components.end())
405  {
406 // define remove button and line edits
407  QPushButton* removeButton = new QPushButton("x");
408 
409  removeButton->setStatusTip(cit->first.c_str());
410  removeButton->setMaximumSize(20, 20);
411  connect(removeButton, SIGNAL(clicked()), SLOT(on_removeButton_clicked()));
412 
413  componentsListLayout->addWidget(removeButton, totalComponents, 0);
414 
415  QLineEdit* className = new QLineEdit(cit->first.c_str());
416  className->setToolTip(cit->first.c_str());
417  componentsListLayout->addWidget(className, totalComponents, 1);
418 
419  std::vector<double> components = cit->second;
420  for (unsigned int i = 0; i < components.size(); i++)
421  {
422  QLineEdit* currentPixel = new QLineEdit(QString::number(components[i]));
423 
424  currentPixel->setMaximumSize(50, currentPixel->maximumHeight());
425 
426  componentsListLayout->addWidget(currentPixel, totalComponents, i + 2);
427  }
428 
429  totalComponents++;
430  ++cit;
431  }
432 }
CoordTracking * m_coordTracking
Coordinate tracking for map display.
std::map< std::string, std::vector< double > > m_components
A set of endmembers and its radiances.
Definition: MixtureModel.h:111
te::qt::widgets::Pan * m_panClickEvent
Pan click event for map display.
void setX(const double &x)
It sets the Point x-coordinate value.
Definition: Point.h:143
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: MixtureModel.h:108
virtual void setResizePolicy(const ResizePolicy &policy)
Sets the resize policy to this map display.
Definition: MapDisplay.cpp:224
Raster decomposition using mixture model.
Definition: MixtureModel.h:59
MixtureModelDialogMDEventFilter * m_keyboardPressTracking
The event filter to detect when user press space bar.
te::color::RGBAColor ** m_selectedPointPattern
The display draw pattern used for selected points.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
A dialog used to execute mixture model decomposition.
MixtureModelDialogMDEventFilter(te::qt::widgets::MapDisplay *parent)
A event filter to handle map display events.
bool m_createErrorRaster
A flag to indicate that output raster will include the error bands.
Definition: MixtureModel.h:154
bool eventFilter(QObject *watched, QEvent *event)
void setY(const double &y)
It sets the Point y-coordinate value.
Definition: Point.h:157
double y
y-coordinate.
Definition: Coord2D.h:87
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
This class implements a concrete tool to geographic zoom operation using the mouse wheel...
Definition: ZoomWheel.h:49
te::qt::widgets::ZoomWheel * m_zoomScroolEvent
Zoom event using mouse scrool for map display.
This class implements a concrete tool to geographic pan operation.
Definition: Pan.h:49
std::vector< std::string > m_inputSensorBands
The names of the sensor/bands.
Definition: MixtureModel.h:110
virtual void setLayerList(const std::list< te::map::AbstractLayerPtr > &layers)
It sets the layer list to be showed in the Map Display.
Definition: MapDisplay.cpp:42
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
#define TE_TRANSPARENT
For an RGBA color this is the value of the alpha-channel for totally transparent. ...
Definition: Config.h:46
bool m_normalizeOutput
A flag to indicate that output raster will be normalized, by default [0, 255].
Definition: MixtureModel.h:153
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
Definition: Raster.cpp:104
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: MixtureModel.h:150
std::string m_strategyName
The mixture model strategy name see each te::rp::MixtureModelStrategyFactory inherited classes docume...
Definition: MixtureModel.h:112
int getSRID() const
Returns the raster spatial reference system identifier.
Definition: Raster.cpp:203
TEMAPEXPORT te::rst::Raster * GetRaster(DataSetLayer *layer)
It gets the raster referenced by the given data set layer.
Definition: Utils.cpp:264
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
void setColor(const std::string &hexColor)
It sets the color using a two hexadecimal RGB-encoded color.
Definition: RGBAColor.h:329
MixtureModelDialog(const te::map::DataSetLayerPtr &inputLayerPtr, const std::string &outpuRasterDSType, const std::map< std::string, std::string > &outpuRasterInfo, QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a basic dialog which is a child of parent, with widget flags set to f. ...
void setMixtureModelStrategyParams(const StrategyParameters &p)
Set specific mixture model strategy parameters.
virtual void setExtent(te::gm::Envelope &e, bool doRefresh=true)
It sets the world visible area and refreshes the contents in the map display.
Definition: MapDisplay.cpp:153
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
void on_coordTracked_changed(QPointF &coordinate)
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
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
boost::intrusive_ptr< DataSetLayer > DataSetLayerPtr
Definition: DataSetLayer.h:165
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:51
MixtureModel output parameters.
Definition: MixtureModel.h:123
Ui::MixtureModelForm * m_uiPtr
User interface.
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
const te::rst::Raster * m_inputRasterPtr
Input raster pointer.
virtual void setSRID(const int &srid, bool doRefresh=true)
It sets a new Spatial Reference System to be used by the Map Display.
Definition: MapDisplay.cpp:78
bool getOutputRaster(boost::shared_ptr< te::rst::Raster > &outputRasterPtr)
Returns the output result raster.
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
This class implements a concrete tool to geographic coordinate tracking on mouse move operation...
Definition: CoordTracking.h:52
MixtureModel input parameters.
Definition: MixtureModel.h:69
te::qt::widgets::MapDisplay * m_mapDisplay
The map display to show the input raster.
double x
x-coordinate.
Definition: Coord2D.h:86
#define PATTERN_W
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::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: MixtureModel.h:109
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.