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);
154 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"\"From\" layer invalid!"));
162 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"\"To\" layer invalid!"));
166 if(m_ui->m_repositoryLineEdit->text().isEmpty())
168 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"Define a repository for the result."));
172 if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
174 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"Define a name for the resulting layer."));
178 std::string fromDataSetName = fromLayer->getDataSetName();
179 std::auto_ptr<te::da::DataSetType> fromSchema = fromLayer->getSchema();
180 std::auto_ptr<te::da::DataSet> fromData = fromLayer->getData();
183 std::string toDataSetName = toLayer->getDataSetName();
184 std::auto_ptr<te::da::DataSetType> toSchema = fromLayer->getSchema();
185 std::auto_ptr<te::da::DataSet> toData = toLayer->getData();
188 std::string outDataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
197 v2v->setInput(fromLayer, toLayer);
201 boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
203 if (boost::filesystem::exists(uri))
205 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"Output file already exists. Remove it or select a new name and try again."));
209 std::size_t idx = outDataSetName.find(
".");
210 if (idx != std::string::npos)
211 outDataSetName=outDataSetName.substr(0,idx);
213 std::map<std::string, std::string> dsinfo;
214 dsinfo[
"URI"] = uri.string();
217 outSource->setConnectionInfo(dsinfo);
220 if (outSource->dataSetExists(outDataSetName))
222 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."));
226 v2v->setOutput(outSource, outDataSetName);
229 boost::uuids::basic_random_generator<boost::mt19937> gen;
230 boost::uuids::uuid u = gen();
231 std::string id_ds = boost::uuids::to_string(u);
234 ds->setConnInfo(dsinfo);
235 ds->setTitle(uri.stem().string());
236 ds->setAccessDriver(
"OGR");
238 ds->setDescription(uri.string());
244 m_outputDatasource = ds;
251 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"The selected output datasource can not be accessed."));
255 if (aux->dataSetExists(outDataSetName))
257 QMessageBox::warning(
this, tr(
"VectorToVector"), tr(
"Dataset already exists. Remove it or select a new name and try again."));
260 this->setCursor(Qt::WaitCursor);
262 v2v->setOutput(aux, outDataSetName);
266 std::map<std::string, std::vector<te::attributefill::OperationType> > selections = getSelections();
268 v2v->setParams(getSelections(), m_outputAttributes);
280 m_outLayer = converter(dt);
284 QMessageBox::warning(
this, tr(
"Vector To Vector"), e.
what());
288 catch(std::exception& e)
290 QMessageBox::warning(
this, tr(
"Vector To Vector"), e.what());
296 this->setCursor(Qt::ArrowCursor);
300 QString err(tr(
"Some errors occurred during execution."));
301 #ifdef TERRALIB_LOGGER_ENABLED
302 err.append(tr(
" The error log can be found at: "));
303 err += m_logPath.c_str();
304 #endif //TERRALIB_LOGGER_ENABLED
306 QMessageBox::warning(
this, tr(
"Vector to Vector"), err);
320 if(m_ui->m_toLayerComboBox->count() <= 1 || m_ui->m_fromLayerComboBox->count() <= 1 )
323 int currentIndex = m_ui->m_fromLayerComboBox->currentIndex();
325 if(currentIndex == m_ui->m_toLayerComboBox->currentIndex())
327 if(currentIndex == 0)
328 m_ui->m_toLayerComboBox->setCurrentIndex(1);
330 m_ui->m_toLayerComboBox->setCurrentIndex(0);
333 setFunctionsByLayer(getCurrentFromLayer());
338 if(m_ui->m_toLayerComboBox->count() <= 1 || m_ui->m_fromLayerComboBox->count() <= 1 )
341 int currentIndex = m_ui->m_toLayerComboBox->currentIndex();
343 if(m_ui->m_fromLayerComboBox->count() > 0 && currentIndex == m_ui->m_fromLayerComboBox->currentIndex())
345 if(currentIndex == 0)
346 m_ui->m_fromLayerComboBox->setCurrentIndex(1);
348 m_ui->m_fromLayerComboBox->setCurrentIndex(0);
351 std::auto_ptr<te::da::DataSetType> toSchema = getCurrentToLayer()->getSchema();
353 std::vector<te::dt::Property*> props = toSchema->getProperties();
354 std::vector<te::dt::Property*> pkProps = toSchema->getPrimaryKey()->getProperties();
355 for(std::size_t i = 0; i < props.size(); ++i)
358 for(std::size_t j = 0; j < pkProps.size(); ++j)
360 if(props[i]->getName() == pkProps[j]->getName())
368 m_outputAttributes.push_back(props[i]->getName());
371 onFromLayerComboBoxCurrentIndexChanged(m_ui->m_fromLayerComboBox->currentIndex());
376 m_ui->m_selectAllComboBox->addItem(
"");
402 m_ui->m_rejectAllComboBox->addItem(
"");
431 std::string layerID = m_ui->m_fromLayerComboBox->itemData(m_ui->m_fromLayerComboBox->currentIndex(), Qt::UserRole).toString().toStdString();
433 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
435 while(it != m_layers.end())
437 if(it->get()->getId() == layerID)
447 std::string layerID = m_ui->m_toLayerComboBox->itemData(m_ui->m_toLayerComboBox->currentIndex(), Qt::UserRole).toString().toStdString();
449 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
451 while(it != m_layers.end())
453 if(it->get()->getId() == layerID)
463 if(item->text().isEmpty())
464 item->setSelected(
false);
469 QString text = m_ui->m_selectAllComboBox->itemText(index);
470 Qt::MatchFlags flag = Qt::MatchEndsWith;
475 QList<QListWidgetItem *> listFound;
476 listFound = m_ui->m_statisticsListWidget->findItems(text, flag);
478 for(
int i=0; i < listFound.size(); ++i)
480 std::vector<std::string> tokens;
483 if(tokens.size() < 2)
486 QString token(tokens[1].c_str());
488 if(token.trimmed() == text)
489 listFound.at(i)->setSelected(
true);
492 m_ui->m_rejectAllComboBox->setCurrentIndex(0);
497 QString text = m_ui->m_selectAllComboBox->itemText(index);
498 Qt::MatchFlags flag = Qt::MatchEndsWith;
503 QList<QListWidgetItem *> listFound;
504 listFound = m_ui->m_statisticsListWidget->findItems(text, flag);
506 for(
int i=0; i < listFound.size(); ++i)
508 std::vector<std::string> tokens;
511 if(tokens.size() < 2)
514 QString token(tokens[1].c_str());
516 if(token.trimmed() == text)
517 listFound.at(i)->setSelected(
false);
520 m_ui->m_selectAllComboBox->setCurrentIndex(0);
525 m_ui->m_newLayerNameLineEdit->clear();
526 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
530 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
532 if(dsPtrList.empty())
535 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
537 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
539 m_outputDatasource = *it;
546 m_ui->m_newLayerNameLineEdit->clear();
547 m_ui->m_repositoryLineEdit->clear();
549 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
550 QString(), tr(
"Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
552 if (fileName.isEmpty())
555 boost::filesystem::path outfile(fileName.toStdString());
556 std::string aux = outfile.leaf().string();
557 m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
558 aux = outfile.string();
559 m_ui->m_repositoryLineEdit->setText(aux.c_str());
562 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
567 std::map<std::string, std::vector<te::attributefill::OperationType> > result;
569 std::auto_ptr<te::da::DataSetType> fromScheme = getCurrentFromLayer()->getSchema();
571 std::vector<std::string> props;
572 for(
int i = 0; i < m_ui->m_statisticsListWidget->count(); ++i)
574 QListWidgetItem* item = m_ui->m_statisticsListWidget->item(i);
576 if(!item->isSelected())
579 std::string itemText = item->text().toStdString();
581 std::vector<std::string> tokens;
584 std::string propName = tokens[0];
585 boost::trim(propName);
589 if(std::find(props.begin(), props.end(), propName) != props.end())
591 std::vector<te::attributefill::OperationType> vec;
592 vec.push_back(operationType);
594 result[propName] = vec;
598 result[propName].push_back(operationType);
607 m_ui->m_selectAllComboBox->setCurrentIndex(0);
608 m_ui->m_rejectAllComboBox->setCurrentIndex(0);
609 m_ui->m_statisticsListWidget->clear();
613 std::auto_ptr<te::da::DataSetType> dst = layer->getSchema();
614 std::auto_ptr<te::da::DataSet> ds = layer->getData();
620 std::string geomPropName = layer->getGeomPropertyName();
622 if(geomPropName.empty())
630 std::vector<te::dt::Property*> props = dst->getProperties();
632 for(std::size_t i = 0; i < props.size(); ++i)
636 if(isValidPropertyType(prop->
getType()))
638 int propertyType = prop->
getType();
642 m_ui->m_statisticsListWidget->addItem(item);
648 m_ui->m_statisticsListWidget->addItem(item);
652 m_ui->m_statisticsListWidget->addItem(item);
656 m_ui->m_statisticsListWidget->addItem(item);
660 m_ui->m_statisticsListWidget->addItem(item);
666 m_ui->m_statisticsListWidget->addItem(item);
670 m_ui->m_statisticsListWidget->addItem(item);
674 m_ui->m_statisticsListWidget->addItem(item);
678 m_ui->m_statisticsListWidget->addItem(item);
682 m_ui->m_statisticsListWidget->addItem(item);
686 m_ui->m_statisticsListWidget->addItem(item);
690 m_ui->m_statisticsListWidget->addItem(item);
694 m_ui->m_statisticsListWidget->addItem(item);
698 m_ui->m_statisticsListWidget->addItem(item);
702 m_ui->m_statisticsListWidget->addItem(item);
706 m_ui->m_statisticsListWidget->addItem(item);
710 m_ui->m_statisticsListWidget->addItem(item);
714 m_ui->m_statisticsListWidget->addItem(item);
718 m_ui->m_statisticsListWidget->addItem(item);
721 if(isClassType(prop->
getType()))
725 m_ui->m_statisticsListWidget->addItem(item);
729 if(isClassType(prop->
getType()) && isPolygon(geomType) && isPolygon(toGeomType))
733 m_ui->m_statisticsListWidget->addItem(item);
737 m_ui->m_statisticsListWidget->addItem(item);
741 m_ui->m_statisticsListWidget->addItem(item);
746 m_ui->m_statisticsListWidget->addItem(item);
750 m_ui->m_statisticsListWidget->addItem(item);
752 if(isPolygon(geomType) && isPolygon(toGeomType))
754 if(isNumProperty(prop->
getType()))
758 m_ui->m_statisticsListWidget->addItem(item);
762 m_ui->m_statisticsListWidget->addItem(item);
767 m_ui->m_statisticsListWidget->addItem(item);
771 m_ui->m_statisticsListWidget->addItem(
"");
774 int lastRow = m_ui->m_statisticsListWidget->count() - 1;
775 delete m_ui->m_statisticsListWidget->item(lastRow);
850 std::auto_ptr<te::da::DataSetType> toSchema = toLayer->getSchema();
875 std::auto_ptr<te::da::DataSetType> toSchema = toLayer->getSchema();
877 std::vector<te::dt::Property*> props = toSchema->getProperties();
878 std::vector<te::dt::Property*> pkProps = toSchema->getPrimaryKey()->getProperties();
880 std::vector<std::string> inputNames;
881 std::vector<std::string> outputNames;
882 for(std::size_t i = 0; i < props.size(); ++i)
885 for(std::size_t j = 0; j < pkProps.size(); ++j)
887 if(props[i]->getName() == pkProps[j]->getName())
896 if(std::find(m_outputAttributes.begin(), m_outputAttributes.end(), props[i]->getName()) != m_outputAttributes.end())
898 outputNames.push_back(props[i]->getName());
902 inputNames.push_back(props[i]->getName());
907 QDialog* dialog =
new QDialog(
this);
908 dialog->setWindowTitle(tr(
"Vector To Vector"));
910 QBoxLayout* vLayout =
new QBoxLayout(QBoxLayout::TopToBottom, dialog);
917 vLayout->addWidget(d);
919 QDialogButtonBox* bbox =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
920 connect(bbox, SIGNAL(accepted()), dialog, SLOT(accept()));
921 connect(bbox, SIGNAL(rejected()), dialog, SLOT(reject()));
922 vLayout->addWidget(bbox);
924 int res = dialog->exec();
926 if(res == QDialog::Accepted)
928 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 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.