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_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
99  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
100 
101  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
102  m_ui->m_helpPushButton->setPageReference("plugins/vp/vp_aggregation.html");
103 
105  m_ui->m_newLayerNameLineEdit->setEnabled(true);
106 }
107 
109 {
110 }
111 
112 void te::vp::AggregationDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers, te::map::AbstractLayerPtr selectedLayer)
113 {
114  if (!selectedLayer)
115  {
116  m_layers = layers;
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  else
128  {
129  m_layers = layers;
130 
131  std::auto_ptr<te::da::DataSetType> dsType = selectedLayer->getSchema();
132  if (dsType->hasGeom())
133  m_ui->m_layersComboBox->addItem(QString(selectedLayer->getTitle().c_str()), QVariant(selectedLayer->getId().c_str()));
134 
135  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
136 
137  while (it != m_layers.end())
138  {
139  if (*it != selectedLayer)
140  {
141  std::auto_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
142  if (dsType->hasGeom())
143  m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
144  }
145  ++it;
146  }
147  }
148 }
149 
150 std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > te::vp::AggregationDialog::getStatisticalSummary()
151 {
152  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > outputStatisticalSummary;
153 
154  QList<QListWidgetItem*> itemList = m_ui->m_outputListWidget->selectedItems();
155 
156  te::stat::StatisticalSummary enumStatisticalSummary;
157  std::map<std::string, std::vector<te::stat::StatisticalSummary> > propname_stat;
158  std::map<std::string, std::vector<te::stat::StatisticalSummary> >::iterator it;
159 
160  for(int i = 0; i < itemList.size(); ++i)
161  {
162  std::vector<std::string> tokens;
163  std::string pname;
164  std::string auxItem = itemList[i]->text().toStdString();
165 
166  boost::split(tokens, auxItem, boost::is_any_of(":"));
167  if(tokens[0] != "")
168  {
169  pname = tokens[0];
170  pname.erase(pname.end() - 1);
171  enumStatisticalSummary = (te::stat::StatisticalSummary)itemList[i]->data(Qt::UserRole).toInt();
172  it = propname_stat.find(pname);
173  if (it != propname_stat.end())
174  it->second.push_back(enumStatisticalSummary);
175  else
176  {
177  std::vector<te::stat::StatisticalSummary> nvec;
178  nvec.push_back(enumStatisticalSummary);
179  propname_stat.insert(std::make_pair(pname, nvec));
180  }
181  }
182  }
183 
184  it = propname_stat.begin();
185  while (it != propname_stat.end())
186  {
187  te::dt::Property* prop = getSelectedPropertyByName(it->first);
188  outputStatisticalSummary.insert(std::make_pair(prop,it->second));
189  ++it;
190  }
191 
192  return outputStatisticalSummary;
193 }
194 
196 {
197  te::dt::Property* selProperty;
198  if(propertyName == "")
199  return 0;
200 
201  for(std::size_t i = 0; i < m_properties.size(); ++i)
202  {
203  if(propertyName == m_properties[i]->getName())
204  {
205  selProperty = m_properties[i];
206  return selProperty;
207  }
208  }
209  return 0;
210 }
211 
212 std::vector<te::dt::Property*> te::vp::AggregationDialog::getSelectedProperties()
213 {
214  std::vector<te::dt::Property*> selProperties;
215 
216  for(int i = 0; i != m_ui->m_propertieslistWidget->count(); ++i)
217  {
218  if(m_ui->m_propertieslistWidget->isItemSelected(m_ui->m_propertieslistWidget->item(i)))
219  {
220  selProperties.push_back(m_properties[i]);
221  }
222  }
223 
224  return selProperties;
225 }
226 
228 {
229  return m_layer;
230 }
231 
233 {
234  m_ui->m_selectAllComboBox->addItem("");
235  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE).c_str()), te::stat::MIN_VALUE);
236  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE).c_str()), te::stat::MAX_VALUE);
237  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEAN).c_str()), te::stat::MEAN);
238  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SUM).c_str()), te::stat::SUM);
239  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::COUNT).c_str()), te::stat::COUNT);
240  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VALID_COUNT).c_str()), te::stat::VALID_COUNT);
241  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::STANDARD_DEVIATION).c_str()), te::stat::STANDARD_DEVIATION);
242  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VARIANCE).c_str()), te::stat::VARIANCE);
243  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SKEWNESS).c_str()), te::stat::SKEWNESS);
244  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::KURTOSIS).c_str()), te::stat::KURTOSIS);
245  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::AMPLITUDE).c_str()), te::stat::AMPLITUDE);
246  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEDIAN).c_str()), te::stat::MEDIAN);
247  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VAR_COEFF).c_str()), te::stat::VAR_COEFF);
248  m_ui->m_selectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MODE).c_str()), te::stat::MODE);
249 
250  m_ui->m_rejectAllComboBox->addItem("");
251  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE).c_str()), te::stat::MIN_VALUE);
252  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE).c_str()), te::stat::MAX_VALUE);
253  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEAN).c_str()), te::stat::MEAN);
254  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SUM).c_str()), te::stat::SUM);
255  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::COUNT).c_str()), te::stat::COUNT);
256  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VALID_COUNT).c_str()), te::stat::VALID_COUNT);
257  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::STANDARD_DEVIATION).c_str()), te::stat::STANDARD_DEVIATION);
258  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VARIANCE).c_str()), te::stat::VARIANCE);
259  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SKEWNESS).c_str()), te::stat::SKEWNESS);
260  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::KURTOSIS).c_str()), te::stat::KURTOSIS);
261  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::AMPLITUDE).c_str()), te::stat::AMPLITUDE);
262  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEDIAN).c_str()), te::stat::MEDIAN);
263  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VAR_COEFF).c_str()), te::stat::VAR_COEFF);
264  m_ui->m_rejectAllComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MODE).c_str()), te::stat::MODE);
265 }
266 
268 {
269  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MIN_VALUE, te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE)));
270  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MAX_VALUE, te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE)));
271  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MEAN, te::stat::GetStatSummaryFullName(te::stat::MEAN)));
272  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::SUM, te::stat::GetStatSummaryFullName(te::stat::SUM)));
273  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::COUNT, te::stat::GetStatSummaryFullName(te::stat::COUNT)));
274  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::VALID_COUNT, te::stat::GetStatSummaryFullName(te::stat::VALID_COUNT)));
275  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::STANDARD_DEVIATION, te::stat::GetStatSummaryFullName(te::stat::STANDARD_DEVIATION)));
276  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::VARIANCE, te::stat::GetStatSummaryFullName(te::stat::VARIANCE)));
277  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::SKEWNESS, te::stat::GetStatSummaryFullName(te::stat::SKEWNESS)));
278  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::KURTOSIS, te::stat::GetStatSummaryFullName(te::stat::KURTOSIS)));
279  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::AMPLITUDE, te::stat::GetStatSummaryFullName(te::stat::AMPLITUDE)));
280  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MEDIAN, te::stat::GetStatSummaryFullName(te::stat::MEDIAN)));
281  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::VAR_COEFF, te::stat::GetStatSummaryFullName(te::stat::VAR_COEFF)));
282  m_StatisticalSummaryMap.insert(StaticalSummaryMap::value_type(te::stat::MODE, te::stat::GetStatSummaryFullName(te::stat::MODE)));
283 }
284 
285 void te::vp::AggregationDialog::setFunctionsByLayer(std::vector<te::dt::Property*> properties)
286 {
287  QStringList propertyList;
288  int propertyType;
289 
290  m_ui->m_selectAllComboBox->setCurrentIndex(0);
291  m_ui->m_rejectAllComboBox->setCurrentIndex(0);
292  m_ui->m_outputListWidget->clear();
293 
294  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(m_selectedLayer.get());
295  te::da::DataSourcePtr dataSource = te::da::GetDataSource(dsLayer->getDataSourceId(), true);
296  const te::da::DataSourceCapabilities dsCapabilities = dataSource->getCapabilities();
297 
298  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
299  {
300  for(size_t i=0; i < properties.size(); ++i)
301  {
302  propertyType = properties[i]->getType();
303  if(propertyType != te::dt::GEOMETRY_TYPE)
304  {
305  if(propertyType == te::dt::STRING_TYPE)
306  {
307  QListWidgetItem* item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MIN_VALUE].c_str());
308  item->setData(Qt::UserRole, QVariant(te::stat::MIN_VALUE));
309  m_ui->m_outputListWidget->addItem(item);
310 
311  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MAX_VALUE].c_str());
312  item->setData(Qt::UserRole, QVariant(te::stat::MAX_VALUE));
313  m_ui->m_outputListWidget->addItem(item);
314 
315  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::COUNT].c_str());
316  item->setData(Qt::UserRole, QVariant(te::stat::COUNT));
317  m_ui->m_outputListWidget->addItem(item);
318 
319  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VALID_COUNT].c_str());
320  item->setData(Qt::UserRole, QVariant(te::stat::VALID_COUNT));
321  m_ui->m_outputListWidget->addItem(item);
322 
323  item = new QListWidgetItem("");
324  m_ui->m_outputListWidget->addItem(item);
325  }
326  else
327  {
328  QListWidgetItem* item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MIN_VALUE].c_str());
329  item->setData(Qt::UserRole, QVariant(te::stat::MIN_VALUE));
330  m_ui->m_outputListWidget->addItem(item);
331 
332  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MAX_VALUE].c_str());
333  item->setData(Qt::UserRole, QVariant(te::stat::MAX_VALUE));
334  m_ui->m_outputListWidget->addItem(item);
335 
336  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MEAN].c_str());
337  item->setData(Qt::UserRole, QVariant(te::stat::MEAN));
338  m_ui->m_outputListWidget->addItem(item);
339 
340  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::SUM].c_str());
341  item->setData(Qt::UserRole, QVariant(te::stat::SUM));
342  m_ui->m_outputListWidget->addItem(item);
343 
344  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::COUNT].c_str());
345  item->setData(Qt::UserRole, QVariant(te::stat::COUNT));
346  m_ui->m_outputListWidget->addItem(item);
347 
348  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VALID_COUNT].c_str());
349  item->setData(Qt::UserRole, QVariant(te::stat::VALID_COUNT));
350  m_ui->m_outputListWidget->addItem(item);
351 
352  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::STANDARD_DEVIATION].c_str());
353  item->setData(Qt::UserRole, QVariant(te::stat::STANDARD_DEVIATION));
354  m_ui->m_outputListWidget->addItem(item);
355 
356  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VARIANCE].c_str());
357  item->setData(Qt::UserRole, QVariant(te::stat::VARIANCE));
358  m_ui->m_outputListWidget->addItem(item);
359 
360  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::AMPLITUDE].c_str());
361  item->setData(Qt::UserRole, QVariant(te::stat::AMPLITUDE));
362  m_ui->m_outputListWidget->addItem(item);
363 
364  item = new QListWidgetItem("");
365  m_ui->m_outputListWidget->addItem(item);
366  }
367  }
368  }
369  }
370  else
371  {
372  for(size_t i=0; i < properties.size(); ++i)
373  {
374  propertyType = properties[i]->getType();
375  if(propertyType != te::dt::GEOMETRY_TYPE)
376  {
377  if(propertyType == te::dt::STRING_TYPE)
378  {
379  QListWidgetItem* item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MIN_VALUE].c_str());
380  item->setData(Qt::UserRole, QVariant(te::stat::MIN_VALUE));
381  m_ui->m_outputListWidget->addItem(item);
382 
383  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MAX_VALUE].c_str());
384  item->setData(Qt::UserRole, QVariant(te::stat::MAX_VALUE));
385  m_ui->m_outputListWidget->addItem(item);
386 
387  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::COUNT].c_str());
388  item->setData(Qt::UserRole, QVariant(te::stat::COUNT));
389  m_ui->m_outputListWidget->addItem(item);
390 
391  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VALID_COUNT].c_str());
392  item->setData(Qt::UserRole, QVariant(te::stat::VALID_COUNT));
393  m_ui->m_outputListWidget->addItem(item);
394 
395  item = new QListWidgetItem("");
396  m_ui->m_outputListWidget->addItem(item);
397  }
398  else
399  {
400  QListWidgetItem* item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MIN_VALUE].c_str());
401  item->setData(Qt::UserRole, QVariant(te::stat::MIN_VALUE));
402  m_ui->m_outputListWidget->addItem(item);
403 
404  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MAX_VALUE].c_str());
405  item->setData(Qt::UserRole, QVariant(te::stat::MAX_VALUE));
406  m_ui->m_outputListWidget->addItem(item);
407 
408  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MEAN].c_str());
409  item->setData(Qt::UserRole, QVariant(te::stat::MEAN));
410  m_ui->m_outputListWidget->addItem(item);
411 
412  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::SUM].c_str());
413  item->setData(Qt::UserRole, QVariant(te::stat::SUM));
414  m_ui->m_outputListWidget->addItem(item);
415 
416  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::COUNT].c_str());
417  item->setData(Qt::UserRole, QVariant(te::stat::COUNT));
418  m_ui->m_outputListWidget->addItem(item);
419 
420  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VALID_COUNT].c_str());
421  item->setData(Qt::UserRole, QVariant(te::stat::VALID_COUNT));
422  m_ui->m_outputListWidget->addItem(item);
423 
424  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::STANDARD_DEVIATION].c_str());
425  item->setData(Qt::UserRole, QVariant(te::stat::STANDARD_DEVIATION));
426  m_ui->m_outputListWidget->addItem(item);
427 
428  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VARIANCE].c_str());
429  item->setData(Qt::UserRole, QVariant(te::stat::VARIANCE));
430  m_ui->m_outputListWidget->addItem(item);
431 
432  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::SKEWNESS].c_str());
433  item->setData(Qt::UserRole, QVariant(te::stat::SKEWNESS));
434  m_ui->m_outputListWidget->addItem(item);
435 
436  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::KURTOSIS].c_str());
437  item->setData(Qt::UserRole, QVariant(te::stat::KURTOSIS));
438  m_ui->m_outputListWidget->addItem(item);
439 
440  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::AMPLITUDE].c_str());
441  item->setData(Qt::UserRole, QVariant(te::stat::AMPLITUDE));
442  m_ui->m_outputListWidget->addItem(item);
443 
444  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MEDIAN].c_str());
445  item->setData(Qt::UserRole, QVariant(te::stat::MEDIAN));
446  m_ui->m_outputListWidget->addItem(item);
447 
448  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::VAR_COEFF].c_str());
449  item->setData(Qt::UserRole, QVariant(te::stat::VAR_COEFF));
450  m_ui->m_outputListWidget->addItem(item);
451 
452  item = new QListWidgetItem(QString(properties[i]->getName().c_str()) + " : " + m_StatisticalSummaryMap[te::stat::MODE].c_str());
453  item->setData(Qt::UserRole, QVariant(te::stat::MODE));
454  m_ui->m_outputListWidget->addItem(item);
455 
456  item = new QListWidgetItem("");
457  m_ui->m_outputListWidget->addItem(item);
458  }
459  }
460  }
461  }
462  int lastRow = m_ui->m_outputListWidget->count() - 1;
463  delete m_ui->m_outputListWidget->item(lastRow);
464 }
465 
467 {
468  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
469 
470  std::string layerID = m_ui->m_layersComboBox->itemData(index, Qt::UserRole).toString().toStdString();
471 
472  m_ui->m_propertieslistWidget->clear();
473 
474  while(it != m_layers.end())
475  {
476  if(layerID == it->get()->getId().c_str())
477  {
478  std::size_t type;
479  te::map::AbstractLayerPtr selectedLayer = it->get();
480  m_selectedLayer = selectedLayer;
481  std::auto_ptr<const te::map::LayerSchema> schema(selectedLayer->getSchema());
482 
483  if(schema->size() == 0)
484  return;
485 
486  te::common::FreeContents(m_properties);
487  m_properties.clear();
488 
489  const std::vector<te::dt::Property*>& properties = schema->getProperties();
490 
491  te::common::Clone(properties, m_properties);
492 
493  setFunctionsByLayer(m_properties);
494 
495  for(size_t i = 0; i < m_properties.size(); ++i)
496  {
497  type = m_properties[i]->getType();
498 
499  if(type != te::dt::GEOMETRY_TYPE)
500  m_ui->m_propertieslistWidget->addItem(m_properties[i]->getName().c_str());
501  }
502 
503  return;
504  }
505  ++it;
506  }
507 }
508 
510 {
511  m_ui->m_outputListWidget->reset();
512  m_ui->m_outputStatisticsGroupBox->setVisible(visible);
513 }
514 
516 {
517  QList<QListWidgetItem*> allItems;
518  int count = m_ui->m_propertieslistWidget->count();
519  for(int index = 0; index < count; ++index)
520  {
521  allItems.push_back(m_ui->m_propertieslistWidget->item(index));
522  }
523 
524  QList<QListWidgetItem*> filteredItems = m_ui->m_propertieslistWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive);
525 
526  for(int i = 0; i < allItems.size(); ++i)
527  {
528  QListWidgetItem* item = allItems.at(i);
529  bool hide = filteredItems.indexOf(item) == -1;
530  item->setHidden(hide);
531  }
532 
533  m_ui->m_propertieslistWidget->update();
534 }
535 
537 {
538  QString text = m_ui->m_selectAllComboBox->itemText(index);
539  Qt::MatchFlags flag = Qt::MatchEndsWith; //The search term matches the end of the item.
540 
541  if(text.isEmpty())
542  return;
543 
544  QList<QListWidgetItem *> listFound;
545  listFound = m_ui->m_outputListWidget->findItems(text, flag);
546 
547  for(int i=0; i < listFound.size(); ++i)
548  listFound.at(i)->setSelected(true);
549 
550  m_ui->m_rejectAllComboBox->setCurrentIndex(0);
551 }
552 
554 {
555  QString text = m_ui->m_selectAllComboBox->itemText(index);
556  Qt::MatchFlags flag = Qt::MatchEndsWith; //The search term matches the end of the item.
557 
558  if(text=="")
559  return;
560 
561  QList<QListWidgetItem *> listFound;
562  listFound = m_ui->m_outputListWidget->findItems(text, flag);
563 
564  for(int i=0; i < listFound.size(); ++i)
565  listFound.at(i)->setSelected(false);
566 
567  m_ui->m_selectAllComboBox->setCurrentIndex(0);
568 }
569 
571 {
572  if(item->text().isEmpty())
573  {
574  item->setSelected(false);
575  }
576 }
577 
579 {
580  m_ui->m_newLayerNameLineEdit->clear();
581  m_ui->m_newLayerNameLineEdit->setEnabled(true);
583  dlg.exec();
584 
585  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
586 
587  if(dsPtrList.empty())
588  return;
589 
590  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
591 
592  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
593 
594  m_outputDatasource = *it;
595 
596  m_toFile = false;
597 }
598 
600 {
601  m_ui->m_newLayerNameLineEdit->clear();
602  m_ui->m_repositoryLineEdit->clear();
603 
604  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."),
605  QString(), tr("Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
606 
607  if (fileName.isEmpty())
608  return;
609 
610  boost::filesystem::path outfile(fileName.toStdString());
611  std::string aux = outfile.leaf().string();
612  m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
613  aux = outfile.string();
614  m_ui->m_repositoryLineEdit->setText(aux.c_str());
615 
616  m_toFile = true;
617  m_ui->m_newLayerNameLineEdit->setEnabled(false);
618 }
619 
621 {
622  if(m_ui->m_layersComboBox->count() == 0)
623  {
624  QMessageBox::information(this, "Aggregation", "Select an input layer.");
625  return;
626  }
627 
628  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(m_selectedLayer.get());
629 
630  if(!dsLayer)
631  {
632  QMessageBox::information(this, "Aggregation", "Can not execute this operation on this type of layer.");
633  return;
634  }
635 
636  const te::da::ObjectIdSet* oidSet = 0;
637 
638  if(m_ui->m_onlySelectedCheckBox->isChecked())
639  {
640  oidSet = m_selectedLayer->getSelected();
641  if(!oidSet)
642  {
643  QMessageBox::information(this, "Aggregation", "Select the layer objects to perform the aggregation operation.");
644  return;
645  }
646  }
647 
648  te::da::DataSourcePtr inDataSource = te::da::GetDataSource(dsLayer->getDataSourceId(), true);
649  if (!inDataSource.get())
650  {
651  QMessageBox::information(this, "Aggregation", "The selected input data source can not be accessed.");
652  return;
653  }
654 
655  std::vector<te::dt::Property*> selProperties = getSelectedProperties();
656  if(selProperties.empty())
657  {
658  QMessageBox::information(this, "Aggregation", "Select at least one grouping attribute.");
659  return;
660  }
661 
662  std::string outputdataset = m_ui->m_newLayerNameLineEdit->text().toStdString();
663 
664  if(m_ui->m_repositoryLineEdit->text().isEmpty())
665  {
666  QMessageBox::information(this, "Aggregation", "Define a repository for the result.");
667  return;
668  }
669 
670  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
671  {
672  QMessageBox::information(this, "Aggregation", "Define a name for the resulting layer.");
673  return;
674  }
675 
676  //progress
679 
680  try
681  {
682  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > outputStatisticalSummary = getStatisticalSummary();
683 
684  bool res;
685 
686  if (m_toFile)
687  {
688  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
689 
690  if (boost::filesystem::exists(uri))
691  {
692  QMessageBox::information(this, "Aggregation", "Output file already exists. Remove it or select a new name and try again.");
693  return;
694  }
695 
696  std::size_t idx = outputdataset.find(".");
697  if (idx != std::string::npos)
698  outputdataset=outputdataset.substr(0,idx);
699 
700  std::map<std::string, std::string> dsinfo;
701  dsinfo["URI"] = uri.string();
702 
704  dsOGR->setConnectionInfo(dsinfo);
705  dsOGR->open();
706  if (dsOGR->dataSetExists(outputdataset))
707  {
708  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.");
709  return;
710  }
711 
712  std::auto_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(dsLayer->getSchema().get(), dsOGR->getCapabilities(), dsOGR->getEncoding()));
713 
714  te::da::AssociateDataSetTypeConverterSRID(converter.get(), dsLayer->getSRID());
715 
716  this->setCursor(Qt::WaitCursor);
717 
718  te::vp::AggregationOp* aggregOp = 0;
719 
720  // select a strategy based on the capabilities of the input datasource
721  const te::da::DataSourceCapabilities dsCapabilities = inDataSource->getCapabilities();
722 
723  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
724  {
725  aggregOp = new te::vp::AggregationQuery();
726  }
727  else
728  {
729  aggregOp = new te::vp::AggregationMemory();
730  }
731 
732  aggregOp->setInput(inDataSource, dsLayer->getDataSetName(), converter, oidSet);
733  aggregOp->setOutput(dsOGR, outputdataset);
734  aggregOp->setParams(selProperties, outputStatisticalSummary);
735 
736  if (!aggregOp->paramsAreValid())
737  res = false;
738  else
739  res = aggregOp->run();
740 
741  if (!res)
742  {
743  this->setCursor(Qt::ArrowCursor);
744  dsOGR->close();
745  QMessageBox::information(this, "Aggregation", "Error: could not generate the aggregation.");
746  reject();
747  }
748  dsOGR->close();
749 
750  delete aggregOp;
751 
752  // let's include the new datasource in the managers
753  boost::uuids::basic_random_generator<boost::mt19937> gen;
754  boost::uuids::uuid u = gen();
755  std::string id_ds = boost::uuids::to_string(u);
756 
758  ds->setConnInfo(dsinfo);
759  ds->setTitle(uri.stem().string());
760  ds->setAccessDriver("OGR");
761  ds->setType("OGR");
762  ds->setDescription(uri.string());
763  ds->setId(id_ds);
764 
765  te::da::DataSourcePtr newds = te::da::DataSourceManager::getInstance().get(id_ds, "OGR", ds->getConnInfo());
766  newds->open();
768  m_outputDatasource = ds;
769  }
770  else
771  {
772  te::da::DataSourcePtr aux = te::da::GetDataSource(m_outputDatasource->getId());
773  if (!aux)
774  {
775  QMessageBox::information(this, "Aggregation", "The selected output datasource can not be accessed.");
776  return;
777  }
778 
779  if (aux->dataSetExists(outputdataset))
780  {
781  QMessageBox::information(this, "Aggregation", "Dataset already exists. Remove it or select a new name and try again.");
782  return;
783  }
784  this->setCursor(Qt::WaitCursor);
785 
786  std::auto_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(dsLayer->getSchema().get(), aux->getCapabilities(), aux->getEncoding()));
787 
788  te::da::AssociateDataSetTypeConverterSRID(converter.get(), dsLayer->getSRID());
789 
790  te::vp::AggregationOp* aggregOp = 0;
791 
792  // select a strategy based on the capabilities of the input datasource
793  const te::da::DataSourceCapabilities dsCapabilities = inDataSource->getCapabilities();
794 
795  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
796  {
797  aggregOp = new te::vp::AggregationQuery();
798  }
799  else
800  {
801  aggregOp = new te::vp::AggregationMemory();
802  }
803 
804  aggregOp->setInput(inDataSource, dsLayer->getDataSetName(), converter, oidSet);
805  aggregOp->setOutput(aux, outputdataset);
806  aggregOp->setParams(selProperties, outputStatisticalSummary);
807 
808  if (!aggregOp->paramsAreValid())
809  res = false;
810  else
811  res = aggregOp->run();
812 
813  delete aggregOp;
814 
815  if (!res)
816  {
817  this->setCursor(Qt::ArrowCursor);
818  QMessageBox::information(this, "Aggregation", "Error: could not generate the aggregation.");
819  reject();
820  }
821  }
822 
823  // creating a layer for the result
824  te::da::DataSourcePtr outDataSource = te::da::GetDataSource(m_outputDatasource->getId());
825 
826  te::qt::widgets::DataSet2Layer converter(m_outputDatasource->getId());
827 
828  te::da::DataSetTypePtr dt(outDataSource->getDataSetType(outputdataset).release());
829  m_layer = converter(dt);
830  }
831  catch(const std::exception& e)
832  {
833  this->setCursor(Qt::ArrowCursor);
834 
835  QMessageBox::information(this, "Aggregation", e.what());
836 
837 #ifdef TERRALIB_LOGGER_ENABLED
838  std::string str = "Aggregation - ";
839  str += e.what();
840  te::common::Logger::logDebug("vp", str.c_str());
841 #endif // TERRALIB_LOGGER_ENABLED
842 
844  return;
845  }
846 
848  this->setCursor(Qt::ArrowCursor);
849 
850  accept();
851 }
852 
854 {
855  reject();
856 }
857 
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
Mean.
Definition: Enums.h:43
A dialog aggregation operation.
Utility functions for the data access module.
const std::string & getDataSetName() const
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::auto_ptr< te::da::DataSetTypeConverter > converter, const te::da::ObjectIdSet *oidSet=0)
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
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
Definition: Utils.cpp:670
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
An converter for DataSetType.
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)
URI C++ Library.
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:94
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
void setLayers(std::list< te::map::AbstractLayerPtr > layers, te::map::AbstractLayerPtr selectedLayer=0)
Set the layer that can be used.
A dialog for selecting a data source.
void onLayerComboBoxChanged(int index)
A class that represents a data source component.
virtual int getSRID() const
It returns the Spatial Reference System ID associated to the Layer.
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.