VectorToVectorDialog.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/attributefill/VectorToVectorDialog.cpp
22 
23  \brief Raster to vector attributefill dialog.
24 */
25 
26 // TerraLib
27 #include "../../attributefill/Enums.h"
28 #include "../../attributefill/Utils.h"
29 #include "../../attributefill/VectorToVectorOp.h"
30 #include "../../attributefill/VectorToVectorMemory.h"
31 #include "../../core/filesystem/FileSystem.h"
32 #include "../../core/logger/Logger.h"
33 #include "../../core/translator/Translator.h"
34 #include "../../common/Exception.h"
35 #include "../../common/progress/ProgressManager.h"
36 #include "../../common/StringUtils.h"
37 #include "../../dataaccess/dataset/DataSetType.h"
38 #include "../../dataaccess/datasource/DataSourceCapabilities.h"
39 #include "../../dataaccess/datasource/DataSourceInfo.h"
40 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
41 #include "../../dataaccess/datasource/DataSourceFactory.h"
42 #include "../../dataaccess/datasource/DataSourceManager.h"
43 #include "../../dataaccess/utils/Utils.h"
44 #include "../../datatype/Enums.h"
45 #include "../../datatype/Property.h"
46 #include "../../geometry/GeometryProperty.h"
47 #include "../../maptools/AbstractLayer.h"
48 #include "../../qt/af/Utils.h"
49 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
50 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
51 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
52 #include "../../qt/widgets/Utils.h"
53 #include "../../qt/widgets/utils/DoubleListWidget.h"
54 #include "../../qt/widgets/utils/FileDialog.h"
55 #include "../../statistics/core/Utils.h"
56 #include "../Config.h"
57 #include "VectorToVectorDialog.h"
58 #include "ui_VectorToVectorDialogForm.h"
59 
60 // Qt
61 #include <QDialog>
62 #include <QDialogButtonBox>
63 #include <QFileDialog>
64 #include <QList>
65 #include <QListWidget>
66 #include <QListWidgetItem>
67 #include <QMessageBox>
68 
69 #if QT_VERSION >= 0x050000
70 #include <QStandardPaths>
71 #else
72 #include <QDesktopServices>
73 #endif
74 
75 // Boost
76 #include <boost/algorithm/string.hpp>
77 #include <boost/filesystem.hpp>
78 #include <boost/lexical_cast.hpp>
79 #include <boost/uuid/random_generator.hpp>
80 #include <boost/uuid/uuid_io.hpp>
81 
83  : QDialog(parent, f),
84  m_ui(new Ui::VectorToVectorDialogForm),
85  m_layers(std::list<te::map::AbstractLayerPtr>()),
86  m_outputAttributes(std::vector<std::string>()),
87  m_path(""),
88  m_toFile(false)
89 {
90  // add controls
91  m_ui->setupUi(this);
92 
94 
95  // add icons
96  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("attributefill-vector2vector-hint").pixmap(112,48));
97  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
98 
99  connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()), this, SLOT(onTargetDatasourceToolButtonPressed()));
100  connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()), this, SLOT(onTargetFileToolButtonPressed()));
101 
102  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
103  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
104 
105  connect(m_ui->m_fromLayerComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onFromLayerComboBoxCurrentIndexChanged(int)));
106  connect(m_ui->m_toLayerComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onToLayerComboBoxCurrentIndexChanged(int)));
107 
108  connect(m_ui->m_statisticsListWidget, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(onStatisticsListWidgetItemPressed(QListWidgetItem*)));
109 
110  connect(m_ui->m_selectAllComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectAllComboBoxChanged(int)));
111  connect(m_ui->m_rejectAllComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onRejectAllComboBoxChanged(int)));
112 
113  connect(m_ui->m_selectAttrToolButton, SIGNAL(pressed()), this, SLOT(onSelectAttrToolButtonPressed()));
114 
115  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
116  m_ui->m_helpPushButton->setPageReference("plugins/attributefill/attrfill_vector_to_vector.html");
117 }
118 
120 
121 void te::attributefill::VectorToVectorDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
122 {
123  m_layers = layers;
124 
125  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
126 
127  while(it != m_layers.end())
128  {
129  std::unique_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
130  if(dsType->hasGeom())
131  {
132  std::string layerName = it->get()->getTitle();
133  std::string layerId = it->get()->getId();
134 
135  m_ui->m_toLayerComboBox->addItem(QString(layerName.c_str()), QVariant(layerId.c_str()));
136  m_ui->m_fromLayerComboBox->addItem(QString(layerName.c_str()), QVariant(layerId.c_str()));
137  }
138  ++it;
139  }
140 
141  if(m_ui->m_fromLayerComboBox->count() > 1)
142  {
143  m_ui->m_fromLayerComboBox->setCurrentIndex(0);
145  m_ui->m_toLayerComboBox->setCurrentIndex(1);
146  }
147 }
148 
150 {
151  return m_outLayer;
152 }
153 
155 {
156  te::map::DataSetLayerPtr fromLayer(dynamic_cast<te::map::DataSetLayer*>(getCurrentFromLayer().get()));
157 
158  if(!fromLayer)
159  {
160  QMessageBox::warning(this, tr("VectorToVector"), tr("\"From\" layer invalid!"));
161  return;
162  }
163 
164  te::map::DataSetLayerPtr toLayer(dynamic_cast<te::map::DataSetLayer*>(getCurrentToLayer().get()));
165 
166  if(!toLayer)
167  {
168  QMessageBox::warning(this, tr("VectorToVector"), tr("\"To\" layer invalid!"));
169  return;
170  }
171 
172  if(m_ui->m_repositoryLineEdit->text().isEmpty())
173  {
174  QMessageBox::warning(this, tr("VectorToVector"), tr("Define a repository for the result."));
175  return;
176  }
177 
178  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
179  {
180  QMessageBox::warning(this, tr("VectorToVector"), tr("Define a name for the resulting layer."));
181  return;
182  }
183 
184  std::unique_ptr<te::da::DataSetType> fromSchema = fromLayer->getSchema();
185  std::unique_ptr<te::da::DataSet> fromData = fromLayer->getData();
186  te::da::DataSourcePtr fromSource = te::da::GetDataSource(fromLayer->getDataSourceId(), true);
187 
188  std::unique_ptr<te::da::DataSetType> toSchema = fromLayer->getSchema();
189  std::unique_ptr<te::da::DataSet> toData = toLayer->getData();
190  te::da::DataSourcePtr toSource = te::da::GetDataSource(toLayer->getDataSourceId(), true);
191 
192  std::string outDataSetName = m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
193  te::da::DataSourcePtr outSource;
194 
195  //progress
197 
198  std::unique_ptr<te::attributefill::VectorToVectorOp> v2v (new te::attributefill::VectorToVectorMemory());
199 
200  v2v->setInput(fromLayer, toLayer);
201 
202  if(m_toFile)
203  {
204  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toUtf8().data());
205 
206  if (te::core::FileSystem::exists(uri.string()))
207  {
208  QMessageBox::warning(this, tr("VectorToVector"), tr("Output file already exists. Remove it or select a new name and try again."));
209  return;
210  }
211 
212  std::size_t idx = outDataSetName.find(".");
213  if (idx != std::string::npos)
214  outDataSetName=outDataSetName.substr(0,idx);
215 
216  std::string dsinfo("file://" + uri.string());
217 
218  outSource.reset(te::da::DataSourceFactory::make("OGR", dsinfo).release());
219  outSource->open();
220 
221  if (outSource->dataSetExists(outDataSetName))
222  {
223  QMessageBox::warning(this, tr("VectorToVector"), 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."));
224  return;
225  }
226 
227  v2v->setOutput(outSource, outDataSetName);
228 
229  // let's include the new datasource in the managers
230  boost::uuids::basic_random_generator<boost::mt19937> gen;
231  boost::uuids::uuid u = gen();
232  std::string id_ds = boost::uuids::to_string(u);
233 
235  ds->setConnInfo(dsinfo);
236  ds->setTitle(uri.stem().string());
237  ds->setAccessDriver("OGR");
238  ds->setType("OGR");
239  ds->setDescription(uri.string());
240  ds->setId(id_ds);
241 
242  te::da::DataSourcePtr newds = te::da::DataSourceManager::getInstance().get(id_ds, "OGR", ds->getConnInfo());
243  newds->open();
246  }
247  else
248  {
250  if (!aux)
251  {
252  QMessageBox::warning(this, tr("VectorToVector"), tr("The selected output datasource can not be accessed."));
253  return;
254  }
255 
256  if (aux->dataSetExists(outDataSetName))
257  {
258  QMessageBox::warning(this, tr("VectorToVector"), tr("Dataset already exists. Remove it or select a new name and try again."));
259  return;
260  }
261  this->setCursor(Qt::WaitCursor);
262 
263  v2v->setOutput(aux, outDataSetName);
264 
265  }
266 
267  v2v->setParams(getSelections(), m_outputAttributes);
268 
269  try
270  {
271  v2v->run();
272 
273  // creating a layer for the result
275 
277 
278  te::da::DataSetTypePtr dt(outDataSource->getDataSetType(outDataSetName).release());
279  m_outLayer = converter(dt);
280  m_outLayer->setSRID(toLayer->getSRID());
281 
282  }
283  catch(te::common::Exception& e)
284  {
285  QMessageBox::warning(this, tr("Vector To Vector"), e.what());
286  reject();
287  }
288  catch(std::exception& e)
289  {
290  QMessageBox::warning(this, tr("Vector To Vector"), e.what());
291  reject();
292  }
293 
294  this->setCursor(Qt::ArrowCursor);
295 
296  if(v2v->hasErrors())
297  {
298  QString err(tr("Some errors occurred during execution."));
299 #ifdef TERRALIB_LOGGER_ENABLED
300  err.append(tr(" The error log can be found at: "));
301 
302  QString userDataDir;
303 
304  // find user data directory
305 #if QT_VERSION >= 0x050000
306  userDataDir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
307 #else
308  userDataDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
309 #endif
310 
311  QString logfile(userDataDir);
312  logfile.append("/log/TerraLib.log");
313 
314  err.append(logfile);
315 #endif //TERRALIB_LOGGER_ENABLED
316 
317  QMessageBox::warning(this, tr("Vector to Vector"), err);
318  }
319 
320  accept();
321 }
322 
324 {
325  reject();
326 }
327 
329 {
330 
331  if(m_ui->m_toLayerComboBox->count() <= 1 || m_ui->m_fromLayerComboBox->count() <= 1 )
332  return;
333 
334  int currentIndex = m_ui->m_fromLayerComboBox->currentIndex();
335 
336  if(currentIndex == m_ui->m_toLayerComboBox->currentIndex())
337  {
338  if(currentIndex == 0)
339  m_ui->m_toLayerComboBox->setCurrentIndex(1);
340  else
341  m_ui->m_toLayerComboBox->setCurrentIndex(0);
342  }
343 
345 }
346 
348 {
349  if(m_ui->m_toLayerComboBox->count() <= 1 || m_ui->m_fromLayerComboBox->count() <= 1 )
350  return;
351 
352  int currentIndex = m_ui->m_toLayerComboBox->currentIndex();
353 
354  if(m_ui->m_fromLayerComboBox->count() > 0 && currentIndex == m_ui->m_fromLayerComboBox->currentIndex())
355  {
356  if(currentIndex == 0)
357  m_ui->m_fromLayerComboBox->setCurrentIndex(1);
358  else
359  m_ui->m_fromLayerComboBox->setCurrentIndex(0);
360  }
361 
362  std::unique_ptr<te::da::DataSetType> toSchema = getCurrentToLayer()->getSchema();
363 
364  std::vector<te::dt::Property*> props = toSchema->getProperties();
365  std::vector<te::dt::Property*> pkProps = toSchema->getPrimaryKey()->getProperties();
366  for(std::size_t i = 0; i < props.size(); ++i)
367  {
368  bool isPk = false;
369  for(std::size_t j = 0; j < pkProps.size(); ++j)
370  {
371  if(props[i]->getName() == pkProps[j]->getName())
372  {
373  isPk = true;
374  break;
375  }
376  }
377 
378  if((props[i]->getType() != te::dt::GEOMETRY_TYPE) && !isPk)
379  m_outputAttributes.push_back(props[i]->getName());
380  }
381 
382  onFromLayerComboBoxCurrentIndexChanged(m_ui->m_fromLayerComboBox->currentIndex());
383 }
384 
386 {
387  m_ui->m_selectAllComboBox->addItem("");
388  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::VALUE).c_str()), te::attributefill::VALUE);
391  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MEAN).c_str()), te::attributefill::MEAN);
392  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::SUM).c_str()), te::attributefill::SUM);
393  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::COUNT).c_str()), te::attributefill::COUNT);
403  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MODE).c_str()), te::attributefill::MODE);
414 
415  m_ui->m_rejectAllComboBox->addItem("");
416  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::VALUE).c_str()), te::attributefill::VALUE);
419  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MEAN).c_str()), te::attributefill::MEAN);
420  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::SUM).c_str()), te::attributefill::SUM);
421  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::COUNT).c_str()), te::attributefill::COUNT);
431  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MODE).c_str()), te::attributefill::MODE);
442 }
443 
445 {
446  std::string layerID = m_ui->m_fromLayerComboBox->itemData(m_ui->m_fromLayerComboBox->currentIndex(), Qt::UserRole).toString().toUtf8().data();
447 
448  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
449 
450  while(it != m_layers.end())
451  {
452  if(it->get()->getId() == layerID)
453  return *it;
454  ++it;
455  }
456 
457  return nullptr;
458 }
459 
461 {
462  std::string layerID = m_ui->m_toLayerComboBox->itemData(m_ui->m_toLayerComboBox->currentIndex(), Qt::UserRole).toString().toUtf8().data();
463 
464  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
465 
466  while(it != m_layers.end())
467  {
468  if(it->get()->getId() == layerID)
469  return *it;
470  ++it;
471  }
472 
473  return nullptr;
474 }
475 
477 {
478  if(item->text().isEmpty())
479  item->setSelected(false);
480 }
481 
483 {
484  QString text = m_ui->m_selectAllComboBox->itemText(index);
485  Qt::MatchFlags flag = Qt::MatchEndsWith; //The search term matches the end of the item.
486 
487  if(text.isEmpty())
488  return;
489 
490  QList<QListWidgetItem *> listFound;
491  listFound = m_ui->m_statisticsListWidget->findItems(text, flag);
492 
493  for(int i=0; i < listFound.size(); ++i)
494  {
495  std::vector<std::string> tokens;
496  te::common::Tokenize(listFound.at(i)->text().toUtf8().data(), tokens, ":");
497 
498  if(tokens.size() < 2)
499  continue;
500 
501  QString token(tokens[1].c_str());
502 
503  if(token.trimmed() == text)
504  listFound.at(i)->setSelected(true);
505  }
506 
507  m_ui->m_rejectAllComboBox->setCurrentIndex(0);
508 }
509 
511 {
512  QString text = m_ui->m_selectAllComboBox->itemText(index);
513  Qt::MatchFlags flag = Qt::MatchEndsWith; //The search term matches the end of the item.
514 
515  if(text=="")
516  return;
517 
518  QList<QListWidgetItem *> listFound;
519  listFound = m_ui->m_statisticsListWidget->findItems(text, flag);
520 
521  for(int i=0; i < listFound.size(); ++i)
522  {
523  std::vector<std::string> tokens;
524  te::common::Tokenize(listFound.at(i)->text().toUtf8().data(), tokens, ":");
525 
526  if(tokens.size() < 2)
527  continue;
528 
529  QString token(tokens[1].c_str());
530 
531  if(token.trimmed() == text)
532  listFound.at(i)->setSelected(false);
533  }
534 
535  m_ui->m_selectAllComboBox->setCurrentIndex(0);
536 }
537 
539 {
540  m_ui->m_newLayerNameLineEdit->clear();
541  m_ui->m_newLayerNameLineEdit->setEnabled(true);
543  dlg.exec();
544 
545  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
546 
547  if(dsPtrList.empty())
548  return;
549 
550  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
551 
552  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
553 
554  m_outputDatasource = *it;
555 
556  m_toFile = false;
557 }
558 
560 {
561  m_ui->m_newLayerNameLineEdit->clear();
562  m_ui->m_repositoryLineEdit->clear();
563 
565 
566  try {
567  fileDialog.exec();
568  }
569  catch (te::common::Exception& ex) {
570  QMessageBox::warning(this, tr("File information"), ex.what());
571  return;
572  }
573 
574  m_ui->m_repositoryLineEdit->setText(fileDialog.getPath().c_str());
575  m_ui->m_newLayerNameLineEdit->setText(fileDialog.getFileName().c_str());
576 
577  m_toFile = true;
578  m_ui->m_newLayerNameLineEdit->setEnabled(false);
579 }
580 
581 std::map<std::string, std::vector<te::attributefill::OperationType> > te::attributefill::VectorToVectorDialog::getSelections()
582 {
583  std::map<std::string, std::vector<te::attributefill::OperationType> > result;
584 
585  std::unique_ptr<te::da::DataSetType> fromScheme = getCurrentFromLayer()->getSchema();
586 
587  std::vector<std::string> props;
588  for(int i = 0; i < m_ui->m_statisticsListWidget->count(); ++i)
589  {
590  QListWidgetItem* item = m_ui->m_statisticsListWidget->item(i);
591 
592  if(!item->isSelected())
593  continue;
594 
595  std::string itemText = item->text().toUtf8().data();
596 
597  std::vector<std::string> tokens;
598  te::common::Tokenize(itemText, tokens, ":");
599 
600  std::string propName = tokens[0];
601  boost::trim(propName);
602 
603  te::attributefill::OperationType operationType = (te::attributefill::OperationType)item->data(Qt::UserRole).toInt();
604 
605  if(std::find(props.begin(), props.end(), propName) != props.end())
606  {
607  std::vector<te::attributefill::OperationType> vec;
608  vec.push_back(operationType);
609 
610  result[propName] = vec;
611  }
612  else
613  {
614  result[propName].push_back(operationType);
615  }
616  }
617 
618  return result;
619 }
620 
622 {
623  m_ui->m_selectAllComboBox->setCurrentIndex(0);
624  m_ui->m_rejectAllComboBox->setCurrentIndex(0);
625  m_ui->m_statisticsListWidget->clear();
626 
628 
629  std::unique_ptr<te::da::DataSetType> dst = layer->getSchema();
630  std::unique_ptr<te::da::DataSet> ds = layer->getData();
631 
632  te::gm::GeometryProperty* geomProp = nullptr;
633 
634  if(dst->hasGeom())
635  {
636  std::string geomPropName = layer->getGeomPropertyName();
637 
638  if(geomPropName.empty())
639  geomProp = te::da::GetFirstGeomProperty(dst.get());
640  else
641  geomProp = dynamic_cast<te::gm::GeometryProperty*>(dst->getProperty(geomPropName));
642  }
643 
644  te::gm::GeomType geomType = geomProp->getGeometryType();
645 
646  std::vector<te::dt::Property*> props = dst->getProperties();
647 
648  for(std::size_t i = 0; i < props.size(); ++i)
649  {
650  te::dt::Property* prop = props[i];
651 
652  if(isValidPropertyType(prop->getType()))
653  {
654  int propertyType = prop->getType();
655 
656  QListWidgetItem* item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VALUE).c_str());
657  item->setData(Qt::UserRole, QVariant(te::attributefill::VALUE));
658  m_ui->m_statisticsListWidget->addItem(item);
659 
660  if(propertyType == te::dt::STRING_TYPE)
661  {
662  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MIN_VALUE).c_str());
663  item->setData(Qt::UserRole, QVariant(te::attributefill::MIN_VALUE));
664  m_ui->m_statisticsListWidget->addItem(item);
665 
666  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MAX_VALUE).c_str());
667  item->setData(Qt::UserRole, QVariant(te::attributefill::MAX_VALUE));
668  m_ui->m_statisticsListWidget->addItem(item);
669 
670  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::COUNT).c_str());
671  item->setData(Qt::UserRole, QVariant(te::attributefill::COUNT));
672  m_ui->m_statisticsListWidget->addItem(item);
673 
674  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VALID_COUNT).c_str());
675  item->setData(Qt::UserRole, QVariant(te::attributefill::VALID_COUNT));
676  m_ui->m_statisticsListWidget->addItem(item);
677 
678  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::COUNT_DISTINCT).c_str());
679  item->setData(Qt::UserRole, QVariant(te::attributefill::COUNT_DISTINCT));
680  m_ui->m_statisticsListWidget->addItem(item);
681  }
682  else
683  {
684  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MIN_VALUE).c_str());
685  item->setData(Qt::UserRole, QVariant(te::attributefill::MIN_VALUE));
686  m_ui->m_statisticsListWidget->addItem(item);
687 
688  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MAX_VALUE).c_str());
689  item->setData(Qt::UserRole, QVariant(te::attributefill::MAX_VALUE));
690  m_ui->m_statisticsListWidget->addItem(item);
691 
692  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MEAN).c_str());
693  item->setData(Qt::UserRole, QVariant(te::attributefill::MEAN));
694  m_ui->m_statisticsListWidget->addItem(item);
695 
696  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::SUM).c_str());
697  item->setData(Qt::UserRole, QVariant(te::attributefill::SUM));
698  m_ui->m_statisticsListWidget->addItem(item);
699 
700  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::COUNT).c_str());
701  item->setData(Qt::UserRole, QVariant(te::attributefill::COUNT));
702  m_ui->m_statisticsListWidget->addItem(item);
703 
704  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VALID_COUNT).c_str());
705  item->setData(Qt::UserRole, QVariant(te::attributefill::VALID_COUNT));
706  m_ui->m_statisticsListWidget->addItem(item);
707 
708  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::COUNT_DISTINCT).c_str());
709  item->setData(Qt::UserRole, QVariant(te::attributefill::COUNT_DISTINCT));
710  m_ui->m_statisticsListWidget->addItem(item);
711 
712  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::STANDARD_DEVIATION).c_str());
713  item->setData(Qt::UserRole, QVariant(te::attributefill::STANDARD_DEVIATION));
714  m_ui->m_statisticsListWidget->addItem(item);
715 
716  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VARIANCE).c_str());
717  item->setData(Qt::UserRole, QVariant(te::attributefill::VARIANCE));
718  m_ui->m_statisticsListWidget->addItem(item);
719 
720  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::SKEWNESS).c_str());
721  item->setData(Qt::UserRole, QVariant(te::attributefill::SKEWNESS));
722  m_ui->m_statisticsListWidget->addItem(item);
723 
724  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::KURTOSIS).c_str());
725  item->setData(Qt::UserRole, QVariant(te::attributefill::KURTOSIS));
726  m_ui->m_statisticsListWidget->addItem(item);
727 
728  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::AMPLITUDE).c_str());
729  item->setData(Qt::UserRole, QVariant(te::attributefill::AMPLITUDE));
730  m_ui->m_statisticsListWidget->addItem(item);
731 
732  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MEDIAN).c_str());
733  item->setData(Qt::UserRole, QVariant(te::attributefill::MEDIAN));
734  m_ui->m_statisticsListWidget->addItem(item);
735 
736  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VAR_COEFF).c_str());
737  item->setData(Qt::UserRole, QVariant(te::attributefill::VAR_COEFF));
738  m_ui->m_statisticsListWidget->addItem(item);
739 
740  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MODE).c_str());
741  item->setData(Qt::UserRole, QVariant(te::attributefill::MODE));
742  m_ui->m_statisticsListWidget->addItem(item);
743  }
744 
745  if(isClassType(prop->getType()))
746  {
747  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::HIGHEST_OCCURRENCE).c_str());
748  item->setData(Qt::UserRole, QVariant(te::attributefill::HIGHEST_OCCURRENCE));
749  m_ui->m_statisticsListWidget->addItem(item);
750  }
751 
752  // This function works only with polygon to polygon
753  if(isClassType(prop->getType()) && isPolygon(geomType) && isPolygon(toGeomType))
754  {
755  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::HIGHEST_INTERSECTION).c_str());
756  item->setData(Qt::UserRole, QVariant(te::attributefill::HIGHEST_INTERSECTION));
757  m_ui->m_statisticsListWidget->addItem(item);
758 
759  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::PERCENT_EACH_CLASS).c_str());
760  item->setData(Qt::UserRole, QVariant(te::attributefill::PERCENT_EACH_CLASS));
761  m_ui->m_statisticsListWidget->addItem(item);
762 
763  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::PERCENT_CLASS).c_str());
764  item->setData(Qt::UserRole, QVariant(te::attributefill::PERCENT_CLASS));
765  m_ui->m_statisticsListWidget->addItem(item);
766  }
767 
768  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MIN_DISTANCE).c_str());
769  item->setData(Qt::UserRole, QVariant(te::attributefill::MIN_DISTANCE));
770  m_ui->m_statisticsListWidget->addItem(item);
771 
772  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MIN_DISTANCE_CENTROID).c_str());
773  item->setData(Qt::UserRole, QVariant(te::attributefill::MIN_DISTANCE_CENTROID));
774  m_ui->m_statisticsListWidget->addItem(item);
775 
776  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::PRESENCE).c_str());
777  item->setData(Qt::UserRole, QVariant(te::attributefill::PRESENCE));
778  m_ui->m_statisticsListWidget->addItem(item);
779 
780  if(isPolygon(geomType) && isPolygon(toGeomType))
781  {
782  if(isNumProperty(prop->getType()))
783  {
784  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::WEIGHTED).c_str());
785  item->setData(Qt::UserRole, QVariant(te::attributefill::WEIGHTED));
786  m_ui->m_statisticsListWidget->addItem(item);
787 
788  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::WEIGHTED_SUM).c_str());
789  item->setData(Qt::UserRole, QVariant(te::attributefill::WEIGHTED_SUM));
790  m_ui->m_statisticsListWidget->addItem(item);
791  }
792 
793  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::PERCENT_TOTAL_AREA).c_str());
794  item->setData(Qt::UserRole, QVariant(te::attributefill::PERCENT_TOTAL_AREA));
795  m_ui->m_statisticsListWidget->addItem(item);
796  }
797  }
798 
799  m_ui->m_statisticsListWidget->addItem("");
800  }
801 
802  int lastRow = m_ui->m_statisticsListWidget->count() - 1;
803  delete m_ui->m_statisticsListWidget->item(lastRow);
804 }
805 
807 {
808  if(type == te::gm::PolygonType ||
809  type == te::gm::PolygonZType ||
810  type == te::gm::PolygonMType ||
811  type == te::gm::PolygonZMType ||
812  type == te::gm::MultiPolygonType ||
813  type == te::gm::MultiPolygonZType ||
814  type == te::gm::MultiPolygonMType ||
816  return true;
817 
818  return false;
819 }
820 
822 {
823  if(type == te::gm::PointType ||
824  type == te::gm::PointZType ||
825  type == te::gm::PointMType ||
826  type == te::gm::PointZMType ||
827  type == te::gm::MultiPointType ||
828  type == te::gm::MultiPointZType ||
829  type == te::gm::MultiPointMType ||
830  type == te::gm::MultiPointZMType)
831  return true;
832 
833  return false;
834 }
835 
837 {
838  if(type == te::dt::CHAR_TYPE ||
839  type == te::dt::UCHAR_TYPE ||
840  type == te::dt::INT16_TYPE ||
841  type == te::dt::UINT16_TYPE ||
842  type == te::dt::INT32_TYPE ||
843  type == te::dt::UINT32_TYPE ||
844  type == te::dt::INT64_TYPE ||
845  type == te::dt::UINT64_TYPE ||
846  type == te::dt::FLOAT_TYPE ||
847  type == te::dt::DOUBLE_TYPE ||
848  type == te::dt::NUMERIC_TYPE ||
849  type == te::dt::STRING_TYPE ||
850  type == te::dt::CINT16_TYPE ||
851  type == te::dt::CINT32_TYPE ||
852  type == te::dt::CFLOAT_TYPE ||
853  type == te::dt::CDOUBLE_TYPE)
854  return true;
855 
856  return false;
857 }
858 
860 {
861  if(type == te::dt::INT16_TYPE ||
862  type == te::dt::UINT16_TYPE ||
863  type == te::dt::INT32_TYPE ||
864  type == te::dt::UINT32_TYPE ||
865  type == te::dt::INT64_TYPE ||
866  type == te::dt::UINT64_TYPE ||
867  type == te::dt::STRING_TYPE)
868  {
869  return true;
870  }
871 
872  return false;
873 }
874 
876 {
878  std::unique_ptr<te::da::DataSetType> toSchema = toLayer->getSchema();
880  te::gm::GeometryProperty* toGeomProp = dynamic_cast<te::gm::GeometryProperty*>(p);
881  return toGeomProp->getGeometryType();
882 }
883 
885 {
886  if(type == te::dt::INT16_TYPE ||
887  type == te::dt::INT32_TYPE ||
888  type == te::dt::INT64_TYPE ||
889  type == te::dt::DOUBLE_TYPE ||
890  type == te::dt::FLOAT_TYPE ||
891  type == te::dt::CINT16_TYPE ||
892  type == te::dt::CINT32_TYPE ||
893  type == te::dt::CDOUBLE_TYPE ||
894  type == te::dt::CFLOAT_TYPE)
895  return true;
896 
897  return false;
898 }
899 
901 {
903  std::unique_ptr<te::da::DataSetType> toSchema = toLayer->getSchema();
904 
905  std::vector<te::dt::Property*> props = toSchema->getProperties();
906  std::vector<te::dt::Property*> pkProps = toSchema->getPrimaryKey()->getProperties();
907 
908  std::vector<std::string> inputNames;
909  std::vector<std::string> outputNames;
910  for(std::size_t i = 0; i < props.size(); ++i)
911  {
912  bool isPk = false;
913  for(std::size_t j = 0; j < pkProps.size(); ++j)
914  {
915  if(props[i]->getName() == pkProps[j]->getName())
916  {
917  isPk = true;
918  break;
919  }
920  }
921 
922  if(props[i]->getType() != te::dt::GEOMETRY_TYPE && !isPk)
923  {
924  if(std::find(m_outputAttributes.begin(), m_outputAttributes.end(), props[i]->getName()) != m_outputAttributes.end())
925  {
926  outputNames.push_back(props[i]->getName());
927  }
928  else
929  {
930  inputNames.push_back(props[i]->getName());
931  }
932  }
933  }
934 
935  QDialog* dialog = new QDialog(this);
936  dialog->setWindowTitle(tr("Vector To Vector"));
937 
938  QBoxLayout* vLayout = new QBoxLayout(QBoxLayout::TopToBottom, dialog);
939 
941  d->setInputValues(inputNames);
942  d->setOutputValues(outputNames);
943  d->setLeftLabel(tr("Layer Attributes"));
944  d->setRightLabel(tr("Selected Attributes"));
945  vLayout->addWidget(d);
946 
947  QDialogButtonBox* bbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
948  connect(bbox, SIGNAL(accepted()), dialog, SLOT(accept()));
949  connect(bbox, SIGNAL(rejected()), dialog, SLOT(reject()));
950  vLayout->addWidget(bbox);
951 
952  int res = dialog->exec();
953 
954  if(res == QDialog::Accepted)
955  {
956  m_outputAttributes.clear();
958  }
959 }
void setRightLabel(const QString &value)
void setLeftLabel(const QString &value)
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)
Geometric property.
Defines a component for choose a file.
Definition: FileDialog.h:52
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
std::vector< std::string > getOutputValues()
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
te::map::AbstractLayerPtr getLayer()
Get the generated layer.
virtual const char * what() const
It outputs the exception message.
te::map::AbstractLayerPtr getCurrentFromLayer()
std::map< std::string, std::vector< te::attributefill::OperationType > > getSelections()
std::list< te::map::AbstractLayerPtr > m_layers
List of layers.
VectorToVectorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
static te::dt::Date ds(2010, 01, 01)
void setFunctionsByLayer(te::map::AbstractLayerPtr layer)
It models a property definition.
Definition: Property.h:59
void setInputValues(std::vector< std::string > values)
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:221
void onStatisticsListWidgetItemPressed(QListWidgetItem *item)
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
void exec()
This method will open the dialog of file selection and populate the class members with the chosen fil...
Definition: FileDialog.cpp:54
std::unique_ptr< Ui::VectorToVectorDialogForm > m_ui
User interface.
URI C++ Library.
Definition: Attributes.h:37
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
te::gm::Polygon * p
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::vector< std::string > m_outputAttributes
int getType() const
It returns the property data type.
Definition: Property.h:161
std::string getPath()
This method will return the chosen path.
Definition: FileDialog.cpp:103
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
te::da::DataSourceInfoPtr m_outputDatasource
DataSource information.
boost::intrusive_ptr< DataSetLayer > DataSetLayerPtr
Definition: DataSetLayer.h:148
A dialog for selecting a data source.
A class that represents a data source component.
std::string getFileName()
This method will return the file name.
Definition: FileDialog.cpp:113
void setOutputValues(std::vector< std::string > values)
TEATTRIBUTEFILLEXPORT std::string GetOperationFullName(const int &e)
Gets the full name of the operation as string.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
OperationType
Define grouping operations type.
Raster to vector attributefill dialog.
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
const std::string & getName() const
It returns the property name.
Definition: Property.h:127