All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AggregationDialog.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/vp/AggregationDialog.cpp
22 
23  \brief A dialog for aggregation operation
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/DataSetType.h"
32 #include "../../dataaccess/dataset/ObjectIdSet.h"
33 #include "../../dataaccess/datasource/DataSourceCapabilities.h"
34 #include "../../dataaccess/datasource/DataSourceInfo.h"
35 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
36 #include "../../dataaccess/datasource/DataSourceFactory.h"
37 #include "../../dataaccess/datasource/DataSourceManager.h"
38 #include "../../dataaccess/utils/Utils.h"
39 #include "../../datatype/Enums.h"
40 #include "../../datatype/Property.h"
41 #include "../../maptools/AbstractLayer.h"
42 #include "../../qt/af/Utils.h"
43 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
44 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
45 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
46 #include "../../statistics/core/Utils.h"
47 #include "../Config.h"
48 #include "../Exception.h"
49 #include "AggregationDialog.h"
50 #include "../AggregationMemory.h"
51 #include "../AggregationOp.h"
52 #include "../AggregationQuery.h"
53 #include "ui_AggregationDialogForm.h"
54 #include "Utils.h"
55 
56 // Qt
57 #include <QFileDialog>
58 #include <QList>
59 #include <QListWidget>
60 #include <QListWidgetItem>
61 #include <QMessageBox>
62 #include <QTreeWidget>
63 
64 // Boost
65 #include <boost/algorithm/string.hpp>
66 #include <boost/filesystem.hpp>
67 #include <boost/uuid/random_generator.hpp>
68 #include <boost/uuid/uuid_io.hpp>
69 
70 te::vp::AggregationDialog::AggregationDialog(QWidget* parent, Qt::WindowFlags f)
71  : QDialog(parent, f),
72  m_ui(new Ui::AggregationDialogForm),
73  m_layers(std::list<te::map::AbstractLayerPtr>()),
74  m_selectedLayer(0),
75  m_toFile(false)
76 {
77 // add controls
78  m_ui->setupUi(this);
79 
80  m_ui->m_outputStatisticsGroupBox->setVisible(false);
81 
82 // add icons
83  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("vp-aggregation-hint").pixmap(112,48));
84  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
85 
88 
89  connect(m_ui->m_layersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onLayerComboBoxChanged(int)));
90  connect(m_ui->m_filterLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onFilterLineEditTextChanged(const QString&)));
91  connect(m_ui->m_calcStatCheckBox, SIGNAL(toggled(bool)), this, SLOT(onCalculateStatistics(bool)));
92  connect(m_ui->m_outputListWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(onOutputListWidgetClicked(QListWidgetItem *)));
93  connect(m_ui->m_selectAllComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectAllComboBoxChanged(int)));
94  connect(m_ui->m_rejectAllComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onRejectAllComboBoxChanged(int)));
95  connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()), this, SLOT(onTargetDatasourceToolButtonPressed()));
96  connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()), this, SLOT(onTargetFileToolButtonPressed()));
97 
98  //connect(m_ui->m_helpPushButton, SIGNAL(clicked()), this, SLOT(onHelpPushButtonClicked()));
99  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
100  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
101 
102  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
103  m_ui->m_helpPushButton->setPageReference("plugins/vp/vp_aggregation.html");
104 
106  m_ui->m_newLayerNameLineEdit->setEnabled(true);
107 }
108 
110 {
111 }
112 
113 void te::vp::AggregationDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
114 {
115  m_layers = layers;
116 
117  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
118 
119  while(it != m_layers.end())
120  {
121  std::auto_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
122  if(dsType->hasGeom())
123  m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
124  ++it;
125  }
126 }
127 
128 std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > te::vp::AggregationDialog::getStatisticalSummary()
129 {
130  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > outputStatisticalSummary;
131 
132  QList<QListWidgetItem*> itemList = m_ui->m_outputListWidget->selectedItems();
133 
134  te::stat::StatisticalSummary enumStatisticalSummary;
135  std::map<std::string, std::vector<te::stat::StatisticalSummary> > propname_stat;
136  std::map<std::string, std::vector<te::stat::StatisticalSummary> >::iterator it;
137 
138  for(int i = 0; i < itemList.size(); ++i)
139  {
140  std::vector<std::string> tokens;
141  std::string pname;
142  std::string auxItem = itemList[i]->text().toStdString();
143 
144  boost::split(tokens, auxItem, boost::is_any_of(":"));
145  if(tokens[0] != "")
146  {
147  pname = tokens[0];
148  pname.erase(pname.end() - 1);
149  enumStatisticalSummary = (te::stat::StatisticalSummary)itemList[i]->data(Qt::UserRole).toInt();
150  it = propname_stat.find(pname);
151  if (it != propname_stat.end())
152  it->second.push_back(enumStatisticalSummary);
153  else
154  {
155  std::vector<te::stat::StatisticalSummary> nvec;
156  nvec.push_back(enumStatisticalSummary);
157  propname_stat.insert(std::make_pair(pname, nvec));
158  }
159  }
160  }
161 
162  it = propname_stat.begin();
163  while (it != propname_stat.end())
164  {
165  te::dt::Property* prop = getSelectedPropertyByName(it->first);
166  outputStatisticalSummary.insert(std::make_pair(prop,it->second));
167  ++it;
168  }
169 
170  return outputStatisticalSummary;
171 }
172 
174 {
175  te::dt::Property* selProperty;
176  if(propertyName == "")
177  return 0;
178 
179  for(std::size_t i = 0; i < m_properties.size(); ++i)
180  {
181  if(propertyName == m_properties[i]->getName())
182  {
183  selProperty = m_properties[i];
184  return selProperty;
185  }
186  }
187  return 0;
188 }
189 
190 std::vector<te::dt::Property*> te::vp::AggregationDialog::getSelectedProperties()
191 {
192  std::vector<te::dt::Property*> selProperties;
193 
194  for(std::size_t i = 0; i != m_ui->m_propertieslistWidget->count(); ++i)
195  {
196  if(m_ui->m_propertieslistWidget->isItemSelected(m_ui->m_propertieslistWidget->item(i)))
197  {
198  selProperties.push_back(m_properties[i]);
199  }
200  }
201 
202  return selProperties;
203 }
204 
206 {
207  return m_layer;
208 }
209 
211 {
212  m_ui->m_selectAllComboBox->addItem("");
213  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE).c_str()), te::stat::MIN_VALUE);
214  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE).c_str()), te::stat::MAX_VALUE);
215  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEAN).c_str()), te::stat::MEAN);
216  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SUM).c_str()), te::stat::SUM);
217  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::COUNT).c_str()), te::stat::COUNT);
218  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VALID_COUNT).c_str()), te::stat::VALID_COUNT);
219  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::STANDARD_DEVIATION).c_str()), te::stat::STANDARD_DEVIATION);
220  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VARIANCE).c_str()), te::stat::VARIANCE);
221  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SKEWNESS).c_str()), te::stat::SKEWNESS);
222  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::KURTOSIS).c_str()), te::stat::KURTOSIS);
223  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::AMPLITUDE).c_str()), te::stat::AMPLITUDE);
224  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEDIAN).c_str()), te::stat::MEDIAN);
225  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VAR_COEFF).c_str()), te::stat::VAR_COEFF);
226  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MODE).c_str()), te::stat::MODE);
227 
228  m_ui->m_rejectAllComboBox->addItem("");
229  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE).c_str()), te::stat::MIN_VALUE);
230  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE).c_str()), te::stat::MAX_VALUE);
231  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEAN).c_str()), te::stat::MEAN);
232  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SUM).c_str()), te::stat::SUM);
233  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::COUNT).c_str()), te::stat::COUNT);
234  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VALID_COUNT).c_str()), te::stat::VALID_COUNT);
235  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::STANDARD_DEVIATION).c_str()), te::stat::STANDARD_DEVIATION);
236  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VARIANCE).c_str()), te::stat::VARIANCE);
237  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SKEWNESS).c_str()), te::stat::SKEWNESS);
238  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::KURTOSIS).c_str()), te::stat::KURTOSIS);
239  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::AMPLITUDE).c_str()), te::stat::AMPLITUDE);
240  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEDIAN).c_str()), te::stat::MEDIAN);
241  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VAR_COEFF).c_str()), te::stat::VAR_COEFF);
242  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MODE).c_str()), te::stat::MODE);
243 }
244 
246 {
247  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MIN_VALUE, te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE)));
248  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MAX_VALUE, te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE)));
249  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MEAN, te::stat::GetStatSummaryFullName(te::stat::MEAN)));
250  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::SUM, te::stat::GetStatSummaryFullName(te::stat::SUM)));
251  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::COUNT, te::stat::GetStatSummaryFullName(te::stat::COUNT)));
252  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::VALID_COUNT, te::stat::GetStatSummaryFullName(te::stat::VALID_COUNT)));
253  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::STANDARD_DEVIATION, te::stat::GetStatSummaryFullName(te::stat::STANDARD_DEVIATION)));
254  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::VARIANCE, te::stat::GetStatSummaryFullName(te::stat::VARIANCE)));
255  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::SKEWNESS, te::stat::GetStatSummaryFullName(te::stat::SKEWNESS)));
256  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::KURTOSIS, te::stat::GetStatSummaryFullName(te::stat::KURTOSIS)));
257  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::AMPLITUDE, te::stat::GetStatSummaryFullName(te::stat::AMPLITUDE)));
258  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MEDIAN, te::stat::GetStatSummaryFullName(te::stat::MEDIAN)));
259  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::VAR_COEFF, te::stat::GetStatSummaryFullName(te::stat::VAR_COEFF)));
260  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MODE, te::stat::GetStatSummaryFullName(te::stat::MODE)));
261 }
262 
263 void te::vp::AggregationDialog::setFunctionsByLayer(std::vector<te::dt::Property*> properties)
264 {
265  QStringList propertyList;
266  int propertyType;
267 
268  m_ui->m_selectAllComboBox->setCurrentIndex(0);
269  m_ui->m_rejectAllComboBox->setCurrentIndex(0);
270  m_ui->m_outputListWidget->clear();
271 
272  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(m_selectedLayer.get());
273  te::da::DataSourcePtr dataSource = te::da::GetDataSource(dsLayer->getDataSourceId(), true);
274  const te::da::DataSourceCapabilities dsCapabilities = dataSource->getCapabilities();
275 
276  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
277  {
278  for(size_t i=0; i < properties.size(); ++i)
279  {
280  propertyType = properties[i]->getType();
281  if(propertyType != te::dt::GEOMETRY_TYPE)
282  {
283  if(propertyType == te::dt::STRING_TYPE)
284  {
285  QListWidgetItem* item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MIN_VALUE].c_str());
286  item->setData(Qt::UserRole, QVariant(te::stat::MIN_VALUE));
287  m_ui->m_outputListWidget->addItem(item);
288 
289  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MAX_VALUE].c_str());
290  item->setData(Qt::UserRole, QVariant(te::stat::MAX_VALUE));
291  m_ui->m_outputListWidget->addItem(item);
292 
293  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::COUNT].c_str());
294  item->setData(Qt::UserRole, QVariant(te::stat::COUNT));
295  m_ui->m_outputListWidget->addItem(item);
296 
297  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VALID_COUNT].c_str());
298  item->setData(Qt::UserRole, QVariant(te::stat::VALID_COUNT));
299  m_ui->m_outputListWidget->addItem(item);
300 
301  item = new QListWidgetItem("");
302  m_ui->m_outputListWidget->addItem(item);
303  }
304  else
305  {
306  QListWidgetItem* item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MIN_VALUE].c_str());
307  item->setData(Qt::UserRole, QVariant(te::stat::MIN_VALUE));
308  m_ui->m_outputListWidget->addItem(item);
309 
310  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MAX_VALUE].c_str());
311  item->setData(Qt::UserRole, QVariant(te::stat::MAX_VALUE));
312  m_ui->m_outputListWidget->addItem(item);
313 
314  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MEAN].c_str());
315  item->setData(Qt::UserRole, QVariant(te::stat::MEAN));
316  m_ui->m_outputListWidget->addItem(item);
317 
318  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::SUM].c_str());
319  item->setData(Qt::UserRole, QVariant(te::stat::SUM));
320  m_ui->m_outputListWidget->addItem(item);
321 
322  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::COUNT].c_str());
323  item->setData(Qt::UserRole, QVariant(te::stat::COUNT));
324  m_ui->m_outputListWidget->addItem(item);
325 
326  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VALID_COUNT].c_str());
327  item->setData(Qt::UserRole, QVariant(te::stat::VALID_COUNT));
328  m_ui->m_outputListWidget->addItem(item);
329 
330  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::STANDARD_DEVIATION].c_str());
331  item->setData(Qt::UserRole, QVariant(te::stat::STANDARD_DEVIATION));
332  m_ui->m_outputListWidget->addItem(item);
333 
334  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VARIANCE].c_str());
335  item->setData(Qt::UserRole, QVariant(te::stat::VARIANCE));
336  m_ui->m_outputListWidget->addItem(item);
337 
338  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::AMPLITUDE].c_str());
339  item->setData(Qt::UserRole, QVariant(te::stat::AMPLITUDE));
340  m_ui->m_outputListWidget->addItem(item);
341 
342  item = new QListWidgetItem("");
343  m_ui->m_outputListWidget->addItem(item);
344  }
345  }
346  }
347  }
348  else
349  {
350  for(size_t i=0; i < properties.size(); ++i)
351  {
352  propertyType = properties[i]->getType();
353  if(propertyType != te::dt::GEOMETRY_TYPE)
354  {
355  if(propertyType == te::dt::STRING_TYPE)
356  {
357  QListWidgetItem* item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MIN_VALUE].c_str());
358  item->setData(Qt::UserRole, QVariant(te::stat::MIN_VALUE));
359  m_ui->m_outputListWidget->addItem(item);
360 
361  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MAX_VALUE].c_str());
362  item->setData(Qt::UserRole, QVariant(te::stat::MAX_VALUE));
363  m_ui->m_outputListWidget->addItem(item);
364 
365  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::COUNT].c_str());
366  item->setData(Qt::UserRole, QVariant(te::stat::COUNT));
367  m_ui->m_outputListWidget->addItem(item);
368 
369  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VALID_COUNT].c_str());
370  item->setData(Qt::UserRole, QVariant(te::stat::VALID_COUNT));
371  m_ui->m_outputListWidget->addItem(item);
372 
373  item = new QListWidgetItem("");
374  m_ui->m_outputListWidget->addItem(item);
375  }
376  else
377  {
378  QListWidgetItem* item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MIN_VALUE].c_str());
379  item->setData(Qt::UserRole, QVariant(te::stat::MIN_VALUE));
380  m_ui->m_outputListWidget->addItem(item);
381 
382  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MAX_VALUE].c_str());
383  item->setData(Qt::UserRole, QVariant(te::stat::MAX_VALUE));
384  m_ui->m_outputListWidget->addItem(item);
385 
386  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MEAN].c_str());
387  item->setData(Qt::UserRole, QVariant(te::stat::MEAN));
388  m_ui->m_outputListWidget->addItem(item);
389 
390  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::SUM].c_str());
391  item->setData(Qt::UserRole, QVariant(te::stat::SUM));
392  m_ui->m_outputListWidget->addItem(item);
393 
394  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::COUNT].c_str());
395  item->setData(Qt::UserRole, QVariant(te::stat::COUNT));
396  m_ui->m_outputListWidget->addItem(item);
397 
398  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VALID_COUNT].c_str());
399  item->setData(Qt::UserRole, QVariant(te::stat::VALID_COUNT));
400  m_ui->m_outputListWidget->addItem(item);
401 
402  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::STANDARD_DEVIATION].c_str());
403  item->setData(Qt::UserRole, QVariant(te::stat::STANDARD_DEVIATION));
404  m_ui->m_outputListWidget->addItem(item);
405 
406  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VARIANCE].c_str());
407  item->setData(Qt::UserRole, QVariant(te::stat::VARIANCE));
408  m_ui->m_outputListWidget->addItem(item);
409 
410  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::SKEWNESS].c_str());
411  item->setData(Qt::UserRole, QVariant(te::stat::SKEWNESS));
412  m_ui->m_outputListWidget->addItem(item);
413 
414  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::KURTOSIS].c_str());
415  item->setData(Qt::UserRole, QVariant(te::stat::KURTOSIS));
416  m_ui->m_outputListWidget->addItem(item);
417 
418  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::AMPLITUDE].c_str());
419  item->setData(Qt::UserRole, QVariant(te::stat::AMPLITUDE));
420  m_ui->m_outputListWidget->addItem(item);
421 
422  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MEDIAN].c_str());
423  item->setData(Qt::UserRole, QVariant(te::stat::MEDIAN));
424  m_ui->m_outputListWidget->addItem(item);
425 
426  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VAR_COEFF].c_str());
427  item->setData(Qt::UserRole, QVariant(te::stat::VAR_COEFF));
428  m_ui->m_outputListWidget->addItem(item);
429 
430  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MODE].c_str());
431  item->setData(Qt::UserRole, QVariant(te::stat::MODE));
432  m_ui->m_outputListWidget->addItem(item);
433 
434  item = new QListWidgetItem("");
435  m_ui->m_outputListWidget->addItem(item);
436  }
437  }
438  }
439  }
440  int lastRow = m_ui->m_outputListWidget->count() - 1;
441  delete m_ui->m_outputListWidget->item(lastRow);
442 }
443 
445 {
446  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
447 
448  std::string layerID = m_ui->m_layersComboBox->itemData(index, Qt::UserRole).toString().toStdString();
449 
450  m_ui->m_propertieslistWidget->clear();
451 
452  while(it != m_layers.end())
453  {
454  if(layerID == it->get()->getId().c_str())
455  {
456  std::size_t type;
457  te::map::AbstractLayerPtr selectedLayer = it->get();
458  m_selectedLayer = selectedLayer;
459  std::auto_ptr<const te::map::LayerSchema> schema(selectedLayer->getSchema());
460 
461  if(schema->size() == 0)
462  return;
463 
464  te::common::FreeContents(m_properties);
465  m_properties.clear();
466 
467  const std::vector<te::dt::Property*>& properties = schema->getProperties();
468 
469  te::common::Clone(properties, m_properties);
470 
471  setFunctionsByLayer(m_properties);
472 
473  for(size_t i = 0; i < m_properties.size(); ++i)
474  {
475  type = m_properties[i]->getType();
476 
477  if(type != te::dt::GEOMETRY_TYPE)
478  m_ui->m_propertieslistWidget->addItem(m_properties[i]->getName().c_str());
479  }
480 
481  return;
482  }
483  ++it;
484  }
485 }
486 
488 {
489  m_ui->m_outputListWidget->reset();
490  m_ui->m_outputStatisticsGroupBox->setVisible(visible);
491 }
492 
494 {
495  QList<QListWidgetItem*> allItems;
496  int count = m_ui->m_propertieslistWidget->count();
497  for(int index = 0; index < count; ++index)
498  {
499  allItems.push_back(m_ui->m_propertieslistWidget->item(index));
500  }
501 
502  QList<QListWidgetItem*> filteredItems = m_ui->m_propertieslistWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive);
503 
504  for(int i = 0; i < allItems.size(); ++i)
505  {
506  QListWidgetItem* item = allItems.at(i);
507  bool hide = filteredItems.indexOf(item) == -1;
508  item->setHidden(hide);
509  }
510 
511  m_ui->m_propertieslistWidget->update();
512 }
513 
515 {
516  QString text = m_ui->m_selectAllComboBox->itemText(index);
517  Qt::MatchFlags flag = Qt::MatchEndsWith; //The search term matches the end of the item.
518 
519  if(text.isEmpty())
520  return;
521 
522  QList<QListWidgetItem *> listFound;
523  listFound = m_ui->m_outputListWidget->findItems(text, flag);
524 
525  for(int i=0; i < listFound.size(); ++i)
526  listFound.at(i)->setSelected(true);
527 
528  m_ui->m_rejectAllComboBox->setCurrentIndex(0);
529 }
530 
532 {
533  QString text = m_ui->m_selectAllComboBox->itemText(index);
534  Qt::MatchFlags flag = Qt::MatchEndsWith; //The search term matches the end of the item.
535 
536  if(text=="")
537  return;
538 
539  QList<QListWidgetItem *> listFound;
540  listFound = m_ui->m_outputListWidget->findItems(text, flag);
541 
542  for(int i=0; i < listFound.size(); ++i)
543  listFound.at(i)->setSelected(false);
544 
545  m_ui->m_selectAllComboBox->setCurrentIndex(0);
546 }
547 
549 {
550  if(item->text().isEmpty())
551  {
552  item->setSelected(false);
553  }
554 }
555 
557 {
558  m_ui->m_newLayerNameLineEdit->clear();
559  m_ui->m_newLayerNameLineEdit->setEnabled(true);
561  dlg.exec();
562 
563  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
564 
565  if(dsPtrList.size() <= 0)
566  return;
567 
568  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
569 
570  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
571 
572  m_outputDatasource = *it;
573 
574  m_toFile = false;
575 }
576 
578 {
579  m_ui->m_newLayerNameLineEdit->clear();
580  m_ui->m_repositoryLineEdit->clear();
581 
582  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."),
583  QString(), tr("Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
584 
585  if (fileName.isEmpty())
586  return;
587 
588  boost::filesystem::path outfile(fileName.toStdString());
589  std::string aux = outfile.leaf().string();
590  m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
591  aux = outfile.string();
592  m_ui->m_repositoryLineEdit->setText(aux.c_str());
593 
594  m_toFile = true;
595  m_ui->m_newLayerNameLineEdit->setEnabled(false);
596 }
597 
599 {
600  QMessageBox::information(this, "Help", "Under development");
601 }
602 
604 {
605  if(m_ui->m_layersComboBox->count() == 0)
606  {
607  QMessageBox::information(this, "Aggregation", "Select an input layer.");
608  return;
609  }
610 
611  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(m_selectedLayer.get());
612 
613  if(!dsLayer)
614  {
615  QMessageBox::information(this, "Aggregation", "Can not execute this operation on this type of layer.");
616  return;
617  }
618 
619  const te::da::ObjectIdSet* oidSet = 0;
620 
621  if(m_ui->m_onlySelectedCheckBox->isChecked())
622  {
623  oidSet = m_selectedLayer->getSelected();
624  if(!oidSet)
625  {
626  QMessageBox::information(this, "Aggregation", "Select the layer objects to perform the aggregation operation.");
627  return;
628  }
629  }
630 
631  te::da::DataSourcePtr inDataSource = te::da::GetDataSource(dsLayer->getDataSourceId(), true);
632  if (!inDataSource.get())
633  {
634  QMessageBox::information(this, "Aggregation", "The selected input data source can not be accessed.");
635  return;
636  }
637 
638  std::vector<te::dt::Property*> selProperties = getSelectedProperties();
639  if(selProperties.empty())
640  {
641  QMessageBox::information(this, "Aggregation", "Select at least one grouping attribute.");
642  return;
643  }
644 
645  std::string outputdataset = m_ui->m_newLayerNameLineEdit->text().toStdString();
646 
647  if(m_ui->m_repositoryLineEdit->text().isEmpty())
648  {
649  QMessageBox::information(this, "Aggregation", "Define a repository for the result.");
650  return;
651  }
652 
653  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
654  {
655  QMessageBox::information(this, "Aggregation", "Define a name for the resulting layer.");
656  return;
657  }
658 
659  //progress
662 
663  try
664  {
665  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > outputStatisticalSummary = getStatisticalSummary();
666 
667  bool res;
668 
669  if (m_toFile)
670  {
671  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
672 
673  if (boost::filesystem::exists(uri))
674  {
675  QMessageBox::information(this, "Aggregation", "Output file already exists. Remove it or select a new name and try again.");
676  return;
677  }
678 
679  std::size_t idx = outputdataset.find(".");
680  if (idx != std::string::npos)
681  outputdataset=outputdataset.substr(0,idx);
682 
683  std::map<std::string, std::string> dsinfo;
684  dsinfo["URI"] = uri.string();
685 
687  dsOGR->setConnectionInfo(dsinfo);
688  dsOGR->open();
689  if (dsOGR->dataSetExists(outputdataset))
690  {
691  QMessageBox::information(this, "Aggregation", "There is already a dataset with the requested name in the output data source. Remove it or select a new name and try again.");
692  return;
693  }
694 
695  this->setCursor(Qt::WaitCursor);
696 
697  te::vp::AggregationOp* aggregOp = 0;
698 
699  // select a strategy based on the capabilities of the input datasource
700  const te::da::DataSourceCapabilities dsCapabilities = inDataSource->getCapabilities();
701 
702  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
703  {
704  aggregOp = new te::vp::AggregationQuery();
705  }
706  else
707  {
708  aggregOp = new te::vp::AggregationMemory();
709  }
710 
711  aggregOp->setInput(inDataSource, dsLayer->getDataSetName(),dsLayer->getSchema(), oidSet);
712  aggregOp->setOutput(dsOGR, outputdataset);
713  aggregOp->setParams(selProperties, outputStatisticalSummary);
714 
715  if (!aggregOp->paramsAreValid())
716  res = false;
717  else
718  res = aggregOp->run();
719 
720  if (!res)
721  {
722  this->setCursor(Qt::ArrowCursor);
723  dsOGR->close();
724  QMessageBox::information(this, "Aggregation", "Error: could not generate the aggregation.");
725  reject();
726  }
727  dsOGR->close();
728 
729  delete aggregOp;
730 
731  // let's include the new datasource in the managers
732  boost::uuids::basic_random_generator<boost::mt19937> gen;
733  boost::uuids::uuid u = gen();
734  std::string id_ds = boost::uuids::to_string(u);
735 
737  ds->setConnInfo(dsinfo);
738  ds->setTitle(uri.stem().string());
739  ds->setAccessDriver("OGR");
740  ds->setType("OGR");
741  ds->setDescription(uri.string());
742  ds->setId(id_ds);
743 
744  te::da::DataSourcePtr newds = te::da::DataSourceManager::getInstance().get(id_ds, "OGR", ds->getConnInfo());
745  newds->open();
747  m_outputDatasource = ds;
748  }
749  else
750  {
751  te::da::DataSourcePtr aux = te::da::GetDataSource(m_outputDatasource->getId());
752  if (!aux)
753  {
754  QMessageBox::information(this, "Aggregation", "The selected output datasource can not be accessed.");
755  return;
756  }
757 
758  if (aux->dataSetExists(outputdataset))
759  {
760  QMessageBox::information(this, "Aggregation", "Dataset already exists. Remove it or select a new name and try again.");
761  return;
762  }
763  this->setCursor(Qt::WaitCursor);
764 
765  te::vp::AggregationOp* aggregOp = 0;
766 
767  // select a strategy based on the capabilities of the input datasource
768  const te::da::DataSourceCapabilities dsCapabilities = inDataSource->getCapabilities();
769 
770  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
771  {
772  aggregOp = new te::vp::AggregationQuery();
773  }
774  else
775  {
776  aggregOp = new te::vp::AggregationMemory();
777  }
778 
779  aggregOp->setInput(inDataSource, dsLayer->getDataSetName(), dsLayer->getSchema(), oidSet);
780  aggregOp->setOutput(aux, outputdataset);
781  aggregOp->setParams(selProperties, outputStatisticalSummary);
782 
783  if (!aggregOp->paramsAreValid())
784  res = false;
785  else
786  res = aggregOp->run();
787 
788  delete aggregOp;
789 
790  if (!res)
791  {
792  this->setCursor(Qt::ArrowCursor);
793  QMessageBox::information(this, "Aggregation", "Error: could not generate the aggregation.");
794  reject();
795  }
796  }
797 
798  // creating a layer for the result
799  te::da::DataSourcePtr outDataSource = te::da::GetDataSource(m_outputDatasource->getId());
800 
801  te::qt::widgets::DataSet2Layer converter(m_outputDatasource->getId());
802 
803  te::da::DataSetTypePtr dt(outDataSource->getDataSetType(outputdataset).release());
804  m_layer = converter(dt);
805  }
806  catch(const std::exception& e)
807  {
808  this->setCursor(Qt::ArrowCursor);
809 
810  QMessageBox::information(this, "Aggregation", e.what());
811 
812 #ifdef TERRALIB_LOGGER_ENABLED
813  std::string str = "Aggregation - ";
814  str += e.what();
815  te::common::Logger::logDebug("vp", str.c_str());
816 #endif // TERRALIB_LOGGER_ENABLED
817 
819  return;
820  }
821 
823  this->setCursor(Qt::ArrowCursor);
824 
825  accept();
826 }
827 
829 {
830  reject();
831 }
832 
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
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::auto_ptr< te::da::DataSetType > inDsetType, const te::da::ObjectIdSet *oidSet=0)
Mean.
Definition: Enums.h:43
A dialog aggregation operation.
Utility functions for the data access module.
const std::string & getDataSetName() const
te::da::DataSourceInfoPtr m_outputDatasource
DataSource information.
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
Skewness.
Definition: Enums.h:49
Total not null values.
Definition: Enums.h:46
Total number of values.
Definition: Enums.h:45
void onCalculateStatistics(bool visible)
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
const QueryCapabilities & getQueryCapabilities() const
It models a property definition.
Definition: Property.h:59
void onFilterLineEditTextChanged(const QString &text)
Minimum value.
Definition: Enums.h:41
std::auto_ptr< Ui::AggregationDialogForm > m_ui
void onOutputListWidgetClicked(QListWidgetItem *item)
virtual bool paramsAreValid()
void removeViewer(int viewerId)
Dettach a progress viewer.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
te::dt::Property * getSelectedPropertyByName(std::string propertyName)
Get the selected property based on selected QListWidgetItem using the name of property.
const std::string & getDataSourceId() const
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
Median.
Definition: Enums.h:52
static std::auto_ptr< DataSource > make(const std::string &dsType)
Kurtosis.
Definition: Enums.h:50
Standard deviation.
Definition: Enums.h:47
Sum of values.
Definition: Enums.h:44
TESTATEXPORT std::string GetStatSummaryFullName(const int &e)
Get the statistical parameter full name ffrom its enumerator.
Definition: Utils.cpp:88
bool supportsSpatialSQLDialect() const
void setStatisticalSummaryMap()
Map Statistical Summary Type enum for an intuitive name.
te::map::AbstractLayerPtr getLayer()
Get the generated layer.
AggregationDialog(QWidget *parent=0, Qt::WindowFlags f=0)
std::map< te::dt::Property *, std::vector< te::stat::StatisticalSummary > > getStatisticalSummary()
Get the Grouping Functions Type based on selected QListWidgetItem.
Coefficient variation.
Definition: Enums.h:53
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
void setStatisticalSummary()
Set Statistical Summary Type for combobox 'm_selectAllComboBox' and 'm_rejectAllComboBox' based on a ...
void setParams(std::vector< te::dt::Property * > &groupProps, std::map< te::dt::Property *, std::vector< te::stat::StatisticalSummary > > &statSum)
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
void onSelectAllComboBoxChanged(int index)
Mode.
Definition: Enums.h:54
A dialog for selecting a data source.
void onLayerComboBoxChanged(int index)
A class that represents a data source component.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
void setFunctionsByLayer(std::vector< te::dt::Property * > properties)
Set Grouping Functions Type for 'm_outputListWidget' based on Selected Layer.
std::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
StatisticalSummary
Define grouping functions type.
Definition: Enums.h:39
void Clone(const std::vector< T * > &src, std::vector< T * > &dst)
This function can be applied to a vector of pointers.
Definition: STLUtils.h:237
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
Variance.
Definition: Enums.h:48
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
Maximum value.
Definition: Enums.h:42
void onRejectAllComboBoxChanged(int index)
virtual bool run()=0
Amplitude.
Definition: Enums.h:51
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
std::vector< te::dt::Property * > getSelectedProperties()
Get the selected properties based on selected QListWidgetItem.