27 #include "../../../dataaccess/dataset/DataSetAdapter.h"
28 #include "../../../dataaccess/dataset/PrimaryKey.h"
29 #include "../../../dataaccess/dataset/DataSetTypeConverter.h"
30 #include "../../../dataaccess/datasource/DataSourceFactory.h"
31 #include "../../../dataaccess/datasource/DataSourceInfo.h"
32 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
33 #include "../../../dataaccess/datasource/DataSourceManager.h"
34 #include "../../../dataaccess/utils/Utils.h"
35 #include "../../../geometry/GeometryProperty.h"
36 #include "../../../maptools/DataSetLayer.h"
37 #include "../../widgets/datasource/selector/DataSourceExplorerDialog.h"
38 #include "../../widgets/srs/SRSManagerDialog.h"
40 #include "ui_DirectExchangerDialogForm.h"
43 #include <QFileDialog>
44 #include <QMessageBox>
47 #include <boost/algorithm/string/replace.hpp>
48 #include <boost/filesystem.hpp>
49 #include <boost/lexical_cast.hpp>
50 #include <boost/uuid/random_generator.hpp>
51 #include <boost/uuid/uuid_io.hpp>
59 m_ui(new Ui::DirectExchangerDialogForm)
65 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"data-exchange-direct-hint").pixmap(112,48));
66 m_ui->m_dsToolButton->setIcon(QIcon::fromTheme(
"datasource"));
75 m_ui->m_helpPushButton->setPageReference(
"widgets/exchanger_direct/exchanger_direct.html");
93 m_ui->m_inputLayerComboBox->clear();
95 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
97 while(it != m_layers.end())
101 std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
103 if(dsType.get() && !dsType->hasRaster())
104 m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
109 if(m_ui->m_inputLayerComboBox->count() > 0)
111 QString s = m_ui->m_inputLayerComboBox->currentText();
113 onInputLayerActivated(s);
116 if(m_ui->m_inputLayerComboBox->count() > 1)
117 m_ui->m_inputLayerComboBox->setEnabled(
true);
122 m_lastDsType = dataSource;
127 m_ui->m_outputDataSourceComboBox->clear();
129 std::vector<te::da::DataSourceInfoPtr> datasources;
133 for(std::size_t i = 0; i < datasources.size(); ++i)
137 if(datasource.get() == 0)
140 const std::string& title = datasource->getTitle();
142 m_ui->m_outputDataSourceComboBox->addItem(title.c_str(), QVariant::fromValue(datasource));
148 int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
152 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Input layer not selected."));
156 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
161 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
165 if(m_ui->m_dataSetLineEdit->text().isEmpty())
167 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output File Name not defined."));
174 std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
177 std::map<std::string, std::string> connInfo;
178 connInfo[
"URI"] = m_ui->m_dataSetLineEdit->text().toStdString();
181 dsOGR->setConnectionInfo(connInfo);
191 if(p && (p->
getSRID() != layer->getSRID()))
196 boost::filesystem::path uri(m_ui->m_dataSetLineEdit->text().toStdString());
198 std::string val = uri.stem().string();
200 dsTypeResult->setName(val);
203 std::map<std::string,std::string> nopt;
205 std::auto_ptr<te::da::DataSet> dataset = layer->getData();
207 dsOGR->createDataSet(dsTypeResult, nopt);
211 dsAdapter->setSRID(layer->getSRID());
213 if(dataset->moveBeforeFirst())
214 dsOGR->add(dsTypeResult->getName(), dsAdapter.get(), dsOGR->getConnectionInfo());
218 QMessageBox::information(
this, tr(
"Exchanger"), tr(
"Layer exported successfully."));
220 catch(
const std::exception& e)
222 QString errMsg(tr(
"Error during exchanger. The reported error is: %1"));
224 errMsg = errMsg.arg(e.what());
226 QMessageBox::information(
this, tr(
"Exchanger"), errMsg);
236 int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
240 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Input layer not selected."));
244 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
249 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected layer."));
253 int idxDataSource = m_ui->m_outputDataSourceComboBox->currentIndex();
257 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Output data source not selected."));
261 QVariant varDataSource = m_ui->m_outputDataSourceComboBox->itemData(idxDataSource, Qt::UserRole);
266 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error getting selected data source."));
270 if(m_ui->m_dataSetLineEdit->text().isEmpty())
272 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Data Set name not defined."));
279 std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
287 dsTypeResult->
setName(m_ui->m_dataSetLineEdit->text().toStdString());
290 if(m_ui->m_spatialIndexCheckBox->isChecked())
295 if(p && (p->
getSRID() != layer->getSRID()))
304 std::string name = m_ui->m_dataSetLineEdit->text().toStdString() +
"_" + p->
getName() +
"_idx";
306 boost::replace_all(name,
".",
"_");
318 if(dsType->getPrimaryKey())
322 std::string name = m_ui->m_dataSetLineEdit->text().toStdString() +
"_" + dsType->getPrimaryKey()->getName() +
"_pk";
324 boost::replace_all(name,
".",
"_");
328 std::vector<te::dt::Property*> props = dsType->getPrimaryKey()->getProperties();
330 for(
size_t t = 0; t < props.size(); ++ t)
339 std::map<std::string,std::string> nopt;
341 std::auto_ptr<te::da::DataSet> dataset = layer->getData();
343 targetDSPtr->createDataSet(dsTypeResult, nopt);
347 dsAdapter->setSRID(layer->getSRID());
349 if(dataset->moveBeforeFirst())
350 targetDSPtr->add(dsTypeResult->getName(), dsAdapter.get(), targetDSPtr->getConnectionInfo());
352 QMessageBox::information(
this, tr(
"Exchanger"), tr(
"Layer exported successfully."));
354 catch(
const std::exception& e)
356 QString errMsg(tr(
"Error during exchanger. The reported error is: %1"));
358 errMsg = errMsg.arg(e.what());
360 QMessageBox::information(
this, tr(
"Exchanger"), errMsg);
370 QString value = m_ui->m_dsTypeComboBox->itemData(index).toString();
372 m_outputDataSourceType = value.toStdString();
374 if(m_outputDataSourceType ==
"POSTGIS")
376 m_exchangeToFile =
false;
378 m_ui->m_outputDataSourceComboBox->setEnabled(
true);
379 m_ui->m_dsToolButton->setEnabled(
true);
380 m_ui->m_dataSetLineEdit->clear();
381 m_ui->m_dataSetLineEdit->setEnabled(
true);
382 m_ui->m_dirToolButton->setEnabled(
false);
383 m_ui->m_spatialIndexCheckBox->setEnabled(
true);
384 m_ui->m_spatialIndexCheckBox->setChecked(
true);
388 else if(m_outputDataSourceType ==
"ADO")
390 m_exchangeToFile =
false;
392 m_ui->m_outputDataSourceComboBox->setEnabled(
true);
393 m_ui->m_dsToolButton->setEnabled(
true);
394 m_ui->m_dataSetLineEdit->clear();
395 m_ui->m_dataSetLineEdit->setEnabled(
true);
396 m_ui->m_dirToolButton->setEnabled(
false);
397 m_ui->m_spatialIndexCheckBox->setEnabled(
false);
398 m_ui->m_spatialIndexCheckBox->setChecked(
false);
402 else if(m_outputDataSourceType ==
"OGR")
404 m_exchangeToFile =
true;
406 m_ui->m_outputDataSourceComboBox->clear();
407 m_ui->m_outputDataSourceComboBox->setEnabled(
false);
408 m_ui->m_dsToolButton->setEnabled(
false);
409 m_ui->m_dataSetLineEdit->clear();
410 m_ui->m_dataSetLineEdit->setEnabled(
false);
411 m_ui->m_dirToolButton->setEnabled(
true);
412 m_ui->m_spatialIndexCheckBox->setEnabled(
false);
413 m_ui->m_spatialIndexCheckBox->setChecked(
false);
419 onDataSourceTypeActivated(m_ui->m_dsTypeComboBox->currentIndex());
421 if(m_ui->m_dataSetLineEdit->isEnabled())
422 m_ui->m_dataSetLineEdit->setText(value);
427 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
428 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);
430 if (fileName.isEmpty())
433 m_ui->m_dataSetLineEdit->setText(fileName);
440 if(!m_lastDsType.empty())
441 dExplorer->setDataSourceToUse(m_lastDsType.c_str());
453 res = exchangeToFile();
455 res = exchangeToDatabase();
463 m_ui->m_dsTypeComboBox->clear();
465 m_ui->m_dsTypeComboBox->addItem(QIcon::fromTheme(
"datasource-postgis"), tr(
"PostGIS"), QVariant(
"POSTGIS"));
466 m_ui->m_dsTypeComboBox->addItem(QIcon::fromTheme(
"datasource-ado"), tr(
"Microsoft Access"), QVariant(
"ADO"));
467 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.
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.
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.