27 #include "../../common/progress/ProgressManager.h"
28 #include "../../common/Translator.h"
29 #include "../../common/STLUtils.h"
30 #include "../../dataaccess/dataset/DataSetType.h"
31 #include "../../dataaccess/datasource/DataSourceCapabilities.h"
32 #include "../../dataaccess/datasource/DataSourceInfo.h"
33 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
34 #include "../../dataaccess/datasource/DataSourceManager.h"
35 #include "../../dataaccess/datasource/DataSourceFactory.h"
36 #include "../../dataaccess/utils/Utils.h"
37 #include "../../qt/af/Utils.h"
38 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
39 #include "../qt/widgets/layer/utils/DataSet2Layer.h"
40 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
41 #include "../../datatype/Enums.h"
42 #include "../../datatype/Property.h"
43 #include "../../maptools/AbstractLayer.h"
44 #include "../../statistics/core/Utils.h"
45 #include "../Config.h"
46 #include "../Exception.h"
49 #include "ui_AggregationDialogForm.h"
50 #include "VectorProcessingConfig.h"
54 #include <QtCore/QList>
55 #include <QtGui/QFileDialog>
56 #include <QtGui/QListWidget>
57 #include <QtGui/QListWidgetItem>
58 #include <QtGui/QMessageBox>
59 #include <QtGui/QTreeWidget>
62 #include <boost/algorithm/string.hpp>
63 #include <boost/filesystem.hpp>
64 #include <boost/uuid/random_generator.hpp>
65 #include <boost/uuid/uuid_io.hpp>
69 m_ui(new Ui::AggregationDialogForm),
77 m_ui->m_outputStatisticsGroupBox->setVisible(
false);
80 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(VP_IMAGES
"/vp-aggregation-hint").pixmap(112,48));
81 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
99 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
100 m_ui->m_helpPushButton->setPageReference(
"plugins/vp/vp_aggregation.html");
103 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
114 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
116 while(it != m_layers.end())
118 std::auto_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
119 if(dsType->hasGeom())
120 m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
127 std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > outputStatisticalSummary;
129 QList<QListWidgetItem*> itemList = m_ui->m_outputListWidget->selectedItems();
132 std::map<std::string, std::vector<te::stat::StatisticalSummary> > propname_stat;
133 std::map<std::string, std::vector<te::stat::StatisticalSummary> >::iterator it;
135 for(
int i = 0; i < itemList.size(); ++i)
137 std::vector<std::string> tokens;
139 std::string auxItem = itemList[i]->text().toStdString();
141 boost::split(tokens, auxItem, boost::is_any_of(
":"));
145 pname.erase(pname.end() - 1);
147 it = propname_stat.find(pname);
148 if (it != propname_stat.end())
149 it->second.push_back(enumStatisticalSummary);
152 std::vector<te::stat::StatisticalSummary> nvec;
153 nvec.push_back(enumStatisticalSummary);
154 propname_stat.insert(std::make_pair(pname, nvec));
159 it = propname_stat.begin();
160 while (it != propname_stat.end())
163 outputStatisticalSummary.insert(std::make_pair(prop,it->second));
167 return outputStatisticalSummary;
174 if(propertyName ==
"")
177 for(std::size_t i = 0; i < m_properties.size(); ++i)
179 if(propertyName == m_properties[i]->getName())
181 selProperty = m_properties[i];
190 std::vector<te::dt::Property*> selProperties;
192 for(std::size_t i = 0; i != m_ui->m_propertieslistWidget->count(); ++i)
194 if(m_ui->m_propertieslistWidget->isItemSelected(m_ui->m_propertieslistWidget->item(i)))
196 selProperties.push_back(m_properties[i]);
200 return selProperties;
210 m_ui->m_selectAllComboBox->addItem(
"");
226 m_ui->m_rejectAllComboBox->addItem(
"");
263 QStringList propertyList;
266 m_ui->m_selectAllComboBox->setCurrentIndex(0);
267 m_ui->m_rejectAllComboBox->setCurrentIndex(0);
268 m_ui->m_outputListWidget->clear();
274 if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
276 for(
size_t i=0; i < properties.size(); ++i)
278 propertyType = properties[i]->getType();
283 QListWidgetItem* item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MIN_VALUE].c_str());
285 m_ui->m_outputListWidget->addItem(item);
287 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MAX_VALUE].c_str());
289 m_ui->m_outputListWidget->addItem(item);
291 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::COUNT].c_str());
293 m_ui->m_outputListWidget->addItem(item);
295 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VALID_COUNT].c_str());
297 m_ui->m_outputListWidget->addItem(item);
299 item =
new QListWidgetItem(
"");
300 m_ui->m_outputListWidget->addItem(item);
304 QListWidgetItem* item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MIN_VALUE].c_str());
306 m_ui->m_outputListWidget->addItem(item);
308 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MAX_VALUE].c_str());
310 m_ui->m_outputListWidget->addItem(item);
312 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MEAN].c_str());
314 m_ui->m_outputListWidget->addItem(item);
316 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::SUM].c_str());
318 m_ui->m_outputListWidget->addItem(item);
320 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::COUNT].c_str());
322 m_ui->m_outputListWidget->addItem(item);
324 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VALID_COUNT].c_str());
326 m_ui->m_outputListWidget->addItem(item);
328 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::STANDARD_DEVIATION].c_str());
330 m_ui->m_outputListWidget->addItem(item);
332 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VARIANCE].c_str());
334 m_ui->m_outputListWidget->addItem(item);
336 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::AMPLITUDE].c_str());
338 m_ui->m_outputListWidget->addItem(item);
340 item =
new QListWidgetItem(
"");
341 m_ui->m_outputListWidget->addItem(item);
348 for(
size_t i=0; i < properties.size(); ++i)
350 propertyType = properties[i]->getType();
355 QListWidgetItem* item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MIN_VALUE].c_str());
357 m_ui->m_outputListWidget->addItem(item);
359 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MAX_VALUE].c_str());
361 m_ui->m_outputListWidget->addItem(item);
363 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::COUNT].c_str());
365 m_ui->m_outputListWidget->addItem(item);
367 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VALID_COUNT].c_str());
369 m_ui->m_outputListWidget->addItem(item);
371 item =
new QListWidgetItem(
"");
372 m_ui->m_outputListWidget->addItem(item);
376 QListWidgetItem* item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MIN_VALUE].c_str());
378 m_ui->m_outputListWidget->addItem(item);
380 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MAX_VALUE].c_str());
382 m_ui->m_outputListWidget->addItem(item);
384 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MEAN].c_str());
386 m_ui->m_outputListWidget->addItem(item);
388 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::SUM].c_str());
390 m_ui->m_outputListWidget->addItem(item);
392 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::COUNT].c_str());
394 m_ui->m_outputListWidget->addItem(item);
396 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VALID_COUNT].c_str());
398 m_ui->m_outputListWidget->addItem(item);
400 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::STANDARD_DEVIATION].c_str());
402 m_ui->m_outputListWidget->addItem(item);
404 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VARIANCE].c_str());
406 m_ui->m_outputListWidget->addItem(item);
408 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::SKEWNESS].c_str());
410 m_ui->m_outputListWidget->addItem(item);
412 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::KURTOSIS].c_str());
414 m_ui->m_outputListWidget->addItem(item);
416 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::AMPLITUDE].c_str());
418 m_ui->m_outputListWidget->addItem(item);
420 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MEDIAN].c_str());
422 m_ui->m_outputListWidget->addItem(item);
424 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::VAR_COEFF].c_str());
426 m_ui->m_outputListWidget->addItem(item);
428 item =
new QListWidgetItem(QString(properties[i]->getName().c_str()) +
" : " + m_StatisticalSummaryMap[
te::stat::MODE].c_str());
430 m_ui->m_outputListWidget->addItem(item);
432 item =
new QListWidgetItem(
"");
433 m_ui->m_outputListWidget->addItem(item);
438 int lastRow = m_ui->m_outputListWidget->count() - 1;
439 delete m_ui->m_outputListWidget->item(lastRow);
444 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
446 std::string layerID = m_ui->m_layersComboBox->itemData(index, Qt::UserRole).toString().toStdString();
448 m_ui->m_propertieslistWidget->clear();
450 while(it != m_layers.end())
452 if(layerID == it->get()->getId().c_str())
456 m_selectedLayer = selectedLayer;
457 std::auto_ptr<const te::map::LayerSchema> schema(selectedLayer->getSchema());
459 if(schema->size() == 0)
463 m_properties.clear();
465 const std::vector<te::dt::Property*>& properties = schema->getProperties();
469 setFunctionsByLayer(m_properties);
471 for(
size_t i = 0; i < m_properties.size(); ++i)
473 type = m_properties[i]->getType();
476 m_ui->m_propertieslistWidget->addItem(m_properties[i]->getName().c_str());
487 m_ui->m_outputListWidget->reset();
488 m_ui->m_outputStatisticsGroupBox->setVisible(visible);
495 m_ui->m_layersComboBox->clear();
497 m_ui->m_selectAllComboBox->setCurrentIndex(0);
498 m_ui->m_rejectAllComboBox->setCurrentIndex(0);
499 m_ui->m_outputListWidget->clear();
502 filteredLayers = m_layers;
504 std::list<te::map::AbstractLayerPtr>::iterator it = filteredLayers.begin();
506 while(it != filteredLayers.end())
508 if(it->get()->getSchema()->hasGeom())
509 m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
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.");
614 QMessageBox::information(
this,
"Aggregation",
"Can not execute this operation on this type of layer.");
619 if (!inDataSource.get())
621 QMessageBox::information(
this,
"Aggregation",
"The selected input data source can not be accessed.");
625 std::vector<te::dt::Property*> selProperties = getSelectedProperties();
626 if(selProperties.empty())
628 QMessageBox::information(
this,
"Aggregation",
"Select at least one grouping attribute.");
632 std::string outputdataset = m_ui->m_newLayerNameLineEdit->text().toStdString();
634 if(m_ui->m_repositoryLineEdit->text().isEmpty())
636 QMessageBox::information(
this,
"Aggregation",
"Define a repository for the result.");
640 if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
642 QMessageBox::information(
this,
"Aggregation",
"Define a name for the resulting layer.");
652 std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > outputStatisticalSummary = getStatisticalSummary();
658 boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
660 if (boost::filesystem::exists(uri))
662 QMessageBox::information(
this,
"Aggregation",
"Output file already exists. Remove it or select a new name and try again.");
666 std::size_t idx = outputdataset.find(
".");
667 if (idx != std::string::npos)
668 outputdataset=outputdataset.substr(0,idx);
670 std::map<std::string, std::string> dsinfo;
671 dsinfo[
"URI"] = uri.string();
674 dsOGR->setConnectionInfo(dsinfo);
676 if (dsOGR->dataSetExists(outputdataset))
678 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.");
682 this->setCursor(Qt::WaitCursor);
687 this->setCursor(Qt::ArrowCursor);
689 QMessageBox::information(
this,
"Aggregation",
"Error: could not generate the aggregation.");
695 boost::uuids::basic_random_generator<boost::mt19937> gen;
696 boost::uuids::uuid u = gen();
697 std::string
id = boost::uuids::to_string(u);
700 ds->setConnInfo(dsinfo);
701 ds->setTitle(uri.stem().string());
702 ds->setAccessDriver(
"OGR");
704 ds->setDescription(uri.string());
710 m_outputDatasource = ds;
717 QMessageBox::information(
this,
"Aggregation",
"The selected output datasource can not be accessed.");
721 if (aux->dataSetExists(outputdataset))
723 QMessageBox::information(
this,
"Aggregation",
"Dataset already exists. Remove it or select a new name and try again.");
726 this->setCursor(Qt::WaitCursor);
730 this->setCursor(Qt::ArrowCursor);
731 QMessageBox::information(
this,
"Aggregation",
"Error: could not generate the aggregation.");
742 m_layer = converter(dt);
744 catch(
const std::exception& e)
746 this->setCursor(Qt::ArrowCursor);
747 QMessageBox::information(
this,
"Aggregation", e.what());
753 this->setCursor(Qt::ArrowCursor);
te::map::AbstractLayerPtr getLayer()
Get the generated layer.
A layer with reference to a dataset.
const std::string & getDataSetName() const
A dialog for selecting a data source.
A dialog aggregation operation.
std::map< te::dt::Property *, std::vector< te::stat::StatisticalSummary > > getStatisticalSummary()
Get the Grouping Functions Type based on selected QListWidgetItem.
void onRejectAllComboBoxChanged(int index)
TEVPEXPORT bool Aggregation(const std::string &inDataset, te::da::DataSource *inDatasource, const std::vector< te::dt::Property * > &groupingProperties, const std::map< te::dt::Property *, std::vector< te::stat::StatisticalSummary > > &statisticalSummary, const std::string &outDataset, te::da::DataSource *outDatasource)
Executes the Aggregation Geographical Operation and persists the result as a dataset in a given outpu...
TESTATEXPORT std::string GetStatSummaryFullName(const int &e)
Get the statistical parameter full name ffrom its enumerator.
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
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...
std::list< te::map::AbstractLayerPtr > GetFilteredLayers(std::string text, std::list< te::map::AbstractLayerPtr > layers)
Get a list of AbstractLayer filtered by the name;.
A class that represents a data source component.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
void Clone(const std::vector< T * > &src, std::vector< T * > &dst)
This function can be applied to a vector of pointers.
void onCancelPushButtonClicked()
void onCalculateStatistics(bool visible)
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
StatisticalSummary
Define grouping functions type.
static std::auto_ptr< DataSource > make(const std::string &dsType)
const std::string & getDataSourceId() const
AggregationDialog(QWidget *parent=0, Qt::WindowFlags f=0)
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
void setFunctionsByLayer(std::vector< te::dt::Property * > properties)
Set Grouping Functions Type for 'm_outputListWidget' based on Selected Layer.
void setStatisticalSummaryMap()
Map Statistical Summary Type enum for an intuitive name.
Aggregation Vector Processing functions.
void onFilterLineEditTextChanged(const QString &text)
void onTargetDatasourceToolButtonPressed()
Utility functions for the data access module.
void onSelectAllComboBoxChanged(int index)
std::vector< te::dt::Property * > getSelectedProperties()
Get the selected properties based on selected QListWidgetItem.
void onTargetFileToolButtonPressed()
boost::shared_ptr< DataSource > DataSourcePtr
te::da::DataSourceInfoPtr m_outputDatasource
DataSource information.
void onLayerComboBoxChanged(int index)
boost::shared_ptr< DataSetType > DataSetTypePtr
It models a property definition.
void onOkPushButtonClicked()
void onHelpPushButtonClicked()
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void onOutputListWidgetClicked(QListWidgetItem *item)
static T & getInstance()
It returns a reference to the singleton instance.
std::auto_ptr< Ui::AggregationDialogForm > m_ui
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void setStatisticalSummary()
Set Statistical Summary Type for combobox 'm_selectAllComboBox' and 'm_rejectAllComboBox' based on a ...
te::dt::Property * getSelectedPropertyByName(std::string propertyName)
Get the selected property based on selected QListWidgetItem using the name of property.