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