SlopeDialog.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/SlopenDialog.cpp
22 
23 \brief A dialog for Slope Grid generation
24 */
25 
26 //terralib
27 #include "../../core/filesystem/FileSystem.h"
28 #include "../../core/translator/Translator.h"
29 #include "../../common/progress/ProgressManager.h"
30 #include "../../dataaccess/datasource/DataSourceFactory.h"
31 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
32 #include "../../dataaccess/datasource/DataSourceManager.h"
33 #include "../../dataaccess/utils/Utils.h"
34 #include "../../geometry/GeometryProperty.h"
35 #include "../../maptools/DataSetLayer.h"
36 #include "../../maptools/RasterContrast.h"
37 #include "../../maptools/Utils.h"
38 #include "../../mnt/core/Slope.h"
39 #include "../../mnt/core/Utils.h"
40 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
41 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
42 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
43 #include "../../qt/widgets/rp/Utils.h"
44 #include "../../qt/widgets/srs/SRSManagerDialog.h"
45 #include "../../qt/widgets/Utils.h"
46 #include "../../qt/widgets/utils/FileDialog.h"
47 #include "../../raster.h"
48 #include "../../rp/Contrast.h"
49 #include "../../srs/SpatialReferenceSystemManager.h"
50 
51 #include "LayerSearchDialog.h"
52 #include "SlopeDialog.h"
53 #include "ui_SlopeDialogForm.h"
54 
55 // Qt
56 #include <QFileDialog>
57 #include <QMessageBox>
58 
59 // BOOST
60 #include <boost/algorithm/string.hpp>
61 #include <boost/filesystem.hpp>
62 #include <boost/lexical_cast.hpp>
63 #include <boost/uuid/random_generator.hpp>
64 #include <boost/uuid/uuid_io.hpp>
65 
66 te::mnt::SlopeDialog::SlopeDialog(QWidget* parent, Qt::WindowFlags f)
67  : QDialog(parent, f),
68  m_ui(new Ui::SlopeDialogForm),
69  m_layers(std::list<te::map::AbstractLayerPtr>())
70 {
71  // add controls
72  m_ui->setupUi(this);
73 
74  m_ui->m_dimLLineEdit->setValidator(new QIntValidator(this));
75  m_ui->m_dimCLineEdit->setValidator(new QIntValidator(this));
76  m_ui->m_resXLineEdit->setValidator(new QDoubleValidator(this));
77  m_ui->m_resYLineEdit->setValidator(new QDoubleValidator(this));
78 
79  //signals
80  connect(m_ui->m_layerSearchToolButton, SIGNAL(clicked()), this, SLOT(onInputLayerToolButtonClicked()));
81  connect(m_ui->m_layersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onInputComboBoxChanged(int)));
82 
83  connect(m_ui->m_dummycheckBox, SIGNAL(toggled(bool)), m_ui->m_dummylineEdit, SLOT(setEnabled(bool)));
84 
85  connect(m_ui->m_sloperadioButton, SIGNAL(toggled(bool)), this, SLOT(onSlopeToggled()));
86  connect(m_ui->m_aspectradioButton, SIGNAL(toggled(bool)), this, SLOT(onAspectToggled()));
87 
88  connect(m_ui->m_resXLineEdit, SIGNAL(editingFinished()), this, SLOT(onResXLineEditEditingFinished()));
89  connect(m_ui->m_resYLineEdit, SIGNAL(editingFinished()), this, SLOT(onResYLineEditEditingFinished()));
90  connect(m_ui->m_dimCLineEdit, SIGNAL(editingFinished()), this, SLOT(onDimCLineEditEditingFinished()));
91  connect(m_ui->m_dimLLineEdit, SIGNAL(editingFinished()), this, SLOT(onDimLLineEditEditingFinished()));
92 
93  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
94  connect(m_ui->m_targetFileToolButton, SIGNAL(clicked()), this, SLOT(onTargetFileToolButtonPressed()));
95  connect(m_ui->m_targetDatasourceToolButton, SIGNAL(clicked()), this, SLOT(onTargetDatasourceToolButtonPressed()));
96 
97  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
98  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
99 
100  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
101  m_ui->m_helpPushButton->setPageReference("plugins/mnt/DTM_Slope.html");
102 
103  m_ui->m_srsToolButton->setIcon(QIcon::fromTheme("srs"));
104  connect(m_ui->m_srsToolButton, SIGNAL(clicked()), this, SLOT(onSrsToolButtonClicked()));
105 
106  m_outsrid = 0;
107 
108 }
109 
111 
112 void te::mnt::SlopeDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
113 {
114  m_layers = layers;
115 
116  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
117 
118  while (it != m_layers.end())
119  {
120  if (it->get())
121  {
122  if (it->get()->isValid())
123  {
124  std::unique_ptr<te::da::DataSetType> dsType(it->get()->getSchema());
125  if (dsType.get())
126  {
127  mntType type = getMNTType(dsType.get());
128 
129  if (type == TIN || type == GRID)
130  m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
131  }
132  }
133  }
134  ++it;
135  }
136 }
137 
139 {
140  LayerSearchDialog search(this->parentWidget());
141  search.setLayers(m_layers);
142  QList<mntType> types;
143  types << TIN << GRID;
144  search.setActive(types);
145 
146  if (search.exec() != QDialog::Accepted)
147  {
148  return;
149  }
150 
151  int index = m_ui->m_layersComboBox->findText(search.getLayer().get()->getTitle().c_str());
152  m_ui->m_layersComboBox->setCurrentIndex(index);
153 }
154 
156 {
157  m_inputLayer = nullptr;
158  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
159  std::string layerID = m_ui->m_layersComboBox->itemData(index, Qt::UserRole).toString().toUtf8().data();
160  while (it != m_layers.end())
161  {
162  if(layerID == it->get()->getId())
163  {
164  m_inputLayer = it->get();
165 
166  setSRID(m_inputLayer->getSRID());
167 
168  std::unique_ptr<te::da::DataSetType> dsType = m_inputLayer->getSchema();
169  if (dsType->hasRaster()) //GRID
170  {
171  std::unique_ptr<te::rst::Raster> in_raster(te::map::GetRaster(m_inputLayer.get()));
172  m_ui->m_resXLineEdit->setText(QString::number(in_raster->getResolutionX()));
173  m_ui->m_resYLineEdit->setText(QString::number(in_raster->getResolutionY()));
174  m_ui->m_dimCLineEdit->setText(QString::number(in_raster->getNumberOfColumns()));
175  m_ui->m_dimLLineEdit->setText(QString::number(in_raster->getNumberOfRows()));
176  m_ui->m_resXLineEdit->setEnabled(false);
177  m_ui->m_resYLineEdit->setEnabled(false);
178  m_ui->m_dimCLineEdit->setEnabled(false);
179  m_ui->m_dimLLineEdit->setEnabled(false);
180  m_ui->m_dummycheckBox->setVisible(true);
181  m_ui->m_dummylineEdit->setVisible(true);
182  m_ui->m_dummylineEdit->setText(QString::number(in_raster->getBand(0)->getProperty()->m_noDataValue));
183  m_inputType = GRID;
184  }
185  else
186  {
187  m_ui->m_dummycheckBox->setVisible(false);
188  m_ui->m_dummylineEdit->setVisible(false);
189  m_ui->m_resXLineEdit->setEnabled(true);
190  m_ui->m_resYLineEdit->setEnabled(true);
191  m_ui->m_dimCLineEdit->setEnabled(true);
192  m_ui->m_dimLLineEdit->setEnabled(true);
193  m_inputType = TIN;
194  }
195  break;
196  }
197  ++it;
198  }
199 }
200 
202 {
203  return m_outputLayer;
204 }
205 
207 {
208  m_ui->m_degreesradioButton->setEnabled(true);
209  m_ui->m_percentradioButton->setEnabled(true);
210 }
211 
213 {
214  m_ui->m_degreesradioButton->setDisabled(true);
215  m_ui->m_percentradioButton->setDisabled(true);
216 }
217 
218 
220 {
221  double resX = m_ui->m_resXLineEdit->text().toDouble();
222 
223  te::gm::Envelope env = m_inputLayer->getExtent();
224 
225  if (!env.isValid())
226  {
227  QMessageBox::warning(this, tr("Slope"), tr("Invalid envelope!"));
228  return;
229  }
230 
231  int maxCols = (int)ceil((env.m_urx - env.m_llx) / resX);
232 
233  m_ui->m_dimCLineEdit->setText(QString::number(maxCols));
234 }
235 
237 {
238  double resY = m_ui->m_resYLineEdit->text().toDouble();
239 
240  te::gm::Envelope env = m_inputLayer->getExtent();
241 
242  if (!env.isValid())
243  {
244  QMessageBox::warning(this, tr("Slope"), tr("Invalid envelope!"));
245  return;
246  }
247 
248  int maxRows = (int)ceil((env.m_ury - env.m_lly) / resY);
249 
250  m_ui->m_dimLLineEdit->setText(QString::number(maxRows));
251 }
252 
254 {
255  int cols = m_ui->m_dimCLineEdit->text().toInt();
256 
257  te::gm::Envelope env = m_inputLayer->getExtent();
258 
259  if (!env.isValid())
260  {
261  QMessageBox::warning(this, tr("Slope"), tr("Invalid envelope!"));
262  return;
263  }
264 
265  double resX = (env.m_urx - env.m_llx) / cols;
266 
267  m_ui->m_resXLineEdit->setText(QString::number(resX));
268 }
269 
271 {
272  int rows = m_ui->m_dimLLineEdit->text().toInt();
273 
274  te::gm::Envelope env = m_inputLayer->getExtent();
275 
276  if (!env.isValid())
277  {
278  QMessageBox::warning(this, tr("Slope"), tr("Invalid envelope!"));
279  return;
280  }
281 
282  double resY = (env.m_ury - env.m_lly) / rows;
283 
284  m_ui->m_resYLineEdit->setText(QString::number(resY));
285 }
286 
288 {
289  m_ui->m_newLayerNameLineEdit->clear();
290  m_ui->m_repositoryLineEdit->clear();
291 
293 
294  try {
295  fileDialog.exec();
296  }
297  catch (const std::exception& e)
298  {
299  QMessageBox::information(this, tr("Slope"), e.what());
300  return;
301  }
302 
303  m_ui->m_repositoryLineEdit->setText(fileDialog.getPath().c_str());
304  m_ui->m_newLayerNameLineEdit->setText(fileDialog.getFileName().c_str());
305 
306  m_ui->m_newLayerNameLineEdit->setEnabled(false);
307 }
308 
310 {
311  m_ui->m_newLayerNameLineEdit->clear();
312  m_ui->m_newLayerNameLineEdit->setEnabled(true);
314  dlg.exec();
315 
316  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
317 
318  if (dsPtrList.empty())
319  return;
320 
321  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
322 
323  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
324 
325  m_outputDatasource = *it;
326 
327 }
328 
330 {
331 
332  //progress
334 
335  try
336  {
337  QApplication::setOverrideCursor(Qt::WaitCursor);
338  if (!m_inputLayer.get())
339  throw te::common::Exception(TE_TR("Select an input layer!"));
340 
341  te::map::DataSetLayer* indsLayer = dynamic_cast<te::map::DataSetLayer*>(m_inputLayer.get());
342  if (!indsLayer)
343  throw te::common::Exception(TE_TR("Can not execute this operation on this type of layer!"));
344 
345  te::da::DataSourcePtr inDataSource = te::da::GetDataSource(indsLayer->getDataSourceId(), true);
346  if (!inDataSource.get())
347  throw te::common::Exception(TE_TR("The selected input data source can not be accessed!"));
348 
349  std::string inDsetName = indsLayer->getDataSetName();
350  std::unique_ptr<te::da::DataSetType> inDsetType(inDataSource->getDataSetType(inDsetName));
351 
352  // Checking consistency of output paramenters
353  if (m_ui->m_repositoryLineEdit->text().isEmpty())
354  throw te::common::Exception(TE_TR("Select a repository for the resulting layer."));
355 
356  if (m_ui->m_newLayerNameLineEdit->text().isEmpty())
357  throw te::common::Exception(TE_TR("Define a name for the resulting layer."));
358 
359  std::string outputdataset = m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
360 
361  std::map<std::string, std::string> outdsinfo;
362  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toUtf8().data());
363 
364  if (te::core::FileSystem::exists(uri.string()))
365  throw te::common::Exception(TE_TR("Output file already exists! Remove it or select a new name and try again."));
366 
367  std::size_t idx = outputdataset.find(".");
368  if (idx != std::string::npos)
369  outputdataset = outputdataset.substr(0, idx);
370 
371  outdsinfo["URI"] = uri.string();
372 
373  te::mnt::Slope *decl = new te::mnt::Slope();
374  decl->setInput(inDataSource, inDsetName, inDataSource->getDataSetType(inDsetName));
375  decl->setOutput(outdsinfo);
376  char grad, slope;
377  if (m_ui->m_aspectradioButton->isChecked())
378  grad = 'a';
379  else
380  grad = 's';
381  if (m_ui->m_degreesradioButton->isChecked())
382  slope = 'g';
383  else
384  slope = 'p';
385 
386  double dummy = m_ui->m_dummylineEdit->text().toDouble();
387 
388  decl->setParams(m_ui->m_resXLineEdit->text().toDouble(), m_ui->m_resYLineEdit->text().toDouble(), grad, slope, m_outsrid, dummy);
389 
390  decl->run();
391 
392  delete decl;
393 
394  m_outputLayer = te::qt::widgets::createLayer("GDAL", outdsinfo);
395 
396  std::unique_ptr<te::rst::Raster> rst(te::map::GetRaster(m_outputLayer.get()));
397 
398  te::qt::widgets::applyRasterMultiResolution(tr("Slope"), rst.get());
399 
401 
402  (m_outputLayer.get())->setRasterContrast(contrast);
403  for (size_t b = 0; b < rst->getNumberOfBands(); ++b)
404  {
405  double min, max;
406  te::mnt::getMinMax(rst.get(), min, max);
407  double gain, offset1, offset2;
408  if (te::rp::Contrast::getGainAndOffset(te::rp::Contrast::InputParameters::LinearContrastT, min, max, 0., 255., gain, offset1, offset2))
409  {
410  contrast->setValues(gain, offset1, offset2, min, max, b);
411  }
412  }
413  }
414  catch (te::common::Exception& e)
415  {
416  QApplication::restoreOverrideCursor();
417  QMessageBox::information(this, tr("Slope "), e.what());
418  return;
419  }
420 
421  QApplication::restoreOverrideCursor();
422  accept();
423 }
424 
426 {
427  reject();
428 }
429 
431 {
432  te::qt::widgets::SRSManagerDialog srsDialog(this);
433  srsDialog.setWindowTitle(tr("Choose the SRS"));
434 
435  if (srsDialog.exec() == QDialog::Rejected)
436  return;
437 
438  int newSRID = srsDialog.getSelectedSRS().first;
439 
440  setSRID(newSRID);
441 }
442 
444 {
445  if (newSRID <= 0)
446  {
447  m_ui->m_resSRIDLabel->setText("No SRS defined");
448  }
449  else
450  {
451  std::string name = te::srs::SpatialReferenceSystemManager::getInstance().getName(newSRID);
452  if (name.size())
453  m_ui->m_resSRIDLabel->setText(name.c_str());
454  else
455  m_ui->m_resSRIDLabel->setText(QString("%1").arg(newSRID));
456  }
457  m_outsrid = newSRID;
458 
459 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
void setOutput(std::map< std::string, std::string > &dsinfo)
Definition: Slope.cpp:38
Defines a component for choose a file.
Definition: FileDialog.h:52
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
void onTargetDatasourceToolButtonPressed()
boost::shared_ptr< DataSource > DataSourcePtr
virtual const char * what() const
It outputs the exception message.
mntType m_inputType
Input type (TIN, GRID)
Definition: SlopeDialog.h:95
void onInputLayerToolButtonClicked()
std::unique_ptr< Ui::SlopeDialogForm > m_ui
Definition: SlopeDialog.h:87
te::map::AbstractLayerPtr getLayer()
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.
void onCancelPushButtonClicked()
A dialog Layer Search.
void onTargetFileToolButtonPressed()
SlopeDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Definition: SlopeDialog.cpp:66
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
A dialog Slope Grid generation.
void onInputComboBoxChanged(int index)
int b
Definition: TsRtree.cpp:32
double m_llx
Lower left corner x-coordinate.
te::da::DataSourceInfoPtr m_outputDatasource
DataSource information.
Definition: SlopeDialog.h:89
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)
te::map::AbstractLayerPtr m_outputLayer
Generated Layer.
Definition: SlopeDialog.h:93
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
void onResXLineEditEditingFinished()
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::unique_ptr< te::da::DataSetType > inDsetType)
Definition: Slope.cpp:28
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
double m_lly
Lower left corner y-coordinate.
void onResYLineEditEditingFinished()
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::string & getDataSetName() const
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const te::core::URI &connInfo)
double m_ury
Upper right corner y-coordinate.
void setActive(const QList< mntType > &types)
A dialog for selecting a data source.
void setParams(double resx, double resy, char gradtype, char slopetype, int srid, double dummy)
Definition: Slope.cpp:43
void onDimLLineEditEditingFinished()
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
std::string getFileName()
This method will return the file name.
Definition: FileDialog.cpp:113
void setSRID(int newSRID)
TEMNTEXPORT te::mnt::mntType getMNTType(const te::da::DataSetType *dt)
te::map::AbstractLayerPtr m_inputLayer
Input layer.
Definition: SlopeDialog.h:92
void onDimCLineEditEditingFinished()
TEMNTEXPORT void getMinMax(te::rst::Raster *inputRst, double &vmin, double &vmax)
A dialog used to build a SRSManagerDialog element.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
virtual const std::string & getDataSourceId() const
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)
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
std::list< te::map::AbstractLayerPtr > m_layers
List of layers.
Definition: SlopeDialog.h:91
bool run()
Definition: Slope.cpp:53