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"
50 #include "../AggregationMemory.h"
51 #include "../AggregationOp.h"
52 #include "../AggregationQuery.h"
53 #include "ui_AggregationDialogForm.h"
57 #include <QFileDialog>
59 #include <QListWidget>
60 #include <QListWidgetItem>
61 #include <QMessageBox>
62 #include <QTreeWidget>
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>
72 m_ui(new Ui::AggregationDialogForm),
80 m_ui->m_outputStatisticsGroupBox->setVisible(
false);
83 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"vp-aggregation-hint").pixmap(112,48));
84 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
102 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
103 m_ui->m_helpPushButton->setPageReference(
"plugins/vp/vp_aggregation.html");
106 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
117 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
119 while(it != m_layers.end())
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()));
130 std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > outputStatisticalSummary;
132 QList<QListWidgetItem*> itemList = m_ui->m_outputListWidget->selectedItems();
135 std::map<std::string, std::vector<te::stat::StatisticalSummary> > propname_stat;
136 std::map<std::string, std::vector<te::stat::StatisticalSummary> >::iterator it;
138 for(
int i = 0; i < itemList.size(); ++i)
140 std::vector<std::string> tokens;
142 std::string auxItem = itemList[i]->text().toStdString();
144 boost::split(tokens, auxItem, boost::is_any_of(
":"));
148 pname.erase(pname.end() - 1);
150 it = propname_stat.find(pname);
151 if (it != propname_stat.end())
152 it->second.push_back(enumStatisticalSummary);
155 std::vector<te::stat::StatisticalSummary> nvec;
156 nvec.push_back(enumStatisticalSummary);
157 propname_stat.insert(std::make_pair(pname, nvec));
162 it = propname_stat.begin();
163 while (it != propname_stat.end())
166 outputStatisticalSummary.insert(std::make_pair(prop,it->second));
170 return outputStatisticalSummary;
176 if(propertyName ==
"")
179 for(std::size_t i = 0; i < m_properties.size(); ++i)
181 if(propertyName == m_properties[i]->getName())
183 selProperty = m_properties[i];
192 std::vector<te::dt::Property*> selProperties;
194 for(std::size_t i = 0; i != m_ui->m_propertieslistWidget->count(); ++i)
196 if(m_ui->m_propertieslistWidget->isItemSelected(m_ui->m_propertieslistWidget->item(i)))
198 selProperties.push_back(m_properties[i]);
202 return selProperties;
212 m_ui->m_selectAllComboBox->addItem(
"");
228 m_ui->m_rejectAllComboBox->addItem(
"");
265 QStringList propertyList;
268 m_ui->m_selectAllComboBox->setCurrentIndex(0);
269 m_ui->m_rejectAllComboBox->setCurrentIndex(0);
270 m_ui->m_outputListWidget->clear();
276 if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
278 for(
size_t i=0; i < properties.size(); ++i)
280 propertyType = properties[i]->getType();
285 QListWidgetItem* item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MIN_VALUE].c_str());
287 m_ui->m_outputListWidget->addItem(item);
289 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MAX_VALUE].c_str());
291 m_ui->m_outputListWidget->addItem(item);
293 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::COUNT].c_str());
295 m_ui->m_outputListWidget->addItem(item);
297 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VALID_COUNT].c_str());
299 m_ui->m_outputListWidget->addItem(item);
301 item =
new QListWidgetItem(
"");
302 m_ui->m_outputListWidget->addItem(item);
306 QListWidgetItem* item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MIN_VALUE].c_str());
308 m_ui->m_outputListWidget->addItem(item);
310 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MAX_VALUE].c_str());
312 m_ui->m_outputListWidget->addItem(item);
314 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MEAN].c_str());
316 m_ui->m_outputListWidget->addItem(item);
318 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::SUM].c_str());
320 m_ui->m_outputListWidget->addItem(item);
322 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::COUNT].c_str());
324 m_ui->m_outputListWidget->addItem(item);
326 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VALID_COUNT].c_str());
328 m_ui->m_outputListWidget->addItem(item);
330 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::STANDARD_DEVIATION].c_str());
332 m_ui->m_outputListWidget->addItem(item);
334 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VARIANCE].c_str());
336 m_ui->m_outputListWidget->addItem(item);
338 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::AMPLITUDE].c_str());
340 m_ui->m_outputListWidget->addItem(item);
342 item =
new QListWidgetItem(
"");
343 m_ui->m_outputListWidget->addItem(item);
350 for(
size_t i=0; i < properties.size(); ++i)
352 propertyType = properties[i]->getType();
357 QListWidgetItem* item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MIN_VALUE].c_str());
359 m_ui->m_outputListWidget->addItem(item);
361 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MAX_VALUE].c_str());
363 m_ui->m_outputListWidget->addItem(item);
365 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::COUNT].c_str());
367 m_ui->m_outputListWidget->addItem(item);
369 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VALID_COUNT].c_str());
371 m_ui->m_outputListWidget->addItem(item);
373 item =
new QListWidgetItem(
"");
374 m_ui->m_outputListWidget->addItem(item);
378 QListWidgetItem* item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MIN_VALUE].c_str());
380 m_ui->m_outputListWidget->addItem(item);
382 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MAX_VALUE].c_str());
384 m_ui->m_outputListWidget->addItem(item);
386 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MEAN].c_str());
388 m_ui->m_outputListWidget->addItem(item);
390 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::SUM].c_str());
392 m_ui->m_outputListWidget->addItem(item);
394 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::COUNT].c_str());
396 m_ui->m_outputListWidget->addItem(item);
398 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VALID_COUNT].c_str());
400 m_ui->m_outputListWidget->addItem(item);
402 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::STANDARD_DEVIATION].c_str());
404 m_ui->m_outputListWidget->addItem(item);
406 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VARIANCE].c_str());
408 m_ui->m_outputListWidget->addItem(item);
410 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::SKEWNESS].c_str());
412 m_ui->m_outputListWidget->addItem(item);
414 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::KURTOSIS].c_str());
416 m_ui->m_outputListWidget->addItem(item);
418 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::AMPLITUDE].c_str());
420 m_ui->m_outputListWidget->addItem(item);
422 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MEDIAN].c_str());
424 m_ui->m_outputListWidget->addItem(item);
426 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VAR_COEFF].c_str());
428 m_ui->m_outputListWidget->addItem(item);
430 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MODE].c_str());
432 m_ui->m_outputListWidget->addItem(item);
434 item =
new QListWidgetItem(
"");
435 m_ui->m_outputListWidget->addItem(item);
440 int lastRow = m_ui->m_outputListWidget->count() - 1;
441 delete m_ui->m_outputListWidget->item(lastRow);
446 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
448 std::string layerID = m_ui->m_layersComboBox->itemData(index, Qt::UserRole).toString().toStdString();
450 m_ui->m_propertieslistWidget->clear();
452 while(it != m_layers.end())
454 if(layerID == it->get()->getId().c_str())
458 m_selectedLayer = selectedLayer;
459 std::auto_ptr<const te::map::LayerSchema> schema(selectedLayer->getSchema());
461 if(schema->size() == 0)
465 m_properties.clear();
467 const std::vector<te::dt::Property*>& properties = schema->getProperties();
471 setFunctionsByLayer(m_properties);
473 for(
size_t i = 0; i < m_properties.size(); ++i)
475 type = m_properties[i]->getType();
478 m_ui->m_propertieslistWidget->addItem(m_properties[i]->getName().c_str());
489 m_ui->m_outputListWidget->reset();
490 m_ui->m_outputStatisticsGroupBox->setVisible(visible);
495 QList<QListWidgetItem*> allItems;
496 int count = m_ui->m_propertieslistWidget->count();
497 for(
int index = 0; index < count; ++index)
499 allItems.push_back(m_ui->m_propertieslistWidget->item(index));
502 QList<QListWidgetItem*> filteredItems = m_ui->m_propertieslistWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive);
504 for(
int i = 0; i < allItems.size(); ++i)
506 QListWidgetItem* item = allItems.at(i);
507 bool hide = filteredItems.indexOf(item) == -1;
508 item->setHidden(hide);
511 m_ui->m_propertieslistWidget->update();
516 QString text = m_ui->m_selectAllComboBox->itemText(index);
517 Qt::MatchFlags flag = Qt::MatchEndsWith;
522 QList<QListWidgetItem *> listFound;
523 listFound = m_ui->m_outputListWidget->findItems(text, flag);
525 for(
int i=0; i < listFound.size(); ++i)
526 listFound.at(i)->setSelected(
true);
528 m_ui->m_rejectAllComboBox->setCurrentIndex(0);
533 QString text = m_ui->m_selectAllComboBox->itemText(index);
534 Qt::MatchFlags flag = Qt::MatchEndsWith;
539 QList<QListWidgetItem *> listFound;
540 listFound = m_ui->m_outputListWidget->findItems(text, flag);
542 for(
int i=0; i < listFound.size(); ++i)
543 listFound.at(i)->setSelected(
false);
545 m_ui->m_selectAllComboBox->setCurrentIndex(0);
550 if(item->text().isEmpty())
552 item->setSelected(
false);
558 m_ui->m_newLayerNameLineEdit->clear();
559 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
563 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
565 if(dsPtrList.size() <= 0)
568 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
570 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
572 m_outputDatasource = *it;
579 m_ui->m_newLayerNameLineEdit->clear();
580 m_ui->m_repositoryLineEdit->clear();
582 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
583 QString(), tr(
"Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
585 if (fileName.isEmpty())
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());
595 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
600 QMessageBox::information(
this,
"Help",
"Under development");
605 if(m_ui->m_layersComboBox->count() == 0)
607 QMessageBox::information(
this,
"Aggregation",
"Select an input layer.");
615 QMessageBox::information(
this,
"Aggregation",
"Can not execute this operation on this type of layer.");
621 if(m_ui->m_onlySelectedCheckBox->isChecked())
623 oidSet = m_selectedLayer->getSelected();
626 QMessageBox::information(
this,
"Aggregation",
"Select the layer objects to perform the aggregation operation.");
632 if (!inDataSource.get())
634 QMessageBox::information(
this,
"Aggregation",
"The selected input data source can not be accessed.");
638 std::vector<te::dt::Property*> selProperties = getSelectedProperties();
639 if(selProperties.empty())
641 QMessageBox::information(
this,
"Aggregation",
"Select at least one grouping attribute.");
645 std::string outputdataset = m_ui->m_newLayerNameLineEdit->text().toStdString();
647 if(m_ui->m_repositoryLineEdit->text().isEmpty())
649 QMessageBox::information(
this,
"Aggregation",
"Define a repository for the result.");
653 if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
655 QMessageBox::information(
this,
"Aggregation",
"Define a name for the resulting layer.");
665 std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > outputStatisticalSummary = getStatisticalSummary();
671 boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
673 if (boost::filesystem::exists(uri))
675 QMessageBox::information(
this,
"Aggregation",
"Output file already exists. Remove it or select a new name and try again.");
679 std::size_t idx = outputdataset.find(
".");
680 if (idx != std::string::npos)
681 outputdataset=outputdataset.substr(0,idx);
683 std::map<std::string, std::string> dsinfo;
684 dsinfo[
"URI"] = uri.string();
687 dsOGR->setConnectionInfo(dsinfo);
689 if (dsOGR->dataSetExists(outputdataset))
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.");
695 this->setCursor(Qt::WaitCursor);
712 aggregOp->
setOutput(dsOGR, outputdataset);
713 aggregOp->
setParams(selProperties, outputStatisticalSummary);
718 res = aggregOp->
run();
722 this->setCursor(Qt::ArrowCursor);
724 QMessageBox::information(
this,
"Aggregation",
"Error: could not generate the aggregation.");
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);
737 ds->setConnInfo(dsinfo);
738 ds->setTitle(uri.stem().string());
739 ds->setAccessDriver(
"OGR");
741 ds->setDescription(uri.string());
747 m_outputDatasource = ds;
754 QMessageBox::information(
this,
"Aggregation",
"The selected output datasource can not be accessed.");
758 if (aux->dataSetExists(outputdataset))
760 QMessageBox::information(
this,
"Aggregation",
"Dataset already exists. Remove it or select a new name and try again.");
763 this->setCursor(Qt::WaitCursor);
781 aggregOp->
setParams(selProperties, outputStatisticalSummary);
786 res = aggregOp->
run();
792 this->setCursor(Qt::ArrowCursor);
793 QMessageBox::information(
this,
"Aggregation",
"Error: could not generate the aggregation.");
804 m_layer = converter(dt);
806 catch(
const std::exception& e)
808 this->setCursor(Qt::ArrowCursor);
810 QMessageBox::information(
this,
"Aggregation", e.what());
812 #ifdef TERRALIB_LOGGER_ENABLED
813 std::string str =
"Aggregation - ";
815 te::common::Logger::logDebug(
"vp", str.c_str());
816 #endif // TERRALIB_LOGGER_ENABLED
823 this->setCursor(Qt::ArrowCursor);
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
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)
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
boost::shared_ptr< DataSource > DataSourcePtr
void onCancelPushButtonClicked()
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.
void onFilterLineEditTextChanged(const QString &text)
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...
static std::auto_ptr< DataSource > make(const std::string &dsType)
TESTATEXPORT std::string GetStatSummaryFullName(const int &e)
Get the statistical parameter full name ffrom its enumerator.
bool supportsSpatialSQLDialect() const
void setStatisticalSummaryMap()
Map Statistical Summary Type enum for an intuitive name.
void onOkPushButtonClicked()
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.
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
void onHelpPushButtonClicked()
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)
void onSelectAllComboBoxChanged(int index)
bool supportsPreparedQueryAPI() const
void onLayerComboBoxChanged(int index)
A class that represents a data source component.
A layer with reference to a dataset.
void setFunctionsByLayer(std::vector< te::dt::Property * > properties)
Set Grouping Functions Type for 'm_outputListWidget' based on Selected Layer.
void onTargetFileToolButtonPressed()
std::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
StatisticalSummary
Define grouping functions type.
void Clone(const std::vector< T * > &src, std::vector< T * > &dst)
This function can be applied to a vector of pointers.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
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...
void onRejectAllComboBoxChanged(int index)
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void onTargetDatasourceToolButtonPressed()
std::vector< te::dt::Property * > getSelectedProperties()
Get the selected properties based on selected QListWidgetItem.