All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  QMessageBox::information(this, "Help", "Under development");
151 }
152 
154 {
155  te::map::DataSetLayerPtr fromLayer(dynamic_cast<te::map::DataSetLayer*>(getCurrentFromLayer().get()));
156 
157  if(!fromLayer)
158  {
159  QMessageBox::warning(this, tr("VectorToVector"), tr("\"From\" layer invalid!"));
160  return;
161  }
162 
163  te::map::DataSetLayerPtr toLayer(dynamic_cast<te::map::DataSetLayer*>(getCurrentToLayer().get()));
164 
165  if(!toLayer)
166  {
167  QMessageBox::warning(this, tr("VectorToVector"), tr("\"To\" layer invalid!"));
168  return;
169  }
170 
171  if(m_ui->m_repositoryLineEdit->text().isEmpty())
172  {
173  QMessageBox::warning(this, tr("VectorToVector"), tr("Define a repository for the result."));
174  return;
175  }
176 
177  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
178  {
179  QMessageBox::warning(this, tr("VectorToVector"), tr("Define a name for the resulting layer."));
180  return;
181  }
182 
183  std::string fromDataSetName = fromLayer->getDataSetName();
184  std::auto_ptr<te::da::DataSetType> fromSchema = fromLayer->getSchema();
185  std::auto_ptr<te::da::DataSet> fromData = fromLayer->getData();
186  te::da::DataSourcePtr fromSource = te::da::GetDataSource(fromLayer->getDataSourceId(), true);
187 
188  std::string toDataSetName = toLayer->getDataSetName();
189  std::auto_ptr<te::da::DataSetType> toSchema = fromLayer->getSchema();
190  std::auto_ptr<te::da::DataSet> toData = toLayer->getData();
191  te::da::DataSourcePtr toSource = te::da::GetDataSource(toLayer->getDataSourceId(), true);
192 
193  std::string outDataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
194  te::da::DataSourcePtr outSource;
195 
196  //progress
199 
200  std::auto_ptr<te::attributefill::VectorToVectorOp> v2v (new te::attributefill::VectorToVectorMemory());
201 
202  v2v->setInput(fromLayer, toLayer);
203 
204  if(m_toFile)
205  {
206  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
207 
208  if (boost::filesystem::exists(uri))
209  {
210  QMessageBox::warning(this, tr("VectorToVector"), tr("Output file already exists. Remove it or select a new name and try again."));
211  return;
212  }
213 
214  std::size_t idx = outDataSetName.find(".");
215  if (idx != std::string::npos)
216  outDataSetName=outDataSetName.substr(0,idx);
217 
218  std::map<std::string, std::string> dsinfo;
219  dsinfo["URI"] = uri.string();
220 
221  outSource.reset(te::da::DataSourceFactory::make("OGR").release());
222  outSource->setConnectionInfo(dsinfo);
223  outSource->open();
224 
225  if (outSource->dataSetExists(outDataSetName))
226  {
227  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."));
228  return;
229  }
230 
231  v2v->setOutput(outSource, outDataSetName);
232 
233  // let's include the new datasource in the managers
234  boost::uuids::basic_random_generator<boost::mt19937> gen;
235  boost::uuids::uuid u = gen();
236  std::string id_ds = boost::uuids::to_string(u);
237 
239  ds->setConnInfo(dsinfo);
240  ds->setTitle(uri.stem().string());
241  ds->setAccessDriver("OGR");
242  ds->setType("OGR");
243  ds->setDescription(uri.string());
244  ds->setId(id_ds);
245 
246  te::da::DataSourcePtr newds = te::da::DataSourceManager::getInstance().get(id_ds, "OGR", ds->getConnInfo());
247  newds->open();
249  m_outputDatasource = ds;
250  }
251  else
252  {
253  te::da::DataSourcePtr aux = te::da::GetDataSource(m_outputDatasource->getId());
254  if (!aux)
255  {
256  QMessageBox::warning(this, tr("VectorToVector"), tr("The selected output datasource can not be accessed."));
257  return;
258  }
259 
260  if (aux->dataSetExists(outDataSetName))
261  {
262  QMessageBox::warning(this, tr("VectorToVector"), tr("Dataset already exists. Remove it or select a new name and try again."));
263  return;
264  }
265  this->setCursor(Qt::WaitCursor);
266 
267  v2v->setOutput(aux, outDataSetName);
268 
269  }
270 
271  std::map<std::string, std::vector<te::attributefill::OperationType> > selections = getSelections();
272 
273  v2v->setParams(getSelections(), m_outputAttributes);
274 
275  try
276  {
277  v2v->run();
278 
279  // creating a layer for the result
280  te::da::DataSourcePtr outDataSource = te::da::GetDataSource(m_outputDatasource->getId());
281 
282  te::qt::widgets::DataSet2Layer converter(m_outputDatasource->getId());
283 
284  te::da::DataSetTypePtr dt(outDataSource->getDataSetType(outDataSetName).release());
285  m_outLayer = converter(dt);
286  }
287  catch(te::common::Exception& e)
288  {
289  QMessageBox::warning(this, tr("Vector To Vector"), e.what());
291  reject();
292  }
293  catch(std::exception& e)
294  {
295  QMessageBox::warning(this, tr("Vector To Vector"), e.what());
297  reject();
298  }
299 
301  this->setCursor(Qt::ArrowCursor);
302 
303  if(v2v->hasErrors())
304  {
305  QString err(tr("Some errors occurred during execution."));
306 #ifdef TERRALIB_LOGGER_ENABLED
307  err.append(tr(" The error log can be found at: "));
308  err += m_logPath.c_str();
309 #endif //TERRALIB_LOGGER_ENABLED
310 
311  QMessageBox::warning(this, tr("Vector to Vector"), err);
312  }
313 
314  accept();
315 }
316 
318 {
319  reject();
320 }
321 
323 {
324 
325  if(m_ui->m_toLayerComboBox->count() <= 1 || m_ui->m_fromLayerComboBox->count() <= 1 )
326  return;
327 
328  int currentIndex = m_ui->m_fromLayerComboBox->currentIndex();
329 
330  if(currentIndex == m_ui->m_toLayerComboBox->currentIndex())
331  {
332  if(currentIndex == 0)
333  m_ui->m_toLayerComboBox->setCurrentIndex(1);
334  else
335  m_ui->m_toLayerComboBox->setCurrentIndex(0);
336  }
337 
338  setFunctionsByLayer(getCurrentFromLayer());
339 }
340 
342 {
343  if(m_ui->m_toLayerComboBox->count() <= 1 || m_ui->m_fromLayerComboBox->count() <= 1 )
344  return;
345 
346  int currentIndex = m_ui->m_toLayerComboBox->currentIndex();
347 
348  if(m_ui->m_fromLayerComboBox->count() > 0 && currentIndex == m_ui->m_fromLayerComboBox->currentIndex())
349  {
350  if(currentIndex == 0)
351  m_ui->m_fromLayerComboBox->setCurrentIndex(1);
352  else
353  m_ui->m_fromLayerComboBox->setCurrentIndex(0);
354  }
355 
356  std::auto_ptr<te::da::DataSetType> toSchema = getCurrentToLayer()->getSchema();
357 
358  std::vector<te::dt::Property*> props = toSchema->getProperties();
359  std::vector<te::dt::Property*> pkProps = toSchema->getPrimaryKey()->getProperties();
360  for(std::size_t i = 0; i < props.size(); ++i)
361  {
362  bool isPk = false;
363  for(std::size_t j = 0; j < pkProps.size(); ++j)
364  {
365  if(props[i]->getName() == pkProps[j]->getName())
366  {
367  isPk = true;
368  break;
369  }
370  }
371 
372  if((props[i]->getType() != te::dt::GEOMETRY_TYPE) && !isPk)
373  m_outputAttributes.push_back(props[i]->getName());
374  }
375 
376  onFromLayerComboBoxCurrentIndexChanged(m_ui->m_fromLayerComboBox->currentIndex());
377 }
378 
380 {
381  m_ui->m_selectAllComboBox->addItem("");
382  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::VALUE).c_str()), te::attributefill::VALUE);
383  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MIN_VALUE).c_str()), te::attributefill::MIN_VALUE);
384  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MAX_VALUE).c_str()), te::attributefill::MAX_VALUE);
385  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MEAN).c_str()), te::attributefill::MEAN);
386  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::SUM).c_str()), te::attributefill::SUM);
387  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::COUNT).c_str()), te::attributefill::COUNT);
390  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::VARIANCE).c_str()), te::attributefill::VARIANCE);
391  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::SKEWNESS).c_str()), te::attributefill::SKEWNESS);
392  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::KURTOSIS).c_str()), te::attributefill::KURTOSIS);
393  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::AMPLITUDE).c_str()), te::attributefill::AMPLITUDE);
394  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MEDIAN).c_str()), te::attributefill::MEDIAN);
395  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::VAR_COEFF).c_str()), te::attributefill::VAR_COEFF);
396  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MODE).c_str()), te::attributefill::MODE);
401  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::PRESENCE).c_str()), te::attributefill::PRESENCE);
402  m_ui->m_selectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::WEIGHTED).c_str()), te::attributefill::WEIGHTED);
406 
407  m_ui->m_rejectAllComboBox->addItem("");
408  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::VALUE).c_str()), te::attributefill::VALUE);
409  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MIN_VALUE).c_str()), te::attributefill::MIN_VALUE);
410  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MAX_VALUE).c_str()), te::attributefill::MAX_VALUE);
411  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MEAN).c_str()), te::attributefill::MEAN);
412  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::SUM).c_str()), te::attributefill::SUM);
413  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::COUNT).c_str()), te::attributefill::COUNT);
416  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::VARIANCE).c_str()), te::attributefill::VARIANCE);
417  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::SKEWNESS).c_str()), te::attributefill::SKEWNESS);
418  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::KURTOSIS).c_str()), te::attributefill::KURTOSIS);
419  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::AMPLITUDE).c_str()), te::attributefill::AMPLITUDE);
420  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MEDIAN).c_str()), te::attributefill::MEDIAN);
421  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::VAR_COEFF).c_str()), te::attributefill::VAR_COEFF);
422  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::MODE).c_str()), te::attributefill::MODE);
427  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::PRESENCE).c_str()), te::attributefill::PRESENCE);
428  m_ui->m_rejectAllComboBox->addItem(QString(te::attributefill::GetOperationFullName(te::attributefill::WEIGHTED).c_str()), te::attributefill::WEIGHTED);
432 }
433 
435 {
436  std::string layerID = m_ui->m_fromLayerComboBox->itemData(m_ui->m_fromLayerComboBox->currentIndex(), Qt::UserRole).toString().toStdString();
437 
438  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
439 
440  while(it != m_layers.end())
441  {
442  if(it->get()->getId() == layerID)
443  return *it;
444  ++it;
445  }
446 
447  return 0;
448 }
449 
451 {
452  std::string layerID = m_ui->m_toLayerComboBox->itemData(m_ui->m_toLayerComboBox->currentIndex(), Qt::UserRole).toString().toStdString();
453 
454  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
455 
456  while(it != m_layers.end())
457  {
458  if(it->get()->getId() == layerID)
459  return *it;
460  ++it;
461  }
462 
463  return 0;
464 }
465 
467 {
468  if(item->text().isEmpty())
469  item->setSelected(false);
470 }
471 
473 {
474  QString text = m_ui->m_selectAllComboBox->itemText(index);
475  Qt::MatchFlags flag = Qt::MatchEndsWith; //The search term matches the end of the item.
476 
477  if(text.isEmpty())
478  return;
479 
480  QList<QListWidgetItem *> listFound;
481  listFound = m_ui->m_statisticsListWidget->findItems(text, flag);
482 
483  for(int i=0; i < listFound.size(); ++i)
484  {
485  std::vector<std::string> tokens;
486  te::common::Tokenize(listFound.at(i)->text().toStdString(), tokens, ":");
487 
488  if(tokens.size() < 2)
489  continue;
490 
491  QString token(tokens[1].c_str());
492 
493  if(token.trimmed() == text)
494  listFound.at(i)->setSelected(true);
495  }
496 
497  m_ui->m_rejectAllComboBox->setCurrentIndex(0);
498 }
499 
501 {
502  QString text = m_ui->m_selectAllComboBox->itemText(index);
503  Qt::MatchFlags flag = Qt::MatchEndsWith; //The search term matches the end of the item.
504 
505  if(text=="")
506  return;
507 
508  QList<QListWidgetItem *> listFound;
509  listFound = m_ui->m_statisticsListWidget->findItems(text, flag);
510 
511  for(int i=0; i < listFound.size(); ++i)
512  {
513  std::vector<std::string> tokens;
514  te::common::Tokenize(listFound.at(i)->text().toStdString(), tokens, ":");
515 
516  if(tokens.size() < 2)
517  continue;
518 
519  QString token(tokens[1].c_str());
520 
521  if(token.trimmed() == text)
522  listFound.at(i)->setSelected(false);
523  }
524 
525  m_ui->m_selectAllComboBox->setCurrentIndex(0);
526 }
527 
529 {
530  m_ui->m_newLayerNameLineEdit->clear();
531  m_ui->m_newLayerNameLineEdit->setEnabled(true);
533  dlg.exec();
534 
535  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
536 
537  if(dsPtrList.size() <= 0)
538  return;
539 
540  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
541 
542  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
543 
544  m_outputDatasource = *it;
545 
546  m_toFile = false;
547 }
548 
550 {
551  m_ui->m_newLayerNameLineEdit->clear();
552  m_ui->m_repositoryLineEdit->clear();
553 
554  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."),
555  QString(), tr("Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
556 
557  if (fileName.isEmpty())
558  return;
559 
560  boost::filesystem::path outfile(fileName.toStdString());
561  std::string aux = outfile.leaf().string();
562  m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
563  aux = outfile.string();
564  m_ui->m_repositoryLineEdit->setText(aux.c_str());
565 
566  m_toFile = true;
567  m_ui->m_newLayerNameLineEdit->setEnabled(false);
568 }
569 
570 std::map<std::string, std::vector<te::attributefill::OperationType> > te::attributefill::VectorToVectorDialog::getSelections()
571 {
572  std::map<std::string, std::vector<te::attributefill::OperationType> > result;
573 
574  std::auto_ptr<te::da::DataSetType> fromScheme = getCurrentFromLayer()->getSchema();
575 
576  std::vector<std::string> props;
577  for(int i = 0; i < m_ui->m_statisticsListWidget->count(); ++i)
578  {
579  QListWidgetItem* item = m_ui->m_statisticsListWidget->item(i);
580 
581  if(!item->isSelected())
582  continue;
583 
584  std::string itemText = item->text().toStdString();
585 
586  std::vector<std::string> tokens;
587  te::common::Tokenize(itemText, tokens, ":");
588 
589  std::string propName = tokens[0];
590  boost::trim(propName);
591 
592  te::attributefill::OperationType operationType = (te::attributefill::OperationType)item->data(Qt::UserRole).toInt();
593 
594  if(std::find(props.begin(), props.end(), propName) != props.end())
595  {
596  std::vector<te::attributefill::OperationType> vec;
597  vec.push_back(operationType);
598 
599  result[propName] = vec;
600  }
601  else
602  {
603  result[propName].push_back(operationType);
604  }
605  }
606 
607  return result;
608 }
609 
611 {
612  m_ui->m_selectAllComboBox->setCurrentIndex(0);
613  m_ui->m_rejectAllComboBox->setCurrentIndex(0);
614  m_ui->m_statisticsListWidget->clear();
615 
616  te::gm::GeomType toGeomType = getCurrentToLayerGeomType();
617 
618  std::auto_ptr<te::da::DataSetType> dst = layer->getSchema();
619  std::auto_ptr<te::da::DataSet> ds = layer->getData();
620 
621  te::gm::GeometryProperty* geomProp = 0;
622 
623  if(dst->hasGeom())
624  {
625  std::string geomPropName = layer->getGeomPropertyName();
626 
627  if(geomPropName.empty())
628  geomProp = te::da::GetFirstGeomProperty(dst.get());
629  else
630  geomProp = dynamic_cast<te::gm::GeometryProperty*>(dst->getProperty(geomPropName));
631  }
632 
633  te::gm::GeomType geomType = geomProp->getGeometryType();
634 
635  std::vector<te::dt::Property*> props = dst->getProperties();
636 
637  for(std::size_t i = 0; i < props.size(); ++i)
638  {
639  te::dt::Property* prop = props[i];
640 
641  if(isValidPropertyType(prop->getType()))
642  {
643  int propertyType = prop->getType();
644 
645  QListWidgetItem* item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VALUE).c_str());
646  item->setData(Qt::UserRole, QVariant(te::attributefill::VALUE));
647  m_ui->m_statisticsListWidget->addItem(item);
648 
649  if(propertyType == te::dt::STRING_TYPE)
650  {
651  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MIN_VALUE).c_str());
652  item->setData(Qt::UserRole, QVariant(te::attributefill::MIN_VALUE));
653  m_ui->m_statisticsListWidget->addItem(item);
654 
655  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MAX_VALUE).c_str());
656  item->setData(Qt::UserRole, QVariant(te::attributefill::MAX_VALUE));
657  m_ui->m_statisticsListWidget->addItem(item);
658 
659  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::COUNT).c_str());
660  item->setData(Qt::UserRole, QVariant(te::attributefill::COUNT));
661  m_ui->m_statisticsListWidget->addItem(item);
662 
663  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VALID_COUNT).c_str());
664  item->setData(Qt::UserRole, QVariant(te::attributefill::VALID_COUNT));
665  m_ui->m_statisticsListWidget->addItem(item);
666  }
667  else
668  {
669  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MIN_VALUE).c_str());
670  item->setData(Qt::UserRole, QVariant(te::attributefill::MIN_VALUE));
671  m_ui->m_statisticsListWidget->addItem(item);
672 
673  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MAX_VALUE).c_str());
674  item->setData(Qt::UserRole, QVariant(te::attributefill::MAX_VALUE));
675  m_ui->m_statisticsListWidget->addItem(item);
676 
677  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MEAN).c_str());
678  item->setData(Qt::UserRole, QVariant(te::attributefill::MEAN));
679  m_ui->m_statisticsListWidget->addItem(item);
680 
681  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::SUM).c_str());
682  item->setData(Qt::UserRole, QVariant(te::attributefill::SUM));
683  m_ui->m_statisticsListWidget->addItem(item);
684 
685  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::COUNT).c_str());
686  item->setData(Qt::UserRole, QVariant(te::attributefill::COUNT));
687  m_ui->m_statisticsListWidget->addItem(item);
688 
689  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VALID_COUNT).c_str());
690  item->setData(Qt::UserRole, QVariant(te::attributefill::VALID_COUNT));
691  m_ui->m_statisticsListWidget->addItem(item);
692 
693  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::STANDARD_DEVIATION).c_str());
694  item->setData(Qt::UserRole, QVariant(te::attributefill::STANDARD_DEVIATION));
695  m_ui->m_statisticsListWidget->addItem(item);
696 
697  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VARIANCE).c_str());
698  item->setData(Qt::UserRole, QVariant(te::attributefill::VARIANCE));
699  m_ui->m_statisticsListWidget->addItem(item);
700 
701  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::SKEWNESS).c_str());
702  item->setData(Qt::UserRole, QVariant(te::attributefill::SKEWNESS));
703  m_ui->m_statisticsListWidget->addItem(item);
704 
705  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::KURTOSIS).c_str());
706  item->setData(Qt::UserRole, QVariant(te::attributefill::KURTOSIS));
707  m_ui->m_statisticsListWidget->addItem(item);
708 
709  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::AMPLITUDE).c_str());
710  item->setData(Qt::UserRole, QVariant(te::attributefill::AMPLITUDE));
711  m_ui->m_statisticsListWidget->addItem(item);
712 
713  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MEDIAN).c_str());
714  item->setData(Qt::UserRole, QVariant(te::attributefill::MEDIAN));
715  m_ui->m_statisticsListWidget->addItem(item);
716 
717  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::VAR_COEFF).c_str());
718  item->setData(Qt::UserRole, QVariant(te::attributefill::VAR_COEFF));
719  m_ui->m_statisticsListWidget->addItem(item);
720 
721  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MODE).c_str());
722  item->setData(Qt::UserRole, QVariant(te::attributefill::MODE));
723  m_ui->m_statisticsListWidget->addItem(item);
724  }
725 
726  if(isClassType(prop->getType()))
727  {
728  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::HIGHEST_OCCURRENCE).c_str());
729  item->setData(Qt::UserRole, QVariant(te::attributefill::HIGHEST_OCCURRENCE));
730  m_ui->m_statisticsListWidget->addItem(item);
731  }
732 
733  // This function works only with polygon to polygon
734  if(isClassType(prop->getType()) && isPolygon(geomType) && isPolygon(toGeomType))
735  {
736  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::HIGHEST_INTERSECTION).c_str());
737  item->setData(Qt::UserRole, QVariant(te::attributefill::HIGHEST_INTERSECTION));
738  m_ui->m_statisticsListWidget->addItem(item);
739 
740  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::PERCENT_EACH_CLASS).c_str());
741  item->setData(Qt::UserRole, QVariant(te::attributefill::PERCENT_EACH_CLASS));
742  m_ui->m_statisticsListWidget->addItem(item);
743 
744  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::PERCENT_CLASS).c_str());
745  item->setData(Qt::UserRole, QVariant(te::attributefill::PERCENT_CLASS));
746  m_ui->m_statisticsListWidget->addItem(item);
747  }
748 
749  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::MIN_DISTANCE).c_str());
750  item->setData(Qt::UserRole, QVariant(te::attributefill::MIN_DISTANCE));
751  m_ui->m_statisticsListWidget->addItem(item);
752 
753  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::PRESENCE).c_str());
754  item->setData(Qt::UserRole, QVariant(te::attributefill::PRESENCE));
755  m_ui->m_statisticsListWidget->addItem(item);
756 
757  if(isPolygon(geomType) && isPolygon(toGeomType))
758  {
759  if(isNumProperty(prop->getType()))
760  {
761  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::WEIGHTED).c_str());
762  item->setData(Qt::UserRole, QVariant(te::attributefill::WEIGHTED));
763  m_ui->m_statisticsListWidget->addItem(item);
764 
765  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::WEIGHTED_SUM).c_str());
766  item->setData(Qt::UserRole, QVariant(te::attributefill::WEIGHTED_SUM));
767  m_ui->m_statisticsListWidget->addItem(item);
768  }
769 
770  item = new QListWidgetItem(QString(prop->getName().c_str()) + " : " + te::attributefill::GetOperationFullName(te::attributefill::PERCENT_TOTAL_AREA).c_str());
771  item->setData(Qt::UserRole, QVariant(te::attributefill::PERCENT_TOTAL_AREA));
772  m_ui->m_statisticsListWidget->addItem(item);
773  }
774  }
775 
776  m_ui->m_statisticsListWidget->addItem("");
777  }
778 
779  int lastRow = m_ui->m_statisticsListWidget->count() - 1;
780  delete m_ui->m_statisticsListWidget->item(lastRow);
781 }
782 
784 {
785  if(type == te::gm::PolygonType ||
786  type == te::gm::PolygonZType ||
787  type == te::gm::PolygonMType ||
788  type == te::gm::PolygonZMType ||
789  type == te::gm::MultiPolygonType ||
790  type == te::gm::MultiPolygonZType ||
791  type == te::gm::MultiPolygonMType ||
793  return true;
794 
795  return false;
796 }
797 
799 {
800  if(type == te::gm::PointType ||
801  type == te::gm::PointZType ||
802  type == te::gm::PointMType ||
803  type == te::gm::PointZMType ||
804  type == te::gm::MultiPointType ||
805  type == te::gm::MultiPointZType ||
806  type == te::gm::MultiPointMType ||
807  type == te::gm::MultiPointZMType)
808  return true;
809 
810  return false;
811 }
812 
814 {
815  if(type == te::dt::CHAR_TYPE ||
816  type == te::dt::UCHAR_TYPE ||
817  type == te::dt::INT16_TYPE ||
818  type == te::dt::UINT16_TYPE ||
819  type == te::dt::INT32_TYPE ||
820  type == te::dt::UINT32_TYPE ||
821  type == te::dt::INT64_TYPE ||
822  type == te::dt::UINT64_TYPE ||
823  type == te::dt::FLOAT_TYPE ||
824  type == te::dt::DOUBLE_TYPE ||
825  type == te::dt::NUMERIC_TYPE ||
826  type == te::dt::STRING_TYPE ||
827  type == te::dt::CINT16_TYPE ||
828  type == te::dt::CINT32_TYPE ||
829  type == te::dt::CFLOAT_TYPE ||
830  type == te::dt::CDOUBLE_TYPE)
831  return true;
832 
833  return false;
834 }
835 
837 {
838  if(type == te::dt::INT16_TYPE ||
839  type == te::dt::UINT16_TYPE ||
840  type == te::dt::INT32_TYPE ||
841  type == te::dt::UINT32_TYPE ||
842  type == te::dt::INT64_TYPE ||
843  type == te::dt::UINT64_TYPE ||
844  type == te::dt::STRING_TYPE)
845  {
846  return true;
847  }
848 
849  return false;
850 }
851 
853 {
854  te::map::AbstractLayerPtr toLayer = getCurrentToLayer();
855  std::auto_ptr<te::da::DataSetType> toSchema = toLayer->getSchema();
857  te::gm::GeometryProperty* toGeomProp = dynamic_cast<te::gm::GeometryProperty*>(p);
858  return toGeomProp->getGeometryType();
859 }
860 
862 {
863  if(type == te::dt::INT16_TYPE ||
864  type == te::dt::INT32_TYPE ||
865  type == te::dt::INT64_TYPE ||
866  type == te::dt::DOUBLE_TYPE ||
867  type == te::dt::FLOAT_TYPE ||
868  type == te::dt::CINT16_TYPE ||
869  type == te::dt::CINT32_TYPE ||
870  type == te::dt::CDOUBLE_TYPE ||
871  type == te::dt::CFLOAT_TYPE)
872  return true;
873 
874  return false;
875 }
876 
878 {
879  te::map::AbstractLayerPtr toLayer = getCurrentToLayer();
880  std::auto_ptr<te::da::DataSetType> toSchema = toLayer->getSchema();
881 
882  std::vector<te::dt::Property*> props = toSchema->getProperties();
883  std::vector<te::dt::Property*> pkProps = toSchema->getPrimaryKey()->getProperties();
884 
885  std::vector<std::string> inputNames;
886  std::vector<std::string> outputNames;
887  for(std::size_t i = 0; i < props.size(); ++i)
888  {
889  bool isPk = false;
890  for(std::size_t j = 0; j < pkProps.size(); ++j)
891  {
892  if(props[i]->getName() == pkProps[j]->getName())
893  {
894  isPk = true;
895  break;
896  }
897  }
898 
899  if(props[i]->getType() != te::dt::GEOMETRY_TYPE && !isPk)
900  {
901  if(std::find(m_outputAttributes.begin(), m_outputAttributes.end(), props[i]->getName()) != m_outputAttributes.end())
902  {
903  outputNames.push_back(props[i]->getName());
904  }
905  else
906  {
907  inputNames.push_back(props[i]->getName());
908  }
909  }
910  }
911 
912  QDialog* dialog = new QDialog(this);
913  dialog->setWindowTitle(tr("Vector To Vector"));
914 
915  QBoxLayout* vLayout = new QBoxLayout(QBoxLayout::TopToBottom, dialog);
916 
918  d->setInputValues(inputNames);
919  d->setOutputValues(outputNames);
920  d->setLeftLabel(tr("Layer Attributes").toStdString());
921  d->setRightLabel(tr("Selected Attributes").toStdString());
922  vLayout->addWidget(d);
923 
924  QDialogButtonBox* bbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
925  connect(bbox, SIGNAL(accepted()), dialog, SLOT(accept()));
926  connect(bbox, SIGNAL(rejected()), dialog, SLOT(reject()));
927  vLayout->addWidget(bbox);
928 
929  int res = dialog->exec();
930 
931  if(res == QDialog::Accepted)
932  {
933  m_outputAttributes.clear();
934  m_outputAttributes = d->getOutputValues();
935  }
936 }
937 
939 {
940  m_logPath = path;
941 }
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:216
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)
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