MNTGenerationDialog.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/mnt/qt/MNTGenerationDialog.cpp
22 
23 \brief A dialog for Retangular Grid generation
24 */
25 
26 //terralib
27 #include "../../core/filesystem/FileSystem.h"
28 #include "../../core/logger/Logger.h"
29 #include "../../core/translator/Translator.h"
30 #include "../../common/Exception.h"
31 #include "../../common/progress/ProgressManager.h"
32 #include "../../dataaccess/datasource/DataSourceFactory.h"
33 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
34 #include "../../dataaccess/datasource/DataSourceManager.h"
35 #include "../../dataaccess/utils/Utils.h"
36 #include "../../geometry/GeometryProperty.h"
37 #include "../../maptools/DataSetLayer.h"
38 #include "../../maptools/RasterContrast.h"
39 #include "../../maptools/Utils.h"
40 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
41 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
42 #include "../../qt/widgets/rp/Utils.h"
43 #include "../../qt/widgets/srs/SRSManagerDialog.h"
44 #include "../../qt/widgets/Utils.h"
45 #include "../../qt/widgets/utils/FileDialog.h"
46 #include "../../raster.h"
47 #include "../../raster/Interpolator.h"
48 #include "../../raster/RasterFactory.h"
49 #include "../../rp/Contrast.h"
50 #include "../../srs/SpatialReferenceSystemManager.h"
51 
52 #include "../core/CalculateGrid.h"
53 #include "../core/SplineGrass.h"
54 #include "../core/SplineGrassMitasova.h"
55 #include "../core/TINCalculateGrid.h"
56 #include "../core/Utils.h"
57 
58 #include "LayerSearchDialog.h"
59 #include "MNTGenerationDialog.h"
60 #include "ui_MNTGenerationDialogForm.h"
61 
62 // Qt
63 #include <QFileDialog>
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::MNTGenerationDialogForm),
76  m_layers(std::list<te::map::AbstractLayerPtr>())
77 {
78  // add controls
79  m_ui->setupUi(this);
80 
81  //signals
82  connect(m_ui->m_vectorradioButton, SIGNAL(toggled(bool)), this, SLOT(onVectorToggled()));
83  connect(m_ui->m_gridradioButton, SIGNAL(toggled(bool)), this, SLOT(onGridToggled()));
84  connect(m_ui->m_isolinesSearchToolButton, SIGNAL(clicked()), this, SLOT(onInputIsolinesToolButtonClicked()));
85  connect(m_ui->m_isolinescomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onIsolinesComboBoxChanged(int)));
86  connect(m_ui->m_sampleSearchToolButton, SIGNAL(clicked()), this, SLOT(onInputSamplesToolButtonClicked()));
87  connect(m_ui->m_samplescomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onSamplesComboBoxChanged(int)));
88  connect(m_ui->m_layersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onInputComboBoxChanged(int)));
89  connect(m_ui->m_layerSearchToolButton, SIGNAL(clicked()), this, SLOT(onlayerSearchToolButtonClicked()));
90 
91  connect(m_ui->m_dummycheckBox, SIGNAL(toggled(bool)), m_ui->m_dummylineEdit, SLOT(setEnabled(bool)));
92 
93  connect(m_ui->m_interpolatorComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(oninterpolatorComboBoxChanged(int)));
94 
95  m_ui->m_dimLLineEdit->setValidator(new QIntValidator(this));
96  m_ui->m_dimCLineEdit->setValidator(new QIntValidator(this));
97  m_ui->m_resXLineEdit->setValidator(new QDoubleValidator(this));
98  m_ui->m_resYLineEdit->setValidator(new QDoubleValidator(this));
99 
100  connect(m_ui->m_resXLineEdit, SIGNAL(editingFinished()), this, SLOT(onResXLineEditEditingFinished()));
101  connect(m_ui->m_resYLineEdit, SIGNAL(editingFinished()), this, SLOT(onResYLineEditEditingFinished()));
102  connect(m_ui->m_dimCLineEdit, SIGNAL(editingFinished()), this, SLOT(onDimCLineEditEditingFinished()));
103  connect(m_ui->m_dimLLineEdit, SIGNAL(editingFinished()), this, SLOT(onDimLLineEditEditingFinished()));
104 
105  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
106  connect(m_ui->m_targetFileToolButton, SIGNAL(clicked()), this, SLOT(onTargetFileToolButtonPressed()));
107  connect(m_ui->m_targetDatasourceToolButton, SIGNAL(clicked()), this, SLOT(onTargetDatasourceToolButtonPressed()));
108 
109  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
110  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
111 
112  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
113  m_ui->m_helpPushButton->setPageReference("plugins/mnt/DTM_DTM.html");
114 
115  m_ui->m_srsToolButton->setIcon(QIcon::fromTheme("srs"));
116  connect(m_ui->m_srsToolButton, SIGNAL(clicked()), this, SLOT(onSrsToolButtonClicked()));
117 
118  for (int i = 2; i < 10; i++)
119  m_ui->m_powerComboBox->addItem(QString::number(i));
120 
121  //Default Values
122  m_ui->m_tensionLineEdit->setText("40");
123  m_ui->m_smothLineEdit->setText("0.1");
124  m_ui->m_minPtsMitLineEdit->setText("50");
125 
126  m_outsrid = 0;
127 
128 }
129 
131 
132 void te::mnt::MNTGenerationDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
133 {
134  m_layers = layers;
135 
136  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
137  size_t nvect = 0;
138  size_t ngrid = 0;
139 
140  while (it != m_layers.end())
141  {
142  if (it->get())
143  {
144  if (it->get()->isValid())
145  {
146  te::map::AbstractLayerPtr layer = it->get();
147  std::unique_ptr<te::da::DataSetType> dsType(layer->getSchema());
148  mntType inputType = getMNTType(dsType.get());
149  if (inputType == GRID)
150  {
151  te::rst::RasterProperty* rasterProp = te::da::GetFirstRasterProperty(dsType.get());
152  if (rasterProp->getBandProperties().size() > 1)
153  {
154  ++it;
155  continue;
156  }
157  }
158  if (inputType == GRID || inputType == TIN)
159  {
160  m_ui->m_layersComboBox->addItem(QString(layer->getTitle().c_str()), QVariant(layer->getId().c_str()));
161  ngrid++;
162  }
163  if (inputType == SAMPLE)
164  {
165  m_ui->m_samplescomboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
166  nvect++;
167  }
168  if (inputType == ISOLINE)
169  {
170  m_ui->m_isolinescomboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
171  nvect++;
172  }
173  }
174  }
175  ++it;
176  }
177  m_ui->m_layersComboBox->insertItem(0, "");
178  m_ui->m_samplescomboBox->insertItem(0, "");
179  m_ui->m_isolinescomboBox->insertItem(0, "");
180 
181  m_ui->m_layersComboBox->setCurrentIndex(0);
182  m_ui->m_samplescomboBox->setCurrentIndex(0);
183  m_ui->m_isolinescomboBox->setCurrentIndex(0);
184 
185  if (nvect > 0)
186  {
187  m_ui->m_vectorradioButton->setChecked(true);
188  onVectorToggled();
189  }
190  else if (ngrid > 0)
191  {
192  m_ui->m_gridradioButton->setChecked(true);
193  onGridToggled();
194  }
195 
196 }
197 
199 {
200  return m_outputLayer;
201 }
202 
204 {
205  if (m_ui->m_gridradioButton->isChecked())
206  return;
207 
208  m_ui->m_inputtabWidget->setTabEnabled(0, true);
209  m_ui->m_inputtabWidget->setTabEnabled(1, true);
210  m_ui->m_inputtabWidget->setTabEnabled(2, false);
211 
212  m_ui->m_interpolatorComboBox->clear();
213 
215 
216  m_ui->m_interpolatorComboBox->addItem("Weighted Avg./Z Value/Quadrant");
217  m_ui->m_interpolatorComboBox->addItem("Weighted Average/Quadrant");
218  m_ui->m_interpolatorComboBox->addItem("Weighted Average");
219  m_ui->m_interpolatorComboBox->addItem("Simple Average");
220  m_ui->m_interpolatorComboBox->addItem("Nearest Neighbor");
221  m_ui->m_interpolatorComboBox->addItem("Bilinear Spline");
222  m_ui->m_interpolatorComboBox->addItem("Bicubic Spline");
223  m_ui->m_interpolatorComboBox->addItem("Mitasova Spline");
224 
225  // m_ui->m_interparamStackedWidget->setEnabled(true);
226 
227  int index = m_sampleinputLayer ? m_ui->m_samplescomboBox->findText(m_sampleinputLayer->getTitle().c_str()) : 0;
229 
230  index = m_isoinputLayer ? m_ui->m_isolinescomboBox->findText(m_isoinputLayer->getTitle().c_str()) : 0;
232 
233 }
234 
236 {
237  if (!m_ui->m_gridradioButton->isChecked())
238  return;
239 
240  m_ui->m_inputtabWidget->setTabEnabled(0, false);
241  m_ui->m_inputtabWidget->setTabEnabled(1, false);
242  m_ui->m_inputtabWidget->setTabEnabled(2, true);
243 
244  m_ui->m_interparamStackedWidget->setCurrentIndex(3);
245 
246  int index = m_inputLayer ? m_ui->m_layersComboBox->findText(m_inputLayer->getTitle().c_str()) : 0;
247 
248  onInputComboBoxChanged(index);
249 }
250 
252 {
253  LayerSearchDialog search(this->parentWidget());
254  search.setLayers(m_layers);
255  QList<mntType> types;
256  types.append(ISOLINE);
257  search.setActive(types);
258 
259  if (search.exec() != QDialog::Accepted)
260  return;
261 
262  int index = m_ui->m_isolinescomboBox->findText(search.getLayer()->getTitle().c_str());
263  m_ui->m_isolinescomboBox->setCurrentIndex(index);
264 
265 }
266 
268 {
269  m_isoinputLayer = nullptr;
270 
271  m_ui->m_isolinesZcomboBox->clear();
272 
273  if (index <= 0)
274  return;
275 
276  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
277  std::string layerID = m_ui->m_isolinescomboBox->itemData(index, Qt::UserRole).toString().toUtf8().data();
278 
279  while (it != m_layers.end())
280  {
281  if(layerID == it->get()->getId())
282  {
283  m_isoinputLayer = it->get();
284 
285  if (m_sampleinputLayer)
286  if (m_isoinputLayer->getSRID() != m_sampleinputLayer->getSRID())
287  {
288  QMessageBox::warning(this, tr("DTM Generation"), tr("Input Layers with different SRIDs!"));
289  }
290 
291  double width = m_sampleinputLayer ? m_sampleinputLayer->getExtent().getWidth() : m_isoinputLayer ? m_isoinputLayer->getExtent().getWidth() : 0;
292  double height = m_sampleinputLayer ? m_sampleinputLayer->getExtent().getHeight() : m_isoinputLayer ? m_isoinputLayer->getExtent().getHeight() : 0;
293 
294  double raio = std::sqrt(width * width + height * height) / 5.;
295  m_ui->m_radiusLineEdit->setText(QString::number(raio, 'f', 4));
296 
297  setSRID(m_isoinputLayer->getSRID());
298 
299  std::unique_ptr<te::da::DataSetType> dsType(m_isoinputLayer->getSchema());
300 
301  m_isoinDataSource = te::da::GetDataSource(m_isoinputLayer->getDataSourceId(), true);
302  if (!m_isoinDataSource.get())
303  return;
304 
307 
308  m_isoinSetName = m_isoinputLayer->getDataSetName();
309 
310  std::unique_ptr<te::da::DataSet> inDset(m_isoinDataSource->getDataSet(m_isoinSetName));
311  std::size_t geo_pos = te::da::GetFirstPropertyPos(inDset.get(), te::dt::GEOMETRY_TYPE);
312  inDset->moveFirst();
313  if (inDset->getGeometry(geo_pos)->is3D())
314  {
315  m_ui->m_isolinesZcomboBox->hide();
316  m_ui->m_isolinesZlabel->hide();
317  return;
318  }
319 
320  m_ui->m_isolinesZcomboBox->show();
321  m_ui->m_isolinesZlabel->show();
322  std::vector<te::dt::Property*> props = dsType->getProperties();
323  for (std::size_t i = 0; i < props.size(); ++i)
324  {
325  switch (props[i]->getType())
326  {
327  case te::dt::FLOAT_TYPE:
328  case te::dt::DOUBLE_TYPE:
329  case te::dt::INT16_TYPE:
330  case te::dt::INT32_TYPE:
331  case te::dt::INT64_TYPE:
332  case te::dt::UINT16_TYPE:
333  case te::dt::UINT32_TYPE:
334  case te::dt::UINT64_TYPE:
336  m_ui->m_isolinesZcomboBox->addItem(QString(props[i]->getName().c_str()), QVariant(props[i]->getName().c_str()));
337  break;
338  default:
339  break;
340  }
341  }
342 
343  break;
344  }
345  it++;
346  }
347 }
348 
350 {
351  LayerSearchDialog search(this->parentWidget());
352  search.setLayers(m_layers);
353  QList<mntType> types;
354  types.append(SAMPLE);
355  search.setActive(types);
356 
357  if (search.exec() != QDialog::Accepted)
358  return;
359 
360  int index = m_ui->m_samplescomboBox->findText(search.getLayer()->getTitle().c_str());
361  m_ui->m_samplescomboBox->setCurrentIndex(index);
362 }
363 
365 {
366  m_sampleinputLayer = nullptr;
367 
368  m_ui->m_samplesZcomboBox->clear();
369  m_ui->m_samplesZcomboBox->hide();
370  m_ui->m_samplesZlabel->hide();
371 
372  if (index <= 0)
373  return;
374 
375  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
376  std::string layerID = m_ui->m_samplescomboBox->itemData(index, Qt::UserRole).toString().toUtf8().data();
377 
378  while (it != m_layers.end())
379  {
380  if(layerID == it->get()->getId())
381  {
382  m_sampleinputLayer = it->get();
383 
384  if (m_isoinputLayer)
385  if (m_isoinputLayer->getSRID() != m_sampleinputLayer->getSRID())
386  {
387  QMessageBox::warning(this, tr("DTM Generation"), tr("Input Layers with different SRIDs!"));
388  }
389 
390  double width = m_sampleinputLayer ? m_sampleinputLayer->getExtent().getWidth() : m_isoinputLayer ? m_isoinputLayer->getExtent().getWidth() : 0;
391  double height = m_sampleinputLayer ? m_sampleinputLayer->getExtent().getHeight() : m_isoinputLayer ? m_isoinputLayer->getExtent().getHeight() : 0;
392 
393  double raio = std::sqrt(width * width + height * height) / 5.;
394  m_ui->m_radiusLineEdit->setText(QString::number(raio, 'f', 4));
395 
396  setSRID(m_sampleinputLayer->getSRID());
397 
398  std::unique_ptr<te::da::DataSetType> dsType(m_sampleinputLayer->getSchema());
399 
401  if (!m_sampleinDataSource.get())
402  return;
403 
404  m_sampleinSetName = m_sampleinputLayer->getDataSetName();
405 
408 
409  std::unique_ptr<te::da::DataSet> inDset(m_sampleinDataSource->getDataSet(m_sampleinSetName));
410  std::size_t geo_pos = te::da::GetFirstPropertyPos(inDset.get(), te::dt::GEOMETRY_TYPE);
411  inDset->moveFirst();
412  std::unique_ptr<te::gm::Geometry> gin(inDset->getGeometry(geo_pos));
413  if (gin->is3D())
414  {
415  m_ui->m_samplesZcomboBox->hide();
416  m_ui->m_samplesZlabel->hide();
417  return;
418  }
419 
420  m_ui->m_samplesZcomboBox->show();
421  m_ui->m_samplesZlabel->show();
422  std::vector<te::dt::Property*> props = dsType->getProperties();
423  for (std::size_t i = 0; i < props.size(); ++i)
424  {
425  switch (props[i]->getType())
426  {
427  case te::dt::FLOAT_TYPE:
428  case te::dt::DOUBLE_TYPE:
429  case te::dt::INT16_TYPE:
430  case te::dt::INT32_TYPE:
431  case te::dt::INT64_TYPE:
432  case te::dt::UINT16_TYPE:
433  case te::dt::UINT32_TYPE:
434  case te::dt::UINT64_TYPE:
436  m_ui->m_samplesZcomboBox->addItem(QString(props[i]->getName().c_str()), QVariant(props[i]->getName().c_str()));
437  break;
438  default:
439  break;
440  }
441  }
442  break;
443  }
444  it++;
445  }
446 }
447 
449 {
450  m_inputLayer = nullptr;
451 
452  if (index <= 0)
453  return;
454 
455  m_ui->m_interpolatorComboBox->clear();
456  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
457  std::string layerID = m_ui->m_layersComboBox->itemData(index, Qt::UserRole).toString().toUtf8().data();
458 
459  while (it != m_layers.end())
460  {
461  if(layerID == it->get()->getId())
462  {
463  m_inputLayer = it->get();
464 
465  setSRID(m_inputLayer->getSRID());
466 
467  m_inDataSource = te::da::GetDataSource(m_inputLayer->getDataSourceId(), true);
468  if (!m_inDataSource.get())
469  return;
470 
471  m_inSetName = m_inputLayer->getDataSetName();
472 
473  std::unique_ptr<te::da::DataSetType> dsType(m_inputLayer->getSchema());
474 
475  m_inputType = getMNTType(dsType.get());
476 
477  if (m_inputType == TIN)
478  {
480  m_ui->m_interpolatorComboBox->addItem("Linear");
481  m_ui->m_interpolatorComboBox->addItem("Quintic without breaklines");
482 
483  std::string tname("type1");
484  if (ds->propertyExists(m_inputLayer->getDataSetName(), tname))
485  {
486  std::string qry("Select type1, type2, type3 from ");
487  qry += m_inputLayer->getTitle();
488  qry += " where (type1 > 3 and type1 < 7) or (type2 > 3 and type2 < 7) or (type3 > 3 and type3 < 7)";
489  std::unique_ptr<te::da::DataSet> dataquery(ds->query(qry));
490  if (!dataquery->isEmpty())
491  m_ui->m_interpolatorComboBox->addItem("Quintic with breaklines");
492  }
493  m_ui->m_dummycheckBox->setVisible(false);
494  m_ui->m_dummylineEdit->setVisible(false);
495  }
496 
497  if (m_inputType == GRID)
498  {
499  m_inputType = GRID;
500  m_ui->m_interpolatorComboBox->addItem("Bilinear");
501  m_ui->m_interpolatorComboBox->addItem("Bicubic");
502 
503  std::size_t rpos = te::da::GetFirstPropertyPos(m_inputLayer->getData().get(), te::dt::RASTER_TYPE);
504  std::unique_ptr<te::rst::Raster> inputRst(m_inputLayer->getData()->getRaster(rpos).release());
505  m_ui->m_dummycheckBox->setVisible(true);
506  m_ui->m_dummylineEdit->setVisible(true);
507  m_ui->m_dummylineEdit->setText(QString::number(inputRst->getBand(0)->getProperty()->m_noDataValue));
508 
509  }
512  break;
513  }
514  it++;
515  }
516 }
517 
519 {
520  LayerSearchDialog search(this->parentWidget());
521  search.setLayers(m_layers);
522  QList<mntType> types;
523  types.append(GRID);
524  types.append(TIN);
525  search.setActive(types);
526 
527  if (search.exec() != QDialog::Accepted)
528  {
529  return;
530  }
531 
532  int index = m_ui->m_layersComboBox->findText(search.getLayer()->getTitle().c_str());
533  m_ui->m_layersComboBox->setCurrentIndex(index);
534 }
535 
537 {
538  std::string inter = m_ui->m_interpolatorComboBox->itemData(index, Qt::UserRole).toString().toUtf8().data();
539 
540  switch (m_inputType)
541  {
542  case SAMPLE:
543  {
544  switch (index)
545  {
546  case 0: //Weighted Average/Z Value/Quadrant
547  case 1: //Weighted Average/Quadrant
548  case 2: //Weighted Average
549  m_ui->m_interparamStackedWidget->setCurrentIndex(0);
550  m_ui->m_powerLabel->show();
551  m_ui->m_powerComboBox->show();
552  m_ui->m_radiusLabel->show();
553  m_ui->m_radiusLineEdit->show();
554  break;
555  case 3: //Simple Average
556  case 4: //Nearest Neighbor
557  m_ui->m_interparamStackedWidget->setCurrentIndex(0);
558  m_ui->m_powerLabel->hide();
559  m_ui->m_powerComboBox->hide();
560  m_ui->m_radiusLabel->show();
561  m_ui->m_radiusLineEdit->show();
562  break;
563  case 5: //Bilinear Spline
564  case 6: //Bicubic Spline
565  m_ui->m_interparamStackedWidget->setCurrentIndex(1);
566  break;
567  case 7: //Mitasova Spline
568  m_ui->m_interparamStackedWidget->setCurrentIndex(2);
569  break;
570  default:
571  break;
572  }
573  break;
574  }
575  case TIN:
576  m_ui->m_interparamStackedWidget->setCurrentIndex(3);
577  switch (index)
578  {
579  case 0:
580  case 1:
581  case 2:
582  break;
583  default:
584  break;
585  }
586  break;
587  case GRID:
588  m_ui->m_interparamStackedWidget->setCurrentIndex(3);
589  switch (index)
590  {
591  case 0:
592  case 1:
593  break;
594  default:
595  break;
596  }
597  break;
598  case OTHER:
599  case ISOLINE:
600  default:
601  m_ui->m_interparamStackedWidget->setCurrentIndex(3);
602  break;
603  }
604 }
605 
607 {
608  double resX = m_ui->m_resXLineEdit->text().toDouble();
609  if (resX == 0.)
610  {
611  m_ui->m_dimCLineEdit->setText("");
612  return;
613  }
614 
615  te::map::AbstractLayerPtr inlayer = nullptr;
616  if (m_ui->m_vectorradioButton->isChecked())
618  else
619  inlayer = m_inputLayer;
620 
621  te::gm::Envelope env = inlayer ? inlayer->getExtent() : te::gm::Envelope(0, 0, 0, 0);
622 
623  if (!env.isValid())
624  {
625  QMessageBox::warning(this, tr("DTM Generation"), tr("Invalid envelope!"));
626  return;
627  }
628 
629  int maxCols = (int)ceil((env.m_urx - env.m_llx) / resX);
630 
631  m_ui->m_dimCLineEdit->setText(QString::number(maxCols));
632 }
633 
635 {
636  double resY = m_ui->m_resYLineEdit->text().toDouble();
637  if (resY == 0.)
638  {
639  m_ui->m_dimLLineEdit->setText("");
640  return;
641  }
642 
643  te::map::AbstractLayerPtr inlayer = nullptr;
644  if (m_ui->m_vectorradioButton->isChecked())
646  else
647  inlayer = m_inputLayer;
648 
649  te::gm::Envelope env = inlayer ? inlayer->getExtent() : te::gm::Envelope(0, 0, 0, 0);
650 
651  if (!env.isValid())
652  {
653  QMessageBox::warning(this, tr("DTM Generation"), tr("Invalid envelope!"));
654  return;
655  }
656 
657  int maxRows = (int)ceil((env.m_ury - env.m_lly) / resY);
658 
659  m_ui->m_dimLLineEdit->setText(QString::number(maxRows));
660 }
661 
663 {
664  int cols = m_ui->m_dimCLineEdit->text().toInt();
665  if (cols == 0)
666  {
667  m_ui->m_resXLineEdit->setText("");
668  return;
669  }
670 
671  te::map::AbstractLayerPtr inlayer = nullptr;
672  if (m_ui->m_vectorradioButton->isChecked())
674  else
675  inlayer = m_inputLayer;
676 
677  te::gm::Envelope env = inlayer ? inlayer->getExtent() : te::gm::Envelope(0, 0, 0, 0);
678 
679  if (!env.isValid())
680  {
681  QMessageBox::warning(this, tr("DTM Generation"), tr("Invalid envelope!"));
682  return;
683  }
684 
685  double resX = (env.m_urx - env.m_llx) / cols;
686 
687  m_ui->m_resXLineEdit->setText(QString::number(resX));
688 }
689 
691 {
692  int rows = m_ui->m_dimLLineEdit->text().toInt();
693  if (rows == 0)
694  {
695  m_ui->m_resYLineEdit->setText("");
696  return;
697  }
698 
699  te::map::AbstractLayerPtr inlayer = nullptr;
700  if (m_ui->m_vectorradioButton->isChecked())
702  else
703  inlayer = m_inputLayer;
704 
705  te::gm::Envelope env = inlayer ? inlayer->getExtent() : te::gm::Envelope(0, 0, 0, 0);
706 
707  if (!env.isValid())
708  {
709  QMessageBox::warning(this, tr("DTM Generation"), tr("Invalid envelope!"));
710  return;
711  }
712 
713  double resY = (env.m_ury - env.m_lly) / rows;
714 
715  m_ui->m_resYLineEdit->setText(QString::number(resY));
716 }
717 
719 {
720  m_ui->m_newLayerNameLineEdit->clear();
721  m_ui->m_repositoryLineEdit->clear();
722 
724 
725  try {
726  fileDialog.exec();
727  }
728  catch (const std::exception& e)
729  {
730  QMessageBox::information(this, tr("DTM Generation"), e.what());
731  return;
732  }
733 
734  m_ui->m_repositoryLineEdit->setText(fileDialog.getPath().c_str());
735  m_ui->m_newLayerNameLineEdit->setText(fileDialog.getFileName().c_str());
736 
737  m_ui->m_newLayerNameLineEdit->setEnabled(false);
738 }
739 
741 {
742  m_ui->m_newLayerNameLineEdit->clear();
743  m_ui->m_newLayerNameLineEdit->setEnabled(true);
745  dlg.exec();
746 
747  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
748 
749  if (dsPtrList.empty())
750  return;
751 
752  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
753 
754  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
755 
756  m_outputDatasource = *it;
757 
758 }
759 
761 {
762  //progress
764 
765  try
766  {
767  QApplication::setOverrideCursor(Qt::WaitCursor);
768 
769  if (!m_inputLayer.get() && !m_isoinputLayer.get() && !m_sampleinputLayer.get())
770  throw te::common::Exception(TE_TR("Select a input layer."));
771 
772  std::map<std::string, std::string> outdsinfo;
773  std::string inDsetName = m_inSetName;
774 
775  // Checking consistency of output paramenters
776  if (m_ui->m_repositoryLineEdit->text().isEmpty())
777  throw te::common::Exception(TE_TR("Select a repository for the resulting layer."));
778 
779  if (m_ui->m_newLayerNameLineEdit->text().isEmpty())
780  throw te::common::Exception(TE_TR("Define a name for the resulting layer."));
781 
782  std::string outputdataset = m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
783  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toUtf8().data());
784 
785  if (te::core::FileSystem::exists(uri.string()))
786  throw te::common::Exception(TE_TR("Output file already exists. Remove it or select a new name and try again."));
787 
788  std::size_t idx = outputdataset.find(".");
789  if (idx != std::string::npos)
790  outputdataset = outputdataset.substr(0, idx);
791 
792  outdsinfo["URI"] = uri.string();
793 
794  double radius = m_ui->m_radiusLineEdit->text().toDouble();
795  int pow = m_ui->m_powerComboBox->currentText().toInt();
796 
797  bool ok;
798  double resxo = m_ui->m_resXLineEdit->text().toDouble(&ok);
799  if (!ok)
800  throw te::common::Exception(TE_TR("Define X resolution."));
801  double resyo = m_ui->m_resYLineEdit->text().toDouble(&ok);
802  if (!ok)
803  throw te::common::Exception(TE_TR("Define Y resolution."));
804 
805  double min = std::numeric_limits<double>::max();
806  double max = std::numeric_limits<double>::min();
807 
808  switch (m_inputType)
809  {
810  case SAMPLE:
811  {
812  int inter_i = m_ui->m_interpolatorComboBox->currentIndex();
813  bool spline = false;
814  switch (inter_i)
815  {
816  case 0://Weighted Avg./Z Value/Quadrant
818  break;
819  case 1://Weighted Average/Quadrant
820  m_inter = MediaQuad;
821  break;
822  case 2://Weighted Average;
824  break;
825  case 3://Simple Average;
826  m_inter = Media;
827  break;
828  case 4://Nearest Neighbor
829  m_inter = Vizinho;
830  break;
831  case 5://Bilinear Spline
833  spline = true;
834  break;
835  case 6://Bicubic Spline
837  spline = true;
838  break;
839  case 7://Mitasova Spline
840  m_inter = Mitasova;
841  spline = true;
842  break;
843  default:
844  break;
845  }
846 
847  if (!spline)
848  {
850 
851  if (m_isoinputLayer.get())
852  {
854  }
855  if (m_sampleinputLayer.get())
856  {
858  }
859  grid->setOutput(outdsinfo);
860  grid->setParams(m_ui->m_isolinesZcomboBox->currentText().toUtf8().data(), m_ui->m_samplesZcomboBox->currentText().toUtf8().data(), resxo, resyo, m_inter, radius, pow);
861  grid->setSRID(m_outsrid);
862 
863  grid->run();
864 
865  min = grid->getMin();
866  max = grid->getMax();
867 
868  delete grid;
869  }
870  else if (m_inter == Mitasova)
871  {
872  int mp = m_ui->m_minPtsMitLineEdit->text().toInt();
873  double t = m_ui->m_tensionLineEdit->text().toDouble();
874  double s = m_ui->m_smothLineEdit->text().toDouble();
876 
877  if (m_isoinputLayer.get())
878  {
880  }
881 
882  if (m_sampleinputLayer.get())
883  {
885  }
886 
887  grid->setOutput(outdsinfo);
888  grid->setParams(m_ui->m_isolinesZcomboBox->currentText().toUtf8().data(), m_ui->m_samplesZcomboBox->currentText().toUtf8().data(), resxo, resyo, m_inter, radius, pow);
889  grid->setSRID(m_outsrid);
890 
891  grid->calculateGrid();
892 
893  min = grid->getMin();
894  max = grid->getMax();
895 
896  delete grid;
897 
898  }
899  else
900  {
901  unsigned int px = m_ui->m_sepXSpinBox->text().toUInt();
902  unsigned int py = m_ui->m_sepYSpinBox->text().toUInt();
903  unsigned int mp = m_ui->m_minPtsSpinBox->text().toUInt();
904  double ov = m_ui->m_overDoubleSpinBox->text().toDouble();
905 
907 
908  if (m_isoinputLayer.get())
909  {
911  }
912  if (m_sampleinputLayer.get())
913  {
915  }
916  grid->setOutput(outdsinfo);
917  grid->setParams(m_ui->m_isolinesZcomboBox->currentText().toUtf8().data(), m_ui->m_samplesZcomboBox->currentText().toUtf8().data(), resxo, resyo, m_inter, radius, pow);
918  grid->setSRID(m_outsrid);
919 
920  grid->generateGrid();
921 
922  min = grid->getMin();
923  max = grid->getMax();
924 
925  delete grid;
926 
927  }
928  break;
929  }
930  case TIN:
931  {
932  int inter_i = m_ui->m_interpolatorComboBox->currentIndex();
933  switch (inter_i)
934  {
935  case 0: //Linear
936  m_inter = Linear;
937  break;
938  case 1: //Quintico
939  m_inter = Quintico;
940  break;
941  case 2: //Quintico breakline
943  }
944 
946 
948  grid->setOutput(outdsinfo);
949  grid->setParams(resxo, resyo, m_inter);
950  grid->setSRID(m_outsrid);
951 
952  grid->run();
953 
954  min = grid->getMin();
955  max = grid->getMax();
956 
957  delete grid;
958  break;
959  }
960  case GRID:
961  {
962  //get input raster
963  std::unique_ptr<te::da::DataSet> inds(m_inputLayer->getData());
964  std::size_t rpos = te::da::GetFirstPropertyPos(inds.get(), te::dt::RASTER_TYPE);
965  std::unique_ptr<te::rst::Raster> inputRst(inds->getRaster(rpos).release());
966  if (inputRst->getNumberOfBands() > 1)
967  {
968  throw te::common::Exception(TE_TR("Layer isn't Regular Grid."));
969  }
970 
971  std::vector< std::complex<double> > dummy;
972  if (m_ui->m_dummycheckBox->isChecked())
973  {
974  bool ok;
975  dummy.push_back(m_ui->m_dummylineEdit->text().toDouble(&ok));
976  if (!ok)
977  throw te::common::Exception(TE_TR("Define Dummy Value."));
978  }
979  else
980  dummy.push_back(inputRst->getBand(0)->getProperty()->m_noDataValue);
981 
982  int inter_i = m_ui->m_interpolatorComboBox->currentIndex();
983  int inter = 0;
984  switch (inter_i)
985  {
986  case 0: //Bilinear
987  inter = te::rst::Bilinear;
988  break;
989  case 1: //Bicubico
990  inter = te::rst::Bicubic;
991  break;
992  }
993 
994  te::rst::Interpolator interp(inputRst.get(), inter, dummy);
995  double resxi = inputRst->getResolutionX();
996  double resyi = inputRst->getResolutionY();
997  unsigned int outputWidth = m_ui->m_dimCLineEdit->text().toUInt();
998  unsigned int outputHeight = m_ui->m_dimLLineEdit->text().toUInt();
999  int X1 = inputRst->getExtent()->getLowerLeftX();
1000  int Y2 = inputRst->getExtent()->getUpperRightY();
1001  te::gm::Coord2D ulc(X1, Y2);
1002  te::rst::Grid* grid = new te::rst::Grid(outputWidth, outputHeight, resxo, resyo, &ulc, m_outsrid);
1003 
1004  std::vector<te::rst::BandProperty*> bands;
1005  bands.push_back(new te::rst::BandProperty(0, te::dt::DOUBLE_TYPE, "DTM GRID"));
1006  bands[0]->m_nblocksx = 1;
1007  bands[0]->m_nblocksy = (int)outputHeight;
1008  bands[0]->m_blkw = (int)outputWidth;
1009  bands[0]->m_blkh = 1;
1010  bands[0]->m_colorInterp = te::rst::GrayIdxCInt;
1011 
1012  te::common::TaskProgress task("Calculating DTM...", te::common::TaskProgress::UNDEFINED, (int)(outputHeight*outputWidth));
1013 
1014  // create raster
1015  std::unique_ptr<te::rst::Raster> outRst(te::rst::RasterFactory::make("GDAL", grid, bands, outdsinfo));
1016  te::rst::Raster* out = outRst.get();
1017 
1018  std::vector<std::complex<double> > value;
1019  double xi, yi, xo, yo;
1020 
1021  for (unsigned int l = 0; l < outputHeight; l++)
1022  {
1023  for (unsigned int c = 0; c < outputWidth; c++)
1024  {
1025  if (!task.isActive())
1026  throw te::common::Exception(TE_TR("Canceled by user"));
1027 
1028  task.pulse();
1029  // Calculate the x and y coordinates of (l,c) corner of the output grid
1030  xo = (X1 + c * resxo + resxo / 2.);
1031  yo = (Y2 - l * resyo - resyo / 2.);
1032 
1033  // Calculate position of point (xs,ys) in the input grid
1034  xi = ((xo - (X1 + resxi / 2.)) / resxi);
1035  yi = (((Y2 - resyi / 2.) - yo) / resyi);
1036 
1037  interp.getValues(xi, yi, value);
1038 
1039  if (value != dummy)
1040  {
1041  max = (value[0].real() > max) ? value[0].real() : max;
1042  min = (value[0].real() < min) ? value[0].real() : min;
1043  }
1044  out->setValues(c, l, value);
1045  }
1046  }
1047  break;
1048  }
1049  case ISOLINE:
1050  case OTHER:
1051  default:
1052  break;
1053  }
1054 
1055  m_outputLayer = te::qt::widgets::createLayer("GDAL", outdsinfo);
1056 
1057  std::unique_ptr<te::rst::Raster> rst(te::map::GetRaster(m_outputLayer.get()));
1058 
1059  te::qt::widgets::applyRasterMultiResolution(tr("DTM Generation"), rst.get());
1060 
1062 
1063  (m_outputLayer.get())->setRasterContrast(contrast);
1064  for (size_t b = 0; b < rst->getNumberOfBands(); ++b)
1065  {
1066  double gain, offset1, offset2;
1067  if (te::rp::Contrast::getGainAndOffset(te::rp::Contrast::InputParameters::LinearContrastT, min, max, 0., 255., gain, offset1, offset2))
1068  {
1069  contrast->setValues(gain, offset1, offset2, min, max, b);
1070  }
1071  }
1072  }
1073  catch (te::common::Exception& e)
1074  {
1075  QApplication::restoreOverrideCursor();
1076  QMessageBox::information(this, "DTM Generation", e.what());
1077  return;
1078  }
1079 
1080  QApplication::restoreOverrideCursor();
1081  accept();
1082 }
1083 
1085 {
1086  reject();
1087 }
1088 
1090 {
1091  te::qt::widgets::SRSManagerDialog srsDialog(this);
1092  srsDialog.setWindowTitle(tr("Choose the SRS"));
1093 
1094  if (srsDialog.exec() == QDialog::Rejected)
1095  return;
1096 
1097  int newSRID = srsDialog.getSelectedSRS().first;
1098 
1099  setSRID(newSRID);
1100 
1101 }
1102 
1104 {
1105  if (newSRID <= 0)
1106  {
1107  m_ui->m_resSRIDLabel->setText("No SRS defined");
1108  }
1109  else
1110  {
1111  std::string name = te::srs::SpatialReferenceSystemManager::getInstance().getName((unsigned int)newSRID);
1112  if (name.size())
1113  m_ui->m_resSRIDLabel->setText(name.c_str());
1114  else
1115  m_ui->m_resSRIDLabel->setText(QString("%1").arg(newSRID));
1116  }
1117  m_outsrid = newSRID;
1118 
1119 }
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::unique_ptr< te::da::DataSetType > inDsetType, InputType type)
It sets the Datasource that is being used to generate TIN.
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
te::da::DataSourcePtr m_isoinDataSource
input DataSourcePtr
virtual void setValues(unsigned int c, unsigned int r, const std::vector< double > &values)
Sets the imaginary attribute values in all complex bands of a cell.
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
Defines a component for choose a file.
Definition: FileDialog.h:52
bool run()
Calculate GRID \ return true or false.
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
Index into a lookup table.
boost::shared_ptr< DataSource > DataSourcePtr
te::map::AbstractLayerPtr m_sampleinputLayer
Input layer.
void TINCalculateGrid()
A raster band description.
Definition: BandProperty.h:61
std::unique_ptr< Ui::MNTGenerationDialogForm > m_ui
te::da::DataSourcePtr m_inDataSource
input DataSourcePtr
virtual const char * what() const
It outputs the exception message.
bool run()
Method that generates a regular Grid from a given TIN.
It interpolates one pixel based on a selected algorithm. Methods currently available are Nearest Neig...
Definition: Interpolator.h:55
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
static bool getGainAndOffset(const InputParameters::ContrastType &type, const double &inRangeMin, const double &inRangeMax, const double &outRangeMin, const double &outRangeMax, double &gain, double &offset1, double &offset2)
Returns gain and offset values for contrast types (when applicable).
double m_urx
Upper right corner x-coordinate.
std::string m_isoinSetName
input DataSetLayer name
A dialog Layer Search.
te::map::AbstractLayerPtr m_inputLayer
Input layer.
double getMin()
Definition: Tin.h:306
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
static te::dt::Date ds(2010, 01, 01)
mntType m_inputType
Input type (SAMPLE, TIN, GRID)
A dialog Retangular Grid generation.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
std::list< te::map::AbstractLayerPtr > m_layers
List of layers.
bool isActive() const
Verify if the task is active.
Raster property.
void setSRID(int srid)
Definition: Tin.cpp:177
te::da::DataSourceInfoPtr m_outputDatasource
DataSource information.
int b
Definition: TsRtree.cpp:32
te::da::DataSourcePtr m_sampleinDataSource
input DataSourcePtr
void setParams(const std::string &attriso, const std::string &attrpt, double resx, double resy, Interpolator gt, double rad, int pow)
It sets the parameters that is being used to save TIN.
double m_llx
Lower left corner x-coordinate.
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
An abstract class for raster data strucutures.
Class to calculate grid from TIN.
te::map::AbstractLayerPtr getLayer()
void exec()
This method will open the dialog of file selection and populate the class members with the chosen fil...
Definition: FileDialog.cpp:54
URI C++ Library.
Definition: Attributes.h:37
double getMax()
Definition: Tin.h:307
list bands
Definition: compose.py:2
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
te::map::AbstractLayerPtr m_outputLayer
Generated Layer.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
double m_lly
Lower left corner y-coordinate.
Bicubic interpolation method.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
This class contains the parameters needed to apply dynamic contrast over a raster.
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
std::string getPath()
This method will return the chosen path.
Definition: FileDialog.cpp:103
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
te::map::AbstractLayerPtr m_isoinputLayer
Input layer.
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const te::core::URI &connInfo)
double m_ury
Upper right corner y-coordinate.
static Raster * make()
It creates and returns an empty raster with default raster driver.
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::unique_ptr< te::da::DataSetType > inDsetType)
It sets the Datasource that is being used to generate TIN.
void setActive(const QList< mntType > &types)
void setOutput(std::map< std::string, std::string > &dsinfo)
It sets the Datasource that is being used to save TIN.
A dialog for selecting a data source.
Bilinear interpolation method.
std::string m_sampleinSetName
input DataSetLayer name
void setParams(double resx, double resy, Interpolator gt)
It sets the parameters that is being used to save TIN.
std::string getFileName()
This method will return the file name.
Definition: FileDialog.cpp:113
void setOutput(std::map< std::string, std::string > &dsinfo)
It sets the Datasource that is being used to save TIN.
std::vector< te::rst::BandProperty * > & getBandProperties()
Returns a reference to the list of bands definitions.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
TEMNTEXPORT te::mnt::mntType getMNTType(const te::da::DataSetType *dt)
MNTGenerationDialog(QWidget *parent=0, Qt::WindowFlags f=0)
A dialog used to build a SRSManagerDialog element.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void CalculateGrid()
std::string m_inSetName
input DataSetLayer name
const std::pair< int, std::string > & getSelectedSRS() const
Returns the selected SRS in the window.
bool isValid() const
It tells if the rectangle is valid or not.
TEQTWIDGETSEXPORT void applyRasterMultiResolution(const QString &toolName, te::rst::Raster *raster)
te::map::AbstractLayerPtr getLayer()