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>
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"));
81 m_ui->m_outputSRIDToolButton->setIcon(QIcon::fromTheme(
"srs"));
82 m_ui->m_helpPushButton->setPageReference(
"widgets/exchanger_direct/exchanger_direct.html");
100 m_ui->m_inputLayerComboBox->clear();
102 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
104 while(it != m_layers.end())
108 std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
110 if(dsType.get() && !dsType->hasRaster())
111 m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
116 if(m_ui->m_inputLayerComboBox->count() > 0)
118 QString s = m_ui->m_inputLayerComboBox->currentText();
120 onInputLayerActivated(s);
123 if(m_ui->m_inputLayerComboBox->count() > 1)
124 m_ui->m_inputLayerComboBox->setEnabled(
true);
129 m_lastDsType = dataSource;
134 m_ui->m_outputDataSourceComboBox->clear();
136 std::vector<te::da::DataSourceInfoPtr> datasources;
140 for(std::size_t i = 0; i < datasources.size(); ++i)
144 if(datasource.get() == 0)
147 const std::string& title = datasource->getTitle();
149 m_ui->m_outputDataSourceComboBox->addItem(title.c_str(), QVariant::fromValue(datasource));
155 int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
159 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Input layer not selected."));
163 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
168 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
172 if(m_ui->m_dataSetLineEdit->text().isEmpty())
174 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output File Name not defined."));
179 int inputSRID = m_ui->m_inputSRIDLineEdit->text().toInt();
181 int outputSRID = m_ui->m_outputSRIDLineEdit->text().toInt();
186 std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
189 std::map<std::string, std::string> connInfo;
190 connInfo[
"URI"] = m_ui->m_dataSetLineEdit->text().toStdString();
193 dsOGR->setConnectionInfo(connInfo);
202 boost::filesystem::path uri(m_ui->m_dataSetLineEdit->text().toStdString());
204 std::string val = uri.stem().string();
209 if(!dsOGR->isDataSetNameValid(dsTypeResult->
getName()))
211 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);
213 if(r == QMessageBox::Yes)
217 dsTypeResult->
setName(newName);
226 std::vector<te::dt::Property* > props = dsTypeResult->
getProperties();
227 std::map<std::size_t, std::string> invalidNames;
228 for(std::size_t i = 0; i < props.size(); ++i)
230 if(!dsOGR->isPropertyNameValid(props[i]->getName()))
232 invalidNames[i] = props[i]->getName();
236 if(!invalidNames.empty())
238 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);
240 if(r == QMessageBox::Yes)
242 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
244 while(it != invalidNames.end())
249 props[it->first]->setName(newName);
256 QString err(tr(
"Some property name is invalid for output datasource:\n\n"));
258 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
260 while(it != invalidNames.end())
263 err.append(it->second.c_str());
273 std::map<std::string,std::string> nopt;
275 std::auto_ptr<te::da::DataSet> dataset = layer->getData();
277 dsOGR->createDataSet(dsTypeResult, nopt);
281 if(dataset->moveBeforeFirst())
282 dsOGR->add(dsTypeResult->
getName(), dsAdapter.get(), dsOGR->getConnectionInfo());
286 QMessageBox::information(
this, tr(
"Exchanger"), tr(
"Layer exported successfully."));
288 catch(
const std::exception& e)
290 QString errMsg(tr(
"Error during exchanger. The reported error is: %1"));
292 errMsg = errMsg.arg(e.what());
294 QMessageBox::information(
this, tr(
"Exchanger"), errMsg);
304 int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
308 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Input layer not selected."));
312 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
317 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
321 int idxDataSource = m_ui->m_outputDataSourceComboBox->currentIndex();
325 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output data source not selected."));
329 QVariant varDataSource = m_ui->m_outputDataSourceComboBox->itemData(idxDataSource, Qt::UserRole);
334 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected data source."));
338 if(m_ui->m_dataSetLineEdit->text().isEmpty())
340 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Data Set name not defined."));
345 int inputSRID = m_ui->m_inputSRIDLineEdit->text().toInt();
347 int outputSRID = m_ui->m_outputSRIDLineEdit->text().toInt();
349 std::auto_ptr<te::da::DataSourceTransactor> transactor;
353 setCursor(Qt::WaitCursor);
356 std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
368 for (
size_t t = 0; t < props.size(); ++t)
380 dsType->setPrimaryKey(0);
386 transactor = targetDSPtr->getTransactor();
394 dsTypeResult->
setName(m_ui->m_dataSetLineEdit->text().toStdString());
397 if(!targetDSPtr->isDataSetNameValid(dsTypeResult->
getName()))
399 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);
401 if(r == QMessageBox::Yes)
405 dsTypeResult->
setName(newName);
414 std::vector<te::dt::Property* > props = dsTypeResult->
getProperties();
415 std::map<std::size_t, std::string> invalidNames;
416 for(std::size_t i = 0; i < props.size(); ++i)
418 if(!targetDSPtr->isPropertyNameValid(props[i]->getName()))
420 invalidNames[i] = props[i]->getName();
424 if(!invalidNames.empty())
426 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);
428 if(r == QMessageBox::Yes)
430 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
432 while(it != invalidNames.end())
437 props[it->first]->setName(newName);
444 QString err(tr(
"Some property name is invalid for output datasource:\n\n"));
446 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
448 while(it != invalidNames.end())
451 err.append(it->second.c_str());
461 std::set<std::string> names;
465 for (std::size_t i = 0; i < props.size(); ++i)
468 std::string propName = props[i]->getName();
479 props[i]->setName(propName);
483 if(m_ui->m_spatialIndexCheckBox->isChecked())
491 std::string name = m_ui->m_dataSetLineEdit->text().toStdString() +
"_" + p->
getName() +
"_idx";
493 boost::replace_all(name,
".",
"_");
507 if (dsType->getPrimaryKey())
511 std::string name = m_ui->m_dataSetLineEdit->text().toStdString() +
"_" + dsType->getPrimaryKey()->getName() +
"_pk";
513 boost::replace_all(name,
".",
"_");
517 std::vector<te::dt::Property*> props = dsType->getPrimaryKey()->getProperties();
519 for (
size_t t = 0; t < props.size(); ++t)
529 std::map<std::string,std::string> nopt;
531 std::auto_ptr<te::da::DataSet> dataset = layer->getData();
535 transactor->createDataSet(dsTypeResult, nopt);
539 if(dataset->moveBeforeFirst())
540 transactor->add(dsTypeResult->
getName(), dsAdapter.get(), targetDSPtr->getConnectionInfo());
544 std::string name = m_ui->m_dataSetLineEdit->text().toStdString() +
"_id";
549 transactor->addProperty(dsTypeResult->
getName(), p);
555 boost::replace_all(name,
".",
"_");
561 transactor->addPrimaryKey(dsTypeResult->
getName(), pk);
564 transactor->commit();
566 setCursor(Qt::ArrowCursor);
567 QMessageBox::information(
this, tr(
"Exchanger"), tr(
"Layer exported successfully."));
569 catch(
const std::exception& e)
571 setCursor(Qt::ArrowCursor);
573 transactor->rollBack();
575 QString errMsg(tr(
"Error during exchanger. The reported error is: %1"));
577 errMsg = errMsg.arg(e.what());
579 QMessageBox::information(
this, tr(
"Exchanger"), errMsg);
589 QString value = m_ui->m_dsTypeComboBox->itemData(index).toString();
591 m_outputDataSourceType = value.toStdString();
593 if(m_outputDataSourceType ==
"POSTGIS")
595 m_exchangeToFile =
false;
597 m_ui->m_outputDataSourceComboBox->setEnabled(
true);
598 m_ui->m_dsToolButton->setEnabled(
true);
599 m_ui->m_dataSetLineEdit->clear();
600 m_ui->m_dataSetLineEdit->setEnabled(
true);
601 m_ui->m_dirToolButton->setEnabled(
false);
602 m_ui->m_spatialIndexCheckBox->setEnabled(
true);
603 m_ui->m_spatialIndexCheckBox->setChecked(
true);
607 else if(m_outputDataSourceType ==
"ADO")
609 m_exchangeToFile =
false;
611 m_ui->m_outputDataSourceComboBox->setEnabled(
true);
612 m_ui->m_dsToolButton->setEnabled(
true);
613 m_ui->m_dataSetLineEdit->clear();
614 m_ui->m_dataSetLineEdit->setEnabled(
true);
615 m_ui->m_dirToolButton->setEnabled(
false);
616 m_ui->m_spatialIndexCheckBox->setEnabled(
false);
617 m_ui->m_spatialIndexCheckBox->setChecked(
false);
621 else if(m_outputDataSourceType ==
"OGR")
623 m_exchangeToFile =
true;
625 m_ui->m_outputDataSourceComboBox->clear();
626 m_ui->m_outputDataSourceComboBox->setEnabled(
false);
627 m_ui->m_dsToolButton->setEnabled(
false);
628 m_ui->m_dataSetLineEdit->clear();
629 m_ui->m_dataSetLineEdit->setEnabled(
false);
630 m_ui->m_dirToolButton->setEnabled(
true);
631 m_ui->m_spatialIndexCheckBox->setEnabled(
false);
632 m_ui->m_spatialIndexCheckBox->setChecked(
false);
638 onDataSourceTypeActivated(m_ui->m_dsTypeComboBox->currentIndex());
640 if(m_ui->m_dataSetLineEdit->isEnabled())
641 m_ui->m_dataSetLineEdit->setText(value);
643 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
648 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
652 int inputSRID = layer->getSRID();
654 m_ui->m_inputSRIDLineEdit->setText(QString::number(inputSRID));
655 m_ui->m_outputSRIDLineEdit->setText(QString::number(inputSRID));
659 m_ui->m_outputSRIDLineEdit->setEnabled(
false);
660 m_ui->m_outputSRIDToolButton->setEnabled(
false);
664 m_ui->m_outputSRIDLineEdit->setEnabled(
true);
665 m_ui->m_outputSRIDToolButton->setEnabled(
true);
671 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
672 QString(), tr(
"Shapefile (*.shp *.SHP);;Mapinfo File (*.mif *.MIF);;KML (*.kml *.KML);;GeoJSON (*.geojson *.GEOJSON);;GML (*.gml *.GML);;DXF (*.dxf *.DXF);;DGN (*.dgn *.DGN);;"),0, QFileDialog::DontConfirmOverwrite);
674 if (fileName.isEmpty())
677 m_ui->m_dataSetLineEdit->setText(fileName);
684 if(!m_lastDsType.empty())
685 dExplorer->setDataSourceToUse(m_lastDsType.c_str());
694 if (m_ui->m_outputSRIDLineEdit->text().isEmpty())
696 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output Layer SRID not defined."));
700 int inputSRID = m_ui->m_inputSRIDLineEdit->text().toInt();
701 int outputSRID = m_ui->m_outputSRIDLineEdit->text().toInt();
705 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid output Layer SRID."));
711 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid input Layer SRID."));
718 res = exchangeToFile();
720 res = exchangeToDatabase();
728 m_ui->m_dsTypeComboBox->clear();
736 std::string dataSourceName = it->first;
738 if (dataSourceName ==
"POSTGIS" || dataSourceName ==
"ADO" || dataSourceName ==
"OGR")
741 QString title = QString::fromStdString(it->second->getTitle());
743 m_ui->m_dsTypeComboBox->addItem(icon, title, QVariant(dataSourceName.c_str()));
753 srsDialog.setWindowTitle(tr(
"Choose the SRS"));
755 if (srsDialog.exec() == QDialog::Rejected)
760 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.
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)
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.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
DataSetType * getResult() const
virtual Property * clone() const =0
It returns a clone of the object.
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.
TECOMMONEXPORT 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.
An converter for DataSetType.
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.
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
static std::auto_ptr< DataSource > make(const std::string &dsType)
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.