27 #include "../../attributefill/Enums.h"
28 #include "../../attributefill/Utils.h"
29 #include "../../attributefill/VectorToVectorOp.h"
30 #include "../../attributefill/VectorToVectorMemory.h"
31 #include "../../common/Exception.h"
32 #include "../../common/progress/ProgressManager.h"
33 #include "../../common/Translator.h"
34 #include "../../common/StringUtils.h"
35 #include "../../dataaccess/dataset/DataSetType.h"
36 #include "../../dataaccess/datasource/DataSourceCapabilities.h"
37 #include "../../dataaccess/datasource/DataSourceInfo.h"
38 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
39 #include "../../dataaccess/datasource/DataSourceFactory.h"
40 #include "../../dataaccess/datasource/DataSourceManager.h"
41 #include "../../dataaccess/utils/Utils.h"
42 #include "../../datatype/Enums.h"
43 #include "../../datatype/Property.h"
44 #include "../../geometry/GeometryProperty.h"
45 #include "../../maptools/AbstractLayer.h"
46 #include "../../qt/af/Utils.h"
47 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
48 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
49 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
50 #include "../../qt/widgets/utils/DoubleListWidget.h"
51 #include "../../qt/widgets/Utils.h"
52 #include "../../statistics/core/Utils.h"
53 #include "../Config.h"
55 #include "ui_VectorToVectorDialogForm.h"
59 #include <QDialogButtonBox>
60 #include <QFileDialog>
62 #include <QListWidget>
63 #include <QListWidgetItem>
64 #include <QMessageBox>
67 #include <boost/algorithm/string.hpp>
68 #include <boost/filesystem.hpp>
69 #include <boost/lexical_cast.hpp>
70 #include <boost/uuid/random_generator.hpp>
71 #include <boost/uuid/uuid_io.hpp>
75 m_ui(new Ui::VectorToVectorDialogForm),
77 m_outputAttributes(std::vector<std::string>()),
88 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"attributefill-vector2vector-hint").pixmap(112,48));
89 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
107 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
108 m_ui->m_helpPushButton->setPageReference(
"plugins/attributefill/attrfill_vector_to_vector.html");
119 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
121 while(it != m_layers.end())
123 std::auto_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
124 if(dsType->hasGeom())
126 std::string layerName = it->get()->getTitle();
127 std::string layerId = it->get()->getId();
129 m_ui->m_toLayerComboBox->addItem(QString(layerName.c_str()), QVariant(layerId.c_str()));
130 m_ui->m_fromLayerComboBox->addItem(QString(layerName.c_str()), QVariant(layerId.c_str()));
135 if(m_ui->m_fromLayerComboBox->count() > 1)
137 m_ui->m_fromLayerComboBox->setCurrentIndex(0);
138 onFromLayerComboBoxCurrentIndexChanged(0);
139 m_ui->m_toLayerComboBox->setCurrentIndex(1);
150 QMessageBox::information(
this,
"Help",
"Under development");
159 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"\"From\" layer invalid!"));
167 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"\"To\" layer invalid!"));
171 if(m_ui->m_repositoryLineEdit->text().isEmpty())
173 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"Define a repository for the result."));
177 if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
179 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"Define a name for the resulting layer."));
183 std::string fromDataSetName = fromLayer->getDataSetName();
184 std::auto_ptr<te::da::DataSetType> fromSchema = fromLayer->getSchema();
185 std::auto_ptr<te::da::DataSet> fromData = fromLayer->getData();
188 std::string toDataSetName = toLayer->getDataSetName();
189 std::auto_ptr<te::da::DataSetType> toSchema = fromLayer->getSchema();
190 std::auto_ptr<te::da::DataSet> toData = toLayer->getData();
193 std::string outDataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
202 v2v->setInput(fromLayer, toLayer);
206 boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
208 if (boost::filesystem::exists(uri))
210 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"Output file already exists. Remove it or select a new name and try again."));
214 std::size_t idx = outDataSetName.find(
".");
215 if (idx != std::string::npos)
216 outDataSetName=outDataSetName.substr(0,idx);
218 std::map<std::string, std::string> dsinfo;
219 dsinfo[
"URI"] = uri.string();
222 outSource->setConnectionInfo(dsinfo);
225 if (outSource->dataSetExists(outDataSetName))
227 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"There is already a dataset with the requested name in the output data source. Remove it or select a new name and try again."));
231 v2v->setOutput(outSource, outDataSetName);
234 boost::uuids::basic_random_generator<boost::mt19937> gen;
235 boost::uuids::uuid u = gen();
236 std::string id_ds = boost::uuids::to_string(u);
239 ds->setConnInfo(dsinfo);
240 ds->setTitle(uri.stem().string());
241 ds->setAccessDriver(
"OGR");
243 ds->setDescription(uri.string());
249 m_outputDatasource = ds;
256 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"The selected output datasource can not be accessed."));
260 if (aux->dataSetExists(outDataSetName))
262 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"Dataset already exists. Remove it or select a new name and try again."));
265 this->setCursor(Qt::WaitCursor);
267 v2v->setOutput(aux, outDataSetName);
271 std::map<std::string, std::vector<te::attributefill::OperationType> > selections = getSelections();
273 v2v->setParams(getSelections(), m_outputAttributes);
285 m_outLayer = converter(dt);
289 QMessageBox::warning(
this, tr(
"Vector To Vector"), e.
what());
293 catch(std::exception& e)
295 QMessageBox::warning(
this, tr(
"Vector To Vector"), e.what());
301 this->setCursor(Qt::ArrowCursor);
305 QString err(tr(
"Some errors occurred during execution."));
306 #ifdef TERRALIB_LOGGER_ENABLED
307 err.append(tr(
" The error log can be found at: "));
308 err += m_logPath.c_str();
309 #endif //TERRALIB_LOGGER_ENABLED
311 QMessageBox::warning(
this, tr(
"Vector to Vector"), err);
325 if(m_ui->m_toLayerComboBox->count() <= 1 || m_ui->m_fromLayerComboBox->count() <= 1 )
328 int currentIndex = m_ui->m_fromLayerComboBox->currentIndex();
330 if(currentIndex == m_ui->m_toLayerComboBox->currentIndex())
332 if(currentIndex == 0)
333 m_ui->m_toLayerComboBox->setCurrentIndex(1);
335 m_ui->m_toLayerComboBox->setCurrentIndex(0);
338 setFunctionsByLayer(getCurrentFromLayer());
343 if(m_ui->m_toLayerComboBox->count() <= 1 || m_ui->m_fromLayerComboBox->count() <= 1 )
346 int currentIndex = m_ui->m_toLayerComboBox->currentIndex();
348 if(m_ui->m_fromLayerComboBox->count() > 0 && currentIndex == m_ui->m_fromLayerComboBox->currentIndex())
350 if(currentIndex == 0)
351 m_ui->m_fromLayerComboBox->setCurrentIndex(1);
353 m_ui->m_fromLayerComboBox->setCurrentIndex(0);
356 std::auto_ptr<te::da::DataSetType> toSchema = getCurrentToLayer()->getSchema();
358 std::vector<te::dt::Property*> props = toSchema->getProperties();
359 std::vector<te::dt::Property*> pkProps = toSchema->getPrimaryKey()->getProperties();
360 for(std::size_t i = 0; i < props.size(); ++i)
363 for(std::size_t j = 0; j < pkProps.size(); ++j)
365 if(props[i]->getName() == pkProps[j]->getName())
373 m_outputAttributes.push_back(props[i]->getName());
376 onFromLayerComboBoxCurrentIndexChanged(m_ui->m_fromLayerComboBox->currentIndex());
381 m_ui->m_selectAllComboBox->addItem(
"");
407 m_ui->m_rejectAllComboBox->addItem(
"");
436 std::string layerID = m_ui->m_fromLayerComboBox->itemData(m_ui->m_fromLayerComboBox->currentIndex(), Qt::UserRole).toString().toStdString();
438 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
440 while(it != m_layers.end())
442 if(it->get()->getId() == layerID)
452 std::string layerID = m_ui->m_toLayerComboBox->itemData(m_ui->m_toLayerComboBox->currentIndex(), Qt::UserRole).toString().toStdString();
454 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
456 while(it != m_layers.end())
458 if(it->get()->getId() == layerID)
468 if(item->text().isEmpty())
469 item->setSelected(
false);
474 QString text = m_ui->m_selectAllComboBox->itemText(index);
475 Qt::MatchFlags flag = Qt::MatchEndsWith;
480 QList<QListWidgetItem *> listFound;
481 listFound = m_ui->m_statisticsListWidget->findItems(text, flag);
483 for(
int i=0; i < listFound.size(); ++i)
485 std::vector<std::string> tokens;
488 if(tokens.size() < 2)
491 QString token(tokens[1].c_str());
493 if(token.trimmed() == text)
494 listFound.at(i)->setSelected(
true);
497 m_ui->m_rejectAllComboBox->setCurrentIndex(0);
502 QString text = m_ui->m_selectAllComboBox->itemText(index);
503 Qt::MatchFlags flag = Qt::MatchEndsWith;
508 QList<QListWidgetItem *> listFound;
509 listFound = m_ui->m_statisticsListWidget->findItems(text, flag);
511 for(
int i=0; i < listFound.size(); ++i)
513 std::vector<std::string> tokens;
516 if(tokens.size() < 2)
519 QString token(tokens[1].c_str());
521 if(token.trimmed() == text)
522 listFound.at(i)->setSelected(
false);
525 m_ui->m_selectAllComboBox->setCurrentIndex(0);
530 m_ui->m_newLayerNameLineEdit->clear();
531 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
535 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
537 if(dsPtrList.size() <= 0)
540 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
542 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
544 m_outputDatasource = *it;
551 m_ui->m_newLayerNameLineEdit->clear();
552 m_ui->m_repositoryLineEdit->clear();
554 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
555 QString(), tr(
"Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
557 if (fileName.isEmpty())
560 boost::filesystem::path outfile(fileName.toStdString());
561 std::string aux = outfile.leaf().string();
562 m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
563 aux = outfile.string();
564 m_ui->m_repositoryLineEdit->setText(aux.c_str());
567 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
572 std::map<std::string, std::vector<te::attributefill::OperationType> > result;
574 std::auto_ptr<te::da::DataSetType> fromScheme = getCurrentFromLayer()->getSchema();
576 std::vector<std::string> props;
577 for(
int i = 0; i < m_ui->m_statisticsListWidget->count(); ++i)
579 QListWidgetItem* item = m_ui->m_statisticsListWidget->item(i);
581 if(!item->isSelected())
584 std::string itemText = item->text().toStdString();
586 std::vector<std::string> tokens;
589 std::string propName = tokens[0];
590 boost::trim(propName);
594 if(std::find(props.begin(), props.end(), propName) != props.end())
596 std::vector<te::attributefill::OperationType> vec;
597 vec.push_back(operationType);
599 result[propName] = vec;
603 result[propName].push_back(operationType);
612 m_ui->m_selectAllComboBox->setCurrentIndex(0);
613 m_ui->m_rejectAllComboBox->setCurrentIndex(0);
614 m_ui->m_statisticsListWidget->clear();
618 std::auto_ptr<te::da::DataSetType> dst = layer->getSchema();
619 std::auto_ptr<te::da::DataSet> ds = layer->getData();
625 std::string geomPropName = layer->getGeomPropertyName();
627 if(geomPropName.empty())
635 std::vector<te::dt::Property*> props = dst->getProperties();
637 for(std::size_t i = 0; i < props.size(); ++i)
641 if(isValidPropertyType(prop->
getType()))
643 int propertyType = prop->
getType();
647 m_ui->m_statisticsListWidget->addItem(item);
653 m_ui->m_statisticsListWidget->addItem(item);
657 m_ui->m_statisticsListWidget->addItem(item);
661 m_ui->m_statisticsListWidget->addItem(item);
665 m_ui->m_statisticsListWidget->addItem(item);
671 m_ui->m_statisticsListWidget->addItem(item);
675 m_ui->m_statisticsListWidget->addItem(item);
679 m_ui->m_statisticsListWidget->addItem(item);
683 m_ui->m_statisticsListWidget->addItem(item);
687 m_ui->m_statisticsListWidget->addItem(item);
691 m_ui->m_statisticsListWidget->addItem(item);
695 m_ui->m_statisticsListWidget->addItem(item);
699 m_ui->m_statisticsListWidget->addItem(item);
703 m_ui->m_statisticsListWidget->addItem(item);
707 m_ui->m_statisticsListWidget->addItem(item);
711 m_ui->m_statisticsListWidget->addItem(item);
715 m_ui->m_statisticsListWidget->addItem(item);
719 m_ui->m_statisticsListWidget->addItem(item);
723 m_ui->m_statisticsListWidget->addItem(item);
726 if(isClassType(prop->
getType()))
730 m_ui->m_statisticsListWidget->addItem(item);
734 if(isClassType(prop->
getType()) && isPolygon(geomType) && isPolygon(toGeomType))
738 m_ui->m_statisticsListWidget->addItem(item);
742 m_ui->m_statisticsListWidget->addItem(item);
746 m_ui->m_statisticsListWidget->addItem(item);
751 m_ui->m_statisticsListWidget->addItem(item);
755 m_ui->m_statisticsListWidget->addItem(item);
757 if(isPolygon(geomType) && isPolygon(toGeomType))
759 if(isNumProperty(prop->
getType()))
763 m_ui->m_statisticsListWidget->addItem(item);
767 m_ui->m_statisticsListWidget->addItem(item);
772 m_ui->m_statisticsListWidget->addItem(item);
776 m_ui->m_statisticsListWidget->addItem(
"");
779 int lastRow = m_ui->m_statisticsListWidget->count() - 1;
780 delete m_ui->m_statisticsListWidget->item(lastRow);
855 std::auto_ptr<te::da::DataSetType> toSchema = toLayer->getSchema();
880 std::auto_ptr<te::da::DataSetType> toSchema = toLayer->getSchema();
882 std::vector<te::dt::Property*> props = toSchema->getProperties();
883 std::vector<te::dt::Property*> pkProps = toSchema->getPrimaryKey()->getProperties();
885 std::vector<std::string> inputNames;
886 std::vector<std::string> outputNames;
887 for(std::size_t i = 0; i < props.size(); ++i)
890 for(std::size_t j = 0; j < pkProps.size(); ++j)
892 if(props[i]->getName() == pkProps[j]->getName())
901 if(std::find(m_outputAttributes.begin(), m_outputAttributes.end(), props[i]->getName()) != m_outputAttributes.end())
903 outputNames.push_back(props[i]->getName());
907 inputNames.push_back(props[i]->getName());
912 QDialog* dialog =
new QDialog(
this);
913 dialog->setWindowTitle(tr(
"Vector To Vector"));
915 QBoxLayout* vLayout =
new QBoxLayout(QBoxLayout::TopToBottom, dialog);
922 vLayout->addWidget(d);
924 QDialogButtonBox* bbox =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
925 connect(bbox, SIGNAL(accepted()), dialog, SLOT(accept()));
926 connect(bbox, SIGNAL(rejected()), dialog, SLOT(reject()));
927 vLayout->addWidget(bbox);
929 int res = dialog->exec();
931 if(res == QDialog::Accepted)
933 m_outputAttributes.clear();
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
void onTargetFileToolButtonPressed()
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
boost::shared_ptr< DataSetType > DataSetTypePtr
void setStatisticalSummary()
boost::shared_ptr< DataSource > DataSourcePtr
te::map::AbstractLayerPtr getLayer()
Get the generated layer.
void onOkPushButtonClicked()
virtual const char * what() const
It outputs the exception message.
bool isValidPropertyType(const int type)
void onTargetDatasourceToolButtonPressed()
bool isPoint(te::gm::GeomType type)
bool isNumProperty(const int type)
te::map::AbstractLayerPtr getCurrentToLayer()
te::map::AbstractLayerPtr getCurrentFromLayer()
std::map< std::string, std::vector< te::attributefill::OperationType > > getSelections()
VectorToVectorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
void onToLayerComboBoxCurrentIndexChanged(int index)
void setFunctionsByLayer(te::map::AbstractLayerPtr layer)
It models a property definition.
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
void onStatisticsListWidgetItemPressed(QListWidgetItem *item)
void removeViewer(int viewerId)
Dettach a progress viewer.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
void onFromLayerComboBoxCurrentIndexChanged(int index)
static std::auto_ptr< DataSource > make(const std::string &dsType)
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
bool isPolygon(te::gm::GeomType type)
void onHelpPushButtonClicked()
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::auto_ptr< Ui::VectorToVectorDialogForm > m_ui
User interface.
void onRejectAllComboBoxChanged(int index)
int getType() const
It returns the property data type.
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
void setLogPath(const std::string &path)
void onSelectAllComboBoxChanged(int index)
te::gm::GeomType getCurrentToLayerGeomType()
boost::intrusive_ptr< DataSetLayer > DataSetLayerPtr
A class that represents a data source component.
bool isClassType(const int type)
TEATTRIBUTEFILLEXPORT std::string GetOperationFullName(const int &e)
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
OperationType
Define grouping operations type.
Raster to vector attributefill dialog.
void onCancelPushButtonClicked()
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
void onSelectAttrToolButtonPressed()
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
const std::string & getName() const
It returns the property name.