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"
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"
78 #include <boost/filesystem.hpp>
79 #include <boost/lexical_cast.hpp>
83 #include <QMessageBox>
84 #include <QTableWidgetItem>
85 #include <QGridLayout>
88 #include <terralib4/kernel/TeLegendEntry.h>
89 #include <terralib4/kernel/TeRasterTransform.h>
90 #include <terralib4/kernel/TeTheme.h>
94 m_hasNonRaster(false),
97 m_ui(new Ui::TL4ConverterWizardForm)
105 m_connectorPage->setSubTitle(tr(
"Connect to a TerraLib 4.x database."));
123 m_resolveNamePage->setSubTitle(tr(
"Some layer names clashes with target data source names. Please, give a new name for the layers showed below"));
129 displayLayout->setContentsMargins(0,0,0,0);
132 labels <<
"" << tr(
"Source Names") << tr(
"Target Names");
137 m_finalPage->setSubTitle(tr(
"Select the TerraLib 4.x Layer converted that you want to create a TerraLib 5 Project Layer."));
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."));
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()));
159 this->setButton(QWizard::HelpButton, helpButton);
170 if(currentId() == PAGE_TERRALIB4_CONNECTOR)
171 return PAGE_LAYER_SELECTION;
173 if(currentId() == PAGE_LAYER_SELECTION)
176 return PAGE_DATASOURCE_SELECTOR;
179 return PAGE_RASTERFOLDER_SELECTOR;
181 return PAGE_LAYER_SELECTION;
184 if(currentId() == PAGE_DATASOURCE_SELECTOR)
187 return PAGE_RASTERFOLDER_SELECTOR;
189 return PAGE_NAME_RESOLVE_SELECTOR;
192 if(currentId() == PAGE_RASTERFOLDER_SELECTOR)
193 return PAGE_NAME_RESOLVE_SELECTOR;
195 if(currentId() == PAGE_NAME_RESOLVE_SELECTOR)
196 return PAGE_FINALPAGE;
198 if(currentId() == PAGE_FINALPAGE)
199 return PAGE_THEME_SELECTION;
206 int current_page_id = currentId();
208 if(current_page_id == PAGE_TERRALIB4_CONNECTOR)
210 if(!validTerraLib4Connection())
215 std::vector<std::string> layers = tl4Ds->
getTL4Layers();
217 std::vector<std::string> tables = tl4Ds->
getTL4Tables();
219 m_layerSelectionPage->setDatasets(layers, tables, rasters);
222 else if(current_page_id == PAGE_LAYER_SELECTION)
224 if(!validLayerSelection())
227 else if(current_page_id == PAGE_DATASOURCE_SELECTOR)
229 m_targetDataSource = *m_datasourceSelectorPage->getSelectorWidget()->getSelecteds().begin();
231 else if(current_page_id == PAGE_RASTERFOLDER_SELECTOR)
233 m_rasterFolderPath = m_rasterFolderSelectionPage->getPath();
235 else if(current_page_id == PAGE_NAME_RESOLVE_SELECTOR)
242 if(nextId() == PAGE_NAME_RESOLVE_SELECTOR)
246 std::vector<QListWidgetItem*> selectedLayerItems = m_layerSelectionPage->getCheckedItems();
248 m_resolveNameTableWidget->clearContents();
249 m_resolveNameTableWidget->setRowCount(selectedLayerItems.size());
256 bool hasConflicts =
false;
258 for(std::size_t i = 0; i < selectedLayerItems.size(); ++i)
260 std::string targetDatasetName = selectedLayerItems[i]->text().toStdString();
263 std::auto_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(selectedLayerItems[i]->text().toStdString()));
265 if(input_dataset_type->hasRaster())
267 QTableWidgetItem *conflictItem = 0;
269 if(boost::filesystem::exists(m_rasterFolderPath +
"/" + targetDatasetName +
".tif"))
273 conflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"delete"),
"");
277 conflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
280 conflictItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
281 m_resolveNameTableWidget->setItem(i, 0, conflictItem);
286 QTableWidgetItem *conflictItem = 0;
288 std::string targetDatasetNameAux = targetDatasetName;
290 if(tl5ds->getType() ==
"POSTGIS")
295 if(tl5ds->dataSetExists(targetDatasetNameAux))
299 conflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"delete"),
"");
303 conflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
306 conflictItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
307 m_resolveNameTableWidget->setItem(i, 0, conflictItem);
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);
314 QTableWidgetItem *newNameItem =
new QTableWidgetItem(targetDatasetName.c_str(), selectedLayerItems[i]->type());
315 m_resolveNameTableWidget->setItem(i, 2, newNameItem);
318 m_resolveNameTableWidget->resizeColumnsToContents();
319 m_resolveNameTableWidget->sortByColumn(1, Qt::AscendingOrder);
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"));
328 m_resolveNamePage->setTitle(tr(
"No Name Conflicts"));
329 m_resolveNamePage->setSubTitle(tr(
"You can change the layer names in the target data source"));
333 catch(
const te::da::Exception& e)
335 QMessageBox::warning(
this, tr(
"Warning"), e.what());
340 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not valid this page!"));
349 setCursor(Qt::WaitCursor);
351 std::map<std::string, std::string> connInfo = m_connectorPage->getConnInfo();
357 m_tl4Database->setConnectionInfo(connInfo);
358 m_tl4Database->open();
360 catch(
const te::da::Exception& e)
362 setCursor(Qt::ArrowCursor);
363 QMessageBox::warning(
this, tr(
"Warning"), e.what());
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."));
372 setCursor(Qt::ArrowCursor);
379 if(!m_layerSelectionPage->hasChecked())
382 QMessageBox::warning(
this,
384 tr(
"Please, select at least one layer for conversion!"));
388 std::vector<std::string> layersNames = m_layerSelectionPage->getChecked();
391 m_hasNonRaster =
false;
398 for(std::size_t i = 0; i < layersNames.size(); ++i)
400 std::auto_ptr<te::da::DataSetType> dst(m_tl4Database->getDataSetType(layersNames[i]));
405 m_hasNonRaster =
true;
408 catch(
const te::da::Exception& e)
410 QMessageBox::warning(
this, tr(
"Warning"), e.what());
415 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not valid layer selection!"));
424 bool hasConflict =
false;
434 catch(
const te::da::Exception& e)
436 QMessageBox::warning(
this, tr(
"Warning"), e.what());
441 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not connect to TerraLib 5 data source!"));
445 int nrows = m_resolveNameTableWidget->rowCount();
447 for(
int i = 0; i != nrows; ++i)
450 QTableWidgetItem* item_source = m_resolveNameTableWidget->item(i, 1);
455 std::string sourceName = item_source->text().toStdString();
458 QTableWidgetItem* item_target = m_resolveNameTableWidget->item(i, 2);
463 std::string targetName = item_target->text().toStdString();
470 std::auto_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(sourceName));
472 if(input_dataset_type->hasRaster())
475 if(boost::filesystem::exists(m_rasterFolderPath +
"/" + targetName +
".tif"))
478 QTableWidgetItem *nonconflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"delete"),
"");
482 QTableWidgetItem *nonconflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
484 m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
490 std::string targetDatasetNameAux = targetName;
492 if(tl5ds->getType() ==
"POSTGIS")
497 if(tl5ds->dataSetExists(targetDatasetNameAux))
500 QTableWidgetItem *nonconflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"delete"),
"");
504 QTableWidgetItem *nonconflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
506 m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
510 catch(
const te::da::Exception& e)
512 QMessageBox::warning(
this, tr(
"Warning"), e.what());
517 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not valid layer names in target data source!"));
524 QString errMsg(tr(
"There still have name conflicts. Please, resolve the indicated conflicts before continue!"));
526 QMessageBox::warning(
this, tr(
"TerraLib 4.x Converter"), errMsg);
536 int rowCount = m_resolveNameTableWidget->rowCount();
538 for(
int i = 0; i < rowCount; ++i)
540 QString targetNameInTable = m_resolveNameTableWidget->item(i, 2)->text();
542 std::string aux = targetNameInTable.toLatin1();
544 if(targetName.c_str() == aux)
553 int rowCount = m_resolveNameTableWidget->rowCount();
555 for(
int i = 0; i < rowCount; ++i)
557 QString oName = m_resolveNameTableWidget->item(i, 1)->text();
559 std::string aux = oName.toLatin1();
561 if(originalName.c_str() == aux)
580 if(!validLayerNames())
587 std::vector<std::pair<std::string, std::string> > problematicDatasets;
588 std::vector<std::string> successfulDatasets;
597 catch(
const te::da::Exception& e)
600 QMessageBox::warning(
this, tr(
"Warning"), e.what());
606 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not connect to TerraLib 5 data source!"));
610 int nrows = m_resolveNameTableWidget->rowCount();
620 for(
int i = 0; i != nrows; ++i)
623 QTableWidgetItem* item_source = m_resolveNameTableWidget->item(i, 1);
631 std::string sourceName = item_source->text().toLatin1();
634 QTableWidgetItem* item_target = m_resolveNameTableWidget->item(i, 2);
642 std::string targetName = item_target->text().toLatin1();
649 std::auto_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(sourceName));
651 if(input_dataset_type->hasRaster())
654 std::auto_ptr<te::da::DataSet> ds = m_tl4Database->getDataSet(sourceName);
656 std::auto_ptr<te::rst::Raster> raster = ds->getRaster(
"Raster");
658 std::string newName = m_rasterFolderPath +
"/" + targetName +
".tif";
662 successfulDatasets.push_back(targetName);
667 input_dataset_type->setName(targetName);
669 std::auto_ptr<te::da::DataSetTypeConverter> dt_adapter(
new te::da::DataSetTypeConverter(input_dataset_type.get(), tl5ds->getCapabilities()));
671 std::auto_ptr<te::da::DataSet> ds(m_tl4Database->getDataSet(sourceName));
675 const std::vector<std::vector<std::size_t> >& indexes = dt_adapter->getConvertedPropertyIndexes();
676 const std::vector<te::da::AttributeConverter>& funcs = dt_adapter->getConverters();
690 for(std::size_t i = 0; i < type->
size(); ++i)
697 ds_adapter->add(p->
getName(), p->
getType(), indexes[i], conversor);
701 ds_adapter->add(p->
getName(), p->
getType(), indexes[i], funcs[i]);
709 ds_adapter->setSRID(finalSrid);
711 std::map<std::string, std::string> opt;
713 ds_adapter->moveBeforeFirst();
715 te::da::Create(tl5ds.get(), dt_adapter->getResult(), ds_adapter.get(), opt);
717 successfulDatasets.push_back(targetName);
722 std::pair<std::string, std::string> dproblem;
723 dproblem.first = sourceName;
724 dproblem.second = e.
what();
726 problematicDatasets.push_back(dproblem);
728 catch(std::exception& e)
730 std::pair<std::string, std::string> dproblem;
731 dproblem.first = sourceName;
732 dproblem.second = e.what();
734 problematicDatasets.push_back(dproblem);
738 std::pair<std::string, std::string> dproblem;
739 dproblem.first = sourceName;
740 dproblem.second =
TE_TR(
"unknown problem in conversion!");
742 problematicDatasets.push_back(dproblem);
755 if(!problematicDatasets.empty())
757 QString error(tr(
"Some TerraLib 4.x Layers could not be converted: \n\n"));
760 for(std::size_t i = 0; i < problematicDatasets.size(); ++i)
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"));
767 QMessageBox message(QMessageBox::Warning, tr(
"TerraLib 4.x Converter"), error, QMessageBox::Ok,
this);
768 message.setDetailedText(details);
776 m_finalPage->setDataSets(successfulDatasets);
778 std::vector<::terralib4::ThemeInfo> themes = tl4Ds->
getTL4Themes();
780 std::vector<::terralib4::ThemeInfo> convertedThemes;
781 for(std::size_t i = 0; i < themes.size(); ++i)
783 for(std::size_t j = 0; j < successfulDatasets.size(); ++j)
785 if(themes[i].m_layerName == getOriginalName(successfulDatasets[j]))
787 convertedThemes.push_back(themes[i]);
792 m_themeSelection->setThemes(convertedThemes);
801 std::vector<std::string> selected = m_finalPage->getSelected();
810 catch(
const te::da::Exception& e)
813 QMessageBox::warning(
this, tr(
"Warning"), e.what());
819 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not connect to TerraLib 5 data source!"));
826 setCursor(Qt::WaitCursor);
828 for(std::size_t i = 0; i < selected.size(); ++i)
832 std::auto_ptr<te::da::DataSetType> sourceDt = m_tl4Database->getDataSetType(getOriginalName(selected[i]));
834 if(sourceDt->hasRaster())
836 std::map<std::string, std::string> connInfo;
837 connInfo[
"URI"] = m_rasterFolderPath +
"/" + selected[i] +
".tif";
845 std::auto_ptr<te::da::DataSetType> dsType = outDataSource->getDataSetType(selected[i]);
849 layer = converter(dt);
857 std::vector<::terralib4::ThemeInfo> themes = m_themeSelection->getThemes();
859 for(std::size_t i = 0; i < themes.size(); ++i)
863 std::string newName = getNewName(themes[i].m_layerName);
865 std::auto_ptr<te::da::DataSetType> sourceDt = m_tl4Database->getDataSetType(newName);
873 if(sourceDt->hasRaster())
875 std::map<std::string, std::string> connInfo;
876 connInfo[
"URI"] = m_rasterFolderPath +
"/" + themes[i].
m_name +
".tif";
884 if(theme->grouping().groupMode_ != TeNoGrouping)
892 std::string band =
"0";
916 std::auto_ptr<te::da::DataSetType> dst = outDataSource->getDataSetType(getNewName(themes[i].m_layerName));
922 layer = converter(dstPtr);
923 layer->setTitle(themes[i].m_name);
930 if(theme->grouping().groupMode_ != TeNoGrouping)
934 layer->setStyle(style->
clone());
943 catch(
const te::da::Exception& e)
945 setCursor(Qt::ArrowCursor);
947 QMessageBox::warning(
this, tr(
"Warning"), e.what());
952 setCursor(Qt::ArrowCursor);
954 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Automatic layer creation failed!"));
961 QMessageBox::warning(
this,
962 tr(
"TerraLib 4.x Converter"),
963 tr(
"This option is not implemented yet!\nWe will provide it soon!"));
virtual void setName(const std::string &name)
It sets the constraint name.
Property * getProperty(std::size_t i) const
It returns the i-th property.
This event signals that a new layer was created.
A selected channel to be display.
te::color::RGBAColor Convert2TerraLib5(TeColor color)
std::auto_ptr< te::qt::widgets::DataSourceSelectorWizardPage > m_datasourceSelectorPage
boost::shared_ptr< DataSetType > DataSetTypePtr
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
CharEncoding
Supported charsets (character encoding).
std::string getNewName(const std::string &originalName)
te::map::Grouping * GetGrouping(TeTheme *theme)
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...
boost::shared_ptr< DataSource > DataSourcePtr
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
A class that models the description of a dataset.
std::auto_ptr< Ui::TL4ConverterWizardForm > m_ui
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.
bool validateCurrentPage()
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
te::se::ChannelSelection * getChannelSelection() const
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo theme)
void setGrayChannel(SelectedChannel *c)
#define TE_TR(message)
It marks a string in order to get translated.
It models a property definition.
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.
std::auto_ptr< QWizardPage > m_resolveNamePage
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...
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()
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
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.
std::vector<::terralib4::ThemeInfo > getTL4Themes()
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
It describes a primary key (pk) constraint.
void setColorMap(ColorMap *c)
void setSourceChannelName(const std::string &name)
int getLayerSRID(const std::string &layerName)
std::auto_ptr< QTableWidget > m_resolveNameTableWidget
bool validTerraLib4Connection()
TL4ConverterWizard(QWidget *parent=0, Qt::WindowFlags f=0)
bool validLayerSelection()
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
te::se::ColorMap * GetRasterGrouping(TeTheme *theme)
SelectedChannel * getGrayChannel() const
std::auto_ptr< TL4FinalPageWizardPage > m_finalPage
std::string getOriginalName(const std::string &targetName)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::auto_ptr< TL4LayerSelectionWizardPage > m_layerSelectionPage
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
std::auto_ptr< TL4RasterFolderSelectionWizardPage > m_rasterFolderSelectionPage
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.