TL4ConverterWizard.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/terralib4/TL4ConverterWizard.cpp
22 
23  \brief A wizard for guiding the conversion of a TerraLib 4.x database to a OGC SFS compliant format or any other format known by OGR and GDAL.
24 */
25 
26 // TerraLib
27 #include "../../../../common/Exception.h"
28 #include "../../../../common/StringUtils.h"
29 #include "../../../../common/progress/ProgressManager.h"
30 #include "../../../../common/progress/TaskProgress.h"
31 #include "../../../../core/filesystem/FileSystem.h"
32 #include "../../../../core/translator/Translator.h"
33 #include "../../../../dataaccess.h"
34 #include "../../../../geometry/GeometryProperty.h"
35 #include "../../../../maptools/DataSetLayer.h"
36 #include "../../../../qt/widgets/datasource/selector/DataSourceSelectorWidget.h"
37 #include "../../../../qt/widgets/datasource/selector/DataSourceSelectorWizardPage.h"
38 #include "../../../../qt/widgets/layer/utils/DataSet2Layer.h"
39 #include "../../../../qt/widgets/help/HelpPushButton.h"
40 #include "../../../../qt/widgets/progress/ProgressViewerDialog.h"
41 #include "../../../../qt/widgets/rp/Utils.h"
42 #include "../../../../qt/widgets/utils/ScopedCursor.h"
43 #include "../../../../qt/af/ApplicationController.h"
44 #include "../../../../qt/af/events/LayerEvents.h"
45 #include "../../../../raster/Utils.h"
46 #include "../../../../terralib4/DataSource.h"
47 #include "../../../../terralib4/ThemeInfo.h"
48 #include "../../../../se/ChannelSelection.h"
49 #include "../../../../se/ColorMap.h"
50 #include "../../../../se/FeatureTypeStyle.h"
51 #include "../../../../se/Fill.h"
52 #include "../../../../se/PolygonSymbolizer.h"
53 #include "../../../../se/RasterSymbolizer.h"
54 #include "../../../../se/Rule.h"
55 #include "../../../../se/SelectedChannel.h"
56 #include "../../../../se/Stroke.h"
57 #include "../../../../se/Style.h"
58 #include "../../../../se/Symbolizer.h"
59 #include "../../../../se/Utils.h"
60 #include "TL4ConverterWizard.h"
61 #include "TL4ConnectorWizardPage.h"
64 #include "TL4FinalPageWizardPage.h"
66 #include "Utils.h"
67 #include "ui_TL4ConverterWizardForm.h"
68 #include "ui_TL4ConnectorWizardPageForm.h"
69 #include "ui_TL4LayerSelectionWizardPageForm.h"
70 #include "ui_TL4RasterFolderSelectionWizardPageForm.h"
71 #include "ui_TL4FinalPageWizardPageForm.h"
72 #include "ui_TL4ThemeSelectionWizardPageForm.h"
73 
74 // STL
75 #include <cassert>
76 
77 // Boost
78 #include <boost/filesystem.hpp>
79 #include <boost/format.hpp>
80 #include <boost/lexical_cast.hpp>
81 
82 // Qt
83 #include <QIcon>
84 #include <QMessageBox>
85 #include <QTableWidgetItem>
86 #include <QGridLayout>
87 
88 // TerraLib 4
89 #include <terralib4/kernel/TeLegendEntry.h>
90 #include <terralib4/kernel/TeRasterTransform.h>
91 #include <terralib4/kernel/TeTheme.h>
92 
94  : QWizard(parent, f),
95  m_hasNonRaster(false),
96  m_hasRaster(false),
97  m_rollback(false),
98  m_ui(new Ui::TL4ConverterWizardForm)
99 {
100 // setup controls
101  m_ui->setupUi(this);
102 
103 // add pages
104  m_connectorPage.reset(new TL4ConnectorWizardPage(this));
105  m_connectorPage->setTitle(tr("TerraLib 4.x Database"));
106  m_connectorPage->setSubTitle(tr("Connect to a TerraLib 4.x database."));
107 
109  m_layerSelectionPage->setTitle(tr("Layer Selection"));
110  m_layerSelectionPage->setSubTitle(tr("Select the layers to be added to the project"));
111 
113  m_datasourceSelectorPage->setTitle(tr("Target Data Source"));
114  m_datasourceSelectorPage->setSubTitle(tr("Select a data source to store the converted data"));
115  m_datasourceSelectorPage->getSelectorWidget()->setSelectionMode(QAbstractItemView::SingleSelection);
116  m_datasourceSelectorPage->getSelectorWidget()->showDataSourceWithRasterSupport(false);
117 
119  m_rasterFolderSelectionPage->setTitle(tr("Target Raster Folder"));
120  m_rasterFolderSelectionPage->setSubTitle(tr("Select folder to store raster data"));
121 
122  m_resolveNamePage.reset(new QWizardPage(this));
123  m_resolveNamePage->setTitle(tr("Resolve Name Conflicts"));
124  m_resolveNamePage->setSubTitle(tr("Some layer names clashes with target data source names. Please, give a new name for the layers showed below"));
125  m_resolveNamePage->setCommitPage(true);
126 
127  QGridLayout* displayLayout = new QGridLayout(m_resolveNamePage.get());
128  m_resolveNameTableWidget.reset(new QTableWidget(m_resolveNamePage.get()));
129  displayLayout->addWidget(m_resolveNameTableWidget.get());
130  displayLayout->setContentsMargins(0,0,0,0);
131  m_resolveNameTableWidget->setColumnCount(3);
132  QStringList labels;
133  labels << "" << tr("Source Names") << tr("Target Names");
134  m_resolveNameTableWidget->setHorizontalHeaderLabels(labels);
135 
136  m_finalPage.reset(new TL4FinalPageWizardPage(this));
137  m_finalPage->setTitle(tr("Layer Creation"));
138  m_finalPage->setSubTitle(tr("Select the TerraLib 4.x Layer converted that you want to create a TerraLib 5 Project Layer."));
139 
141  m_themeSelection->setTitle(tr("Theme Creation"));
142  m_themeSelection->setSubTitle(tr("Select the TerraLib 4.x Theme of converted Layers that you want to create a TerraLib 5 Project Layer, with informations like Visual and Grouping."));
143  m_themeSelection->setFinalPage(true);
144 
150  setPage(PAGE_FINALPAGE, m_finalPage.get());
151  setPage(PAGE_THEME_SELECTION, m_themeSelection.get());
152 
153  connect(this->button(QWizard::NextButton), SIGNAL(pressed()), this, SLOT(next()));
154  connect(this->button(QWizard::BackButton), SIGNAL(pressed()), this, SLOT(back()));
155  connect(this->button(QWizard::FinishButton), SIGNAL(pressed()), this, SLOT(finish()));
156  connect(this->button(QWizard::CommitButton), SIGNAL(pressed()), this, SLOT(commit()));
157 
159 
160  this->setButton(QWizard::HelpButton, helpButton);
161 
162  helpButton->setPageReference("plugins/terralib4/Terralib4Converter.html");
163 }
164 
166 {
167 }
168 
170 {
171  if(currentId() == PAGE_TERRALIB4_CONNECTOR)
172  return PAGE_LAYER_SELECTION;
173 
174  if(currentId() == PAGE_LAYER_SELECTION)
175  {
176  if(m_hasNonRaster)
178 
179  if(m_hasRaster)
181 
182  return PAGE_LAYER_SELECTION;
183  }
184 
185  if(currentId() == PAGE_DATASOURCE_SELECTOR)
186  {
187  if(m_hasRaster)
189 
191  }
192 
193  if(currentId() == PAGE_RASTERFOLDER_SELECTOR)
195 
196  if(currentId() == PAGE_NAME_RESOLVE_SELECTOR)
197  return PAGE_FINALPAGE;
198 
199  if(currentId() == PAGE_FINALPAGE)
200  return PAGE_THEME_SELECTION;
201 
202  return -1;
203 }
204 
206 {
207  int current_page_id = currentId();
208 
209  if(current_page_id == PAGE_TERRALIB4_CONNECTOR)
210  {
212  return false;
213 
214  ::terralib4::DataSource* tl4Ds = dynamic_cast<::terralib4::DataSource*>(m_tl4Database.get());
215 
216  std::vector<std::string> layers = tl4Ds->getTL4Layers();
217  std::vector<std::string> rasters = tl4Ds->getTL4Rasters();
218  std::vector<std::string> tables = tl4Ds->getTL4Tables();
219  m_rasterFiles = tl4Ds->getTL4RasterFiles();
220 
221  m_layerSelectionPage->setDatasets(layers, tables, rasters, m_rasterFiles);
222 
223  }
224  else if(current_page_id == PAGE_LAYER_SELECTION)
225  {
226  if(!validLayerSelection())
227  return false;
228  }
229  else if(current_page_id == PAGE_DATASOURCE_SELECTOR)
230  {
231  m_targetDataSource = *m_datasourceSelectorPage->getSelectorWidget()->getSelecteds().begin();
232  }
233  else if(current_page_id == PAGE_RASTERFOLDER_SELECTOR)
234  {
236  }
237  else if(current_page_id == PAGE_NAME_RESOLVE_SELECTOR)
238  {
239  return !m_rollback;
240  }
241 
242  try
243  {
245  {
246  te::qt::widgets::ScopedCursor sc(Qt::WaitCursor);
247 
248  std::vector<QListWidgetItem*> selectedLayerItems = m_layerSelectionPage->getCheckedItems();
249 
250  m_resolveNameTableWidget->clearContents();
251  m_resolveNameTableWidget->setRowCount((int)selectedLayerItems.size());
252 
253  te::da::DataSourcePtr tl5ds;
254 
255  if(m_hasNonRaster)
256  tl5ds = te::da::DataSourceManager::getInstance().get(m_targetDataSource->getId(), m_targetDataSource->getType(), m_targetDataSource->getConnInfo());
257 
258  bool hasConflicts = false;
259 
260  for(std::size_t i = 0; i < selectedLayerItems.size(); ++i)
261  {
262  std::string targetDatasetName = selectedLayerItems[i]->text().toUtf8().data();
263 
264  // is it a raster?
265  std::unique_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(selectedLayerItems[i]->text().toUtf8().data()));
266 
267  if(input_dataset_type->hasRaster())
268  {
269  QTableWidgetItem *conflictItem = 0;
270 
271  if(te::core::FileSystem::exists(m_rasterFolderPath + "/" + targetDatasetName + ".tif"))
272  {
273  hasConflicts = true;
274 
275  conflictItem = new QTableWidgetItem(QIcon::fromTheme("delete"), "");
276  }
277  else
278  {
279  conflictItem = new QTableWidgetItem(QIcon::fromTheme("check"), "");
280  }
281 
282  conflictItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
283  m_resolveNameTableWidget->setItem((int)i, 0, conflictItem);
284  }
285  else
286  {
287  // non-raster
288  QTableWidgetItem *conflictItem = 0;
289 
290  std::string targetDatasetNameAux = targetDatasetName;
291 
292  if(tl5ds->getType() == "POSTGIS")
293  {
294  targetDatasetNameAux = te::common::Convert2LCase(targetDatasetName);
295  }
296 
297  if(tl5ds->dataSetExists(targetDatasetNameAux))
298  {
299  hasConflicts = true;
300 
301  conflictItem = new QTableWidgetItem(QIcon::fromTheme("delete"), "");
302  }
303  else
304  {
305  conflictItem = new QTableWidgetItem(QIcon::fromTheme("check"), "");
306  }
307 
308  conflictItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
309  m_resolveNameTableWidget->setItem((int)i, 0, conflictItem);
310  }
311 
312  QTableWidgetItem *oldNameItem = new QTableWidgetItem(selectedLayerItems[i]->text(), selectedLayerItems[i]->type());
313  oldNameItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
314  m_resolveNameTableWidget->setItem((int)i, 1, oldNameItem);
315 
316  bool changed = false;
317  std::string strChanged = te::common::ReplaceSpecialChars(targetDatasetName, changed);
318 
319  if (changed)
320  targetDatasetName = strChanged;
321 
322  QTableWidgetItem *newNameItem = new QTableWidgetItem(targetDatasetName.c_str(), selectedLayerItems[i]->type());
323  m_resolveNameTableWidget->setItem((int)i, 2, newNameItem);
324  }
325 
326  m_resolveNameTableWidget->resizeColumnsToContents();
327  m_resolveNameTableWidget->sortByColumn(1, Qt::AscendingOrder);
328 
329  if(hasConflicts)
330  {
331  m_resolveNamePage->setTitle(tr("Resolve Name Conflicts"));
332  m_resolveNamePage->setSubTitle(tr("Some layer names clash with target data source dataset names. Please, give a new name for the layers showed below"));
333  }
334  else
335  {
336  m_resolveNamePage->setTitle(tr("No Name Conflicts"));
337  m_resolveNamePage->setSubTitle(tr("You can change the layer names in the target data source"));
338  }
339  }
340  }
341  catch(const te::da::Exception& e)
342  {
343  QMessageBox::warning(this, tr("Warning"), e.what());
344  return false;
345  }
346  catch(...)
347  {
348  QMessageBox::warning(this, tr("Warning"), tr("Could not valid this page!"));
349  return false;
350  }
351 
352  return true;
353 }
354 
356 {
357  setCursor(Qt::WaitCursor);
358 
359  const std::string& connInfo = m_connectorPage->getConnInfo();
360 
361  if (connInfo.empty())
362  {
363  setCursor(Qt::ArrowCursor);
364  QMessageBox::warning(this, tr("Warning"), tr("Invalid connection parameters!"));
365  return false;
366  }
367 
368  try
369  {
370  te::qt::widgets::ScopedCursor sc(Qt::WaitCursor);
371  m_tl4Database = te::da::DataSourceFactory::make("TERRALIB4", connInfo);
372  m_tl4Database->open();
373  }
374  catch(const te::da::Exception& e)
375  {
376  setCursor(Qt::ArrowCursor);
377  QMessageBox::warning(this, tr("Warning"), e.what());
378  return false;
379  }
380  catch(...)
381  {
382  setCursor(Qt::ArrowCursor);
383  QMessageBox::warning(this, tr("Warning"), tr("A connection to the informed Terralib 4.x database could not be established. Please, verify the informed parameters."));
384  return false;
385  }
386  setCursor(Qt::ArrowCursor);
387 
388  return true;
389 }
390 
392 {
393  if(!m_layerSelectionPage->hasChecked())
394  {
395  // TODO: ao inves de dar esta mensagem deve-se desabilitar o botao next quando nenhum item esta selecionado!
396  QMessageBox::warning(this,
397  tr("Warning"),
398  tr("Please, select at least one layer for conversion!"));
399  return false;
400  }
401 
402  std::vector<std::string> layersNames = m_layerSelectionPage->getChecked();
403 
404 // do we have vector or raster layers?
405  m_hasNonRaster = false;
406  m_hasRaster = false;
407 
408  try
409  {
410  te::qt::widgets::ScopedCursor sc(Qt::WaitCursor);
411 
412  for(std::size_t i = 0; i < layersNames.size(); ++i)
413  {
414  std::unique_ptr<te::da::DataSetType> dst(m_tl4Database->getDataSetType(layersNames[i]));
415 
416  if(dst->hasRaster())
417  m_hasRaster = true;
418  else
419  m_hasNonRaster = true;
420  }
421  }
422  catch(const te::da::Exception& e)
423  {
424  QMessageBox::warning(this, tr("Warning"), e.what());
425  return false;
426  }
427  catch(...)
428  {
429  QMessageBox::warning(this, tr("Warning"), tr("Could not valid layer selection!"));
430  return false;
431  }
432 
433  return true;
434 }
435 
437 {
438  bool hasConflict = false;
439 
440 // TODO: acrescentar try e catch para evitar problemas
441  te::da::DataSourcePtr tl5ds;
442 
443  try
444  {
445  if(m_hasNonRaster)
446  tl5ds = te::da::DataSourceManager::getInstance().get(m_targetDataSource->getId(), m_targetDataSource->getType(), m_targetDataSource->getConnInfo());
447  }
448  catch(const te::da::Exception& e)
449  {
450  QMessageBox::warning(this, tr("Warning"), e.what());
451  return false;
452  }
453  catch(...)
454  {
455  QMessageBox::warning(this, tr("Warning"), tr("Could not connect to TerraLib 5 data source!"));
456  return false;
457  }
458 
459  int nrows = m_resolveNameTableWidget->rowCount();
460 
461  for(int i = 0; i != nrows; ++i)
462  {
463 // get original dataset name
464  QTableWidgetItem* item_source = m_resolveNameTableWidget->item(i, 1);
465 
466  if(item_source == 0)
467  throw te::common::Exception(TE_TR("Invalid source table item!"));
468 
469  std::string sourceName = item_source->text().toUtf8().data();
470 
471 // get target dataset name
472  QTableWidgetItem* item_target = m_resolveNameTableWidget->item(i, 2);
473 
474  if(item_target == 0)
475  throw te::common::Exception(TE_TR("Invalid target table item!"));
476 
477  std::string targetName = item_target->text().toUtf8().data();
478 
479 // ask if the dataset is a raster
480  try
481  {
482  te::qt::widgets::ScopedCursor sc(Qt::WaitCursor);
483 
484  std::unique_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(sourceName));
485 
486  if(input_dataset_type->hasRaster())
487  {
488 // yes!
489  if(te::core::FileSystem::exists(m_rasterFolderPath + "/" + targetName + ".tif"))
490  {
491  hasConflict = true;
492  }
493  else
494  {
495  QTableWidgetItem *nonconflictItem = new QTableWidgetItem(QIcon::fromTheme("check"), "");
496 
497  m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
498  }
499  }
500  else
501  {
502 // no!
503  std::string targetDatasetNameAux = targetName;
504 
505  if(tl5ds->getType() == "POSTGIS")
506  {
507  targetDatasetNameAux = te::common::Convert2LCase(targetName);
508  }
509 
510  if(tl5ds->dataSetExists(targetDatasetNameAux))
511  {
512  hasConflict = true;
513  }
514  else
515  {
516  QTableWidgetItem *nonconflictItem = new QTableWidgetItem(QIcon::fromTheme("check"), "");
517 
518  m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
519  }
520  }
521  }
522  catch(const te::da::Exception& e)
523  {
524  QMessageBox::warning(this, tr("Warning"), e.what());
525  return false;
526  }
527  catch(...)
528  {
529  QMessageBox::warning(this, tr("Warning"), tr("Could not valid layer names in target data source!"));
530  return false;
531  }
532  }
533 
534  if(hasConflict)
535  {
536  QString errMsg(tr("There still have name conflicts. Please, resolve the indicated conflicts before continue!"));
537 
538  QMessageBox::warning(this, tr("TerraLib 4.x Converter"), errMsg);
539 
540  return false;
541  }
542 
543  return true;
544 }
545 
546 std::string te::qt::plugins::terralib4::TL4ConverterWizard::getOriginalName(const std::string& targetName)
547 {
548  int rowCount = m_resolveNameTableWidget->rowCount();
549 
550  for(int i = 0; i < rowCount; ++i)
551  {
552  QString originalNameInTable = m_resolveNameTableWidget->item(i, 1)->text();
553  QString targetNameInTable = m_resolveNameTableWidget->item(i, 2)->text();
554 
555  std::string originalFromTable = originalNameInTable.toUtf8().data();
556  std::string targetFromTable = targetNameInTable.toUtf8().data();
557 
558  if (targetFromTable == targetName)
559  return originalFromTable;
560  }
561 
562  return "";
563 }
564 
565 std::string te::qt::plugins::terralib4::TL4ConverterWizard::getNewName(const std::string& originalName)
566 {
567  int rowCount = m_resolveNameTableWidget->rowCount();
568 
569  for(int i = 0; i < rowCount; ++i)
570  {
571  QString oName = m_resolveNameTableWidget->item(i, 1)->text();
572 
573  std::string aux = oName.toUtf8().data();
574 
575  if(originalName.c_str() == aux)
576  return m_resolveNameTableWidget->item(i, 2)->text().toUtf8().data();
577 
578  }
579 
580  return "";
581 }
582 
584 {
585  QWizard::back();
586 }
587 
589 {
590  QWizard::next();
591 }
592 
594 {
595  if(!validLayerNames())
596  {
597  m_rollback = true;
598  return;
599  }
600 
601 // TeRasterFiles will not be converted -. put them at the problematics!
602  std::vector<std::pair<std::string, std::string> > problematicDatasets;
603 
604  for (std::size_t i = 0; i < m_rasterFiles.size(); ++i)
605  {
606  std::pair<std::string, std::string> dproblem;
607  dproblem.first = m_rasterFiles[i].first.c_str(); //"External raster (TeRasterFile) will not be converted.
608  dproblem.second = m_rasterFiles[i].second.c_str();
609 
610  problematicDatasets.push_back(dproblem);
611  }
612 
613 // validation successful => convert the source layers!
614 
615  std::vector<std::string> successfulDatasets;
616 
617  te::da::DataSourcePtr tl5ds;
618 
619  try
620  {
621  if(m_hasNonRaster)
622  tl5ds = te::da::DataSourceManager::getInstance().get(m_targetDataSource->getId(), m_targetDataSource->getType(), m_targetDataSource->getConnInfo());
623  }
624  catch(const te::da::Exception& e)
625  {
626  m_rollback = true;
627  QMessageBox::warning(this, tr("Warning"), e.what());
628  return;
629  }
630  catch(...)
631  {
632  m_rollback = true;
633  QMessageBox::warning(this, tr("Warning"), tr("Could not connect to TerraLib 5 data source!"));
634  return;
635  }
636 
637  int nrows = m_resolveNameTableWidget->rowCount();
638 
639  //progress
642 
643  te::common::TaskProgress task("TerraLib4 Converter...");
644  task.setTotalSteps(nrows);
645  task.useTimer(true);
646 
647  for(int i = 0; i != nrows; ++i)
648  {
649 // get original dataset name
650  QTableWidgetItem* item_source = m_resolveNameTableWidget->item(i, 1);
651 
652  if(item_source == 0)
653  {
655  throw te::common::Exception(TE_TR("Invalid source table item!"));
656  }
657 
658  std::string sourceName = item_source->text().toUtf8().data();
659 
660 // get target dataset name
661  QTableWidgetItem* item_target = m_resolveNameTableWidget->item(i, 2);
662 
663  if(item_target == 0)
664  {
666  throw te::common::Exception(TE_TR("Invalid target table item!"));
667  }
668 
669  std::string targetName = item_target->text().toUtf8().data();
670 
671 // ask if the dataset is a raster
672  try
673  {
674  te::qt::widgets::ScopedCursor sc(Qt::WaitCursor);
675 
676  std::unique_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(sourceName));
677 
678  if(input_dataset_type->hasRaster())
679  {
680 // yes!
681  std::unique_ptr<te::da::DataSet> ds = m_tl4Database->getDataSet(sourceName);
682 
683  std::unique_ptr<te::rst::Raster> raster = ds->getRaster("Raster");
684 
685  std::string newName = m_rasterFolderPath + "/" + targetName + ".tif";
686 
687  te::rst::CreateCopy(*raster.release(), newName);
688 
689  successfulDatasets.push_back(targetName);
690  }
691  else
692  {
693 // no!
694  input_dataset_type->setName(targetName);
695 
696  if (input_dataset_type->size() == 1 && input_dataset_type->getProperty(0)->getName() == "spatial_data")
697  {
698  std::pair<std::string, std::string> dproblem;
699  dproblem.first = sourceName;
700  dproblem.second = TE_TR("Layer invalid: table without attributes!");
701 
702  problematicDatasets.push_back(dproblem);
703 
704  continue;
705  }
706 
707  std::unique_ptr<te::da::DataSetTypeConverter> dt_adapter(new te::da::DataSetTypeConverter(input_dataset_type.get(), tl5ds->getCapabilities()));
708 
709  std::unique_ptr<te::da::DataSet> ds(m_tl4Database->getDataSet(sourceName));
710 
711  std::unique_ptr<te::da::DataSetAdapter> ds_adapter(new te::da::DataSetAdapter(ds.get()));//te::da::CreateAdapter(ds.get(), dt_adapter.get()));
712 
713  const std::vector<std::vector<std::size_t> >& indexes = dt_adapter->getConvertedPropertyIndexes();
714  const std::vector<te::da::AttributeConverter>& funcs = dt_adapter->getConverters();
715 
716  te::da::DataSetType* type = dt_adapter->getResult();
717 
718  te::da::PrimaryKey* pk = 0;
719  pk = type->getPrimaryKey();
720 
721  if(pk)
722  {
723  pk->setName(te::common::Convert2LCase(targetName) + "_pk");
724  }
725 
726  for(std::size_t i = 0; i < type->size(); ++i)
727  {
728  te::dt::Property* p = type->getProperty(i);
729 
730  ds_adapter->add(p->getName(), p->getType(), indexes[i], funcs[i]);
731  }
732 
733  ::terralib4::DataSource* tl4Ds = dynamic_cast<::terralib4::DataSource*>(m_tl4Database.get());
734 
735  int finalSrid = tl4Ds->getLayerSRID(sourceName);
736 
737  te::da::AssociateDataSetTypeConverterSRID(dt_adapter.get(), finalSrid);
738 
739  std::map<std::string, std::string> opt;
740 
741  ds_adapter->moveBeforeFirst();
742 
743  te::da::Create(tl5ds.get(), dt_adapter->getResult(), ds_adapter.get(), opt);
744 
745  successfulDatasets.push_back(targetName);
746  }
747  }
748  catch(const te::common::Exception& e)
749  {
750  std::pair<std::string, std::string> dproblem;
751  dproblem.first = sourceName;
752  dproblem.second = e.what();
753 
754  problematicDatasets.push_back(dproblem);
755  }
756  catch(std::exception& e)
757  {
758  std::pair<std::string, std::string> dproblem;
759  dproblem.first = sourceName;
760  dproblem.second = e.what();
761 
762  problematicDatasets.push_back(dproblem);
763  }
764  catch(...)
765  {
766  std::pair<std::string, std::string> dproblem;
767  dproblem.first = sourceName;
768  dproblem.second = TE_TR("unknown problem in conversion!");
769 
770  problematicDatasets.push_back(dproblem);
771  }
772 
773  if (task.isActive() == false)
774  {
776  throw te::common::Exception(TE_TR("Operation canceled!"));
777  }
778 
779  task.pulse();
780  }
781 
782 // give a warning
783  if(!problematicDatasets.empty())
784  {
785  QString error(tr("Some TerraLib 4.x Layers could not be converted: \n\n"));
786  QString details;
787 
788  for(std::size_t i = 0; i < problematicDatasets.size(); ++i)
789  {
790  error.append(QString(" - ") + problematicDatasets[i].first.c_str() + QString(""));
791  details.append(problematicDatasets[i].first.c_str() + QString(":\n"));
792  details.append(problematicDatasets[i].second.c_str() + QString("\n\n"));
793  }
794 
795  QMessageBox message(QMessageBox::Warning, tr("TerraLib 4.x Converter"), error, QMessageBox::Ok, this);
796  message.setDetailedText(details);
797 
798  message.exec();
799  }
800 
801 // fill next page!
802  ::terralib4::DataSource* tl4Ds = dynamic_cast<::terralib4::DataSource*>(m_tl4Database.get());
803 
804  m_finalPage->setDataSets(successfulDatasets);
805 
806  std::vector<::terralib4::ThemeInfo> themes = tl4Ds->getTL4Themes();
807 
808  std::vector<::terralib4::ThemeInfo> convertedThemes;
809  for (std::size_t i = 0; i < successfulDatasets.size(); ++i)
810  {
811  std::string originalLayerName = getOriginalName(successfulDatasets[i]);
812 
813  for (std::size_t j = 0; j < themes.size(); ++j)
814  {
815  std::string themeLayerName = themes[j].m_layerName;
816 
817  if (themeLayerName == originalLayerName)
818  {
819  convertedThemes.push_back(themes[j]);
820  }
821  }
822  }
823 
824  m_themeSelection->setThemes(convertedThemes);
825 
826  m_rollback = false;
827 
829 }
830 
832 {
833  std::vector<std::string> selected = m_finalPage->getSelected();
834 
835  te::da::DataSourcePtr outDataSource;
836 
837  try
838  {
839  if(m_hasNonRaster)
840  outDataSource = te::da::DataSourceManager::getInstance().find(m_targetDataSource->getId());
841  }
842  catch(const te::da::Exception& e)
843  {
844  m_rollback = true;
845  QMessageBox::warning(this, tr("Warning"), e.what());
846  return;
847  }
848  catch(...)
849  {
850  m_rollback = true;
851  QMessageBox::warning(this, tr("Warning"), tr("Could not connect to TerraLib 5 data source!"));
852  return;
853  }
854 
855  try
856  {
857  //te::qt::widgets::ScopedCursor sc(Qt::WaitCursor);
858  setCursor(Qt::WaitCursor);
859 
860  for(std::size_t i = 0; i < selected.size(); ++i)
861  {
862  te::map::AbstractLayerPtr layer = 0;
863 
864  std::unique_ptr<te::da::DataSetType> sourceDt = m_tl4Database->getDataSetType(getOriginalName(selected[i]));
865 
866  if(sourceDt->hasRaster())
867  {
868  std::map<std::string, std::string> connInfo;
869  connInfo["URI"] = m_rasterFolderPath + "/" + selected[i] + ".tif";
870 
871  layer = te::qt::widgets::createLayer("GDAL", connInfo);
872  }
873  else
874  {
876 
877  std::unique_ptr<te::da::DataSetType> dsType = outDataSource->getDataSetType(selected[i]);
878 
879  te::da::DataSetTypePtr dt(dsType.release());
880 
881  layer = converter(dt);
882  }
883 
884  te::qt::af::evt::LayerAdded evt(layer);
885 
886  emit triggered(&evt);
887  }
888 
889  std::vector<::terralib4::ThemeInfo> themes = m_themeSelection->getThemes();
890 
891  for(std::size_t i = 0; i < themes.size(); ++i)
892  {
893  te::map::AbstractLayerPtr layer = 0;
894 
895  std::string newName = getNewName(themes[i].m_layerName);
896 
897  std::unique_ptr<te::da::DataSetType> sourceDt = m_tl4Database->getDataSetType(themes[i].m_layerName);
898 
899  ::terralib4::DataSource* tl4Ds = dynamic_cast<::terralib4::DataSource*>(m_tl4Database.get());
900 
901  TeTheme* theme = tl4Ds->getTL4Theme(themes[i]);
902 
903  te::se::Style* style = 0;
904 
905  if(sourceDt->hasRaster())
906  {
907  std::map<std::string, std::string> connInfo;
908  connInfo["URI"] = m_rasterFolderPath + "/" + newName + ".tif";
909 
910  layer = te::qt::widgets::createLayer("GDAL", connInfo);
911  layer->setTitle(theme->name());
912  style = Convert2TerraLib5(0, theme, true);
913 
915 
916  if(theme->grouping().groupMode_ != TeNoGrouping)
917  {
918  te::se::ColorMap* cm = 0;
919 
920  cm = GetRasterGrouping(theme);
921 
922  symb->setColorMap(cm);
923 
924  std::string band = "0";
925 
927 
928  if(cs->getGrayChannel())
929  {
930  te::se::SelectedChannel* scGray = cs->getGrayChannel();
931 
932  scGray->setSourceChannelName(band);
933  }
934  else
935  {
937 
938  scGray->setSourceChannelName(band);
939 
940  cs->setGrayChannel(scGray);
941  }
942 
944  }
945  }
946  else
947  {
948  std::unique_ptr<te::da::DataSetType> dst = outDataSource->getDataSetType(getNewName(themes[i].m_layerName));
949 
951 
952  te::da::DataSetTypePtr dstPtr(dst.release());
953 
954  layer = converter(dstPtr);
955  bool changed;
956  layer->setTitle(te::common::ReplaceSpecialChars(themes[i].m_name, changed));
957 
958  // Get Style
959  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(dstPtr.get());
960 
961  style = Convert2TerraLib5(geomProp->getGeometryType(), theme);
962 
963  if(theme->grouping().groupMode_ != TeNoGrouping)
964  layer->setGrouping(GetGrouping(theme, style));
965  }
966 
967  layer->setStyle(style);
968 
969  te::qt::af::evt::LayerAdded evt(layer);
970 
971  emit triggered(&evt);
972  }
973  }
974  catch(const te::da::Exception& e)
975  {
976  setCursor(Qt::ArrowCursor);
977  m_rollback = true;
978  QMessageBox::warning(this, tr("Warning"), e.what());
979  return;
980  }
981  catch(...)
982  {
983  setCursor(Qt::ArrowCursor);
984  m_rollback = true;
985  QMessageBox::warning(this, tr("Warning"), tr("Automatic layer creation failed!"));
986  return;
987  }
988 
989  setCursor(Qt::ArrowCursor);
990 }
991 
993 {
994  QMessageBox::warning(this,
995  tr("TerraLib 4.x Converter"),
996  tr("This option is not implemented yet!\nWe will provide it soon!"));
997 }
998 
999 
1000 
1001 
1002 
1003 
1004 
virtual void setName(const std::string &name)
It sets the constraint name.
Definition: Constraint.h:126
Property * getProperty(std::size_t i) const
It returns the i-th property.
This event signals that a new layer was created.
Definition: LayerEvents.h:71
te::map::Grouping * GetGrouping(TeTheme *theme, te::se::Style *style)
unsigned int band
A selected channel to be display.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
std::unique_ptr< Ui::TL4ConverterWizardForm > m_ui
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
std::string getNewName(const std::string &originalName)
std::unique_ptr< TL4ConnectorWizardPage > m_connectorPage
boost::shared_ptr< DataSource > DataSourcePtr
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
Definition: StringUtils.h:202
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
A class that models the description of a dataset.
Definition: DataSetType.h:72
void setPageReference(const QString &ref)
Sets the documentation page reference.
virtual const char * what() const
It outputs the exception message.
void useTimer(bool flag)
Used to define if task use progress timer information.
TESEEXPORT RasterSymbolizer * GetRasterSymbolizer(Style *s)
Try to get raster symbolizer from a style.
TERASTEREXPORT te::rst::RasterPtr CreateCopy(const te::rst::Raster &rin, const std::string &uri, const std::string &rType=std::string("GDAL"))
Create a new raster from existing one.
std::vector< std::string > getTL4Rasters()
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
std::unique_ptr< TL4FinalPageWizardPage > m_finalPage
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
te::se::ChannelSelection * getChannelSelection() const
void setGrayChannel(SelectedChannel *c)
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
std::string ReplaceSpecialChars(const std::string &str, bool &changed)
It replace special characters of a string.
Definition: StringUtils.h:376
It models a property definition.
Definition: Property.h:59
bool isActive() const
Verify if the task is active.
std::vector< std::pair< std::string, std::string > > m_rasterFiles
void setTotalSteps(int value)
Set the task total stepes.
An converter for DataSetType.
A wizard page that provides the capability of connecting to a TerraLib 4.x database.
void removeViewer(int viewerId)
Dettach a progress viewer (AbstractProgressViewer destructor calls this method).
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
TEDATAACCESSEXPORT void Create(DataSource *ds, DataSetType *dt, DataSet *d, std::size_t limit=0)
It creates the dataset definition in a data source and then fill it with data from the input dataset...
void setColorCompositionType(ColorCompositionType cct)
std::unique_ptr< te::da::DataSource > m_tl4Database
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
std::unique_ptr< QWizardPage > m_resolveNamePage
void triggered(te::qt::af::evt::Event *e)
std::vector< std::pair< std::string, std::string > > getTL4RasterFiles()
te::gm::Polygon * p
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
std::size_t size() const
It returns the number of properties of the CompositeProperty.
std::vector< std::string > getTL4Tables()
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
int getType() const
It returns the property data type.
Definition: Property.h:161
std::vector<::terralib4::ThemeInfo > getTL4Themes()
std::unique_ptr< te::qt::widgets::DataSourceSelectorWizardPage > m_datasourceSelectorPage
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const te::core::URI &connInfo)
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
void setColorMap(ColorMap *c)
void setSourceChannelName(const std::string &name)
int getLayerSRID(const std::string &layerName)
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo &theme)
TL4ConverterWizard(QWidget *parent=0, Qt::WindowFlags f=0)
list rasters
Definition: compose.py:3
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
std::unique_ptr< QTableWidget > m_resolveNameTableWidget
SelectedChannel * getGrayChannel() const
std::unique_ptr< TL4LayerSelectionWizardPage > m_layerSelectionPage
std::string getOriginalName(const std::string &targetName)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
An adapter for DataSet.
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
Definition: ColorMap.h:61
std::unique_ptr< TL4RasterFolderSelectionWizardPage > m_rasterFolderSelectionPage
std::unique_ptr< TL4ThemeSelectionWizardPage > m_themeSelection
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48
ChannelSelection specifies the false-color channel selection for a multi-spectral raster source (such...
std::vector< std::string > getTL4Layers()
const std::string & getName() const
It returns the property name.
Definition: Property.h:127