27 #include "../../../common/StringUtils.h" 28 #include "../../../dataaccess/dataset/DataSetAdapter.h" 29 #include "../../../dataaccess/dataset/PrimaryKey.h" 30 #include "../../../dataaccess/dataset/DataSetTypeConverter.h" 31 #include "../../../dataaccess/datasource/DataSourceFactory.h" 32 #include "../../../dataaccess/datasource/DataSourceInfo.h" 33 #include "../../../dataaccess/datasource/DataSourceInfoManager.h" 34 #include "../../../dataaccess/datasource/DataSourceManager.h" 35 #include "../../../dataaccess/datasource/DataSourceTransactor.h" 36 #include "../../../dataaccess/utils/Utils.h" 37 #include "../../../geometry/GeometryProperty.h" 38 #include "../../../maptools/DataSetLayer.h" 39 #include "../../../srs/Config.h" 40 #include "../../widgets/datasource/selector/DataSourceExplorerDialog.h" 41 #include "../../widgets/datasource/core/DataSourceType.h" 42 #include "../../widgets/datasource/core/DataSourceTypeManager.h" 43 #include "../../widgets/srs/SRSManagerDialog.h" 45 #include "ui_DirectExchangerDialogForm.h" 48 #include <QFileDialog> 49 #include <QMessageBox> 52 #include <boost/algorithm/string/replace.hpp> 53 #include <boost/filesystem.hpp> 54 #include <boost/lexical_cast.hpp> 55 #include <boost/uuid/random_generator.hpp> 56 #include <boost/uuid/uuid_io.hpp> 62 te::qt::widgets::DirectExchangerDialog::DirectExchangerDialog(
QWidget* parent, Qt::WindowFlags f)
64 m_ui(new
Ui::DirectExchangerDialogForm)
70 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"data-exchange-direct-hint").pixmap(112,48));
71 m_ui->m_dsToolButton->setIcon(QIcon::fromTheme(
"datasource"));
74 connect(m_ui->m_okPushButton, SIGNAL(clicked()),
this, SLOT(onOkPushButtonClicked()));
75 connect(m_ui->m_dirToolButton, SIGNAL(clicked()),
this, SLOT(onDirToolButtonClicked()));
76 connect(m_ui->m_dsToolButton, SIGNAL(clicked()),
this, SLOT(onDataSoruceToolButtonClicked()));
77 connect(m_ui->m_dsTypeComboBox, SIGNAL(activated(
int)),
this, SLOT(onDataSourceTypeActivated(
int)));
78 connect(m_ui->m_inputLayerComboBox, SIGNAL(activated(QString)),
this, SLOT(onInputLayerActivated(QString)));
79 connect(m_ui->m_outputSRIDToolButton, SIGNAL(clicked()), SLOT(onOutputLayerSRSTriggered()));
81 m_ui->m_outputSRIDToolButton->setIcon(QIcon::fromTheme(
"srs"));
82 m_ui->m_helpPushButton->setPageReference(
"widgets/exchanger_direct/exchanger_direct.html");
85 m_outputDataSourceType =
"";
86 m_exchangeToFile =
false;
89 setOutputDataSources();
98 m_ui->m_inputLayerComboBox->clear();
100 std::list<te::map::AbstractLayerPtr>::iterator it =
m_layers.begin();
106 std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
108 if(dsType.get() && !dsType->hasRaster())
109 m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
114 if(
m_ui->m_inputLayerComboBox->count() > 0)
116 QString s =
m_ui->m_inputLayerComboBox->currentText();
121 if(
m_ui->m_inputLayerComboBox->count() > 1)
122 m_ui->m_inputLayerComboBox->setEnabled(
true);
132 m_ui->m_outputDataSourceComboBox->clear();
134 std::vector<te::da::DataSourceInfoPtr> datasources;
138 for(std::size_t i = 0; i < datasources.size(); ++i)
142 if(datasource.get() ==
nullptr)
145 const std::string& title = datasource->getTitle();
147 m_ui->m_outputDataSourceComboBox->addItem(title.c_str(), QVariant::fromValue(datasource));
153 int idxLayer =
m_ui->m_inputLayerComboBox->currentIndex();
157 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Input layer not selected."));
161 QVariant varLayer =
m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
166 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
170 if(
m_ui->m_dataSetLineEdit->text().isEmpty())
172 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output File Name not defined."));
177 int inputSRID =
m_ui->m_inputSRIDLineEdit->text().toInt();
179 int outputSRID =
m_ui->m_outputSRIDLineEdit->text().toInt();
184 std::unique_ptr<te::da::DataSetType> dsType = layer->getSchema();
186 QFileInfo fileInfo(
m_ui->m_dataSetLineEdit->text());
189 std::string connInfo(
"file://" + std::string(fileInfo.absoluteFilePath().toUtf8().data()));
191 if (fileInfo.suffix().toUtf8().data() == std::string(
"csv"))
192 connInfo +=
"?GEOMETRY=AS_WKT";
194 if (connInfo.find(
"(") != std::string::npos || connInfo.find(
")") != std::string::npos)
196 throw te::common::Exception(tr(
"The output name has invalid characters (parentheses).\nRemoves these characters to perform the operation.").toUtf8());
201 if (dsOGR.get() ==
nullptr)
212 boost::filesystem::path uri(
m_ui->m_dataSetLineEdit->text().toUtf8().data());
214 std::string val = uri.stem().string();
219 if(!dsOGR->isDataSetNameValid(dsTypeResult->
getName()))
221 int r = QMessageBox::question(
this, tr(
"Exchanger"), tr(
"Layer name invalid for output datasource. Would you like to normalize the name?"), QMessageBox::Yes, QMessageBox::No);
223 if(r == QMessageBox::Yes)
227 dsTypeResult->
setName(newName);
236 std::vector<te::dt::Property* > props = dsTypeResult->
getProperties();
237 std::map<std::size_t, std::string> invalidNames;
238 for(std::size_t i = 0; i < props.size(); ++i)
240 if(!dsOGR->isPropertyNameValid(props[i]->getName()))
242 invalidNames[i] = props[i]->getName();
246 if(!invalidNames.empty())
248 int r = QMessageBox::question(
this, tr(
"Exchanger"), tr(
"Some property name is invalid for output datasource. Would you like to normalize the name?"), QMessageBox::Yes, QMessageBox::No);
250 if(r == QMessageBox::Yes)
252 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
254 while(it != invalidNames.end())
259 props[it->first]->setName(newName);
266 QString err(tr(
"Some property name is invalid for output datasource:\n\n"));
268 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
270 while(it != invalidNames.end())
273 err.append(it->second.c_str());
283 std::map<std::string,std::string> nopt;
285 std::unique_ptr<te::da::DataSet> dataset = layer->getData();
287 dsOGR->createDataSet(dsTypeResult, nopt);
291 if(dataset->moveBeforeFirst())
292 dsOGR->add(dsTypeResult->
getName(), dsAdapter.get(), nopt);
296 QMessageBox::information(
this, tr(
"Exchanger"), tr(
"Layer exported successfully."));
298 catch(
const std::exception& e)
300 QString errMsg(tr(
"Error during exchanger. The reported error is: %1"));
302 errMsg = errMsg.arg(e.what());
304 QMessageBox::information(
this, tr(
"Exchanger"), errMsg);
310 QString errMsg(tr(
"Error during exchanger."));
312 QMessageBox::information(
this, tr(
"Exchanger"), errMsg);
322 int idxLayer =
m_ui->m_inputLayerComboBox->currentIndex();
326 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Input layer not selected."));
330 QVariant varLayer =
m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
335 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
339 int idxDataSource =
m_ui->m_outputDataSourceComboBox->currentIndex();
343 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output data source not selected."));
347 QVariant varDataSource =
m_ui->m_outputDataSourceComboBox->itemData(idxDataSource, Qt::UserRole);
352 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected data source."));
356 if(
m_ui->m_dataSetLineEdit->text().isEmpty())
358 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Data Set name not defined."));
363 int inputSRID =
m_ui->m_inputSRIDLineEdit->text().toInt();
365 int outputSRID =
m_ui->m_outputSRIDLineEdit->text().toInt();
367 std::unique_ptr<te::da::DataSourceTransactor> transactor;
371 setCursor(Qt::WaitCursor);
374 std::unique_ptr<te::da::DataSetType> dsType = layer->getSchema();
386 for (
size_t t = 0; t < props.size(); ++t)
388 std::vector<std::string> tokens;
391 size_t tkSize = tokens.size();
392 props[t]->setName(tokens[tkSize - 1]);
404 dsType->setPrimaryKey(
nullptr);
410 if (!targetDSPtr->isOpened())
413 transactor = targetDSPtr->getTransactor();
421 dsTypeResult->
setName(
m_ui->m_dataSetLineEdit->text().toUtf8().data());
424 if(!targetDSPtr->isDataSetNameValid(dsTypeResult->
getName()))
426 int r = QMessageBox::question(
this, tr(
"Exchanger"), tr(
"Layer name invalid for output datasource. Would you like to normalize the name?"), QMessageBox::Yes, QMessageBox::No);
428 if(r == QMessageBox::Yes)
432 dsTypeResult->
setName(newName);
441 std::vector<te::dt::Property* > props = dsTypeResult->
getProperties();
442 std::map<std::size_t, std::string> invalidNames;
443 for(std::size_t i = 0; i < props.size(); ++i)
445 if(!targetDSPtr->isPropertyNameValid(props[i]->getName()))
447 invalidNames[i] = props[i]->getName();
451 if(!invalidNames.empty())
453 int r = QMessageBox::question(
this, tr(
"Exchanger"), tr(
"Some property name is invalid for output datasource. Would you like to normalize the name?"), QMessageBox::Yes, QMessageBox::No);
455 if(r == QMessageBox::Yes)
457 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
459 while(it != invalidNames.end())
464 props[it->first]->setName(newName);
471 QString err(tr(
"Some property name is invalid for output datasource:\n\n"));
473 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
475 while(it != invalidNames.end())
478 err.append(it->second.c_str());
488 std::set<std::string> names;
492 for (std::size_t i = 0; i < props.size(); ++i)
495 std::string propName = props[i]->getName();
506 props[i]->setName(propName);
510 if(
m_ui->m_spatialIndexCheckBox->isChecked())
518 std::string name = std::string(
m_ui->m_dataSetLineEdit->text().toUtf8().data()) +
"_" + p->
getName() +
"_idx";
520 boost::replace_all(name,
".",
"_");
534 if (dsType->getPrimaryKey())
538 std::string name = std::string(
m_ui->m_dataSetLineEdit->text().toUtf8().data()) +
"_" + dsType->getPrimaryKey()->getName() +
"_pk";
540 boost::replace_all(name,
".",
"_");
544 std::vector<te::dt::Property*> props = dsType->getPrimaryKey()->getProperties();
546 for (
size_t t = 0; t < props.size(); ++t)
556 std::map<std::string,std::string> nopt;
558 std::unique_ptr<te::da::DataSet> dataset = layer->getData();
562 transactor->createDataSet(dsTypeResult, nopt);
566 if(dataset->moveBeforeFirst())
567 transactor->add(dsTypeResult->
getName(), dsAdapter.get(), nopt);
571 std::string name = std::string(
m_ui->m_dataSetLineEdit->text().toUtf8().data()) +
"_id";
574 p->setAutoNumber(
true);
576 transactor->addProperty(dsTypeResult->
getName(),
p);
582 boost::replace_all(name,
".",
"_");
588 transactor->addPrimaryKey(dsTypeResult->
getName(), pk);
591 transactor->commit();
593 setCursor(Qt::ArrowCursor);
594 QMessageBox::information(
this, tr(
"Exchanger"), tr(
"Layer exported successfully."));
596 catch(
const std::exception& e)
598 setCursor(Qt::ArrowCursor);
600 if (transactor.get())
601 transactor->rollBack();
603 QString errMsg(tr(
"Error during exchanger. The reported error is: %1"));
605 errMsg = errMsg.arg(e.what());
607 QMessageBox::information(
this, tr(
"Exchanger"), errMsg);
617 QString value =
m_ui->m_dsTypeComboBox->itemData(index).toString();
625 m_ui->m_outputDataSourceComboBox->setEnabled(
true);
626 m_ui->m_dsToolButton->setEnabled(
true);
627 m_ui->m_dataSetLineEdit->clear();
628 m_ui->m_dataSetLineEdit->setEnabled(
true);
629 m_ui->m_dirToolButton->setEnabled(
false);
630 m_ui->m_spatialIndexCheckBox->setEnabled(
true);
631 m_ui->m_spatialIndexCheckBox->setChecked(
true);
639 m_ui->m_outputDataSourceComboBox->setEnabled(
true);
640 m_ui->m_dsToolButton->setEnabled(
true);
641 m_ui->m_dataSetLineEdit->clear();
642 m_ui->m_dataSetLineEdit->setEnabled(
true);
643 m_ui->m_dirToolButton->setEnabled(
false);
644 m_ui->m_spatialIndexCheckBox->setEnabled(
false);
645 m_ui->m_spatialIndexCheckBox->setChecked(
false);
653 m_ui->m_outputDataSourceComboBox->clear();
654 m_ui->m_outputDataSourceComboBox->setEnabled(
false);
655 m_ui->m_dsToolButton->setEnabled(
false);
656 m_ui->m_dataSetLineEdit->clear();
657 m_ui->m_dataSetLineEdit->setEnabled(
false);
658 m_ui->m_dirToolButton->setEnabled(
true);
659 m_ui->m_spatialIndexCheckBox->setEnabled(
false);
660 m_ui->m_spatialIndexCheckBox->setChecked(
false);
668 if(
m_ui->m_dataSetLineEdit->isEnabled())
669 m_ui->m_dataSetLineEdit->setText(value);
671 QVariant varLayer =
m_ui->m_inputLayerComboBox->itemData(
m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
676 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
680 int inputSRID = layer->getSRID();
682 m_ui->m_inputSRIDLineEdit->setText(QString::number(inputSRID));
683 m_ui->m_outputSRIDLineEdit->setText(QString::number(inputSRID));
687 m_ui->m_outputSRIDLineEdit->setEnabled(
false);
688 m_ui->m_outputSRIDToolButton->setEnabled(
false);
692 m_ui->m_outputSRIDLineEdit->setEnabled(
true);
693 m_ui->m_outputSRIDToolButton->setEnabled(
true);
699 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
700 QString(), tr(
"Shapefile (*.shp *.SHP);;Mapinfo File (*.mif *.MIF);;KML (*.kml *.KML);;GeoJSON (*.geojson *.GEOJSON);;GML (*.gml *.GML);;DXF (*.dxf *.DXF);;DGN (*.dgn *.DGN);; CSV (*.csv *.CSV);;"),
nullptr, QFileDialog::DontConfirmOverwrite);
702 if (fileName.isEmpty())
705 m_ui->m_dataSetLineEdit->setText(fileName);
722 if (
m_ui->m_outputSRIDLineEdit->text().isEmpty())
724 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output Layer SRID not defined."));
728 int inputSRID =
m_ui->m_inputSRIDLineEdit->text().toInt();
729 int outputSRID =
m_ui->m_outputSRIDLineEdit->text().toInt();
733 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid output Layer SRID."));
739 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid input Layer SRID."));
756 m_ui->m_dsTypeComboBox->clear();
764 std::string dataSourceName = it->first;
766 if (dataSourceName ==
"POSTGIS" || dataSourceName ==
"ADO" || dataSourceName ==
"OGR")
769 QString title = QString::fromUtf8(it->second->getTitle().c_str());
771 m_ui->m_dsTypeComboBox->addItem(icon, title, QVariant(dataSourceName.c_str()));
781 srsDialog.setWindowTitle(tr(
"Choose the SRS"));
783 if (srsDialog.exec() == QDialog::Rejected)
788 m_ui->m_outputSRIDLineEdit->setText(QString::number(srid.first));
virtual void setName(const std::string &name)
It sets the constraint name.
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) Q_DECLARE_METATYPE(te
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
An atomic property like an integer or double.
boost::shared_ptr< DataSource > DataSourcePtr
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
TEDATAACCESSEXPORT bool HasLinkedTable(te::da::DataSetType *type)
It checks if the datasettype has a linked table.
A class that models the description of a dataset.
DataSetType * getResult() const
void setIndexType(IndexType t)
It sets the index type.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
void add(te::dt::Property *p)
It adds the property to the list of properties of the index.
te::dt::Property * clone() const
It returns a clone of the object.
std::string ReplaceSpecialChars(const std::string &str, bool &changed)
It replace special characters of a string.
It models a property definition.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
void setDefaultValue(std::string *d)
It sets the default value associated to the property, or NULL if none is associated.
virtual te::dt::AbstractData * clone() const
It clones the linestring.
An converter for DataSetType.
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 setName(const std::string &name)
It sets the property name.
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
A direct exchanger dialog for ADO, POSTGIS and SHP data sources.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
It describes a primary key (pk) constraint.
void setRequired(bool r)
It tells if the property is required or not.
void setName(const std::string &name)
It sets the index name.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
It describes an index associated to a DataSetType.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
const std::string & getName() const
It returns the property name.