27 #include "../../core/logger/Logger.h" 28 #include "../../core/filesystem/FileSystem.h" 29 #include "../../core/translator/Translator.h" 30 #include "../../common/progress/ProgressManager.h" 31 #include "../../common/StringUtils.h" 32 #include "../../common/progress/TaskProgress.h" 33 #include "../../dataaccess/dataset/DataSetAdapter.h" 34 #include "../../dataaccess/dataset/DataSetTypeConverter.h" 35 #include "../../dataaccess/datasource/DataSource.h" 36 #include "../../dataaccess/datasource/DataSourceCapabilities.h" 37 #include "../../dataaccess/datasource/DataSourceInfoManager.h" 38 #include "../../dataaccess/datasource/DataSourceFactory.h" 39 #include "../../dataaccess/datasource/DataSourceManager.h" 40 #include "../../dataaccess/datasource/DataSourceTransactor.h" 41 #include "../../dataaccess/utils/Utils.h" 42 #include "../../maptools/QueryLayer.h" 43 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h" 44 #include "../../qt/widgets/layer/utils/DataSet2Layer.h" 45 #include "../../qt/widgets/progress/ProgressViewerDialog.h" 46 #include "../../qt/widgets/utils/FileDialog.h" 48 #include "../../vp/Merge.h" 51 #include "ui_MergeDialogForm.h" 53 #include "../../vp/AlgorithmParams.h" 54 #include "../../vp/ComplexData.h" 55 #include "../../vp/InputParams.h" 59 #include <QFileDialog> 60 #include <QMessageBox> 63 #include <boost/filesystem.hpp> 64 #include <boost/uuid/random_generator.hpp> 65 #include <boost/uuid/uuid_io.hpp> 69 te::vp::MergeDialog::MergeDialog(
QWidget* parent, Qt::WindowFlags f)
71 m_ui(new
Ui::MergeDialogForm),
78 connect(m_ui->m_targetLayerComboBox, SIGNAL(currentIndexChanged(
int)),
this, SLOT(onTargetLayerComboBoxChanged(
int)));
79 connect(m_ui->m_originLayerComboBox, SIGNAL(currentIndexChanged(
int)),
this, SLOT(onOriginLayerComboBoxChanged(
int)));
80 connect(m_ui->m_okPushButton, SIGNAL(clicked()),
this, SLOT(onOkPushButtonClicked()));
81 connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()),
this, SLOT(onTargetDatasourceToolButtonPressed()));
82 connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()),
this, SLOT(onTargetFileToolButtonPressed()));
83 connect(m_ui->m_outputGroupBox, SIGNAL(toggled(
bool)),
this, SLOT(onOutputGroupBoxToggled(
bool)));
86 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"vp-merge-hint").pixmap(112,48));
87 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
89 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
90 m_ui->m_helpPushButton->setPageReference(
"plugins/vp/vp_merge.html");
93 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
100 std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
102 while (it != layers.end())
104 std::unique_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
105 if (dsType->hasGeom())
122 std::list<te::map::AbstractLayerPtr>::iterator it =
m_layers.begin();
128 m_ui->m_targetLayerComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant::fromValue(*it));
134 QVariant varLayer =
m_ui->m_targetLayerComboBox->itemData(
m_ui->m_targetLayerComboBox->currentIndex(), Qt::UserRole);
142 int currIndex =
m_ui->m_targetLayerComboBox->currentIndex();
144 std::list<te::map::AbstractLayerPtr>::iterator it =
m_layers.begin();
150 m_ui->m_originLayerComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant::fromValue(*it));
154 m_ui->m_originLayerComboBox->removeItem(currIndex);
158 QVariant varLayer =
m_ui->m_originLayerComboBox->itemData(
m_ui->m_originLayerComboBox->currentIndex(), Qt::UserRole);
171 m_ui->m_newLayerNameLineEdit->clear();
172 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
176 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
178 if(dsPtrList.empty())
181 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
183 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
192 m_ui->m_newLayerNameLineEdit->clear();
193 m_ui->m_repositoryLineEdit->clear();
201 QMessageBox::warning(
this, tr(
"File information"), ex.
what());
205 m_ui->m_repositoryLineEdit->setText(fileDialog.
getPath().c_str());
206 m_ui->m_newLayerNameLineEdit->setText(fileDialog.
getFileName().c_str());
209 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
216 bool isUpdate = !
m_ui->m_outputGroupBox->isChecked();
220 int res = QMessageBox::question(
this, tr(
"Merge"), tr(
"The origin layer information will be inserted into the target layer.\n Do you want to continue the operation?"), QMessageBox::Yes, QMessageBox::Cancel);
221 if(res == QMessageBox::Cancel)
225 if (
m_ui->m_targetLayerComboBox->currentText().isEmpty() ||
m_ui->m_originLayerComboBox->currentText().isEmpty())
227 QMessageBox::information(
this, tr(
"Merge"), tr(
"It is necessary at least two layer to operate!"));
232 if (!isUpdate &&
m_ui->m_repositoryLineEdit->text().isEmpty())
234 QMessageBox::information(
this, tr(
"Merge"), tr(
"Select a repository for the resulting layer!"));
238 if (!isUpdate &&
m_ui->m_newLayerNameLineEdit->text().isEmpty())
240 QMessageBox::information(
this, tr(
"Merge"), tr(
"Define a name for the resulting layer!"));
247 if (targetSrid <= 0 || originSrid <= 0)
249 QMessageBox::information(
this, tr(
"Merge"), tr(
"All layers must have SRID!"));
255 std::string outputdataset =
m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
273 task->setTotalSteps(2);
275 std::string logMsg =
TE_TR(
"Getting Target Layer Data...");
276 task->setMessage(logMsg);
280 std::unique_ptr<te::da::DataSourceTransactor> targetTransactor = targetSource->getTransactor();
286 logMsg =
TE_TR(
"Getting Origin Layer Data...");
287 task->setMessage(logMsg);
291 std::unique_ptr<te::da::DataSourceTransactor> originTransactor = originSource->getTransactor();
293 std::unique_ptr<te::da::DataSet> originConnDs;
301 originConnDs = originTransactor->query(q);
318 std::unique_ptr<te::da::DataSet> targetDs(targetAdapter);
319 std::unique_ptr<te::da::DataSet> originDs(originAdapter);
334 std::vector<te::vp::InputParams> params;
335 params.push_back(inputParam);
336 params.push_back(mergeParam);
342 std::map<std::string, te::dt::AbstractData*> specificParams;
343 specificParams[
"ATTRIBUTES"] = attrs;
344 specificParams[
"ISUPDATE"] = isUp;
352 std::string ogrDsinfo(
"file://");
353 boost::filesystem::path ogrUri;
357 ogrUri =
m_ui->m_repositoryLineEdit->text().toUtf8().data();
361 QMessageBox::information(
this, tr(
"Merge"), tr(
"Output file already exists. Remove it or select a new name and try again."));
365 std::size_t idx = outputdataset.find(
".");
366 if (idx != std::string::npos)
367 outputdataset = outputdataset.substr(0, idx);
369 ogrDsinfo += ogrUri.string();
374 if (auxSource->dataSetExists(outputdataset))
376 QMessageBox::information(
this, tr(
"Merge"), tr(
"There is already a dataset with the requested name in the output data source. Remove it or select a new name and try again."));
387 QMessageBox::information(
this, tr(
"Merge"), tr(
"The selected output datasource can not be accessed."));
390 if (auxSource->dataSetExists(outputdataset))
392 QMessageBox::information(
this, tr(
"Merge"), tr(
"Dataset already exists. Remove it or select a new name and try again."));
418 boost::uuids::basic_random_generator<boost::mt19937> gen;
419 boost::uuids::uuid u = gen();
420 std::string
id = boost::uuids::to_string(u);
423 ds->setConnInfo(ogrDsinfo);
424 ds->setTitle(ogrUri.stem().string());
425 ds->setAccessDriver(
"OGR");
427 ds->setDescription(ogrUri.string());
455 QMessageBox::information(
this, tr(
"Merge"), tr(
"Successfully Merge!"));
460 QMessageBox::warning(
this, tr(
"Merge"), e.
what());
463 catch (
const std::exception& e)
465 QMessageBox::warning(
this, tr(
"Merge"), e.what());
480 QVariant varLayer =
m_ui->m_targetLayerComboBox->itemData(index, Qt::UserRole);
483 m_ui->m_originLayerComboBox->clear();
494 m_ui->m_outputGroupBox->setChecked(
true);
500 QVariant varLayer =
m_ui->m_originLayerComboBox->itemData(index, Qt::UserRole);
510 m_ui->m_attributeTableWidget->setRowCount(0);
512 std::unique_ptr<te::da::DataSetType> targetSchema;
513 std::unique_ptr<te::da::DataSetType> originSchema;
525 std::vector<te::dt::Property*> originProps = originSchema->getProperties();
527 std::vector<te::dt::Property*> targetProps = targetSchema->getProperties();
530 for (std::size_t i = 0; i < targetProps.size(); ++i)
535 m_ui->m_attributeTableWidget->insertRow(rowCount);
537 QTableWidgetItem* item =
new QTableWidgetItem(targetProps[i]->getName().c_str());
538 item->setFlags(Qt::ItemIsEnabled);
539 m_ui->m_attributeTableWidget->setItem(rowCount, 0, item);
542 cb->setParent(
m_ui->m_attributeTableWidget);
544 m_ui->m_attributeTableWidget->setCellWidget(rowCount, 1, cb);
548 std::size_t fSize = targetProps.size();
549 std::size_t sSize = originProps.size();
550 if (targetProps.size() < originProps.size())
552 int diference =
static_cast<int>(sSize - fSize);
554 for (
int i = 0; i < diference; ++i)
557 m_ui->m_attributeTableWidget->insertRow(rowCount);
559 QTableWidgetItem* item =
new QTableWidgetItem();
560 m_ui->m_attributeTableWidget->setItem(rowCount, 0, item);
563 cb->setParent(
m_ui->m_attributeTableWidget);
565 m_ui->m_attributeTableWidget->setCellWidget(rowCount, 1, cb);
569 #if (QT_VERSION >= 0x050000) 570 m_ui->m_attributeTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
572 m_ui->m_attributeTableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
578 QComboBox* result =
new QComboBox();
584 for (std::size_t i = 0; i < props.size(); ++i)
586 if (mainProp->
getType() == props[i]->getType())
593 result->addItem(props[i]->getName().c_str());
600 result->setCurrentIndex(sameName);
608 QComboBox* result =
new QComboBox();
611 for (std::size_t i = 0; i < props.size(); ++i)
615 result->addItem(props[i]->getName().c_str());
624 std::vector<std::pair<std::string, std::string> > result;
626 int rowCount =
m_ui->m_attributeTableWidget->rowCount();
628 for (std::size_t i = 0; i < static_cast<std::size_t>(rowCount); ++i)
630 std::string fp =
m_ui->m_attributeTableWidget->item(static_cast<int>(i), 0)->text().toUtf8().data();;
632 QComboBox* cb =
dynamic_cast<QComboBox*
>(
m_ui->m_attributeTableWidget->cellWidget(static_cast<int>(i), 1));
634 std::string sp = cb->currentText().toUtf8().data();
636 result.push_back(std::pair<std::string, std::string>(fp, sp));
648 QMessageBox::warning(
this, tr(
"Merge"), tr(
"To use target layer as output (update), it must be a DataSet Layer!"));
649 m_ui->m_outputGroupBox->setChecked(
true);
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
boost::shared_ptr< DataSetType > DataSetTypePtr
void updateTargetLayerComboBox()
boost::shared_ptr< DataSource > DataSourcePtr
QComboBox * getAllAttributesComboBox(std::vector< te::dt::Property * > props)
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.
virtual const char * what() const
It outputs the exception message.
This class can be used to inform the progress of a task.
void onTargetFileToolButtonPressed()
void setOutputDataSetName(const std::string &outputDataSetName)
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
A layer resulting from a query.
std::list< te::map::AbstractLayerPtr > m_layers
List of layers.
static te::dt::Date ds(2010, 01, 01)
std::unique_ptr< Ui::MergeDialogForm > m_ui
A class that informs what the dataset implementation of a given data source can perform.
QComboBox * getPossibleAttributesComboBox(te::dt::Property *mainProp, std::vector< te::dt::Property * > props)
#define TE_TR(message)
It marks a string in order to get translated.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
void setOutputDataSource(te::da::DataSourcePtr outputDataSource)
#define TE_LOG_INFO(message)
Use this tag in order to log a message to the TerraLib default logger with the INFO level...
te::map::AbstractLayerPtr m_originSelectedLayer
Second layer selected.
It models a property definition.
void Union(const Envelope &rhs)
It updates the envelop with coordinates of another envelope.
An converter for DataSetType.
void onOriginLayerComboBoxChanged(int index)
A template for complex data types.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
void onTargetDatasourceToolButtonPressed()
static te::dt::TimeDuration dt(20, 30, 50, 11)
void onOutputGroupBoxToggled(bool on)
te::map::AbstractLayerPtr m_outputLayer
Generated Layer.
void onCancelPushButtonClicked()
A dialog merge operation.
te::da::Select * getQuery() const
void updateOriginLayerComboBox()
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
A Select models a query to be used when retrieving data from a DataSource.
int getType() const
It returns the property data type.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
void onOkPushButtonClicked()
std::vector< std::pair< std::string, std::string > > getTablePropertiesNames()
te::map::AbstractLayerPtr m_targetSelectedLayer
Target layer selected.
A class that represents a data source component.
void setInputParams(const std::vector< te::vp::InputParams > &setInputParams)
te::map::AbstractLayerPtr getLayer()
Get the generated layer.
bool executeMemory(te::vp::AlgorithmParams *mainParams)
te::da::DataSourceInfoPtr m_outputDatasource
DataSource information.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
const DataSetCapabilities & getDataSetCapabilities() const
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void setSpecificParams(const std::map< std::string, te::dt::AbstractData * > &specificParams)
void updateAttrTableWidget()
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void onTargetLayerComboBoxChanged(int index)
const std::string & getName() const
It returns the property name.