KernelMapDialog.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/KernelMapDialog.cpp
22 
23  \brief A dialog to calculate the kernel map of a dataset.
24 */
25 
26 // TerraLib
27 #include "../../core/logger/Logger.h"
28 #include "../../common/progress/ProgressManager.h"
29 #include "../../core/translator/Translator.h"
30 #include "../../common/STLUtils.h"
31 #include "../../dataaccess/datasource/DataSource.h"
32 #include "../../dataaccess/utils/Utils.h"
33 #include "../../geometry/GeometryProperty.h"
34 #include "../../maptools/DataSetLayer.h"
35 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
36 #include "../core/KernelMapOperation.h"
37 #include "../Exception.h"
38 #include "KernelMapDialog.h"
39 #include "Utils.h"
40 #include "ui_KernelMapDialogForm.h"
41 
42 // Qt
43 #include <QFileDialog>
44 #include <QFileInfo>
45 #include <QMessageBox>
46 #include <QValidator>
47 
48 // STL
49 #include <memory>
50 
51 // Boost
52 #include <boost/algorithm/string.hpp>
53 #include <boost/filesystem.hpp>
54 #include <boost/uuid/random_generator.hpp>
55 #include <boost/uuid/uuid_io.hpp>
56 
58 
59 te::sa::KernelMapDialog::KernelMapDialog(QWidget* parent, Qt::WindowFlags f)
60  : QDialog(parent, f),
61  m_ui(new Ui::KernelMapDialogForm)
62 {
63 // add controls
64  m_ui->setupUi(this);
65 
66  m_ui->m_nColsLineEdit->setValidator(new QIntValidator(this));
67 
68  fillKernelParameters();
69 
70 // add icons
71  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("sa-kernelmap-hint").pixmap(112,48));
72 
73 // connectors
74  connect(m_ui->m_inputLayerComboBox, SIGNAL(activated(int)), this, SLOT(onInputLayerComboBoxActivated(int)));
75  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
76  connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()), this, SLOT(onTargetFileToolButtonPressed()));
77 
78 // help info
79  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
80  m_ui->m_helpPushButton->setPageReference("plugins/sa/sa_kernelmap.html");
81 }
82 
84 
85 void te::sa::KernelMapDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
86 {
87  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
88 
89  while(it != layers.end())
90  {
92 
93  if(l->isValid())
94  {
95  std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
96 
97  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
98 
99  if(dsLayer && dsType->hasGeom())
100  m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
101  }
102 
103  ++it;
104  }
105 
106 // fill attributes combo
107  if(m_ui->m_inputLayerComboBox->count() > 0)
109 }
110 
112 {
113  return m_outputLayer;
114 }
115 
117 {
118  //function
119  m_ui->m_functionComboBox->clear();
120 
121  m_ui->m_functionComboBox->addItem("Quartic", QVariant(te::sa::Quartic));
122  m_ui->m_functionComboBox->addItem("Normal", QVariant(te::sa::Normal));
123  m_ui->m_functionComboBox->addItem("Triangular", QVariant(te::sa::Triangular));
124  m_ui->m_functionComboBox->addItem("Uniform", QVariant(te::sa::Uniform));
125  m_ui->m_functionComboBox->addItem("Negative Exponential", QVariant(te::sa::Negative_Exp));
126 
127  //estimation
128  m_ui->m_estimationComboBox->clear();
129 
130  m_ui->m_estimationComboBox->addItem("Density", QVariant(te::sa::Density));
131  m_ui->m_estimationComboBox->addItem("Spatial Moving Average", QVariant(te::sa::Spatial_Moving_Average));
132  m_ui->m_estimationComboBox->addItem("Probability", QVariant(te::sa::Probability));
133 }
134 
136 {
137  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
138 
140 
141  std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
142 
143  std::vector<te::dt::Property*> propVec = dsType->getProperties();
144 
145  m_ui->m_attrLayerComboBox->clear();
146 
147  for(std::size_t t = 0; t < propVec.size(); ++t)
148  {
149  int dataType = propVec[t]->getType();
150 
151  if (dataType == te::dt::INT16_TYPE || dataType == te::dt::UINT16_TYPE ||
152  dataType == te::dt::INT32_TYPE || dataType == te::dt::UINT32_TYPE ||
153  dataType == te::dt::INT64_TYPE || dataType == te::dt::UINT64_TYPE ||
154  dataType == te::dt::FLOAT_TYPE || dataType == te::dt::DOUBLE_TYPE)
155  {
156  m_ui->m_attrLayerComboBox->addItem(propVec[t]->getName().c_str(), dataType);
157  }
158  }
159 
160  //output data restriction for point representation
161  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(dsType.get());
162 
163  if (geomProp)
164  {
165  if (geomProp->getGeometryType() == te::gm::PointType ||
166  geomProp->getGeometryType() == te::gm::PointZType ||
167  geomProp->getGeometryType() == te::gm::PointMType ||
168  geomProp->getGeometryType() == te::gm::PointZMType ||
169  geomProp->getGeometryType() == te::gm::PointKdType ||
170  geomProp->getGeometryType() == te::gm::MultiPointType ||
171  geomProp->getGeometryType() == te::gm::MultiPointZType ||
172  geomProp->getGeometryType() == te::gm::MultiPointMType ||
174  {
175  m_ui->m_gridRadioButton->setChecked(true);
176  m_ui->m_attrRadioButton->setEnabled(false);
177  m_ui->m_attrNameLineEdit->clear();
178  }
179  else
180  {
181  m_ui->m_gridRadioButton->setChecked(true);
182  m_ui->m_attrRadioButton->setEnabled(true);
183  m_ui->m_attrNameLineEdit->clear();
184  }
185  }
186 }
187 
188 
190 {
191  // check input parameters
192  if(m_ui->m_repositoryLineEdit->text().isEmpty())
193  {
194  QMessageBox::information(this, tr("Warning"), tr("Define a repository for the result."));
195  return;
196  }
197 
198  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
199  {
200  QMessageBox::information(this, tr("Warning"), tr("Define a name for the resulting layer."));
201  return;
202  }
203 
204  //get selected layer
205  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
207 
208  //set input kernel parameters
210 
211  kInParams->m_functionType = (te::sa::KernelFunctionType) m_ui->m_functionComboBox->itemData(m_ui->m_functionComboBox->currentIndex()).toInt();
212  kInParams->m_estimationType = (te::sa::KernelEstimationType) m_ui->m_estimationComboBox->itemData(m_ui->m_estimationComboBox->currentIndex()).toInt();
213 
214  kInParams->m_useAdaptativeRadius = m_ui->m_useAdaptRadiusCheckBox->isChecked();
215 
216  if(!m_ui->m_useAdaptRadiusCheckBox->isChecked())
217  {
218  kInParams->m_radiusPercentValue = m_ui->m_radiusSpinBox->value();
219  }
220 
221  if(m_ui->m_useAttrLayerCheckBox->isChecked())
222  {
223  kInParams->m_intensityAttrName = m_ui->m_attrLayerComboBox->currentText().toUtf8().data();
224  }
225 
226  kInParams->m_ds = l->getData();
227  kInParams->m_dsType = l->getSchema();
228 
229  //set output kernel parameters
231 
232  kOutParams->m_outputPath = m_ui->m_repositoryLineEdit->text().toUtf8().data();
233  kOutParams->m_outputDataSetName = m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
234 
235  if(m_ui->m_gridRadioButton->isChecked())
236  {
237  if(m_ui->m_nColsLineEdit->text().isEmpty())
238  {
239  QMessageBox::information(this, tr("Warning"), tr("Define the number of columns to the output grid."));
240  return;
241  }
242 
243  kOutParams->m_storageType = te::sa::Grid;
244  kOutParams->m_nCols = m_ui->m_nColsLineEdit->text().toInt();
245  }
246  else if(m_ui->m_attrRadioButton->isChecked())
247  {
248  if(m_ui->m_attrNameLineEdit->text().isEmpty())
249  {
250  QMessageBox::information(this, tr("Warning"), tr("Define the attribute name to the output data."));
251  return;
252  }
253 
254  kOutParams->m_storageType = te::sa::Attribute;
255  kOutParams->m_outputAttrName = m_ui->m_attrNameLineEdit->text().toUtf8().data();
256  }
257 
258  //progress
260 
261  QApplication::setOverrideCursor(Qt::WaitCursor);
262 
263  //execute kernel
264  try
265  {
267 
268  op.setInputParameters(kInParams);
269  op.setOutputParameters(kOutParams);
270 
271  op.execute();
272  }
273  catch(const std::exception& e)
274  {
275  QMessageBox::warning(this, tr("Warning"), e.what());
276 
277  QApplication::restoreOverrideCursor();
278 
279  return;
280  }
281  catch(...)
282  {
283  QMessageBox::warning(this, tr("Warning"), tr("Kernel Map internal error."));
284 
285  QApplication::restoreOverrideCursor();
286 
287  return;
288  }
289 
290  QApplication::restoreOverrideCursor();
291 
292  //save generated information
293  te::da::DataSourcePtr outputDataSource;
294 
295  std::string dataSetName;
296 
297  if(m_ui->m_gridRadioButton->isChecked())
298  {
299  outputDataSource = te::sa::CreateGDALDataSource(m_ui->m_repositoryLineEdit->text().toUtf8().data(), m_ui->m_newLayerNameLineEdit->text().toUtf8().data());
300 
301  dataSetName = std::string(m_ui->m_newLayerNameLineEdit->text().toUtf8().data()) + ".tif";
302  }
303  else if(m_ui->m_attrRadioButton->isChecked())
304  {
305  outputDataSource = te::sa::CreateOGRDataSource(m_ui->m_repositoryLineEdit->text().toUtf8().data(), m_ui->m_newLayerNameLineEdit->text().toUtf8().data());
306 
307  dataSetName = m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
308  }
309 
310  //create layer
311  m_outputLayer = te::sa::CreateLayer(outputDataSource, dataSetName);
312 
313  //create legend
314  if(m_ui->m_gridRadioButton->isChecked())
315  {
317  }
318  else if(m_ui->m_attrRadioButton->isChecked())
319  {
320  te::sa::CreateKernelGrouping(m_outputLayer, m_ui->m_attrNameLineEdit->text().toUtf8().data());
321  }
322 
323  accept();
324 }
325 
327 {
328  m_ui->m_newLayerNameLineEdit->clear();
329  m_ui->m_repositoryLineEdit->clear();
330 
331  QString fileName = QFileDialog::getExistingDirectory(this, tr("Set output location..."));
332 
333  if (fileName.isEmpty())
334  return;
335 
336  m_ui->m_repositoryLineEdit->setText(fileName);
337 }
Geometric property.
TESAEXPORT void CreateKernelColorMaping(te::map::AbstractLayerPtr layer)
Class that represents the kernel input parameters.
Definition: KernelParams.h:54
boost::shared_ptr< DataSource > DataSourcePtr
TESAEXPORT te::da::DataSourcePtr CreateOGRDataSource(std::string repository)
te::sa::KernelOutputType m_storageType
Kernel storage type.
Definition: KernelParams.h:117
Class used to calculate the kernel map of a dataset.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
TESAEXPORT te::da::DataSourcePtr CreateGDALDataSource(std::string path, std::string dataSetName)
Class that represents the kernel output parameters.
Definition: KernelParams.h:95
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
void setOutputParameters(te::sa::KernelOutputParams *outParams)
std::unique_ptr< Ui::KernelMapDialogForm > m_ui
void onInputLayerComboBoxActivated(int index)
te::map::AbstractLayerPtr getOutputLayer()
URI C++ Library.
Definition: Attributes.h:37
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
std::string m_outputAttrName
Attribute with the new attr name (if KernelOutputType is Attribute)
Definition: KernelParams.h:121
Utility functions for the data access module.
std::string m_outputPath
Attribute with URI of the output file.
Definition: KernelParams.h:122
virtual void execute()
Function to execute the kernel operation.
TESAEXPORT te::map::AbstractLayerPtr CreateLayer(te::da::DataSourcePtr ds, std::string dataSetName)
std::string m_outputDataSetName
Attribute that defines the output dataset name.
Definition: KernelParams.h:123
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
TESAEXPORT void CreateKernelGrouping(te::map::AbstractLayerPtr layer, std::string kernelAttr)
A dialog to calculate the kernel map of a dataset.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
te::map::AbstractLayerPtr m_outputLayer
Generated Layer.
int m_nCols
Attribute with number of columns to create the grid (if KernelOutputType is Grid) ...
Definition: KernelParams.h:120
void setInputParameters(te::sa::KernelInputParams *inParams)