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((
int)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((
int)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((
int)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((
int)i, 1, oldNameItem);
314 QTableWidgetItem *newNameItem =
new QTableWidgetItem(targetDatasetName.c_str(), selectedLayerItems[i]->type());
315 m_resolveNameTableWidget->setItem((
int)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"))
481 QTableWidgetItem *nonconflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
483 m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
489 std::string targetDatasetNameAux = targetName;
491 if(tl5ds->getType() ==
"POSTGIS")
496 if(tl5ds->dataSetExists(targetDatasetNameAux))
502 QTableWidgetItem *nonconflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
504 m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
508 catch(
const te::da::Exception& e)
510 QMessageBox::warning(
this, tr(
"Warning"), e.what());
515 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not valid layer names in target data source!"));
522 QString errMsg(tr(
"There still have name conflicts. Please, resolve the indicated conflicts before continue!"));
524 QMessageBox::warning(
this, tr(
"TerraLib 4.x Converter"), errMsg);
534 int rowCount = m_resolveNameTableWidget->rowCount();
536 for(
int i = 0; i < rowCount; ++i)
538 QString targetNameInTable = m_resolveNameTableWidget->item(i, 2)->text();
540 std::string aux = targetNameInTable.toLatin1();
542 if(targetName.c_str() == aux)
551 int rowCount = m_resolveNameTableWidget->rowCount();
553 for(
int i = 0; i < rowCount; ++i)
555 QString oName = m_resolveNameTableWidget->item(i, 1)->text();
557 std::string aux = oName.toLatin1();
559 if(originalName.c_str() == aux)
578 if(!validLayerNames())
585 std::vector<std::pair<std::string, std::string> > problematicDatasets;
586 std::vector<std::string> successfulDatasets;
595 catch(
const te::da::Exception& e)
598 QMessageBox::warning(
this, tr(
"Warning"), e.what());
604 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not connect to TerraLib 5 data source!"));
608 int nrows = m_resolveNameTableWidget->rowCount();
618 for(
int i = 0; i != nrows; ++i)
621 QTableWidgetItem* item_source = m_resolveNameTableWidget->item(i, 1);
629 std::string sourceName = item_source->text().toLatin1();
632 QTableWidgetItem* item_target = m_resolveNameTableWidget->item(i, 2);
640 std::string targetName = item_target->text().toLatin1();
647 std::auto_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(sourceName));
649 if(input_dataset_type->hasRaster())
652 std::auto_ptr<te::da::DataSet> ds = m_tl4Database->getDataSet(sourceName);
654 std::auto_ptr<te::rst::Raster> raster = ds->getRaster(
"Raster");
656 std::string newName = m_rasterFolderPath +
"/" + targetName +
".tif";
660 successfulDatasets.push_back(targetName);
665 input_dataset_type->setName(targetName);
667 std::auto_ptr<te::da::DataSetTypeConverter> dt_adapter(
new te::da::DataSetTypeConverter(input_dataset_type.get(), tl5ds->getCapabilities()));
669 std::auto_ptr<te::da::DataSet> ds(m_tl4Database->getDataSet(sourceName));
673 const std::vector<std::vector<std::size_t> >& indexes = dt_adapter->getConvertedPropertyIndexes();
674 const std::vector<te::da::AttributeConverter>& funcs = dt_adapter->getConverters();
688 for(std::size_t i = 0; i < type->
size(); ++i)
695 ds_adapter->add(p->
getName(), p->
getType(), indexes[i], conversor);
699 ds_adapter->add(p->
getName(), p->
getType(), indexes[i], funcs[i]);
709 std::map<std::string, std::string> opt;
711 ds_adapter->moveBeforeFirst();
713 te::da::Create(tl5ds.get(), dt_adapter->getResult(), ds_adapter.get(), opt);
715 successfulDatasets.push_back(targetName);
720 std::pair<std::string, std::string> dproblem;
721 dproblem.first = sourceName;
722 dproblem.second = e.
what();
724 problematicDatasets.push_back(dproblem);
726 catch(std::exception& e)
728 std::pair<std::string, std::string> dproblem;
729 dproblem.first = sourceName;
730 dproblem.second = e.what();
732 problematicDatasets.push_back(dproblem);
736 std::pair<std::string, std::string> dproblem;
737 dproblem.first = sourceName;
738 dproblem.second =
TE_TR(
"unknown problem in conversion!");
740 problematicDatasets.push_back(dproblem);
753 if(!problematicDatasets.empty())
755 QString error(tr(
"Some TerraLib 4.x Layers could not be converted: \n\n"));
758 for(std::size_t i = 0; i < problematicDatasets.size(); ++i)
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"));
765 QMessageBox message(QMessageBox::Warning, tr(
"TerraLib 4.x Converter"), error, QMessageBox::Ok,
this);
766 message.setDetailedText(details);
774 m_finalPage->setDataSets(successfulDatasets);
776 std::vector<::terralib4::ThemeInfo> themes = tl4Ds->
getTL4Themes();
778 std::vector<::terralib4::ThemeInfo> convertedThemes;
779 for(std::size_t i = 0; i < themes.size(); ++i)
781 for(std::size_t j = 0; j < successfulDatasets.size(); ++j)
783 if(themes[i].m_layerName == getOriginalName(successfulDatasets[j]))
785 convertedThemes.push_back(themes[i]);
790 m_themeSelection->setThemes(convertedThemes);
799 std::vector<std::string> selected = m_finalPage->getSelected();
808 catch(
const te::da::Exception& e)
811 QMessageBox::warning(
this, tr(
"Warning"), e.what());
817 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not connect to TerraLib 5 data source!"));
824 setCursor(Qt::WaitCursor);
826 for(std::size_t i = 0; i < selected.size(); ++i)
830 std::auto_ptr<te::da::DataSetType> sourceDt = m_tl4Database->getDataSetType(getOriginalName(selected[i]));
832 if(sourceDt->hasRaster())
834 std::map<std::string, std::string> connInfo;
835 connInfo[
"URI"] = m_rasterFolderPath +
"/" + selected[i] +
".tif";
843 std::auto_ptr<te::da::DataSetType> dsType = outDataSource->getDataSetType(selected[i]);
847 layer = converter(dt);
855 std::vector<::terralib4::ThemeInfo> themes = m_themeSelection->getThemes();
857 for(std::size_t i = 0; i < themes.size(); ++i)
861 std::string newName = getNewName(themes[i].m_layerName);
863 std::auto_ptr<te::da::DataSetType> sourceDt = m_tl4Database->getDataSetType(newName);
871 if(sourceDt->hasRaster())
873 std::map<std::string, std::string> connInfo;
874 connInfo[
"URI"] = m_rasterFolderPath +
"/" + themes[i].
m_name +
".tif";
882 if(theme->grouping().groupMode_ != TeNoGrouping)
890 std::string band =
"0";
914 std::auto_ptr<te::da::DataSetType> dst = outDataSource->getDataSetType(getNewName(themes[i].m_layerName));
920 layer = converter(dstPtr);
921 layer->setTitle(themes[i].m_name);
928 if(theme->grouping().groupMode_ != TeNoGrouping)
932 layer->setStyle(style->
clone());
941 catch(
const te::da::Exception& e)
943 setCursor(Qt::ArrowCursor);
945 QMessageBox::warning(
this, tr(
"Warning"), e.what());
950 setCursor(Qt::ArrowCursor);
952 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Automatic layer creation failed!"));
959 QMessageBox::warning(
this,
960 tr(
"TerraLib 4.x Converter"),
961 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.
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
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
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
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo &theme)
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.