All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../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 "../../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 
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 }
87 
88 void te::sa::BayesGlobalDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
89 {
90  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
91 
92  while(it != layers.end())
93  {
95 
96  if(l->isValid())
97  {
98  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
99 
100  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
101 
102  if(dsLayer && dsType->hasGeom())
103  m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
104  }
105 
106  ++it;
107  }
108 
109 // fill attributes combo
110  if(m_ui->m_inputLayerComboBox->count() > 0)
111  onInputLayerComboBoxActivated(0);
112 }
113 
115 {
116  return m_outputLayer;
117 }
118 
120 {
121  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
122 
124 
125  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
126 
127  std::vector<te::dt::Property*> propVec = dsType->getProperties();
128 
129  m_ui->m_attrEventComboBox->clear();
130  m_ui->m_attrPopComboBox->clear();
131 
132  for(std::size_t t = 0; t < propVec.size(); ++t)
133  {
134  int dataType = propVec[t]->getType();
135 
136  if (dataType == te::dt::INT16_TYPE || dataType == te::dt::UINT16_TYPE ||
137  dataType == te::dt::INT32_TYPE || dataType == te::dt::UINT32_TYPE ||
138  dataType == te::dt::INT64_TYPE || dataType == te::dt::UINT64_TYPE ||
139  dataType == te::dt::FLOAT_TYPE || dataType == te::dt::DOUBLE_TYPE)
140  {
141  m_ui->m_attrEventComboBox->addItem(propVec[t]->getName().c_str(), dataType);
142  m_ui->m_attrPopComboBox->addItem(propVec[t]->getName().c_str(), dataType);
143  }
144  }
145 }
146 
148 {
149  // check input parameters
150  if(m_ui->m_repositoryLineEdit->text().isEmpty())
151  {
152  QMessageBox::information(this, tr("Warning"), tr("Define a repository for the result."));
153  return;
154  }
155 
156  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
157  {
158  QMessageBox::information(this, tr("Warning"), tr("Define a name for the resulting layer."));
159  return;
160  }
161 
162 //get selected layer
163  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
165 
166  std::auto_ptr<te::da::DataSetType> dataSetType = l->getSchema();
167  std::auto_ptr<te::da::DataSet> dataSet = l->getData();
168 
169 //create datasource to save the output information
170  std::string dataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
171 
172  std::size_t idx = dataSetName.find(".");
173  if (idx != std::string::npos)
174  dataSetName=dataSetName.substr(0,idx);
175 
176  te::da::DataSourcePtr outputDataSource;
177 
178  if(m_toFile)
179  {
180  outputDataSource = te::sa::CreateOGRDataSource(m_ui->m_repositoryLineEdit->text().toStdString());
181  }
182  else
183  {
184  outputDataSource = te::da::GetDataSource(m_outputDatasource->getId());
185  }
186 
188 
189  inParams->m_ds = dataSet;
190  inParams->m_dsType = dataSetType;
191  inParams->m_eventAttrName = m_ui->m_attrEventComboBox->currentText().toStdString();
192  inParams->m_populationAttrName = m_ui->m_attrPopComboBox->currentText().toStdString();
193  inParams->m_rate = m_ui->m_rateComboBox->currentText().toDouble();
194 
196 
197  outParams->m_ds = outputDataSource;
198  outParams->m_outputDataSetName = dataSetName;
199 
200  //progress
203 
204  QApplication::setOverrideCursor(Qt::WaitCursor);
205 
206  try
207  {
209 
210  op.setParameters(inParams, outParams);
211 
212  op.execute();
213  }
214  catch(const std::exception& e)
215  {
216  QMessageBox::warning(this, tr("Warning"), e.what());
217 
218  QApplication::restoreOverrideCursor();
219 
221 
222  return;
223  }
224  catch(...)
225  {
226  QMessageBox::warning(this, tr("Warning"), tr("Internal Error.Global Bayes not calculated."));
227 
228  QApplication::restoreOverrideCursor();
229 
231 
232  return;
233  }
234 
235  QApplication::restoreOverrideCursor();
236 
238 
239  //create layer
240  m_outputLayer = te::sa::CreateLayer(outputDataSource, dataSetName);
241 
242  //create legend
243  te::sa::CreateBayesGrouping(m_outputLayer.get());
244 
245  accept();
246 }
247 
249 {
250  m_ui->m_newLayerNameLineEdit->clear();
251  m_ui->m_newLayerNameLineEdit->setEnabled(true);
252 
254  dlg.exec();
255 
256  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
257 
258  if(dsPtrList.size() <= 0)
259  return;
260 
261  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
262 
263  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
264 
265  m_outputDatasource = *it;
266 
267  m_toFile = false;
268 }
269 
271 {
272  m_ui->m_newLayerNameLineEdit->clear();
273  m_ui->m_repositoryLineEdit->clear();
274 
275  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."), QString(), tr("Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
276 
277  if (fileName.isEmpty())
278  return;
279 
280  boost::filesystem::path outfile(fileName.toStdString());
281 
282  m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
283 
284  m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
285 
286  m_ui->m_newLayerNameLineEdit->setEnabled(false);
287 
288  m_toFile = true;
289 }
290 
292 {
293  m_ui->m_rateComboBox->clear();
294 
295  m_ui->m_rateComboBox->addItem("1");
296  m_ui->m_rateComboBox->addItem("10");
297  m_ui->m_rateComboBox->addItem("100");
298  m_ui->m_rateComboBox->addItem("1000");
299  m_ui->m_rateComboBox->addItem("10000");
300  m_ui->m_rateComboBox->addItem("100000");
301 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
Class that represents the Bayes output parameters.
Definition: BayesParams.h:94
Utility functions for the data access module.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
TESAEXPORT te::da::DataSourcePtr CreateOGRDataSource(std::string repository)
Definition: Utils.cpp:63
std::auto_ptr< te::da::DataSet > m_ds
Attribute with data set.
Definition: BayesParams.h:76
A dialog to calculate the global empirical bayes of a dataset.
void execute()
Function to execute the bayes operation.
te::map::AbstractLayerPtr getOutputLayer()
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
std::string m_outputDataSetName
Attribute that defines the output dataset name.
Definition: BayesParams.h:113
void onInputLayerComboBoxActivated(int index)
BayesGlobalDialog(QWidget *parent=0, Qt::WindowFlags f=0)
std::string m_eventAttrName
Attribute from dataset with event information.
Definition: BayesParams.h:79
void removeViewer(int viewerId)
Dettach a progress viewer.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
TESAEXPORT void CreateBayesGrouping(te::map::AbstractLayerPtr layer)
Definition: Utils.cpp:131
Class used to execute the bayes global operations.
te::da::DataSourcePtr m_ds
Pointer to the output datasource.
Definition: BayesParams.h:111
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
double m_rate
Attribute with multiplicative rate correction.
Definition: BayesParams.h:83
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)
Definition: Utils.cpp:122
void setParameters(te::sa::BayesInputParams *inParams, te::sa::BayesOutputParams *outParams)
A dialog for selecting a data source.
std::string m_populationAttrName
Attribute from dataset with population information.
Definition: BayesParams.h:80
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
std::auto_ptr< te::da::DataSetType > m_dsType
Attribute used to access the data set metadata.
Definition: BayesParams.h:75
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::auto_ptr< Ui::BayesGlobalDialogForm > m_ui