All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ImportTableDialog.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/addressgeocoding/ImportTableDialog.cpp
22 
23  \brief A dialog to import table with address to geocoding operation
24 */
25 
26 // TerraLib
27 #include "../../common/Logger.h"
28 #include "../../common/progress/ProgressManager.h"
29 #include "../../common/Translator.h"
30 #include "../../common/STLUtils.h"
31 #include "../../dataaccess/dataset/DataSetType.h"
32 #include "../../dataaccess/dataset/DataSetTypeConverter.h"
33 #include "../../dataaccess/dataset/ObjectIdSet.h"
34 #include "../../dataaccess/datasource/DataSourceCapabilities.h"
35 #include "../../dataaccess/datasource/DataSource.h"
36 #include "../../dataaccess/datasource/DataSourceInfo.h"
37 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
38 #include "../../dataaccess/datasource/DataSourceFactory.h"
39 #include "../../dataaccess/datasource/DataSourceManager.h"
40 #include "../../dataaccess/utils/Utils.h"
41 #include "../../datatype/Enums.h"
42 #include "../../datatype/Property.h"
43 #include "../../maptools/AbstractLayer.h"
44 #include "../../memory/DataSet.h"
45 #include "../../qt/af/Utils.h"
46 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
47 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
48 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
49 #include "../../qt/widgets/property/DataSetAdapterWidget.h"
50 #include "../../qt/widgets/table/DataSetTableView.h"
51 #include "../../qt/widgets/Utils.h"
52 #include "../../statistics/core/Utils.h"
53 #include "../Config.h"
54 #include "../Exception.h"
55 #include "ImportTableDialog.h"
56 #include "ui_ImportTableDialogForm.h"
57 
58 // Qt
59 #include <QCheckBox>
60 #include <QFileDialog>
61 #include <QGridLayout>
62 #include <QMessageBox>
63 
64 // Boost
65 #include <boost/algorithm/string.hpp>
66 #include <boost/filesystem.hpp>
67 #include <boost/uuid/random_generator.hpp>
68 #include <boost/uuid/uuid_io.hpp>
69 
71  : QDialog(parent, f),
72  m_ui(new Ui::ImportTableDialogForm)
73 {
74 // add controls
75  m_ui->setupUi(this);
76 
77  //Adjusting the dataSetTableView that will be used to display the address data
78  m_tblView.reset(new te::qt::widgets::DataSetTableView(m_ui->m_dataPreviewFrame));
79  QGridLayout* dataPreviewLayout = new QGridLayout(m_ui->m_dataPreviewFrame);
80  dataPreviewLayout->addWidget(m_tblView.get());
81  dataPreviewLayout->setContentsMargins(0, 0, 0, 0);
82 
83  m_tblView->setAlternatingRowColors(true);
84  m_tblView->verticalHeader()->setVisible(false);
85  m_tblView->setSelectionMode(QAbstractItemView::NoSelection);
86 
87 
88  connect(m_ui->m_filePushButton, SIGNAL(clicked()), this, SLOT(onInputDataToolButtonTriggered()));
89 
90  connect(m_ui->m_helpPushButton, SIGNAL(clicked()), this, SLOT(onHelpPushButtonClicked()));
91  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
92  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
93 
94 }
95 
97 {
98 }
99 
101 {
102  return m_dataSource;
103 }
104 
106 {
107  return m_dataSet;
108 }
109 
111 {
112  try
113  {
114  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Textual File"), te::qt::widgets::GetFilePathFromSettings("tabular"), tr("Comma Separated Value (*.csv *.CSV);; dBASE (*.dbf *.DBF)"),
115  0, QFileDialog::ReadOnly);
116 
117  if(fileName.isEmpty())
118  return;
119 
120  QFileInfo info(fileName);
121  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "tabular");
122 
123  //Getting the connection info
124  std::string ogrInfo("connection_string=" + fileName.toStdString());
125  std::map<std::string, std::string> connInfo;
126  connInfo["URI"] = fileName.toStdString();
127 
128  boost::filesystem::path uri(fileName.toStdString());
129  std::string file = uri.stem().string();
130 
131  //Creating a DataSource
132  static boost::uuids::basic_random_generator<boost::mt19937> gen;
133  boost::uuids::uuid u = gen();
134 
136  dsInfo->setConnInfo(connInfo);
137  dsInfo->setId(boost::uuids::to_string(u));
138  dsInfo->setTitle(fileName.toStdString());
139  dsInfo->setDescription("");
140  dsInfo->setAccessDriver("OGR");
141  dsInfo->setType("OGR");
142 
144 
145  m_dataSource = te::da::DataSourceFactory::make(dsInfo->getAccessDriver());
146  m_dataSource->setConnectionInfo(dsInfo->getConnInfo());
147 
148  m_dataSource->setId(boost::uuids::to_string(u));
149  m_dataSource->open();
150 
151  //Creating the DataSet and DataType
152  m_dataSet = m_dataSource->getDataSet(file);
153  std::vector<std::string> datasetNames = m_dataSource->getDataSetNames();
154 
155  if(!datasetNames.empty())
156  m_dataType = m_dataSource->getDataSetType(datasetNames[0]);
157  else
158  {
159  QMessageBox::critical( this, tr("Error"),tr("The file could not be read!"));
160  return;
161  }
162 
163  m_ui->m_fileLineEdit->setText(fileName);
164 
165  //Creating the DataSetConverter
166  m_dsConverter.reset(new te::da::DataSetTypeConverter(m_dataType.get()));
167 
168  //Filling the data preview table
169  std::vector<std::size_t> properties;
170  for(std::size_t i = 0; i < m_dataSet->getNumProperties(); ++i)
171  properties.push_back(i);
172 
173  //The table will display 10 rows of the data for previewing purposes
174  std::auto_ptr<te::mem::DataSet> memFeature((new te::mem::DataSet(*m_dataSet.get(), properties, 10)));
175 
176  m_tblView->setDataSet(memFeature.release(), m_dataSource->getEncoding());
177  m_tblView->resizeColumnsToContents();
178  m_tblView->show();
179  }
180  catch(const std::exception& e)
181  {
182  QMessageBox::warning(this, tr("Tabular File"), e.what());
183  }
184 }
185 
187 {
188  QMessageBox::information(this, "Help", "Under development");
189 }
190 
192 {
193  this->close();
194 }
195 
197 {
198  reject();
199 }
200 
std::auto_ptr< Ui::ImportTableDialogForm > m_ui
A dialog to import table with address to geocoding operation.
ImportTableDialog(QWidget *parent=0, Qt::WindowFlags f=0)
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
An converter for DataSetType.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
static std::auto_ptr< DataSource > make(const std::string &dsType)
std::auto_ptr< te::qt::widgets::DataSetTableView > m_tblView
std::auto_ptr< te::da::DataSet > getDataSet()
A customized table view for te::map::AbstractLayer objects. Uses a te::qt::widgets::DataSetModel as i...
A class that represents a data source component.
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
Definition: Utils.cpp:376
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr