All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DirectExchangerDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/exchanger/DirectExchangerDialog.h
22 
23  \brief A direct exchanger dialog for ADO, POSTGIS and SHP data sources
24 */
25 
26 // TerraLib
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"
39 #include "DirectExchangerDialog.h"
40 #include "ui_DirectExchangerDialogForm.h"
41 
42 // Qt
43 #include <QtGui/QFileDialog>
44 #include <QtGui/QMessageBox>
45 
46 // Boost
47 #include <boost/filesystem.hpp>
48 #include <boost/lexical_cast.hpp>
49 #include <boost/uuid/random_generator.hpp>
50 #include <boost/uuid/uuid_io.hpp>
51 
52 
55 
57  : QDialog(parent, f),
58  m_ui(new Ui::DirectExchangerDialogForm)
59 {
60 // setup widget
61  m_ui->setupUi(this);
62 
63 // add icons
64  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("data-exchange-direct-hint").pixmap(112,48));
65  m_ui->m_dsToolButton->setIcon(QIcon::fromTheme("datasource"));
66 
67 //connectors
68  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
69  connect(m_ui->m_dirToolButton, SIGNAL(clicked()), this, SLOT(onDirToolButtonClicked()));
70  connect(m_ui->m_dsToolButton, SIGNAL(clicked()), this, SLOT(onDataSoruceToolButtonClicked()));
71  connect(m_ui->m_dsTypeComboBox, SIGNAL(activated(int)), this, SLOT(onDataSourceTypeActivated(int)));
72  connect(m_ui->m_inputLayerComboBox, SIGNAL(activated(QString)), this, SLOT(onInputLayerActivated(QString)));
73 
74  m_ui->m_helpPushButton->setPageReference("widgets/exchanger_direct/exchanger_direct.html");
75 
76  //starup interface
78  m_exchangeToFile = false;
79 
81 }
82 
84 {
85 }
86 
87 void te::qt::widgets::DirectExchangerDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
88 {
89  m_layers = layers;
90 
91  m_ui->m_inputLayerComboBox->clear();
92 
93  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
94 
95  while(it != m_layers.end())
96  {
98 
99  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
100 
101  if(dsType.get() && dsType->hasGeom())
102  m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
103 
104  ++it;
105  }
106 
107  if(m_ui->m_inputLayerComboBox->count() > 0)
108  {
109  QString s = m_ui->m_inputLayerComboBox->currentText();
110 
111  onInputLayerActivated(s);
112  }
113 
114  if(m_ui->m_inputLayerComboBox->count() > 1)
115  m_ui->m_inputLayerComboBox->setEnabled(true);
116 }
117 
119 {
120  m_ui->m_outputDataSourceComboBox->clear();
121 
122  std::vector<te::da::DataSourceInfoPtr> datasources;
123 
124  te::da::DataSourceInfoManager::getInstance().getByType(m_outputDataSourceType, datasources);
125 
126  for(std::size_t i = 0; i < datasources.size(); ++i)
127  {
128  const te::da::DataSourceInfoPtr& datasource = datasources[i];
129 
130  if(datasource.get() == 0)
131  continue;
132 
133  const std::string& title = datasource->getTitle();
134 
135  m_ui->m_outputDataSourceComboBox->addItem(title.c_str(), QVariant::fromValue(datasource));
136  }
137 }
138 
140 {
141  int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
142 
143  if(idxLayer == -1)
144  {
145  QMessageBox::warning(this, tr("Warning"), tr("Input layer not selected."));
146  return false;
147  }
148 
149  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
150  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
151 
152  if(!layer.get())
153  {
154  QMessageBox::warning(this, tr("Warning"), tr("Error getting selected layer."));
155  return false;
156  }
157 
158  if(m_ui->m_dataSetLineEdit->text().isEmpty())
159  {
160  QMessageBox::warning(this, tr("Warning"), tr("Output File Name not defined."));
161  return false;
162  }
163 
164  try
165  {
166  //create adapter
167  std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
168 
169  //create data source
170  std::map<std::string, std::string> connInfo;
171  connInfo["URI"] = m_ui->m_dataSetLineEdit->text().toStdString();
172 
173  std::auto_ptr<te::da::DataSource> dsOGR = te::da::DataSourceFactory::make(m_outputDataSourceType);
174  dsOGR->setConnectionInfo(connInfo);
175  dsOGR->open();
176 
177  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(dsType.get(), dsOGR->getCapabilities());
178 
179  te::da::DataSetType* dsTypeResult = converter->getResult();
180 
181  boost::filesystem::path uri(m_ui->m_dataSetLineEdit->text().toStdString());
182 
183  std::string val = uri.stem().string();
184 
185  dsTypeResult->setName(val);
186 
187  //exchange
188  std::map<std::string,std::string> nopt;
189 
190  std::auto_ptr<te::da::DataSet> dataset = layer->getData();
191 
192  dsOGR->createDataSet(dsTypeResult, nopt);
193 
194  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), converter));
195 
196  dsAdapter->setSRID(layer->getSRID());
197 
198  if(dataset->moveBeforeFirst())
199  dsOGR->add(dsTypeResult->getName(), dsAdapter.get(), dsOGR->getConnectionInfo());
200 
201  dsOGR->close();
202 
203  QMessageBox::information(this, tr("Exchanger"), tr("Layer exported successfully."));
204  }
205  catch(const std::exception& e)
206  {
207  QString errMsg(tr("Error during exchanger. The reported error is: %1"));
208 
209  errMsg = errMsg.arg(e.what());
210 
211  QMessageBox::information(this, tr("Exchanger"), errMsg);
212 
213  return false;
214  }
215 
216  return true;
217 }
218 
220 {
221  int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
222 
223  if(idxLayer == -1)
224  {
225  QMessageBox::warning(this, tr("Warning"), tr("Input layer not selected."));
226  return false;
227  }
228 
229  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
230  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
231 
232  if(!layer.get())
233  {
234  QMessageBox::warning(this, tr("Warning"), tr("Error getting selected layer."));
235  return false;
236  }
237 
238  int idxDataSource = m_ui->m_outputDataSourceComboBox->currentIndex();
239 
240  if(idxLayer == -1)
241  {
242  QMessageBox::warning(this, tr("Warning"), tr("Output data source not selected."));
243  return false;
244  }
245 
246  QVariant varDataSource = m_ui->m_outputDataSourceComboBox->itemData(idxDataSource, Qt::UserRole);
247  te::da::DataSourceInfoPtr dsInfo = varDataSource.value<te::da::DataSourceInfoPtr>();
248 
249  if(!dsInfo.get())
250  {
251  QMessageBox::warning(this, tr("Warning"), tr("Error getting selected data source."));
252  return false;
253  }
254 
255  if(m_ui->m_dataSetLineEdit->text().isEmpty())
256  {
257  QMessageBox::warning(this, tr("Warning"), tr("Data Set name not defined."));
258  return false;
259  }
260 
261  try
262  {
263  //create adapter
264  std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
265 
266  te::da::DataSourcePtr targetDSPtr = te::da::DataSourceManager::getInstance().get(dsInfo->getId(), dsInfo->getType(), dsInfo->getConnInfo());
267 
268  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(dsType.get(), targetDSPtr->getCapabilities());
269 
270  te::da::DataSetType* dsTypeResult = converter->getResult();
271 
272  dsTypeResult->setName(m_ui->m_dataSetLineEdit->text().toStdString());
273 
274  //create index
275  if(m_ui->m_spatialIndexCheckBox->isChecked())
276  {
278 
279  if(p)
280  {
281  te::da::Index* idx = new te::da::Index(dsTypeResult);
282 
283  std::string name = m_ui->m_dataSetLineEdit->text().toStdString() + "_" + p->getName() + "_idx";
284  idx->setName(name);
286 
287  te::dt::Property* pClone = p->clone();
288 
289  idx->add(pClone);
290  }
291  }
292 
293  //create primary key
294  if(dsType->getPrimaryKey())
295  {
296  te::da::PrimaryKey* pk = new te::da::PrimaryKey(dsTypeResult);
297 
298  std::string name = m_ui->m_dataSetLineEdit->text().toStdString() + "_" + dsType->getPrimaryKey()->getName() + "_pk";
299 
300  pk->setName(name);
301 
302  std::vector<te::dt::Property*> props = dsType->getPrimaryKey()->getProperties();
303 
304  for(size_t t = 0; t < props.size(); ++ t)
305  {
306  te::dt::Property* p = props[t]->clone();
307 
308  pk->add(p);
309  }
310  }
311 
312  //exchange
313  std::map<std::string,std::string> nopt;
314 
315  std::auto_ptr<te::da::DataSet> dataset = layer->getData();
316 
317  targetDSPtr->createDataSet(dsTypeResult, nopt);
318 
319  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), converter));
320 
321  dsAdapter->setSRID(layer->getSRID());
322 
323  if(dataset->moveBeforeFirst())
324  targetDSPtr->add(dsTypeResult->getName(), dsAdapter.get(), targetDSPtr->getConnectionInfo());
325 
326  QMessageBox::information(this, tr("Exchanger"), tr("Layer exported successfully."));
327  }
328  catch(const std::exception& e)
329  {
330  QString errMsg(tr("Error during exchanger. The reported error is: %1"));
331 
332  errMsg = errMsg.arg(e.what());
333 
334  QMessageBox::information(this, tr("Exchanger"), errMsg);
335 
336  return false;
337  }
338 
339  return true;
340 }
341 
343 {
344  QString value = m_ui->m_dsTypeComboBox->itemData(index).toString();
345 
346  m_outputDataSourceType = value.toStdString();
347 
348  if(m_outputDataSourceType == "POSTGIS")
349  {
350  m_exchangeToFile = false;
351 
352  m_ui->m_outputDataSourceComboBox->setEnabled(true);
353  m_ui->m_dsToolButton->setEnabled(true);
354  m_ui->m_dataSetLineEdit->clear();
355  m_ui->m_dataSetLineEdit->setEnabled(true);
356  m_ui->m_dirToolButton->setEnabled(false);
357  m_ui->m_spatialIndexCheckBox->setEnabled(true);
358  m_ui->m_spatialIndexCheckBox->setChecked(true);
359 
360  setDataSources();
361  }
362  else if(m_outputDataSourceType == "ADO")
363  {
364  m_exchangeToFile = false;
365 
366  m_ui->m_outputDataSourceComboBox->setEnabled(true);
367  m_ui->m_dsToolButton->setEnabled(true);
368  m_ui->m_dataSetLineEdit->clear();
369  m_ui->m_dataSetLineEdit->setEnabled(true);
370  m_ui->m_dirToolButton->setEnabled(false);
371  m_ui->m_spatialIndexCheckBox->setEnabled(false);
372  m_ui->m_spatialIndexCheckBox->setChecked(false);
373 
374  setDataSources();
375  }
376  else if(m_outputDataSourceType == "OGR")
377  {
378  m_exchangeToFile = true;
379 
380  m_ui->m_outputDataSourceComboBox->clear();
381  m_ui->m_outputDataSourceComboBox->setEnabled(false);
382  m_ui->m_dsToolButton->setEnabled(false);
383  m_ui->m_dataSetLineEdit->clear();
384  m_ui->m_dataSetLineEdit->setEnabled(false);
385  m_ui->m_dirToolButton->setEnabled(true);
386  m_ui->m_spatialIndexCheckBox->setEnabled(false);
387  m_ui->m_spatialIndexCheckBox->setChecked(false);
388  }
389 }
390 
392 {
393  onDataSourceTypeActivated(m_ui->m_dsTypeComboBox->currentIndex());
394 
395  if(m_ui->m_dataSetLineEdit->isEnabled())
396  m_ui->m_dataSetLineEdit->setText(value);
397 }
398 
400 {
401  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."),
402  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);
403 
404  if (fileName.isEmpty())
405  return;
406 
407  m_ui->m_dataSetLineEdit->setText(fileName);
408 }
409 
411 {
412  std::auto_ptr<te::qt::widgets::DataSourceExplorerDialog> dExplorer(new te::qt::widgets::DataSourceExplorerDialog(this));
413 
414  dExplorer->exec();
415 
416  setDataSources();
417 }
418 
420 {
421  bool res = false;
422 
423  if(m_exchangeToFile)
424  res = exchangeToFile();
425  else
426  res = exchangeToDatabase();
427 
428  if(res)
429  accept();
430 }
431 
433 {
434  m_ui->m_dsTypeComboBox->clear();
435 
436  m_ui->m_dsTypeComboBox->addItem(QIcon::fromTheme("datasource-postgis"), tr("PostGIS"), QVariant("POSTGIS"));
437  m_ui->m_dsTypeComboBox->addItem(QIcon::fromTheme("datasource-ado"), tr("Microsoft Access"), QVariant("ADO"));
438  m_ui->m_dsTypeComboBox->addItem(QIcon::fromTheme("datasource-ogr"), tr("File - OGR Formats"), QVariant("OGR"));
439 }
It describes an index associated to a DataSetType.
Definition: Index.h:54
An converter for DataSetType.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:504
void setName(const std::string &name)
It sets the index name.
Definition: Index.h:162
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
const std::string & getName() const
It returns the property name.
Definition: Property.h:126
static std::auto_ptr< DataSource > make(const std::string &dsType)
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:136
virtual void setName(const std::string &name)
It sets the constraint name.
Definition: Constraint.h:126
A dialog for selecting a data source.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
A direct exchanger dialog for ADO, POSTGIS and SHP data sources.
std::auto_ptr< Ui::DirectExchangerDialogForm > m_ui
void add(te::dt::Property *p)
It adds the property to the list of properties of the index.
Definition: Index.h:197
void setDataSources()
Set the list of data sources that can be used.
A class that models the description of a dataset.
Definition: DataSetType.h:72
void setIndexType(IndexType t)
It sets the index type.
Definition: Index.h:176
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
It models a property definition.
Definition: Property.h:59
te::dt::Property * clone() const
It returns a clone of the object.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
DirectExchangerDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Geometric property.
virtual Property * clone() const =0
It returns a clone of the object.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:591