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 void te::qt::widgets::PostGIS2SHPDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
69 {
70  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
71 
72  while(it != layers.end())
73  {
75 
76  std::string dsName = l->getDataSourceId();
77 
79 
80  if(dsPtr->getType() == "POSTGIS") // TENSO
81  m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
82 
83  ++it;
84  }
85 }
86 
88 {
89  QMessageBox::information(this, "Help", "Under development");
90 }
91 
93 {
94  int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
95 
96  if(idxLayer == -1)
97  {
98  QMessageBox::warning(this, tr("Warning"), tr("Input layer not selected."));
99  return;
100  }
101 
102  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
103  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
104 
105  if(!layer.get())
106  {
107  QMessageBox::warning(this, tr("Warning"), tr("Error getting selected layer."));
108  return;
109  }
110 
111  if(m_ui->m_dataSetLineEdit->text().isEmpty())
112  {
113  QMessageBox::warning(this, tr("Warning"), tr("Output File Name not defined."));
114  return;
115  }
116 
117 
118  try
119  {
120  //create adapter
121  std::unique_ptr<te::da::DataSetType> dsType = layer->getSchema();
122 
123  if(dsType->size() == 0)
124  te::da::LoadProperties(dsType.get(), layer->getDataSourceId());
125 
126  //create data source
127  std::unique_ptr<te::da::DataSource> dsOGR = te::da::DataSourceFactory::make("OGR", ("file://" + std::string(m_ui->m_dataSetLineEdit->text().toUtf8().data())));
128  dsOGR->open();
129 
130  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(dsType.get(), dsOGR->getCapabilities());
131 
132  te::da::DataSetType* dsTypeResult = converter->getResult();
133 
134  dsTypeResult->setName(m_ui->m_dataSetLineEdit->text().toUtf8().data());
135 
136  //exchange
137  std::map<std::string,std::string> nopt;
138 
139  std::unique_ptr<te::da::DataSet> dataset = layer->getData();
140 
141  dsOGR->createDataSet(dsTypeResult, nopt);
142 
143  std::unique_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), converter));
144 
145  if(dataset->moveBeforeFirst())
146  dsOGR->add(dsTypeResult->getName(), dsAdapter.get(), nopt);
147 
148  dsOGR->close();
149 
150  QMessageBox::information(this, tr("Exchanger"), tr("Layer exported successfully."));
151  }
152  catch(const std::exception& e)
153  {
154  QString errMsg(tr("Error during exchanger. The reported error is: %1"));
155 
156  errMsg = errMsg.arg(e.what());
157 
158  QMessageBox::information(this, tr("Exchanger"), errMsg);
159  }
160 
161  accept();
162 }
163 
165 {
166  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."),
167  QString(), tr("Shapefile (*.shp *.SHP);;"),nullptr, QFileDialog::DontConfirmOverwrite);
168 
169  if (fileName.isEmpty())
170  return;
171 
172  m_ui->m_dataSetLineEdit->setText(fileName);
173 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::unique_ptr< Ui::PostGis2SHPDialogForm > m_ui
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
URI C++ Library.
Definition: Attributes.h:37
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) Q_DECLARE_METATYPE(te
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
A exchanger dialog from PostGis to SHP operation.