All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GeostatisticalMethodsDialog.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/sa/qt/GeostatisticalMethodsDialog.cpp
22 
23  \brief A dialog with geostatistical methods to measure the spatial variability of attribute of a dataset.
24 */
25 
26 // TerraLib
27 #include "../../common/Logger.h"
28 #include "../../common/progress/ProgressManager.h"
29 #include "../../common/Translator.h"
30 #include "../../common/STLUtils.h"
31 #include "../../dataaccess/dataset/DataSet.h"
32 #include "../../dataaccess/utils/Utils.h"
33 #include "../../geometry/GeometryProperty.h"
34 #include "../core/GeostatisticalFunctions.h"
35 #include "../core/GeostatisticalMethodSemivariogram.h"
36 #include "../core/GeostatisticalModel.h"
37 #include "../core/GeostatisticalModelExponential.h"
38 #include "../core/GeostatisticalModelGaussian.h"
39 #include "../core/GeostatisticalModelSpherical.h"
40 #include "../core/Utils.h"
41 #include "../Enums.h"
42 #include "../Exception.h"
44 #include "ui_GeostatisticalMethodsDialogForm.h"
45 
46 // Qt
47 #include <QGridLayout>
48 #include <QMessageBox>
49 #include <QValidator>
50 
51 //QWT
52 #include <qwt_legend.h>
53 #include <qwt_plot.h>
54 #include <qwt_symbol.h>
55 
56 // STL
57 #include <memory>
58 
60 
62  : QDialog(parent, f),
63  m_ui(new Ui::GeostatisticalMethodsDialogForm),
64  m_method(0)
65 {
66 // add controls
67  m_ui->setupUi(this);
68 
69  m_ui->m_nLagsLineEdit->setValidator(new QIntValidator(this));
70  m_ui->m_lagsIncrementLineEdit->setValidator(new QDoubleValidator(this));
71  m_ui->m_angleDirLineEdit->setValidator(new QDoubleValidator(this));
72  m_ui->m_angleTolLineEdit->setValidator(new QDoubleValidator(this));
73 
74  m_ui->m_adjustGroupBox->setEnabled(false);
75  m_ui->m_changeAttrToolButton->setEnabled(false);
76 
78 
79 // add icons
80  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("sa-measurespatialvar-hint").pixmap(112,48));
81  m_ui->m_changeAttrToolButton->setIcon(QIcon::fromTheme("view-refresh"));
82 
83 // connectors
84  connect(m_ui->m_inputLayerComboBox, SIGNAL(activated(int)), this, SLOT(onInputLayerComboBoxActivated(int)));
85  connect(m_ui->m_changeAttrToolButton, SIGNAL(clicked()), this, SLOT(onChangeAttrToolButtonClicked()));
86  connect(m_ui->m_applyPushButton, SIGNAL(clicked()), this, SLOT(onApplyPushButtonClicked()));
87  connect(m_ui->m_modelComboBox, SIGNAL(activated(int)), this, SLOT(calculate()));
88  connect(m_ui->m_nuggetHorizontalSlider, SIGNAL(sliderMoved(int)), this, SLOT(calculate()));
89  connect(m_ui->m_sillHorizontalSlider, SIGNAL(sliderMoved(int)), this, SLOT(calculate()));
90  connect(m_ui->m_rangeHorizontalSlider, SIGNAL(sliderMoved(int)), this, SLOT(calculate()));
91 
92  //create chart
93  QGridLayout* chartLayout = new QGridLayout(m_ui->m_chartWidget);
95  chartLayout->addWidget(m_chartDisplay);
96 
97 
98  m_chartDisplay->setAxisTitle(QwtPlot::xBottom, tr("h"));
99  m_chartDisplay->setAxisTitle(QwtPlot::yLeft, tr("Y(h)"));
100 
101  //start plot objects
104  m_scatterChartMethod->setSymbol(new QwtSymbol( QwtSymbol::XCross, QBrush( Qt::red ), QPen( Qt::red, 3 ), QSize( 8, 8 )));
106  m_scatterChartMethod->setTitle(tr("Data"));
107 
110  m_scatterChartModel->setPen(Qt::blue);
111  m_scatterChartModel->setSymbol(0);
112  m_scatterChartModel->setStyle(QwtPlotCurve::Lines);
114  m_scatterChartModel->setTitle(tr("Model"));
115 
116  m_chartDisplay->insertLegend(new QwtLegend(), QwtPlot::RightLegend);
117 
118  // help info
119  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
120  m_ui->m_helpPushButton->setPageReference("plugins/sa/sa_geostatisticalmethods.html");
121 }
122 
124 {
125  delete m_method;
126 
127  m_methodMatrix.clear();
128 
129  m_modelMatrix.clear();
130 }
131 
132 void te::sa::GeostatisticalMethodsDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
133 {
134  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
135 
136  while(it != layers.end())
137  {
139 
140  if(l->isValid())
141  {
142  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
143 
144  m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
145  }
146 
147  ++it;
148  }
149 
150 // fill attributes combo
151  if(m_ui->m_inputLayerComboBox->count() > 0)
152  onInputLayerComboBoxActivated(0);
153 }
154 
156 {
157  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
158 
160 
161  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
162 
163  std::vector<te::dt::Property*> propVec = dsType->getProperties();
164 
165  m_ui->m_attributeComboBox->clear();
166 
167  for(std::size_t t = 0; t < propVec.size(); ++t)
168  {
169  int dataType = propVec[t]->getType();
170 
171  if (dataType == te::dt::INT16_TYPE || dataType == te::dt::UINT16_TYPE ||
172  dataType == te::dt::INT32_TYPE || dataType == te::dt::UINT32_TYPE ||
173  dataType == te::dt::INT64_TYPE || dataType == te::dt::UINT64_TYPE ||
174  dataType == te::dt::FLOAT_TYPE || dataType == te::dt::DOUBLE_TYPE)
175  {
176  m_ui->m_attributeComboBox->addItem(propVec[t]->getName().c_str(), dataType);
177  }
178  }
179 
180  //reset lag increment information
181  std::auto_ptr<te::da::DataSet> ds = l->getData();
182  double lagIncrement = l->getExtent().getWidth() / ds->size();
183 
184  QString strLagIncrement;
185  strLagIncrement.setNum(lagIncrement);
186 
187  m_ui->m_lagsIncrementLineEdit->setText(strLagIncrement);
188 }
189 
191 {
192  //disable adjust options
193  m_ui->m_adjustGroupBox->setEnabled(false);
194  m_ui->m_changeAttrToolButton->setEnabled(false);
195 
196  //check input interface parameters
197  if(m_ui->m_nLagsLineEdit->text().isEmpty())
198  {
199  QMessageBox::warning(this, tr("Warning"), tr("Number of lags not defined."));
200  return;
201  }
202 
203  if(m_ui->m_lagsIncrementLineEdit->text().isEmpty())
204  {
205  QMessageBox::warning(this, tr("Warning"), tr("Lag increment not defined."));
206  return;
207  }
208 
209  if(m_ui->m_angleDirLineEdit->text().isEmpty())
210  {
211  QMessageBox::warning(this, tr("Warning"), tr("Angular direction not defined."));
212  return;
213  }
214 
215  if(m_ui->m_angleTolLineEdit->text().isEmpty())
216  {
217  QMessageBox::warning(this, tr("Warning"), tr("Angular tolerance not defined."));
218  return;
219  }
220 
221  //get selected layer
222  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
224 
225  //get dataset and dataset type
226  std::auto_ptr<te::da::DataSet> dataSet = l->getData();
227  std::auto_ptr<te::da::DataSetType> dataSetType = l->getSchema();
228 
229  //get properties information
230  std::string attrName = m_ui->m_attributeComboBox->currentText().toStdString();
231  int attrIdx = te::da::GetPropertyIndex(dataSet.get(), attrName);
232 
233  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(dataSetType.get());
234  int geomIdx = te::da::GetPropertyIndex(dataSet.get(), geomProp->getName());
235 
236  //create geostatistical method
237  delete m_method;
238 
239  int typeIdx = m_ui->m_typeComboBox->currentIndex();
240  te::sa::GeostatisticalMethodType type = (te::sa::GeostatisticalMethodType)m_ui->m_typeComboBox->itemData(typeIdx).toInt();
241 
242  if(type == te::sa::Semivariogram)
243  {
245  }
246  else
247  {
248  m_method = 0;
249  return;
250  }
251 
252  //set geostatistical method parameters
253  m_method->setNumberOfLags((std::size_t)m_ui->m_nLagsLineEdit->text().toInt());
254  m_method->setLagIncrement(m_ui->m_lagsIncrementLineEdit->text().toDouble());
255  m_method->setAngleDirection(m_ui->m_angleDirLineEdit->text().toDouble());
256  m_method->setAngleTolerance(m_ui->m_angleTolLineEdit->text().toDouble());
257  m_method->setMatrix(te::sa::CreateMatrixFromDataSet(dataSet.get(), attrIdx, geomIdx));
258 
259  //calculate moments
260  double mean = 0.;
261  double variance = 0.;
262 
263  te::sa::CalculateMoments(m_method->getMatrix(), mean, variance);
264 
265  //reset adjust values
266  resetAdjustParameters(mean, variance);
267 
268  //run
269  calculate();
270 
271  //enable adjust options
272  m_ui->m_adjustGroupBox->setEnabled(true);
273  m_ui->m_changeAttrToolButton->setEnabled(true);
274 }
275 
277 {
278  //get selected layer
279  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
281 
282  //get dataset and dataset type
283  std::auto_ptr<te::da::DataSet> dataSet = l->getData();
284 
285  //get properties information
286  std::string attrName = m_ui->m_attributeComboBox->currentText().toStdString();
287  int attrIdx = te::da::GetPropertyIndex(dataSet.get(), attrName);
288 
289  te::sa::SetMainDiagonal(m_method->getMatrix(), dataSet.get(), attrIdx);
290 
291  //calculate moments
292  double mean = 0.;
293  double variance = 0.;
294 
295  te::sa::CalculateMoments(m_method->getMatrix(), mean, variance);
296 
297  //reset adjust values
298  resetAdjustParameters(mean, variance);
299 
300  //run
301  calculate();
302 }
303 
305 {
306  //create geostatistical model
307  int modelIdx = m_ui->m_modelComboBox->currentIndex();
308  te::sa::GeostatisticalModelType type = (te::sa::GeostatisticalModelType)m_ui->m_modelComboBox->itemData(modelIdx).toInt();
309 
310  te::sa::GeostatisticalModel* model = 0;
311 
312  if(type == te::sa::Spherical)
314  else if(type == te::sa::Exponential)
316  else if(type == te::sa::Gaussian)
318 
319  model->setNugget((double)m_ui->m_nuggetHorizontalSlider->value());
320  model->setSill((double)m_ui->m_sillHorizontalSlider->value());
321  model->setRange((double)m_ui->m_rangeHorizontalSlider->value());
322 
323  //generate output matrix
324  m_methodMatrix = m_method->calculate();
325  m_modelMatrix = model->calculate(m_methodMatrix);
326 
327  delete model;
328 
329  //plot
330  plot();
331 }
332 
334 {
335  //fill msv types
336  m_ui->m_typeComboBox->clear();
337 
338  m_ui->m_typeComboBox->addItem("Semivariogram", QVariant(te::sa::Semivariogram));
339 
340  //fill msv models
341  m_ui->m_modelComboBox->clear();
342 
343  m_ui->m_modelComboBox->addItem("Spherical", QVariant(te::sa::Spherical));
344  m_ui->m_modelComboBox->addItem("Exponential", QVariant(te::sa::Exponential));
345  m_ui->m_modelComboBox->addItem("Gaussian", QVariant(te::sa::Gaussian));
346 }
347 
349 {
350  //set nugget value
351  double min = 0.;
352  double max = variance;
353  double nuggetVar = (max - min)/mean;
354 
355  m_ui->m_nuggetHorizontalSlider->setRange(min - nuggetVar, max + nuggetVar);
356  m_ui->m_nuggetHorizontalSlider->setValue(min);
357  m_ui->m_nuggetLabel->setNum(min);
358 
359  //set sill value
360  min = 0;
361  max = max + variance;
362  double sillVar = (max - min)/mean;
363 
364  m_ui->m_sillHorizontalSlider->setRange(min - sillVar, max + sillVar);
365  m_ui->m_sillHorizontalSlider->setValue(min);
366  m_ui->m_sillLabel->setNum(min);
367 
368  //set range value
369  double rangeVar = m_ui->m_nLagsLineEdit->text().toDouble();
370  min = m_ui->m_lagsIncrementLineEdit->text().toDouble();
371  max = min * rangeVar;
372 
373  m_ui->m_rangeHorizontalSlider->setRange(min - rangeVar, max + rangeVar);
374  m_ui->m_rangeHorizontalSlider->setValue(min);
375  m_ui->m_rangeLabel->setNum(min);
376 }
377 
379 {
380  m_chartDisplay->setTitle(m_ui->m_typeComboBox->currentText());
381 
382  //plot method curve
383  std::vector<double> methodh;
384  std::vector<double> methodyh;
385 
386  for(std::size_t t = 1; t < m_methodMatrix.size1(); ++t)
387  {
388  methodh.push_back(m_methodMatrix(t, 0));
389  methodyh.push_back(m_methodMatrix(t, 1));
390  }
391 
392  m_scatterMethod->setXValues(methodh);
393  m_scatterMethod->setYValues(methodyh);
394  m_scatterMethod->calculateMinMaxValues();
395  m_scatterChartMethod->setData();
396 
397  //plot model curve
398  std::vector<double> modelh;
399  std::vector<double> modelyh;
400 
401  for(std::size_t t = 0; t < m_modelMatrix.size1(); ++t)
402  {
403  modelh.push_back(m_modelMatrix(t, 0));
404  modelyh.push_back(m_modelMatrix(t, 1));
405  }
406 
407  m_scatterModel->setXValues(modelh);
408  m_scatterModel->setYValues(modelyh);
409  m_scatterModel->calculateMinMaxValues();
410  m_scatterChartModel->setData();
411 
412  //replot the chart display
413  m_chartDisplay->replot();
414 }
void setSill(double value)
Function to set the sill value.
Class that represents the geostatistical exponential model.
te::qt::widgets::ScatterChart * m_scatterChartModel
Geometric property.
Class that represents the geostatistical spherical model.
The empirical variogram cannot be computed at every lag distance h and due to variation in the estima...
te::qt::widgets::ChartDisplay * m_chartDisplay
TESAEXPORT void SetMainDiagonal(boost::numeric::ublas::matrix< double > &matrix, te::da::DataSet *dataSet, int attrIdx)
Function used to set new values in the matrix main diagonal.
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
Definition: Utils.cpp:919
std::auto_ptr< Ui::GeostatisticalMethodsDialogForm > m_ui
TESAEXPORT boost::numeric::ublas::matrix< double > CreateMatrixFromDataSet(te::da::DataSet *dataSet, int attrIdx, int geomIdx)
Function used to create a matrix with values, distance and angle for each element from dataset...
Class that represents the geostatistical gaussian model.
TESAEXPORT void CalculateMoments(const boost::numeric::ublas::matrix< double > &matrix, double &mean, double &variance)
Function used to calculate mean and variance from a matrix.
A class to represent a scatter.
Definition: Scatter.h:51
Semivariogram is a function describing the degree of spatial dependence of a spatial random field...
A dialog with geostatistical methods to measure the spatial variability of attribute of a dataset...
A class to represent a chart display.
Definition: ChartDisplay.h:65
virtual boost::numeric::ublas::matrix< double > calculate(boost::numeric::ublas::matrix< double > matrix)=0
Function to calculate the geostatistical information from model.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
A class to represent a scatter chart.
Definition: ScatterChart.h:55
GeostatisticalModelType
Geostatistical models for measure of Spatial Variability.
Definition: Enums.h:136
GeostatisticalMethodType
Geostatistical methods for measure of Spatial Variability.
Definition: Enums.h:126
GeostatisticalMethodsDialog(QWidget *parent=0, Qt::WindowFlags f=0)
void resetAdjustParameters(double mean, double variance)
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void setNugget(double value)
Function to set the nugget value.
void setRange(double value)
Function to set the range value.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
te::qt::widgets::ScatterChart * m_scatterChartMethod