All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OGRConnectorDialog.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/plugins/datasource/ogr/OGRConnectorDialog.cpp
22 
23  \brief A dialog window for showing the OGR connector widget.
24 */
25 
26 // TerraLib
27 #include "../../../../common/Translator.h"
28 #include "../../../../dataaccess/datasource/DataSource.h"
29 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
31 #include "../../../../dataaccess/datasource/DataSourceManager.h"
32 #include "../../../af/Utils.h"
33 #include "../../../widgets/Exception.h"
34 #include "../../../widgets/Utils.h"
35 #include "OGRConnectorDialog.h"
36 #include "ui_OGRConnectorDialogForm.h"
37 #include "Utils.h"
38 
39 // Boost
40 #include <boost/uuid/random_generator.hpp>
41 #include <boost/uuid/uuid_io.hpp>
42 #include <boost/filesystem.hpp>
43 #include <boost/lexical_cast.hpp>
44 
45 // Qt
46 #include <QFileInfo>
47 #include <QFileDialog>
48 #include <QMessageBox>
49 
50 // STL
51 #include <cassert>
52 
54  : QDialog(parent, f),
55  m_ui(new Ui::OGRConnectorDialogForm)
56 {
57 // add controls
58  m_ui->setupUi(this);
59 
60 // connect signal and slots
61  connect(m_ui->m_openPushButton, SIGNAL(pressed()), this, SLOT(openPushButtonPressed()));
62  connect(m_ui->m_testPushButton, SIGNAL(pressed()), this, SLOT(testPushButtonPressed()));
63  connect(m_ui->m_searchFeatureToolButton, SIGNAL(pressed()), this, SLOT(searchFeatureToolButtonPressed()));
64 
65  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
66  m_ui->m_helpPushButton->setPageReference("plugins/ogr/ogr.html");
67 }
68 
70 {
71 }
72 
74 {
75  return m_datasource;
76 }
77 
79 {
80  return m_driver;
81 }
82 
84 {
85  m_datasource = ds;
86 
87  if(m_datasource.get() != 0)
88  {
89  setConnectionInfo(m_datasource->getConnInfo());
90 
91  m_ui->m_datasourceTitleLineEdit->setText(QString::fromStdString(m_datasource->getTitle()));
92 
93  m_ui->m_datasourceDescriptionTextEdit->setText(QString::fromStdString(m_datasource->getDescription()));
94  }
95 }
96 
98 {
99  try
100  {
101 // check if driver is loaded
102  if(te::da::DataSourceFactory::find("OGR") == 0)
103  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for OGR data sources!"));
104 
105 // get data source connection info based on form data
106  std::map<std::string, std::string> dsInfo;
107 
108  getConnectionInfo(dsInfo);
109 
110 // perform connection
111  //m_driver.reset(te::da::DataSourceFactory::open("OGR", dsInfo));
112  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR");
113  ds->setConnectionInfo(dsInfo);
114  ds->open();
115  m_driver.reset(ds.release());
116 
117  if(m_driver.get() == 0)
118  throw te::qt::widgets::Exception(TE_TR("Could not open dataset via OGR due to an unknown error!"));
119 
120  QString title = m_ui->m_datasourceTitleLineEdit->text().trimmed();
121 
122  if(title.isEmpty())
123  title = m_ui->m_featureRepoLineEdit->text();
124 
125  if(m_datasource.get() == 0)
126  {
127 // create a new data source based on form data
128  m_datasource.reset(new te::da::DataSourceInfo);
129 
130  m_datasource->setConnInfo(dsInfo);
131 
132  boost::uuids::basic_random_generator<boost::mt19937> gen;
133  boost::uuids::uuid u = gen();
134  std::string dsId = boost::uuids::to_string(u);
135 
136  m_datasource->setId(dsId);
137  m_driver->setId(dsId);
138  m_datasource->setTitle(title.toStdString());
139  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().toStdString());
140  m_datasource->setAccessDriver("OGR");
141  m_datasource->setType("OGR");
142  }
143  else
144  {
145  m_driver->setId(m_datasource->getId());
146  m_datasource->setConnInfo(dsInfo);
147  m_datasource->setTitle(title.toLatin1().data());
148  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().trimmed().toLatin1().data());
149  }
150 
151  if(m_ui->m_fileRadioButton->isChecked())
152  {
153  QString path = m_ui->m_featureRepoLineEdit->text().trimmed();
154  if(IsShapeFile(path) && !HasShapeFileSpatialIndex(path))
155  {
156  if(QMessageBox::question(this, windowTitle(), tr("Do you want create spatial index to the selected ESRI ShapeFile?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
157  {
158  std::vector<std::string> datasetNames = m_driver->getDataSetNames();
159  assert(!datasetNames.empty());
160 
161  std::string command = "CREATE SPATIAL INDEX ON " + datasetNames[0];
162 
163  QApplication::setOverrideCursor(Qt::WaitCursor);
164 
165  m_driver->execute(command);
166 
167  QApplication::restoreOverrideCursor();
168 
169  QMessageBox::information(this, windowTitle(), "Spatial index created with successfully!");
170  }
171  }
172  }
173  }
174  catch(const std::exception& e)
175  {
176  QMessageBox::warning(this,
177  tr("TerraLib Qt Components"),
178  tr(e.what()));
179  return;
180  }
181  catch(...)
182  {
183  QMessageBox::warning(this,
184  tr("TerraLib Qt Components"),
185  tr("Unknown error while opening feature via OGR!"));
186  return;
187  }
188 
189  accept();
190 }
191 
193 {
194  try
195  {
196 // check if driver is loaded
197  if(te::da::DataSourceFactory::find("OGR") == 0)
198  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for OGR data sources!"));
199 
200 // get data source connection info based on form data
201  std::map<std::string, std::string> dsInfo;
202 
203  getConnectionInfo(dsInfo);
204 
205 // perform connection
206  std::auto_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make("OGR"));
207  ds->setConnectionInfo(dsInfo);
208  ds->open();
209 
210  if(ds.get() == 0)
211  throw te::qt::widgets::Exception(TE_TR("Could not open feature repository via OGR!"));
212 
213  if (m_ui->m_dirRadioButton->isChecked() && ds->getNumberOfDataSets() <= 0)
214  QMessageBox::information(this,
215  tr("TerraLib Qt Components"),
216  tr("Directory does not contain datasets!"));
217  else
218  QMessageBox::information(this,
219  tr("TerraLib Qt Components"),
220  tr("Data source is ok!"));
221  }
222  catch(const std::exception& e)
223  {
224  QMessageBox::warning(this,
225  tr("TerraLib Qt Components"),
226  tr(e.what()));
227  }
228  catch(...)
229  {
230  QMessageBox::warning(this,
231  tr("TerraLib Qt Components"),
232  tr("Unknown error while testing OGR data source!"));
233  }
234 }
235 
237 {
238  if(m_ui->m_fileRadioButton->isChecked())
239  {
240  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Vector File"), te::qt::widgets::GetFilePathFromSettings("vector"), tr("Esri Shapefile (*.shp *.SHP);; Mapinfo File (*.mif *.MIF);; GeoJSON (*.geojson *.GeoJSON);; GML (*.gml *.GML);; KML (*.kml *.KML);; All Files (*.*)"),
241  0, QFileDialog::ReadOnly);
242 
243  // Web Feature Service - WFS (*.xml *.XML *.wfs *.WFS);; => not stable yet!
244 
245  if(fileName.isEmpty())
246  return;
247 
248  QFileInfo info(fileName);
249 
250  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "vector");
251 
252  m_ui->m_featureRepoLineEdit->setText(fileName);
253  }
254  else if(m_ui->m_dirRadioButton->isChecked())
255  {
256  QString dirName = QFileDialog::getExistingDirectory(this, tr("Select a directory with shape files"), te::qt::widgets::GetFilePathFromSettings("vector"), QFileDialog::ShowDirsOnly);
257 
258  if(dirName.isEmpty())
259  return;
260 
261  te::qt::widgets::AddFilePathToSettings(dirName, "vector");
262 
263  m_ui->m_featureRepoLineEdit->setText(dirName);
264  }
265  else
266  {
267  QMessageBox::warning(this,
268  tr("TerraLib Qt Components"),
269  tr("Sorry, network files are not implemented yet!\nWe will provide it soon!"));
270  }
271 }
272 
273 void te::qt::plugins::ogr::OGRConnectorDialog::getConnectionInfo(std::map<std::string, std::string>& connInfo) const
274 {
275  connInfo.clear();
276 
277  QString qstr = m_ui->m_featureRepoLineEdit->text().trimmed();
278 
279  if(qstr.isEmpty())
280  throw te::qt::widgets::Exception(TE_TR("Please select a feature file first!"));
281 
282  connInfo["URI"] = qstr.toLatin1().data();
283 }
284 
285 void te::qt::plugins::ogr::OGRConnectorDialog::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
286 {
287  std::map<std::string, std::string>::const_iterator it = connInfo.find("URI");
288  std::map<std::string, std::string>::const_iterator itend = connInfo.end();
289 
290  if(it != itend)
291  {
292  m_ui->m_featureRepoLineEdit->setText(it->second.c_str());
293  m_ui->m_dirRadioButton->setChecked(true);
294  return;
295  }
296 
297  it = connInfo.find("SOURCE");
298 
299  if(it != itend)
300  {
301  m_ui->m_featureRepoLineEdit->setText(it->second.c_str());
302  m_ui->m_fileRadioButton->setChecked(true);
303  }
304 }
305 
const te::da::DataSourcePtr & getDriver() const
std::auto_ptr< Ui::OGRConnectorDialogForm > m_ui
static bool find(const std::string &dsType)
void setConnectionInfo(const std::map< std::string, std::string > &connInfo)
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Definition: Utils.cpp:367
bool IsShapeFile(const QString &path)
Definition: Utils.cpp:36
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
const te::da::DataSourceInfoPtr & getDataSource() const
static std::auto_ptr< DataSource > make(const std::string &dsType)
A dialog window for showing the OGR connector widget.
void set(const te::da::DataSourceInfoPtr &ds)
A class that represents a data source component.
Utility routines for the TerraLib Application Framework module.
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
Definition: Utils.cpp:376
OGRConnectorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
bool HasShapeFileSpatialIndex(const QString &path)
Definition: Utils.cpp:49
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void getConnectionInfo(std::map< std::string, std::string > &connInfo) const