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