All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PostGIS2SHPDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 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/PostGIS2SHPDialog.h
22 
23  \brief A exchanger dialog from PostGis to SHP operation
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/DataSetAdapter.h"
28 #include "../../../dataaccess/dataset/DataSetTypeConverter.h"
29 #include "../../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../../dataaccess/datasource/DataSourceInfo.h"
31 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
32 #include "../../../dataaccess/datasource/DataSourceManager.h"
33 #include "../../../dataaccess/utils/Utils.h"
34 #include "../../../geometry/GeometryProperty.h"
35 #include "../../../maptools/DataSetLayer.h"
36 #include "PostGIS2SHPDialog.h"
37 #include "ui_PostGIS2SHPDialogForm.h"
38 
39 // Qt
40 #include <QFileDialog>
41 #include <QMessageBox>
42 
43 // Boost
44 #include <boost/uuid/random_generator.hpp>
45 #include <boost/uuid/uuid_io.hpp>
46 
49 
50 te::qt::widgets::PostGIS2SHPDialog::PostGIS2SHPDialog(QWidget* parent, Qt::WindowFlags f)
51  : QDialog(parent, f),
52  m_ui(new Ui::PostGis2SHPDialogForm)
53 {
54 // setup widget
55  m_ui->setupUi(this);
56 
57 // add icons
58  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("data-exchange-pgis-shp-hint").pixmap(112,48));
59 
60 //connectors
61  connect(m_ui->m_helpPushButton, SIGNAL(clicked()), this, SLOT(onHelpPushButtonClicked()));
62  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
63  connect(m_ui->m_dirToolButton, SIGNAL(clicked()), this, SLOT(onDirToolButtonClicked()));
64 }
65 
67 {
68 }
69 
70 void te::qt::widgets::PostGIS2SHPDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
71 {
72  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
73 
74  while(it != layers.end())
75  {
77 
78  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
79 
80  if(!dsLayer)
81  continue;
82 
83  std::string dsName = dsLayer->getDataSourceId();
84 
86 
87  if(dsPtr->getType() == "POSTGIS") // TENSO
88  m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
89 
90  ++it;
91  }
92 }
93 
95 {
96  QMessageBox::information(this, "Help", "Under development");
97 }
98 
100 {
101  int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
102 
103  if(idxLayer == -1)
104  {
105  QMessageBox::warning(this, tr("Warning"), tr("Input layer not selected."));
106  return;
107  }
108 
109  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
110  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
111 
112  if(!layer.get())
113  {
114  QMessageBox::warning(this, tr("Warning"), tr("Error getting selected layer."));
115  return;
116  }
117 
118  if(m_ui->m_dataSetLineEdit->text().isEmpty())
119  {
120  QMessageBox::warning(this, tr("Warning"), tr("Output File Name not defined."));
121  return;
122  }
123 
124 
125  try
126  {
127  //create adapter
128  std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
129 
130  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(layer.get());
131 
132  if(dsType->size() == 0)
133  te::da::LoadProperties(dsType.get(), dsLayer->getDataSourceId());
134 
135 
136  //create data source
137  std::map<std::string, std::string> connInfo;
138  connInfo["URI"] = m_ui->m_dataSetLineEdit->text().toStdString();
139  connInfo["DRIVER"] = "ESRI Shapefile"; // MUITO TENSO
140 
141  std::auto_ptr<te::da::DataSource> dsOGR = te::da::DataSourceFactory::make("OGR"); // TENSO
142  dsOGR->setConnectionInfo(connInfo);
143  dsOGR->open();
144 
145  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(dsType.get(), dsOGR->getCapabilities());
146 
147  te::da::DataSetType* dsTypeResult = converter->getResult();
148 
149  dsTypeResult->setName(m_ui->m_dataSetLineEdit->text().toStdString());
150 
151  //exchange
152  std::map<std::string,std::string> nopt;
153 
154  std::auto_ptr<te::da::DataSet> dataset = layer->getData();
155 
156  dsOGR->createDataSet(dsTypeResult, nopt);
157 
158  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), converter));
159 
160  if(dataset->moveBeforeFirst())
161  dsOGR->add(dsTypeResult->getName(), dsAdapter.get(), dsOGR->getConnectionInfo());
162 
163  dsOGR->close();
164 
165  QMessageBox::information(this, tr("Exchanger"), tr("Layer exported successfully."));
166  }
167  catch(const std::exception& e)
168  {
169  QString errMsg(tr("Error during exchanger. The reported error is: %1"));
170 
171  errMsg = errMsg.arg(e.what());
172 
173  QMessageBox::information(this, tr("Exchanger"), errMsg);
174  }
175 
176  accept();
177 }
178 
180 {
181  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."),
182  QString(), tr("Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
183 
184  if (fileName.isEmpty())
185  return;
186 
187  m_ui->m_dataSetLineEdit->setText(fileName);
188 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
PostGIS2SHPDialog(QWidget *parent=0, Qt::WindowFlags f=0)
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:120
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A class that models the description of a dataset.
Definition: DataSetType.h:72
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
An converter for DataSetType.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
const std::string & getDataSourceId() const
static std::auto_ptr< DataSource > make(const std::string &dsType)
std::auto_ptr< Ui::PostGis2SHPDialogForm > m_ui
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
A exchanger dialog from PostGis to SHP operation.