All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QueryDataSourceDialog.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/query/QueryDataSourceDialog.cpp
22 
23  \brief This file defines a class for a Query DataSource Dialog Dialog.
24 */
25 
26 // TerraLib
27 #include "../../../common/StringUtils.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/dataset/DataSetAdapter.h"
30 #include "../../../dataaccess/dataset/DataSetTypeConverter.h"
31 #include "../../../dataaccess/dataset/DataSetType.h"
32 #include "../../../dataaccess/dataset/ObjectIdSet.h"
33 #include "../../../dataaccess/datasource/DataSourceCapabilities.h"
34 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
35 #include "../../../dataaccess/datasource/DataSourceInfo.h"
36 #include "../../../dataaccess/datasource/DataSourceManager.h"
37 #include "../../../dataaccess/query/SQLDialect.h"
38 #include "../../../dataaccess/query/SQLFunctionEncoder.h"
39 #include "../../../dataaccess/utils/Utils.h"
40 #include "../../../datatype/SimpleProperty.h"
41 #include "../../../geometry/GeometryProperty.h"
42 #include "../../../maptools/DataSetLayer.h"
43 #include "../datasource/selector/DataSourceSelectorDialog.h"
44 #include "../layer/utils/DataSet2Layer.h"
45 #include "QueryDataSourceDialog.h"
46 #include "ui_QueryDataSourceDialogForm.h"
47 
48 // Qt
49 #include <QCursor>
50 #include <QFileDialog>
51 #include <QGridLayout>
52 #include <QMessageBox>
53 #include <QTextStream>
54 
55 // STL
56 #include <cassert>
57 #include <memory>
58 
59 // Boost
60 #include <boost/filesystem.hpp>
61 #include <boost/uuid/random_generator.hpp>
62 #include <boost/uuid/uuid_io.hpp>
63 
65 
67  : QDialog(parent, f),
68  m_ui(new Ui::QueryDataSourceDialogForm)
69 {
70  m_ui->setupUi(this);
71 
72  m_ui->m_applyToolButton->setIcon(QIcon::fromTheme("media-playback-start-green"));
73  m_ui->m_clearToolButton->setIcon(QIcon::fromTheme("edit-clear"));
74  m_ui->m_applySelToolButton->setIcon(QIcon::fromTheme("pointer-selection"));
75  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
76  m_ui->m_createLayerlToolButton->setIcon(QIcon::fromTheme("layer-new"));
77 
78 
79  m_ui->m_saveSqlToolButton->setIcon(QIcon::fromTheme("document-save-as"));
80  m_ui->m_openSqlToolButton->setIcon(QIcon::fromTheme("document-open"));
81 
82  //table view
84  m_ui->m_tableView->setModel(m_tableModel);
85 
86  //dataset display
88 
89  m_appMapDisplay = 0;
90 
91  QGridLayout* displayGridLayout = new QGridLayout(m_ui->m_displayWidget);
92  displayGridLayout->setContentsMargins(0, 0, 0, 0);
93  displayGridLayout->addWidget(m_dataSetDisplay);
94 
95  // Signals¨& slots
96  connect(m_ui->m_dataSourceComboBox, SIGNAL(activated(int)), this, SLOT(onDataSourceSelected(int)));
97  connect(m_ui->m_baseDataSetComboBox, SIGNAL(activated(int)), this, SLOT(onBaseDataSetSelected(int)));
98  connect(m_ui->m_dataSetListWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(onDataSetItemClicked(QListWidgetItem*)));
99  connect(m_ui->m_applyToolButton, SIGNAL(clicked()), this, SLOT(onApplyPushButtonClicked()));
100  connect(m_ui->m_clearToolButton, SIGNAL(clicked()), this, SLOT(onClearPushButtonClicked()));
101  connect(m_ui->m_saveSqlToolButton, SIGNAL(clicked()), this, SLOT(onSaveSqlToolButtonClicked()));
102  connect(m_ui->m_openSqlToolButton, SIGNAL(clicked()), this, SLOT(onOpenSqlToolButtonClicked()));
103  connect(m_ui->m_sqlEditorTextEdit, SIGNAL(textChanged()), this, SLOT(onSQLEditorTextChanged()));
104  connect(m_ui->m_applySelToolButton, SIGNAL(clicked()), this, SLOT(onApplySelToolButtonClicked()));
105  connect(m_ui->m_createLayerlToolButton, SIGNAL(pressed()), this, SLOT(onCreateLayerToolButtonClicked()));
106  connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()), this, SLOT(onTargetDatasourceToolButtonPressed()));
107  connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()), this, SLOT(onTargetFileToolButtonPressed()));
108 
109  //load data sources information
111 
112  //
113  m_ui->m_helpPushButton->setPageReference("widgets/query/query_datasource.html");
114 }
115 
117 {
118  m_keyWords.clear();
119 }
120 
121 void te::qt::widgets::QueryDataSourceDialog::setLayerList(std::list<te::map::AbstractLayerPtr> layerList)
122 {
123  m_layerList = layerList;
124 
125  if(m_ui->m_baseDataSetComboBox->count() > 0)
126  onBaseDataSetSelected(0);
127 }
128 
130 {
131  m_appMapDisplay = appMapDisplay;
132 }
133 
135 {
136  m_ui->m_dataSourceComboBox->clear();
137 
138  std::map<std::string, te::da::DataSourceInfoPtr>::const_iterator it = te::da::DataSourceInfoManager::getInstance().begin();
139  std::map<std::string, te::da::DataSourceInfoPtr>::const_iterator itend =te::da::DataSourceInfoManager::getInstance().end();
140 
141  while(it != itend)
142  {
143  if(it->second->getType() != "GDAL")
144  m_ui->m_dataSourceComboBox->addItem(it->second->getTitle().c_str(), QVariant(it->second->getId().c_str()));
145 
146  ++it;
147  }
148 
149  if(m_ui->m_dataSourceComboBox->count() != 0)
150  onDataSourceSelected(m_ui->m_dataSourceComboBox->currentIndex());
151 }
152 
154 {
155  m_keyWords.clear();
156 
157  // add defaul SQL key words
158  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("SELECT", Qt::blue));
159  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("FROM", Qt::blue));
160  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("WHERE", Qt::blue));
161  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("JOIN", Qt::blue));
162  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("INNER", Qt::blue));
163  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("LEFT", Qt::blue));
164  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("RIGHT", Qt::blue));
165  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("AS", Qt::blue));
166  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("GROUP", Qt::blue));
167  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("ORDER", Qt::blue));
168  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type("BY", Qt::blue));
169 
170  //add data source specific key words
171  std::string dataSourceId = m_ui->m_dataSourceComboBox->itemData(m_ui->m_dataSourceComboBox->currentIndex()).toString().toStdString();
172 
173  te::da::DataSourcePtr ds = te::da::GetDataSource(dataSourceId);
174 
175  const te::da::SQLDialect* dialect = ds->getDialect();
176 
177  std::vector<std::string> names = dialect->getRegisteredNames();
178 
179  for(std::size_t t = 0; t < names.size(); ++t)
180  {
181  QString s = names[t].c_str();
182  QString sUpper = s.toUpper();
183 
184  m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(sUpper.toStdString(), Qt::red));
185  }
186 
187 }
188 
190 {
191  m_ui->m_baseDataSetComboBox->clear();
192  m_ui->m_dataSetListWidget->clear();
193  m_ui->m_attrDataSetListWidget->clear();
194 
195  std::string dataSourceId = m_ui->m_dataSourceComboBox->itemData(index).toString().toStdString();
196 
197  te::da::DataSourcePtr ds = te::da::GetDataSource(dataSourceId);
198 
199  if(!ds->isOpened())
200  ds->open();
201 
202  std::vector<std::string> dataSetNames;
203  te::da::GetDataSetNames(dataSetNames, dataSourceId);
204 
205  for(std::size_t t = 0; t < dataSetNames.size(); ++t)
206  {
207  m_ui->m_baseDataSetComboBox->addItem(dataSetNames[t].c_str());
208  m_ui->m_dataSetListWidget->addItem(dataSetNames[t].c_str());
209  }
210 
211  if(m_ui->m_baseDataSetComboBox->count() > 0)
212  onBaseDataSetSelected(0);
213 
214  buildMap();
215 }
216 
218 {
219  std::string dataSet = m_ui->m_baseDataSetComboBox->itemText(index).toStdString();
220 
221  m_ui->m_layerComboBox->clear();
222 
223  std::list<te::map::AbstractLayerPtr>::iterator it = m_layerList.begin();
224 
225  while(it != m_layerList.end())
226  {
228 
229  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
230 
231  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
232 
233  if(dsLayer && dsType->getName() == dataSet)
234  m_ui->m_layerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
235 
236  ++it;
237  }
238 }
239 
241 {
242  m_ui->m_attrDataSetListWidget->clear();
243 
244  if(!item)
245  return;
246 
247  std::string dataSourceId = m_ui->m_dataSourceComboBox->itemData(m_ui->m_dataSourceComboBox->currentIndex()).toString().toStdString();
248 
249  std::string dataSetName = item->text().toStdString();
250 
251  te::da::DataSetType* dsType = te::da::GetDataSetType(dataSetName, dataSourceId);
252 
253  std::vector<te::dt::Property*> propVec = dsType->getProperties();
254 
255  for(std::size_t t = 0; t < propVec.size(); ++t)
256  {
257  m_ui->m_attrDataSetListWidget->addItem(propVec[t]->getName().c_str());
258  }
259 
260  delete dsType;
261 }
262 
264 {
265  if(m_ui->m_sqlEditorTextEdit->toPlainText().isEmpty())
266  return;
267 
268  std::string dataSourceId = m_ui->m_dataSourceComboBox->itemData(m_ui->m_dataSourceComboBox->currentIndex()).toString().toStdString();
269 
270  te::da::DataSourcePtr ds = te::da::GetDataSource(dataSourceId);
271 
272  std::string dataSetName = m_ui->m_baseDataSetComboBox->currentText().toStdString();
273 
274  te::da::DataSetTypePtr dsType(te::da::GetDataSetType(dataSetName, dataSourceId));
275 
276  std::string sql = "";
277 
278  if(m_ui->m_sqlEditorTextEdit->textCursor().selectedText().isEmpty())
279  sql = m_ui->m_sqlEditorTextEdit->toPlainText().toStdString();
280  else
281  sql = m_ui->m_sqlEditorTextEdit->textCursor().selectedText().toStdString();
282 
283  //get dataset
284  std::auto_ptr<te::da::DataSet> dataSet;
285 
286  try
287  {
288  dataSet = ds->query(sql);
289  }
290  catch(const std::exception& e)
291  {
292  m_dataSetDisplay->clear();
293  m_tableModel->setDataSet(0, ds->getEncoding());
294 
295  std::string errorMessage = "SQL Error: ";
296  errorMessage += e.what();
297  errorMessage += "\n";
298  errorMessage += "-------------------------------------------------------------------------\n";
299 
300  m_ui->m_logPlainTextEdit->appendPlainText(errorMessage.c_str());
301 
302  m_ui->m_tabWidget->setCurrentIndex(1);
303 
304  return;
305  }
306 
307  std::string message = "SQL Done: ";
308  message += sql;
309  message += "\n";
310  message += "-------------------------------------------------------------------------\n";
311 
312  m_ui->m_logPlainTextEdit->appendPlainText(message.c_str());
313 
314  //draw dataset
315  m_dataSetDisplay->clear();
316 
317  bool draw = false;
318  for(std::size_t t = 0; t < dataSet->getNumProperties(); ++t)
319  {
320  int type = dataSet->getPropertyDataType(t);
321 
322  if(type == te::dt::GEOMETRY_TYPE)
323  {
324  draw = true;
325  break;
326  }
327  }
328 
329  if(draw)
330  m_dataSetDisplay->draw(dsType, ds, dataSet.get());
331  else
332  m_dataSetDisplay->clear();
333 
334  //show dataset on table
335  m_tableModel->setDataSet(dataSet.release(), ds->getEncoding());
336 
337  m_ui->m_tabWidget->setCurrentIndex(0);
338 }
339 
341 {
342  m_ui->m_sqlEditorTextEdit->clear();
343 
344  m_dataSetDisplay->clear();
345 
346  m_tableModel->setDataSet(0, te::common::LATIN1);
347 }
348 
350 {
351  disconnect(m_ui->m_sqlEditorTextEdit, SIGNAL(textChanged()), this, SLOT(onSQLEditorTextChanged()));
352 
353  QString sql = m_ui->m_sqlEditorTextEdit->toPlainText();
354 
355  int curPos = m_ui->m_sqlEditorTextEdit->textCursor().position();
356 
357  disconnect(m_ui->m_sqlEditorTextEdit, SIGNAL(textChanged()), this, SLOT(onSQLEditorTextChanged()));
358 
359  m_ui->m_sqlEditorTextEdit->clear();
360 
361  QStringList words = sql.split(' ');
362 
363  bool isAttrValue = false;
364 
365  for(int i = 0; i < words.size(); ++i)
366  {
367  QString w = words.value(i).toUpper();
368 
369  std::string strW = w.toStdString();
370 
371  std::map<std::string, Qt::GlobalColor>::iterator it = m_keyWords.find(strW);
372 
373  bool removeAttrValue = false;
374 
375  if(it != m_keyWords.end())
376  {
377  m_ui->m_sqlEditorTextEdit->setFontWeight(QFont::Bold);
378  m_ui->m_sqlEditorTextEdit->setTextColor(it->second);
379  }
380  else
381  {
382  bool okNum = false;
383 
384  words.value(i).toDouble(&okNum);
385 
386  m_ui->m_sqlEditorTextEdit->setFontWeight(QFont::Normal);
387 
388  if(okNum)
389  m_ui->m_sqlEditorTextEdit->setTextColor(Qt::darkGreen);
390  else
391  {
392  if(!strW.empty())
393  {
394  std::string initChar = strW.substr(0, 1);
395  if(!isAttrValue && (initChar == "'" || initChar == "\""))
396  isAttrValue = true;
397 
398  if(strW.size() > 1)
399  {
400  std::string lastChar = strW.substr(strW.size() - 1, 1);
401  if(isAttrValue && (lastChar == "'" || lastChar == "\""))
402  removeAttrValue = true;
403  }
404  }
405 
406  if(isAttrValue)
407  m_ui->m_sqlEditorTextEdit->setTextColor(Qt::magenta);
408  else
409  m_ui->m_sqlEditorTextEdit->setTextColor(Qt::black);
410  }
411  }
412 
413  m_ui->m_sqlEditorTextEdit->insertPlainText(words.value(i));
414 
415  if(removeAttrValue)
416  isAttrValue = false;
417 
418  if(i < words.size() - 1)
419  m_ui->m_sqlEditorTextEdit->insertPlainText(" ");
420  }
421 
422  QTextCursor c = m_ui->m_sqlEditorTextEdit->textCursor();
423  c.setPosition(curPos);
424  m_ui->m_sqlEditorTextEdit->setTextCursor(c);
425 
426  connect(m_ui->m_sqlEditorTextEdit, SIGNAL(textChanged()), this, SLOT(onSQLEditorTextChanged()));
427 }
428 
430 {
431  //select file
432  QString path = QFileDialog::getSaveFileName(this, tr("Set a SQL file..."), "", tr("SQL File *.sql"));
433 
434  if(path.isNull())
435  return;
436 
437  //open file
438  QFile file(path);
439 
440  if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
441  {
442  QMessageBox::warning(this, tr("Query DataSource"), file.errorString());
443  return;
444  }
445 
446  // save to file
447  QTextStream out(&file);
448 
449  out << m_ui->m_sqlEditorTextEdit->toPlainText();
450 
451  file.close();
452 }
453 
455 {
456  //select file
457  QString path = QFileDialog::getOpenFileName(this, tr("Select a SQL file..."), "", tr("SQL File *.sql"));
458 
459  if(path.isNull())
460  return;
461 
462  //open file
463  QFile file(path);
464 
465  if(!file.open(QIODevice::ReadOnly))
466  {
467  QMessageBox::warning(this, tr("Query DataSource"), file.errorString());
468  return;
469  }
470 
471  // show file
472  QTextStream in(&file);
473 
474  m_ui->m_sqlEditorTextEdit->clear();
475 
476  while(!in.atEnd())
477  {
478  QString line = in.readLine();
479 
480  m_ui->m_sqlEditorTextEdit->append(line);
481  }
482 
483  file.close();
484 }
485 
487 {
488  if(m_ui->m_sqlEditorTextEdit->toPlainText().isEmpty())
489  {
490  QMessageBox::information(this, tr("Warning"), tr("SQL not defined."));
491  return;
492  }
493 
494  QVariant varLayer = m_ui->m_layerComboBox->itemData(m_ui->m_layerComboBox->currentIndex(), Qt::UserRole);
495  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
496 
497  if(!layer.get())
498  {
499  QMessageBox::warning(this, tr("Query DataSource"), tr("No layer selected."));
500  return;
501  }
502 
503  std::string dataSourceId = m_ui->m_dataSourceComboBox->itemData(m_ui->m_dataSourceComboBox->currentIndex()).toString().toStdString();
504 
505  te::da::DataSourcePtr ds = te::da::GetDataSource(dataSourceId);
506 
507  std::string dataSetName = m_ui->m_baseDataSetComboBox->currentText().toStdString();
508 
509  te::da::DataSetTypePtr dsType(te::da::GetDataSetType(dataSetName, dataSourceId));
510 
511  std::string sql = "";
512 
513  if(m_ui->m_sqlEditorTextEdit->textCursor().selectedText().isEmpty())
514  sql = m_ui->m_sqlEditorTextEdit->toPlainText().toStdString();
515  else
516  sql = m_ui->m_sqlEditorTextEdit->textCursor().selectedText().toStdString();
517 
518  //get dataset
519  std::auto_ptr<te::da::DataSet> dataSet;
520 
521  try
522  {
523  dataSet = ds->query(sql);
524  }
525  catch(...)
526  {
527  QMessageBox::warning(this, tr("Query DataSource"), tr("Error executing SQL."));
528  return;
529  }
530 
531  try
532  {
533  if(m_ui->m_newSelRadioButton->isChecked())
534  {
535  // Generates the oids
536  dataSet->moveBeforeFirst();
537  te::da::ObjectIdSet* oids = te::da::GenerateOIDSet(dataSet.get(), dsType.get());
538 
539  oids->setExpressionByInClause();
540 
541  layer->clearSelected();
542  layer->select(oids);
543  }
544  else if(m_ui->m_addSelRadioButton->isChecked())
545  {
546  // Generates the oids
547  dataSet->moveBeforeFirst();
548  te::da::ObjectIdSet* oids = te::da::GenerateOIDSet(dataSet.get(), dsType.get());
549 
550  oids->setExpressionByInClause();
551 
552  layer->select(oids);
553  }
554 
555  if(m_appMapDisplay)
556  {
557  m_appMapDisplay->refresh();
558  }
559  }
560  catch(te::common::Exception& e)
561  {
562  QMessageBox::warning(this, tr("Query DataSource"), tr("Error selecting objects: ") + e.what());
563  return;
564  }
565 
566  QMessageBox::information(this, tr("Query DataSource"), tr("Selection done."));
567 }
568 
569 
571 {
572  // check input parameters
573  if(m_ui->m_sqlEditorTextEdit->toPlainText().isEmpty())
574  {
575  QMessageBox::information(this, tr("Warning"), tr("SQL not defined."));
576  return;
577  }
578 
579  if(m_ui->m_repositoryLineEdit->text().isEmpty())
580  {
581  QMessageBox::information(this, tr("Warning"), tr("Define a repository for the result."));
582  return;
583  }
584 
585  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
586  {
587  QMessageBox::information(this, tr("Warning"), tr("Define a name for the resulting layer."));
588  return;
589  }
590 
591  //create dataset
592  std::string dataSourceId = m_ui->m_dataSourceComboBox->itemData(m_ui->m_dataSourceComboBox->currentIndex()).toString().toStdString();
593 
594  te::da::DataSourcePtr ds = te::da::GetDataSource(dataSourceId);
595 
596  std::string inputDataSetName = m_ui->m_baseDataSetComboBox->currentText().toStdString();
597 
598  te::da::DataSetTypePtr inputDataSetType(te::da::GetDataSetType(inputDataSetName, dataSourceId));
599 
600  std::string sql = "";
601 
602  if(m_ui->m_sqlEditorTextEdit->textCursor().selectedText().isEmpty())
603  sql = m_ui->m_sqlEditorTextEdit->toPlainText().toStdString();
604  else
605  sql = m_ui->m_sqlEditorTextEdit->textCursor().selectedText().toStdString();
606 
607  std::auto_ptr<te::da::DataSet> dataSet;
608 
609  try
610  {
611  dataSet = ds->query(sql);
612  }
613  catch(...)
614  {
615  QMessageBox::warning(this, tr("Query DataSource"), tr("Error executing SQL."));
616  return;
617  }
618 
619  //create / get data source
620  te::da::DataSourcePtr outputDataSource;
621 
622  if(m_toFile)
623  {
624  //create new data source
625  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
626 
627  std::map<std::string, std::string> dsInfo;
628  dsInfo["URI"] = uri.string();
629 
630  boost::uuids::basic_random_generator<boost::mt19937> gen;
631  boost::uuids::uuid u = gen();
632  std::string id_ds = boost::uuids::to_string(u);
633 
635  dsInfoPtr->setConnInfo(dsInfo);
636  dsInfoPtr->setTitle(uri.stem().string());
637  dsInfoPtr->setAccessDriver("OGR");
638  dsInfoPtr->setType("OGR");
639  dsInfoPtr->setDescription(uri.string());
640  dsInfoPtr->setId(id_ds);
641 
643 
644  outputDataSource = te::da::DataSourceManager::getInstance().get(id_ds, "OGR", dsInfoPtr->getConnInfo());
645  }
646  else
647  {
648  outputDataSource = te::da::GetDataSource(m_outputDatasource->getId());
649  }
650 
651  //get output dataset name
652  std::string dataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
653 
654  std::size_t idx = dataSetName.find(".");
655  if (idx != std::string::npos)
656  dataSetName=dataSetName.substr(0,idx);
657 
658  //save data
659  std::auto_ptr<te::da::DataSetType> dsType(new te::da::DataSetType(dataSetName));
660 
661  dataSet->moveFirst();
662 
663  std::set<std::string> names;
664 
665  int srid = 0;
666 
667  for(std::size_t t = 0; t < dataSet->getNumProperties(); ++t)
668  {
669  //check if the property name its duplicated
670  std::string propName = dataSet->getPropertyName(t);
671 
672  int count = 1;
673  while(names.find(propName) != names.end())
674  {
675  propName += "_";
676  propName += te::common::Convert2String(count);
677  }
678 
679  names.insert(propName);
680 
681  //create output property
682  te::dt::Property* p = 0;
683  if(dataSet->getPropertyDataType(t) != te::dt::GEOMETRY_TYPE)
684  {
685  p = new te::dt::SimpleProperty(propName, dataSet->getPropertyDataType(t));
686  }
687  else
688  {
689  std::auto_ptr<te::gm::Geometry> geom = dataSet->getGeometry(t);
690 
691  srid = geom->getSRID();
692 
693  p = new te::gm::GeometryProperty(propName, srid, geom->getGeomTypeId());
694  }
695 
696  //add property to output datasetype
697  if(p)
698  {
699  dsType->add(p);
700  }
701  else
702  {
703  QMessageBox::warning(this, tr("Query DataSource"), tr("Error creating output dataset."));
704  return;
705  }
706  }
707 
708  dataSet->moveBeforeFirst();
709 
710  //create converter in case property name changed
711  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(dsType.get(), outputDataSource->getCapabilities());
712 
713  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataSet.get(), converter));
714 
715  dsAdapter->setSRID(srid);
716 
717  //save data
718  std::map<std::string, std::string> options;
719 
720  outputDataSource->createDataSet(dsType.get(), options);
721 
722  outputDataSource->add(dataSetName, dsAdapter.get(), options);
723 
724  //create layer
725  try
726  {
727  te::qt::widgets::DataSet2Layer converter(outputDataSource->getId());
728 
729  te::da::DataSetTypePtr dt(outputDataSource->getDataSetType(dataSetName).release());
730 
731  te::map::AbstractLayerPtr layer = converter(dt);
732 
733  emit createNewLayer(layer);
734  }
735  catch(te::common::Exception& e)
736  {
737  QMessageBox::warning(this, tr("Query DataSource"), tr("Error creating layer. ") + e.what());
738  return;
739  }
740 
741  QMessageBox::information(this, tr("Query DataSource"), tr("Layer created."));
742 }
743 
745 {
746  m_ui->m_newLayerNameLineEdit->clear();
747  m_ui->m_newLayerNameLineEdit->setEnabled(true);
748 
750  dlg.exec();
751 
752  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
753 
754  if(dsPtrList.size() <= 0)
755  return;
756 
757  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
758 
759  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
760 
761  m_outputDatasource = *it;
762 
763  m_toFile = false;
764 }
765 
767 {
768  m_ui->m_newLayerNameLineEdit->clear();
769  m_ui->m_repositoryLineEdit->clear();
770 
771  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."), QString(), tr("Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
772 
773  if (fileName.isEmpty())
774  return;
775 
776  boost::filesystem::path outfile(fileName.toStdString());
777 
778  m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
779 
780  m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
781 
782  m_ui->m_newLayerNameLineEdit->setEnabled(false);
783 
784  m_toFile = true;
785 }
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
Geometric property.
te::da::SQLDialect * dialect
Definition: WFSDialect.h:1
te::qt::widgets::MapDisplay * m_appMapDisplay
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
An atomic property like an integer or double.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual const char * what() const
It outputs the exception message.
Definition: Exception.cpp:58
QueryDataSourceDialog(QWidget *parent=0, Qt::WindowFlags f=0)
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
te::qt::widgets::DataSetDisplay * m_dataSetDisplay
A widget to control the display of a set of layers.
Definition: MapDisplay.h:66
std::auto_ptr< Ui::QueryDataSourceDialogForm > m_ui
It models a property definition.
Definition: Property.h:59
An converter for DataSetType.
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
void setExpressionByInClause(const std::string source="")
te::qt::widgets::DataSetTableModel * m_tableModel
A map display for a dataset.
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
Definition: Utils.cpp:412
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
Definition: Utils.cpp:153
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
void setLayerList(std::list< te::map::AbstractLayerPtr > layerList)
This method is used to set the list of layers.
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
A dialog for selecting a data source.
void setAppMapDisplay(te::qt::widgets::MapDisplay *appMapDisplay)
A class that represents a data source component.
std::vector< std::string > getRegisteredNames() const
It gets the all registered names from registed functions.
Definition: SQLDialect.cpp:51
TEDATAACCESSEXPORT DataSetType * GetDataSetType(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:228
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
void onDataSetItemClicked(QListWidgetItem *item)
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:51
A table model representing a te::da::DataSet.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
This file defines a class for a Query DataSource Dialog Dialog.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr