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 "../../widgets/datasource/selector/DataSourceExplorerDialog.h"
40 #include "../../widgets/srs/SRSManagerDialog.h"
42 #include "ui_DirectExchangerDialogForm.h"
45 #include <QFileDialog>
46 #include <QMessageBox>
49 #include <boost/algorithm/string/replace.hpp>
50 #include <boost/filesystem.hpp>
51 #include <boost/lexical_cast.hpp>
52 #include <boost/uuid/random_generator.hpp>
53 #include <boost/uuid/uuid_io.hpp>
61 m_ui(new Ui::DirectExchangerDialogForm)
67 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"data-exchange-direct-hint").pixmap(112,48));
68 m_ui->m_dsToolButton->setIcon(QIcon::fromTheme(
"datasource"));
77 m_ui->m_helpPushButton->setPageReference(
"widgets/exchanger_direct/exchanger_direct.html");
95 m_ui->m_inputLayerComboBox->clear();
97 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
99 while(it != m_layers.end())
103 std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
105 if(dsType.get() && !dsType->hasRaster())
106 m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
111 if(m_ui->m_inputLayerComboBox->count() > 0)
113 QString s = m_ui->m_inputLayerComboBox->currentText();
115 onInputLayerActivated(s);
118 if(m_ui->m_inputLayerComboBox->count() > 1)
119 m_ui->m_inputLayerComboBox->setEnabled(
true);
124 m_lastDsType = dataSource;
129 m_ui->m_outputDataSourceComboBox->clear();
131 std::vector<te::da::DataSourceInfoPtr> datasources;
135 for(std::size_t i = 0; i < datasources.size(); ++i)
139 if(datasource.get() == 0)
142 const std::string& title = datasource->getTitle();
144 m_ui->m_outputDataSourceComboBox->addItem(title.c_str(), QVariant::fromValue(datasource));
150 int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
154 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Input layer not selected."));
158 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
163 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
167 if(m_ui->m_dataSetLineEdit->text().isEmpty())
169 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output File Name not defined."));
176 std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
179 std::map<std::string, std::string> connInfo;
180 connInfo[
"URI"] = m_ui->m_dataSetLineEdit->text().toStdString();
183 dsOGR->setConnectionInfo(connInfo);
193 if(p && (p->
getSRID() != layer->getSRID()))
198 boost::filesystem::path uri(m_ui->m_dataSetLineEdit->text().toStdString());
200 std::string val = uri.stem().string();
202 dsTypeResult->setName(val);
205 if(!dsOGR->isDataSetNameValid(dsTypeResult->getName()))
207 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);
209 if(r == QMessageBox::Yes)
213 dsTypeResult->setName(newName);
222 std::vector<te::dt::Property* > props = dsTypeResult->getProperties();
223 std::map<std::size_t, std::string> invalidNames;
224 for(std::size_t i = 0; i < props.size(); ++i)
226 if(!dsOGR->isPropertyNameValid(props[i]->getName()))
228 invalidNames[i] = props[i]->getName();
232 if(!invalidNames.empty())
234 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);
236 if(r == QMessageBox::Yes)
238 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
240 while(it != invalidNames.end())
245 props[it->first]->setName(newName);
252 QString err(tr(
"Some property name is invalid for output datasource:\n\n"));
254 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
256 while(it != invalidNames.end())
259 err.append(it->second.c_str());
269 std::map<std::string,std::string> nopt;
271 std::auto_ptr<te::da::DataSet> dataset = layer->getData();
273 dsOGR->createDataSet(dsTypeResult, nopt);
277 dsAdapter->setSRID(layer->getSRID());
279 if(dataset->moveBeforeFirst())
280 dsOGR->add(dsTypeResult->getName(), dsAdapter.get(), dsOGR->getConnectionInfo());
284 QMessageBox::information(
this, tr(
"Exchanger"), tr(
"Layer exported successfully."));
286 catch(
const std::exception& e)
288 QString errMsg(tr(
"Error during exchanger. The reported error is: %1"));
290 errMsg = errMsg.arg(e.what());
292 QMessageBox::information(
this, tr(
"Exchanger"), errMsg);
302 int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
306 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Input layer not selected."));
310 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
315 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
319 int idxDataSource = m_ui->m_outputDataSourceComboBox->currentIndex();
323 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output data source not selected."));
327 QVariant varDataSource = m_ui->m_outputDataSourceComboBox->itemData(idxDataSource, Qt::UserRole);
332 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected data source."));
336 if(m_ui->m_dataSetLineEdit->text().isEmpty())
338 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Data Set name not defined."));
342 std::auto_ptr<te::da::DataSourceTransactor> transactor;
346 setCursor(Qt::WaitCursor);
349 std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
353 transactor = targetDSPtr->getTransactor();
359 dsTypeResult->
setName(m_ui->m_dataSetLineEdit->text().toStdString());
362 if(!targetDSPtr->isDataSetNameValid(dsTypeResult->getName()))
364 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);
366 if(r == QMessageBox::Yes)
370 dsTypeResult->setName(newName);
379 std::vector<te::dt::Property* > props = dsTypeResult->getProperties();
380 std::map<std::size_t, std::string> invalidNames;
381 for(std::size_t i = 0; i < props.size(); ++i)
383 if(!targetDSPtr->isPropertyNameValid(props[i]->getName()))
385 invalidNames[i] = props[i]->getName();
389 if(!invalidNames.empty())
391 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);
393 if(r == QMessageBox::Yes)
395 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
397 while(it != invalidNames.end())
402 props[it->first]->setName(newName);
409 QString err(tr(
"Some property name is invalid for output datasource:\n\n"));
411 std::map<std::size_t, std::string>::iterator it = invalidNames.begin();
413 while(it != invalidNames.end())
416 err.append(it->second.c_str());
426 if(m_ui->m_spatialIndexCheckBox->isChecked())
431 if(p && (p->
getSRID() != layer->getSRID()))
440 std::string name = m_ui->m_dataSetLineEdit->text().toStdString() +
"_" + p->
getName() +
"_idx";
442 boost::replace_all(name,
".",
"_");
454 if(dsType->getPrimaryKey())
458 std::string name = m_ui->m_dataSetLineEdit->text().toStdString() +
"_" + dsType->getPrimaryKey()->getName() +
"_pk";
460 boost::replace_all(name,
".",
"_");
464 std::vector<te::dt::Property*> props = dsType->getPrimaryKey()->getProperties();
466 for(
size_t t = 0; t < props.size(); ++ t)
475 std::map<std::string,std::string> nopt;
477 std::auto_ptr<te::da::DataSet> dataset = layer->getData();
481 transactor->createDataSet(dsTypeResult, nopt);
485 dsAdapter->setSRID(layer->getSRID());
487 if(dataset->moveBeforeFirst())
488 transactor->add(dsTypeResult->getName(), dsAdapter.get(), targetDSPtr->getConnectionInfo());
490 transactor->commit();
492 setCursor(Qt::ArrowCursor);
493 QMessageBox::information(
this, tr(
"Exchanger"), tr(
"Layer exported successfully."));
495 catch(
const std::exception& e)
497 setCursor(Qt::ArrowCursor);
499 transactor->rollBack();
501 QString errMsg(tr(
"Error during exchanger. The reported error is: %1"));
503 errMsg = errMsg.arg(e.what());
505 QMessageBox::information(
this, tr(
"Exchanger"), errMsg);
515 QString value = m_ui->m_dsTypeComboBox->itemData(index).toString();
517 m_outputDataSourceType = value.toStdString();
519 if(m_outputDataSourceType ==
"POSTGIS")
521 m_exchangeToFile =
false;
523 m_ui->m_outputDataSourceComboBox->setEnabled(
true);
524 m_ui->m_dsToolButton->setEnabled(
true);
525 m_ui->m_dataSetLineEdit->clear();
526 m_ui->m_dataSetLineEdit->setEnabled(
true);
527 m_ui->m_dirToolButton->setEnabled(
false);
528 m_ui->m_spatialIndexCheckBox->setEnabled(
true);
529 m_ui->m_spatialIndexCheckBox->setChecked(
true);
533 else if(m_outputDataSourceType ==
"ADO")
535 m_exchangeToFile =
false;
537 m_ui->m_outputDataSourceComboBox->setEnabled(
true);
538 m_ui->m_dsToolButton->setEnabled(
true);
539 m_ui->m_dataSetLineEdit->clear();
540 m_ui->m_dataSetLineEdit->setEnabled(
true);
541 m_ui->m_dirToolButton->setEnabled(
false);
542 m_ui->m_spatialIndexCheckBox->setEnabled(
false);
543 m_ui->m_spatialIndexCheckBox->setChecked(
false);
547 else if(m_outputDataSourceType ==
"OGR")
549 m_exchangeToFile =
true;
551 m_ui->m_outputDataSourceComboBox->clear();
552 m_ui->m_outputDataSourceComboBox->setEnabled(
false);
553 m_ui->m_dsToolButton->setEnabled(
false);
554 m_ui->m_dataSetLineEdit->clear();
555 m_ui->m_dataSetLineEdit->setEnabled(
false);
556 m_ui->m_dirToolButton->setEnabled(
true);
557 m_ui->m_spatialIndexCheckBox->setEnabled(
false);
558 m_ui->m_spatialIndexCheckBox->setChecked(
false);
564 onDataSourceTypeActivated(m_ui->m_dsTypeComboBox->currentIndex());
566 if(m_ui->m_dataSetLineEdit->isEnabled())
567 m_ui->m_dataSetLineEdit->setText(value);
572 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
573 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);
575 if (fileName.isEmpty())
578 m_ui->m_dataSetLineEdit->setText(fileName);
585 if(!m_lastDsType.empty())
586 dExplorer->setDataSourceToUse(m_lastDsType.c_str());
598 res = exchangeToFile();
600 res = exchangeToDatabase();
608 m_ui->m_dsTypeComboBox->clear();
610 m_ui->m_dsTypeComboBox->addItem(QIcon::fromTheme(
"datasource-postgis"), tr(
"PostGIS"), QVariant(
"POSTGIS"));
611 m_ui->m_dsTypeComboBox->addItem(QIcon::fromTheme(
"datasource-ado"), tr(
"Microsoft Access"), QVariant(
"ADO"));
612 m_ui->m_dsTypeComboBox->addItem(QIcon::fromTheme(
"datasource-ogr"), tr(
"File - OGR Formats"), QVariant(
"OGR"));
virtual void setName(const std::string &name)
It sets the constraint name.
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
boost::shared_ptr< DataSource > DataSourcePtr
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.
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.
An converter for DataSetType.
void setName(const std::string &name)
It sets the property name.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
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 setName(const std::string &name)
It sets the index name.
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.