SHP2PostGISDialog.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/SHP2PostGISDialog.h
22 
23  \brief A exchanger dialog from SHP to PostGis operation
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/DataSetAdapter.h"
28 #include "../../../dataaccess/dataset/DataSetTypeConverter.h"
29 #include "../../../dataaccess/datasource/DataSourceInfo.h"
30 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
31 #include "../../../dataaccess/datasource/DataSourceManager.h"
32 #include "../../../dataaccess/utils/Utils.h"
33 #include "../../../geometry/GeometryProperty.h"
34 #include "../../../maptools/DataSetLayer.h"
35 #include "SHP2PostGISDialog.h"
36 #include "ui_SHP2PostGISDialogForm.h"
37 
38 // Qt
39 #include <QMessageBox>
40 
43 
44 te::qt::widgets::SHP2PostGISDialog::SHP2PostGISDialog(QWidget* parent, Qt::WindowFlags f)
45  : QDialog(parent, f),
46  m_ui(new Ui::SHP2PostGisDialogForm)
47 {
48 // setup widget
49  m_ui->setupUi(this);
50 
51 // add icons
52  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("data-exchange-shp-pgis-hint").pixmap(112,48));
53 
54 //connectors
55  connect(m_ui->m_helpPushButton, SIGNAL(clicked()), this, SLOT(onHelpPushButtonClicked()));
56  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
57 
58 //set list of data sources
59  setDataSources();
60 }
61 
63 
64 void te::qt::widgets::SHP2PostGISDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
65 {
66  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
67 
68  while(it != layers.end())
69  {
71 
72  std::string dsName = l->getDataSourceId();
73 
75 
76  if(dsPtr->getType() == "OGR") // TENSO
77  m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
78 
79  ++it;
80  }
81 
82  if(m_ui->m_inputLayerComboBox->count() > 0)
83  {
84  QString s = m_ui->m_inputLayerComboBox->currentText();
85 
86  m_ui->m_dataSetLineEdit->setText(s);
87  }
88 }
89 
91 {
92  m_ui->m_outputDataSourceComboBox->clear();
93 
94  std::vector<te::da::DataSourceInfoPtr> datasources;
95 
96  te::da::DataSourceInfoManager::getInstance().getByType("POSTGIS", datasources); // TENSO
97 
98  for(std::size_t i = 0; i < datasources.size(); ++i)
99  {
100  const te::da::DataSourceInfoPtr& datasource = datasources[i];
101 
102  if(datasource.get() == nullptr)
103  continue;
104 
105  const std::string& title = datasource->getTitle();
106 
107  m_ui->m_outputDataSourceComboBox->addItem(title.c_str(), QVariant::fromValue(datasource));
108  }
109 }
110 
112 {
113  QMessageBox::information(this, "Help", "Under development");
114 }
115 
117 {
118  int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
119 
120  if(idxLayer == -1)
121  {
122  QMessageBox::warning(this, tr("Warning"), tr("Input layer not selected."));
123  return;
124  }
125 
126  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
127  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
128 
129  if(!layer.get())
130  {
131  QMessageBox::warning(this, tr("Warning"), tr("Error getting selected layer."));
132  return;
133  }
134 
135  int idxDataSource = m_ui->m_outputDataSourceComboBox->currentIndex();
136 
137  if(idxLayer == -1)
138  {
139  QMessageBox::warning(this, tr("Warning"), tr("Output data source not selected."));
140  return;
141  }
142 
143  QVariant varDataSource = m_ui->m_outputDataSourceComboBox->itemData(idxDataSource, Qt::UserRole);
144  te::da::DataSourceInfoPtr dsInfo = varDataSource.value<te::da::DataSourceInfoPtr>();
145 
146  if(!dsInfo.get())
147  {
148  QMessageBox::warning(this, tr("Warning"), tr("Error getting selected data source."));
149  return;
150  }
151 
152  if(m_ui->m_dataSetLineEdit->text().isEmpty())
153  {
154  QMessageBox::warning(this, tr("Warning"), tr("Data Set name not defined."));
155  return;
156  }
157 
158  try
159  {
160  //create adapter
161  std::unique_ptr<te::da::DataSetType> dsType = layer->getSchema();
162 
163  if(dsType->size() == 0)
164  te::da::LoadProperties(dsType.get(), layer->getDataSourceId());
165 
166  te::da::DataSourcePtr targetDSPtr = te::da::DataSourceManager::getInstance().get(dsInfo->getId(), dsInfo->getType(), dsInfo->getConnInfo());
167 
168  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(dsType.get(), targetDSPtr->getCapabilities());
169 
170  te::da::DataSetType* dsTypeResult = converter->getResult();
171 
172  dsTypeResult->setName(m_ui->m_dataSetLineEdit->text().toUtf8().data());
173 
174  //create index
175  if(m_ui->m_spatialIndexCheckBox->isChecked())
176  {
178 
179  if(p)
180  {
181  te::da::Index* idx = new te::da::Index(dsTypeResult);
182 
183  std::string name = p->getName() + "_idx";
184  idx->setName(name);
186 
187  te::dt::Property* pClone = p->clone();
188 
189  idx->add(pClone);
190  }
191  }
192 
193  //exchange
194  std::map<std::string,std::string> nopt;
195 
196  std::unique_ptr<te::da::DataSet> dataset = layer->getData();
197 
198  targetDSPtr->createDataSet(dsTypeResult, nopt);
199 
200  std::unique_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), converter));
201 
202  if(dataset->moveBeforeFirst())
203  targetDSPtr->add(dsTypeResult->getName(), dsAdapter.get(), nopt);
204 
205  QMessageBox::information(this, tr("Exchanger"), tr("Layer exported successfully."));
206  }
207  catch(const std::exception& e)
208  {
209  QString errMsg(tr("Error during exchanger. The reported error is: %1"));
210 
211  errMsg = errMsg.arg(e.what());
212 
213  QMessageBox::information(this, tr("Exchanger"), errMsg);
214  }
215 
216  accept();
217 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Geometric property.
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
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.
Definition: Property.h:59
An converter for DataSetType.
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
A exchanger dialog from SHP to PostGis operation.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
URI C++ Library.
Definition: Attributes.h:37
te::gm::Polygon * p
void setDataSources()
Set the list of data sources that can be used.
void setName(const std::string &name)
It sets the index name.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) Q_DECLARE_METATYPE(te
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
std::unique_ptr< Ui::SHP2PostGisDialogForm > m_ui
const std::string & getName() const
It returns the property name.
Definition: Property.h:127