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