BayesGlobalDialog.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/BayesGlobalDialog.cpp
22 
23  \brief A dialog to calculate the global empirical bayes 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/datasource/selector/DataSourceSelectorDialog.h"
36 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
37 #include "../core/BayesGlobalOperation.h"
38 #include "../core/BayesParams.h"
39 #include "../core/Utils.h"
40 #include "../Exception.h"
41 #include "BayesGlobalDialog.h"
42 #include "Utils.h"
43 #include "ui_BayesGlobalDialogForm.h"
44 
45 // Qt
46 #include <QFileDialog>
47 #include <QFileInfo>
48 #include <QMessageBox>
49 #include <QValidator>
50 
51 // STL
52 #include <memory>
53 
54 // Boost
55 #include <boost/filesystem.hpp>
56 
57 
59 
60 te::sa::BayesGlobalDialog::BayesGlobalDialog(QWidget* parent, Qt::WindowFlags f)
61  : QDialog(parent, f),
62  m_ui(new Ui::BayesGlobalDialogForm)
63 {
64 // add controls
65  m_ui->setupUi(this);
66 
67  fillRateCorrection();
68 
69 // add icons
70  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("sa-bayesglobal-hint").pixmap(112,48));
71  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
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_targetDatasourceToolButton, SIGNAL(pressed()), this, SLOT(onTargetDatasourceToolButtonPressed()));
77  connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()), this, SLOT(onTargetFileToolButtonPressed()));
78 
79 // help info
80  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
81  m_ui->m_helpPushButton->setPageReference("plugins/sa/sa_bayesglobal.html");
82 }
83 
85 
86 void te::sa::BayesGlobalDialog::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::unique_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)
110 }
111 
113 {
114  return m_outputLayer;
115 }
116 
118 {
119  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
120 
122 
123  std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
124 
125  std::vector<te::dt::Property*> propVec = dsType->getProperties();
126 
127  m_ui->m_attrEventComboBox->clear();
128  m_ui->m_attrPopComboBox->clear();
129 
130  for(std::size_t t = 0; t < propVec.size(); ++t)
131  {
132  int dataType = propVec[t]->getType();
133 
134  if (dataType == te::dt::INT16_TYPE || dataType == te::dt::UINT16_TYPE ||
135  dataType == te::dt::INT32_TYPE || dataType == te::dt::UINT32_TYPE ||
136  dataType == te::dt::INT64_TYPE || dataType == te::dt::UINT64_TYPE ||
137  dataType == te::dt::FLOAT_TYPE || dataType == te::dt::DOUBLE_TYPE)
138  {
139  m_ui->m_attrEventComboBox->addItem(propVec[t]->getName().c_str(), dataType);
140  m_ui->m_attrPopComboBox->addItem(propVec[t]->getName().c_str(), dataType);
141  }
142  }
143 }
144 
146 {
147  // check input parameters
148  if(m_ui->m_repositoryLineEdit->text().isEmpty())
149  {
150  QMessageBox::information(this, tr("Warning"), tr("Define a repository for the result."));
151  return;
152  }
153 
154  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
155  {
156  QMessageBox::information(this, tr("Warning"), tr("Define a name for the resulting layer."));
157  return;
158  }
159 
160 //get selected layer
161  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
163 
164  std::unique_ptr<te::da::DataSetType> dataSetType = l->getSchema();
165  std::unique_ptr<te::da::DataSet> dataSet = l->getData();
166 
167 //create datasource to save the output information
168  std::string dataSetName = m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
169 
170  std::size_t idx = dataSetName.find(".");
171  if (idx != std::string::npos)
172  dataSetName=dataSetName.substr(0,idx);
173 
174  te::da::DataSourcePtr outputDataSource;
175 
176  if(m_toFile)
177  {
178  outputDataSource = te::sa::CreateOGRDataSource(m_ui->m_repositoryLineEdit->text().toUtf8().data());
179  }
180  else
181  {
182  outputDataSource = te::da::GetDataSource(m_outputDatasource->getId());
183  }
184 
186 
187  inParams->m_ds = std::move(dataSet);
188  inParams->m_dsType = std::move(dataSetType);
189  inParams->m_eventAttrName = m_ui->m_attrEventComboBox->currentText().toUtf8().data();
190  inParams->m_populationAttrName = m_ui->m_attrPopComboBox->currentText().toUtf8().data();
191  inParams->m_rate = m_ui->m_rateComboBox->currentText().toDouble();
192 
194 
195  outParams->m_ds = outputDataSource;
196  outParams->m_outputDataSetName = dataSetName;
197 
198  //progress
200 
201  QApplication::setOverrideCursor(Qt::WaitCursor);
202 
203  try
204  {
206 
207  op.setParameters(inParams, outParams);
208 
209  op.execute();
210  }
211  catch(const std::exception& e)
212  {
213  QMessageBox::warning(this, tr("Warning"), e.what());
214 
215  QApplication::restoreOverrideCursor();
216 
217  return;
218  }
219  catch(...)
220  {
221  QMessageBox::warning(this, tr("Warning"), tr("Internal Error.Global Bayes not calculated."));
222 
223  QApplication::restoreOverrideCursor();
224 
225  return;
226  }
227 
228  QApplication::restoreOverrideCursor();
229 
230  //create layer
231  m_outputLayer = te::sa::CreateLayer(outputDataSource, dataSetName);
232 
233  //create legend
235 
236  accept();
237 }
238 
240 {
241  m_ui->m_newLayerNameLineEdit->clear();
242  m_ui->m_newLayerNameLineEdit->setEnabled(true);
243 
245  dlg.exec();
246 
247  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
248 
249  if(dsPtrList.empty())
250  return;
251 
252  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
253 
254  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
255 
256  m_outputDatasource = *it;
257 
258  m_toFile = false;
259 }
260 
262 {
263  m_ui->m_newLayerNameLineEdit->clear();
264  m_ui->m_repositoryLineEdit->clear();
265 
266  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."), QString(), tr("Shapefile (*.shp *.SHP);;"),nullptr, QFileDialog::DontConfirmOverwrite);
267 
268  if (fileName.isEmpty())
269  return;
270 
271  boost::filesystem::path outfile(fileName.toUtf8().data());
272 
273  m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
274 
275  m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
276 
277  m_ui->m_newLayerNameLineEdit->setEnabled(false);
278 
279  m_toFile = true;
280 }
281 
283 {
284  m_ui->m_rateComboBox->clear();
285 
286  m_ui->m_rateComboBox->addItem("1");
287  m_ui->m_rateComboBox->addItem("10");
288  m_ui->m_rateComboBox->addItem("100");
289  m_ui->m_rateComboBox->addItem("1000");
290  m_ui->m_rateComboBox->addItem("10000");
291  m_ui->m_rateComboBox->addItem("100000");
292 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Class that represents the Bayes output parameters.
Definition: BayesParams.h:94
te::da::DataSourceInfoPtr m_outputDatasource
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
boost::shared_ptr< DataSource > DataSourcePtr
TESAEXPORT te::da::DataSourcePtr CreateOGRDataSource(std::string repository)
A dialog to calculate the global empirical bayes of a dataset.
void execute()
Function to execute the bayes operation.
te::map::AbstractLayerPtr getOutputLayer()
te::map::AbstractLayerPtr m_outputLayer
Generated Layer.
std::string m_outputDataSetName
Attribute that defines the output dataset name.
Definition: BayesParams.h:113
void onInputLayerComboBoxActivated(int index)
TESAEXPORT void CreateBayesGrouping(te::map::AbstractLayerPtr layer)
URI C++ Library.
Definition: Attributes.h:37
std::unique_ptr< Ui::BayesGlobalDialogForm > m_ui
Utility functions for the data access module.
Class used to execute the bayes global operations.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
te::da::DataSourcePtr m_ds
Pointer to the output datasource.
Definition: BayesParams.h:111
Class that represents the Bayes input parameters.
Definition: BayesParams.h:56
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
TESAEXPORT te::map::AbstractLayerPtr CreateLayer(te::da::DataSourcePtr ds, std::string dataSetName)
void setParameters(te::sa::BayesInputParams *inParams, te::sa::BayesOutputParams *outParams)
A dialog for selecting a data source.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr