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((int)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((int)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((int)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((int)i, 1, oldNameItem);
313 
314  QTableWidgetItem *newNameItem = new QTableWidgetItem(targetDatasetName.c_str(), selectedLayerItems[i]->type());
315  m_resolveNameTableWidget->setItem((int)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  }
479  else
480  {
481  QTableWidgetItem *nonconflictItem = new QTableWidgetItem(QIcon::fromTheme("check"), "");
482 
483  m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
484  }
485  }
486  else
487  {
488 // no!
489  std::string targetDatasetNameAux = targetName;
490 
491  if(tl5ds->getType() == "POSTGIS")
492  {
493  targetDatasetNameAux = te::common::Convert2LCase(targetName);
494  }
495 
496  if(tl5ds->dataSetExists(targetDatasetNameAux))
497  {
498  hasConflict = true;
499  }
500  else
501  {
502  QTableWidgetItem *nonconflictItem = new QTableWidgetItem(QIcon::fromTheme("check"), "");
503 
504  m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
505  }
506  }
507  }
508  catch(const te::da::Exception& e)
509  {
510  QMessageBox::warning(this, tr("Warning"), e.what());
511  return false;
512  }
513  catch(...)
514  {
515  QMessageBox::warning(this, tr("Warning"), tr("Could not valid layer names in target data source!"));
516  return false;
517  }
518  }
519 
520  if(hasConflict)
521  {
522  QString errMsg(tr("There still have name conflicts. Please, resolve the indicated conflicts before continue!"));
523 
524  QMessageBox::warning(this, tr("TerraLib 4.x Converter"), errMsg);
525 
526  return false;
527  }
528 
529  return true;
530 }
531 
532 std::string te::qt::plugins::terralib4::TL4ConverterWizard::getOriginalName(const std::string& targetName)
533 {
534  int rowCount = m_resolveNameTableWidget->rowCount();
535 
536  for(int i = 0; i < rowCount; ++i)
537  {
538  QString targetNameInTable = m_resolveNameTableWidget->item(i, 2)->text();
539 
540  std::string aux = targetNameInTable.toLatin1();
541 
542  if(targetName.c_str() == aux)
543  return aux;
544  }
545 
546  return "";
547 }
548 
549 std::string te::qt::plugins::terralib4::TL4ConverterWizard::getNewName(const std::string& originalName)
550 {
551  int rowCount = m_resolveNameTableWidget->rowCount();
552 
553  for(int i = 0; i < rowCount; ++i)
554  {
555  QString oName = m_resolveNameTableWidget->item(i, 1)->text();
556 
557  std::string aux = oName.toLatin1();
558 
559  if(originalName.c_str() == aux)
560  return aux;
561  }
562 
563  return "";
564 }
565 
567 {
568  QWizard::back();
569 }
570 
572 {
573  QWizard::next();
574 }
575 
577 {
578  if(!validLayerNames())
579  {
580  m_rollback = true;
581  return;
582  }
583 
584 // validation successful => convert the source layers!
585  std::vector<std::pair<std::string, std::string> > problematicDatasets;
586  std::vector<std::string> successfulDatasets;
587 
588  te::da::DataSourcePtr tl5ds;
589 
590  try
591  {
592  if(m_hasNonRaster)
593  tl5ds = te::da::DataSourceManager::getInstance().get(m_targetDataSource->getId(), m_targetDataSource->getType(), m_targetDataSource->getConnInfo());
594  }
595  catch(const te::da::Exception& e)
596  {
597  m_rollback = true;
598  QMessageBox::warning(this, tr("Warning"), e.what());
599  return;
600  }
601  catch(...)
602  {
603  m_rollback = true;
604  QMessageBox::warning(this, tr("Warning"), tr("Could not connect to TerraLib 5 data source!"));
605  return;
606  }
607 
608  int nrows = m_resolveNameTableWidget->rowCount();
609 
610  //progress
613 
614  te::common::TaskProgress task("TerraLib4 Converter...");
615  task.setTotalSteps(nrows);
616  task.useTimer(true);
617 
618  for(int i = 0; i != nrows; ++i)
619  {
620 // get original dataset name
621  QTableWidgetItem* item_source = m_resolveNameTableWidget->item(i, 1);
622 
623  if(item_source == 0)
624  {
626  throw te::common::Exception(TE_TR("Invalid source table item!"));
627  }
628 
629  std::string sourceName = item_source->text().toLatin1();
630 
631 // get target dataset name
632  QTableWidgetItem* item_target = m_resolveNameTableWidget->item(i, 2);
633 
634  if(item_target == 0)
635  {
637  throw te::common::Exception(TE_TR("Invalid target table item!"));
638  }
639 
640  std::string targetName = item_target->text().toLatin1();
641 
642 // ask if the dataset is a raster
643  try
644  {
645  te::qt::widgets::ScopedCursor sc(Qt::WaitCursor);
646 
647  std::auto_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(sourceName));
648 
649  if(input_dataset_type->hasRaster())
650  {
651 // yes!
652  std::auto_ptr<te::da::DataSet> ds = m_tl4Database->getDataSet(sourceName);
653 
654  std::auto_ptr<te::rst::Raster> raster = ds->getRaster("Raster");
655 
656  std::string newName = m_rasterFolderPath + "/" + targetName + ".tif";
657 
658  te::rst::CreateCopy(*raster.release(), newName);
659 
660  successfulDatasets.push_back(targetName);
661  }
662  else
663  {
664 // no!
665  input_dataset_type->setName(targetName);
666 
667  std::auto_ptr<te::da::DataSetTypeConverter> dt_adapter(new te::da::DataSetTypeConverter(input_dataset_type.get(), tl5ds->getCapabilities()));
668 
669  std::auto_ptr<te::da::DataSet> ds(m_tl4Database->getDataSet(sourceName));
670 
671  std::auto_ptr<te::da::DataSetAdapter> ds_adapter(new te::da::DataSetAdapter(ds.get()));//te::da::CreateAdapter(ds.get(), dt_adapter.get()));
672 
673  const std::vector<std::vector<std::size_t> >& indexes = dt_adapter->getConvertedPropertyIndexes();
674  const std::vector<te::da::AttributeConverter>& funcs = dt_adapter->getConverters();
675 
676  te::da::DataSetType* type = dt_adapter->getResult();
677 
678  te::da::PrimaryKey* pk = 0;
679  pk = type->getPrimaryKey();
680 
681  if(pk)
682  {
683  pk->setName(te::common::Convert2LCase(targetName) + "_pk");
684  }
685 
686  te::common::CharEncoding encTo = tl5ds->getEncoding();
687 
688  for(std::size_t i = 0; i < type->size(); ++i)
689  {
690  te::dt::Property* p = type->getProperty(i);
691 
692  if(p->getType() == te::dt::STRING_TYPE)
693  {
694  te::da::CharEncodingConverter conversor(encTo);
695  ds_adapter->add(p->getName(), p->getType(), indexes[i], conversor);
696  }
697  else
698  {
699  ds_adapter->add(p->getName(), p->getType(), indexes[i], funcs[i]);
700  }
701  }
702 
703  ::terralib4::DataSource* tl4Ds = dynamic_cast<::terralib4::DataSource*>(m_tl4Database.get());
704 
705  int finalSrid = tl4Ds->getLayerSRID(sourceName);
706 
707  te::da::AssociateDataSetTypeConverterSRID(dt_adapter.get(), finalSrid);
708 
709  std::map<std::string, std::string> opt;
710 
711  ds_adapter->moveBeforeFirst();
712 
713  te::da::Create(tl5ds.get(), dt_adapter->getResult(), ds_adapter.get(), opt);
714 
715  successfulDatasets.push_back(targetName);
716  }
717  }
718  catch(const te::common::Exception& e)
719  {
720  std::pair<std::string, std::string> dproblem;
721  dproblem.first = sourceName;
722  dproblem.second = e.what();
723 
724  problematicDatasets.push_back(dproblem);
725  }
726  catch(std::exception& e)
727  {
728  std::pair<std::string, std::string> dproblem;
729  dproblem.first = sourceName;
730  dproblem.second = e.what();
731 
732  problematicDatasets.push_back(dproblem);
733  }
734  catch(...)
735  {
736  std::pair<std::string, std::string> dproblem;
737  dproblem.first = sourceName;
738  dproblem.second = TE_TR("unknown problem in conversion!");
739 
740  problematicDatasets.push_back(dproblem);
741  }
742 
743  if (task.isActive() == false)
744  {
746  throw te::common::Exception(TE_TR("Operation canceled!"));
747  }
748 
749  task.pulse();
750  }
751 
752 // give a warning
753  if(!problematicDatasets.empty())
754  {
755  QString error(tr("Some TerraLib 4.x Layers could not be converted: \n\n"));
756  QString details;
757 
758  for(std::size_t i = 0; i < problematicDatasets.size(); ++i)
759  {
760  error.append(QString(" - ") + problematicDatasets[i].first.c_str() + QString(""));
761  details.append(problematicDatasets[i].first.c_str() + QString(":\n"));
762  details.append(problematicDatasets[i].second.c_str() + QString("\n\n"));
763  }
764 
765  QMessageBox message(QMessageBox::Warning, tr("TerraLib 4.x Converter"), error, QMessageBox::Ok, this);
766  message.setDetailedText(details);
767 
768  message.exec();
769  }
770 
771 // fill next page!
772  ::terralib4::DataSource* tl4Ds = dynamic_cast<::terralib4::DataSource*>(m_tl4Database.get());
773 
774  m_finalPage->setDataSets(successfulDatasets);
775 
776  std::vector<::terralib4::ThemeInfo> themes = tl4Ds->getTL4Themes();
777 
778  std::vector<::terralib4::ThemeInfo> convertedThemes;
779  for(std::size_t i = 0; i < themes.size(); ++i)
780  {
781  for(std::size_t j = 0; j < successfulDatasets.size(); ++j)
782  {
783  if(themes[i].m_layerName == getOriginalName(successfulDatasets[j]))
784  {
785  convertedThemes.push_back(themes[i]);
786  }
787  }
788  }
789 
790  m_themeSelection->setThemes(convertedThemes);
791 
792  m_rollback = false;
793 
795 }
796 
798 {
799  std::vector<std::string> selected = m_finalPage->getSelected();
800 
801  te::da::DataSourcePtr outDataSource;
802 
803  try
804  {
805  if(m_hasNonRaster)
806  outDataSource = te::da::DataSourceManager::getInstance().find(m_targetDataSource->getId());
807  }
808  catch(const te::da::Exception& e)
809  {
810  m_rollback = true;
811  QMessageBox::warning(this, tr("Warning"), e.what());
812  return;
813  }
814  catch(...)
815  {
816  m_rollback = true;
817  QMessageBox::warning(this, tr("Warning"), tr("Could not connect to TerraLib 5 data source!"));
818  return;
819  }
820 
821  try
822  {
823  //te::qt::widgets::ScopedCursor sc(Qt::WaitCursor);
824  setCursor(Qt::WaitCursor);
825 
826  for(std::size_t i = 0; i < selected.size(); ++i)
827  {
828  te::map::AbstractLayerPtr layer = 0;
829 
830  std::auto_ptr<te::da::DataSetType> sourceDt = m_tl4Database->getDataSetType(getOriginalName(selected[i]));
831 
832  if(sourceDt->hasRaster())
833  {
834  std::map<std::string, std::string> connInfo;
835  connInfo["URI"] = m_rasterFolderPath + "/" + selected[i] + ".tif";
836 
837  layer = te::qt::widgets::createLayer("GDAL", connInfo);
838  }
839  else
840  {
841  te::qt::widgets::DataSet2Layer converter(m_targetDataSource->getId());
842 
843  std::auto_ptr<te::da::DataSetType> dsType = outDataSource->getDataSetType(selected[i]);
844 
845  te::da::DataSetTypePtr dt(dsType.release());
846 
847  layer = converter(dt);
848  }
849 
850  te::qt::af::evt::LayerAdded evt(layer);
851 
853  }
854 
855  std::vector<::terralib4::ThemeInfo> themes = m_themeSelection->getThemes();
856 
857  for(std::size_t i = 0; i < themes.size(); ++i)
858  {
859  te::map::AbstractLayerPtr layer = 0;
860 
861  std::string newName = getNewName(themes[i].m_layerName);
862 
863  std::auto_ptr<te::da::DataSetType> sourceDt = m_tl4Database->getDataSetType(newName);
864 
865  ::terralib4::DataSource* tl4Ds = dynamic_cast<::terralib4::DataSource*>(m_tl4Database.get());
866 
867  TeTheme* theme = tl4Ds->getTL4Theme(themes[i]);
868 
869  te::se::Style* style = 0;
870 
871  if(sourceDt->hasRaster())
872  {
873  std::map<std::string, std::string> connInfo;
874  connInfo["URI"] = m_rasterFolderPath + "/" + themes[i].m_name + ".tif";
875 
876  layer = te::qt::widgets::createLayer("GDAL", connInfo);
877 
878  style = Convert2TerraLib5(0, theme, true);
879 
881 
882  if(theme->grouping().groupMode_ != TeNoGrouping)
883  {
884  te::se::ColorMap* cm = 0;
885 
886  cm = GetRasterGrouping(theme);
887 
888  symb->setColorMap(cm);
889 
890  std::string band = "0";
891 
893 
894  if(cs->getGrayChannel())
895  {
896  te::se::SelectedChannel* scGray = cs->getGrayChannel();
897 
898  scGray->setSourceChannelName(band);
899  }
900  else
901  {
903 
904  scGray->setSourceChannelName(band);
905 
906  cs->setGrayChannel(scGray);
907  }
908 
910  }
911  }
912  else
913  {
914  std::auto_ptr<te::da::DataSetType> dst = outDataSource->getDataSetType(getNewName(themes[i].m_layerName));
915 
916  te::qt::widgets::DataSet2Layer converter(m_targetDataSource->getId());
917 
918  te::da::DataSetTypePtr dstPtr(dst.release());
919 
920  layer = converter(dstPtr);
921  layer->setTitle(themes[i].m_name);
922 
923  // Get Style
924  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(dstPtr.get());
925 
926  style = Convert2TerraLib5(geomProp->getGeometryType(), theme);
927 
928  if(theme->grouping().groupMode_ != TeNoGrouping)
929  layer->setGrouping(GetGrouping(theme));
930  }
931 
932  layer->setStyle(style->clone());
933 
934  delete style;
935 
936  te::qt::af::evt::LayerAdded evt(layer);
937 
939  }
940  }
941  catch(const te::da::Exception& e)
942  {
943  setCursor(Qt::ArrowCursor);
944  m_rollback = true;
945  QMessageBox::warning(this, tr("Warning"), e.what());
946  return;
947  }
948  catch(...)
949  {
950  setCursor(Qt::ArrowCursor);
951  m_rollback = true;
952  QMessageBox::warning(this, tr("Warning"), tr("Automatic layer creation failed!"));
953  return;
954  }
955 }
956 
958 {
959  QMessageBox::warning(this,
960  tr("TerraLib 4.x Converter"),
961  tr("This option is not implemented yet!\nWe will provide it soon!"));
962 }
963 
964 
965 
966 
967 
968 
969 
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:200
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
Definition: Utils.cpp:670
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
void setGrayChannel(SelectedChannel *c)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
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
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo &theme)
Definition: DataSource.cpp:230
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