VolumeDialog.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/VolumeDialog.cpp
22 
23 \brief A dialog for Calculate Volume
24 */
25 
26 //terralib
27 #include "../../common/Exception.h"
28 #include "../../common/progress/ProgressManager.h"
29 #include "../../core/translator/Translator.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/Utils.h"
37 #include "../../mnt/core/Utils.h"
38 #include "../../mnt/core/Volume.h"
39 #include "../../qt/af/ApplicationController.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 "../../raster.h"
44 
45 
46 #include "LayerSearchDialog.h"
47 #include "VolumeDialog.h"
48 #include "VolumeResultDialog.h"
49 #include "ui_VolumeDialogForm.h"
50 
51 // Qt
52 #include <QFileDialog>
53 #include <QMessageBox>
54 
55 te::mnt::VolumeDialog::VolumeDialog(QWidget* parent, Qt::WindowFlags f)
56  : QDialog(parent, f),
57  m_ui(new Ui::VolumeDialogForm),
58  m_layers(std::list<te::map::AbstractLayerPtr>())
59 {
60  // add controls
61  m_ui->setupUi(this);
62 
63  //signals
64  connect(m_ui->m_rasterSearchToolButton, SIGNAL(clicked()), this, SLOT(onInputRasterToolButtonClicked()));
65  connect(m_ui->m_rasterlayersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onRasterInputComboBoxChanged(int)));
66 
67  connect(m_ui->m_dummycheckBox, SIGNAL(toggled(bool)), m_ui->m_dummylineEdit, SLOT(setEnabled(bool)));
68  connect(m_ui->m_dummylineEdit, SIGNAL(editingFinished()), this, SLOT(onDummyLineEditEditingFinished()));
69 
70  connect(m_ui->m_vectorSearchToolButton, SIGNAL(clicked()), this, SLOT(onInputVectorToolButtonClicked()));
71  connect(m_ui->m_vectorlayersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onVectorInputComboBoxChanged(int)));
72 
73  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
74  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
75 
76  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
77  m_ui->m_helpPushButton->setPageReference("plugins/mnt/DTM_Volume.html");
78 
79  setModal(false);
80 
81 }
82 
84 
85 void te::mnt::VolumeDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
86 {
87  m_layers = layers;
88 
89  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
90 
91  while (it != m_layers.end())
92  {
93  if (it->get())
94  {
95  if (it->get()->isValid())
96  {
97  std::unique_ptr<te::da::DataSetType> dsType(it->get()->getSchema());
98  mntType type = getMNTType(dsType.get());
99 
100  if (type == TIN)
101  m_ui->m_vectorlayersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
102 
103  if (type == GRID)
104  m_ui->m_rasterlayersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
105 
106  dsType.release();
107  }
108  }
109  ++it;
110  }
111 }
112 
113 
115 {
116 }
117 
119 {
120  LayerSearchDialog search(this->parentWidget());
121  search.setLayers(m_layers);
122  QList<mntType> types;
123  types.append(GRID);
124  search.setActive(types);
125 
126  if (search.exec() != QDialog::Accepted)
127  {
128  return;
129  }
130 
131  int index = m_ui->m_rasterlayersComboBox->findText(search.getLayer().get()->getTitle().c_str());
132  m_ui->m_rasterlayersComboBox->setCurrentIndex(index);
133 
134 }
135 
137 {
138  m_rasterinputLayer = nullptr;
139  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
140  std::string layerID = m_ui->m_rasterlayersComboBox->itemData(index, Qt::UserRole).toString().toUtf8().data();
141  while (it != m_layers.end())
142  {
143  if(layerID == it->get()->getId())
144  {
145  m_rasterinputLayer = it->get();
146  //std::unique_ptr<te::da::DataSetType> dsType = m_rasterinputLayer->getSchema();
147  //te::rst::RasterProperty* rasterProp = te::da::GetFirstRasterProperty(dsType.get());
148  //te::map::DataSetLayer* indsLayer = dynamic_cast<te::map::DataSetLayer*>(m_rasterinputLayer.get());
149  //te::da::DataSourcePtr inDataSource = te::da::GetDataSource(indsLayer->getDataSourceId(), true);
150  //std::unique_ptr<te::da::DataSet> dsRaster = inDataSource->getDataSet(indsLayer->getDataSetName());
151  //std::unique_ptr<te::rst::Raster> in_raster = dsRaster->getRaster(rasterProp->getName());
152  std::unique_ptr<te::rst::Raster> in_raster(te::map::GetRaster(m_rasterinputLayer.get()));
153  m_ui->m_dummylineEdit->setText(QString::number(in_raster->getBand(0)->getProperty()->m_noDataValue));
154  // in_raster.release();
155  //dsRaster.release();
156  // dsType.release();
157  }
158  ++it;
159  }
160 }
161 
163 {
164 }
165 
167 {
168  LayerSearchDialog search(this->parentWidget(), nullptr, false);
169  search.setLayers(m_layers);
170  QList<mntType> types;
171  types.append(TIN);
172  search.setActive(types);
173 
174  if (search.exec() != QDialog::Accepted)
175  {
176  return;
177  }
178 
179  int index = m_ui->m_vectorlayersComboBox->findText(search.getLayer().get()->getTitle().c_str());
180  m_ui->m_vectorlayersComboBox->setCurrentIndex(index);
181 }
182 
184 {
185  m_vectorinputLayer = nullptr;
186  m_ui->m_attributeComboBox->clear();
187  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
188  std::string layerID = m_ui->m_vectorlayersComboBox->itemData(index, Qt::UserRole).toString().toUtf8().data();
189  while (it != m_layers.end())
190  {
191  if(layerID == it->get()->getId())
192  {
193  m_vectorinputLayer = it->get();
194 
195  std::unique_ptr<te::da::DataSetType> dsType = m_vectorinputLayer->getSchema();
196  std::vector<te::dt::Property*> props = dsType->getProperties();
197  for (std::size_t i = 0; i < props.size(); ++i)
198  {
199  m_ui->m_attributeComboBox->addItem(QString(props[i]->getName().c_str()), QVariant(props[i]->getName().c_str()));
200  }
201  dsType.release();
202  }
203  ++it;
204  }
205 }
206 
208 {
209  //progress
211 
212  try
213  {
214  QApplication::setOverrideCursor(Qt::WaitCursor);
215 
216  if (!m_rasterinputLayer.get() || !m_vectorinputLayer.get())
217  throw te::common::Exception(TE_TR("Select an input layer!"));
218 
219  if (m_rasterinputLayer->getSRID() != m_vectorinputLayer->getSRID())
220  throw te::common::Exception(TE_TR("Can not execute this operation with different SRIDs geometries!"));
221 
222  te::map::DataSetLayer* indsrasterLayer = dynamic_cast<te::map::DataSetLayer*>(m_rasterinputLayer.get());
223  if (!indsrasterLayer)
224  throw te::common::Exception(TE_TR("Can not execute this operation on this type of layer!"));
225 
226  te::da::DataSourcePtr inrasterDataSource = te::da::GetDataSource(indsrasterLayer->getDataSourceId(), true);
227  if (!inrasterDataSource.get())
228  throw te::common::Exception(TE_TR("The selected input data source can not be accessed!"));
229 
230  std::string inrasterDsetName = indsrasterLayer->getDataSetName();
231  std::unique_ptr<te::da::DataSetType> inrasterDsetType(inrasterDataSource->getDataSetType(inrasterDsetName));
232 
233  const te::da::ObjectIdSet* OidSet = nullptr;
234  if (m_ui->m_selectCheckBox->isChecked())
235  {
236  OidSet = m_vectorinputLayer->getSelected();
237  if (!OidSet)
238  throw te::common::Exception(TE_TR("Select the layer objects to perform the intersection operation."));
239  }
240 
241  te::map::DataSetLayer* indsvectorLayer = dynamic_cast<te::map::DataSetLayer*>(m_vectorinputLayer.get());
242  if (!indsvectorLayer)
243  throw te::common::Exception(TE_TR("Can not execute this operation on this type of layer!"));
244 
245  te::da::DataSourcePtr invectorDataSource = te::da::GetDataSource(indsvectorLayer->getDataSourceId(), true);
246  if (!invectorDataSource.get())
247  throw te::common::Exception(TE_TR("The selected input data source can not be accessed!"));
248 
249  std::string invectorDsetName = indsvectorLayer->getDataSetName();
250  std::unique_ptr<te::da::DataSetType> invectorDsetType(invectorDataSource->getDataSetType(invectorDsetName));
251 
252  Volume *calvol = new te::mnt::Volume();
253 
254  std::string attr = m_ui->m_attributeComboBox->currentText().toUtf8().data();
255 
256  int srid = m_vectorinputLayer->getSRID();
257  calvol->setSRID(srid);
258  calvol->setInput(inrasterDataSource, inrasterDsetName, std::move(inrasterDsetType), invectorDataSource, invectorDsetName, std::move(invectorDsetType), OidSet);
259  calvol->setParams(m_ui->m_quotaLineEdit->text().toDouble(), attr, m_ui->m_dummylineEdit->text().toDouble());
260 
261  if (calvol->run())
262  {
263  QApplication::restoreOverrideCursor();
264  std::vector<std::string> polyvec;
265  std::vector<std::string> cortevec;
266  std::vector<std::string> aterrovec;
267  std::vector<std::string> areavec;
268  std::vector<std::string> iquotavec;
269  calvol->getResults(polyvec, cortevec, aterrovec, areavec, iquotavec);
270 
271  te::mnt::VolumeResultDialog result(polyvec,
272  cortevec,
273  aterrovec,
274  areavec,
275  iquotavec,
276  attr,
277  this->parentWidget());
278 
279  if (result.exec() != QDialog::Accepted)
280  {
281  return;
282  }
283  }
284  delete calvol;
285  }
286  catch (const std::exception& e)
287  {
288  QApplication::restoreOverrideCursor();
289  QMessageBox::information(this, tr("Volume "), e.what());
290  return;
291  }
292 
293  QApplication::restoreOverrideCursor();
294  accept();
295 }
296 
298 {
299  reject();
300 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
std::unique_ptr< Ui::VolumeDialogForm > m_ui
Definition: VolumeDialog.h:81
void onInputVectorToolButtonClicked()
te::map::AbstractLayerPtr m_vectorinputLayer
Input layer.
Definition: VolumeDialog.h:85
boost::shared_ptr< DataSource > DataSourcePtr
void onInputRasterToolButtonClicked()
VolumeDialog(QWidget *parent=0, Qt::WindowFlags f=0)
void onDummyLineEditEditingFinished()
A dialog Layer Search.
std::list< te::map::AbstractLayerPtr > m_layers
List of layers.
Definition: VolumeDialog.h:83
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void onRasterInputComboBoxChanged(int index)
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
A dialog Show Calculate Volume Results.
A dialog Calculate Volume.
void onVectorInputComboBoxChanged(int index)
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
te::map::AbstractLayerPtr getLayer()
URI C++ Library.
Definition: Attributes.h:37
te::map::AbstractLayerPtr m_rasterinputLayer
Input layer.
Definition: VolumeDialog.h:84
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
const std::string & getDataSetName() const
void setActive(const QList< mntType > &types)
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
TEMNTEXPORT te::mnt::mntType getMNTType(const te::da::DataSetType *dt)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
virtual const std::string & getDataSourceId() const