SmoothIsolinesDialog.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
6 
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License,
9 or (at your option) any later version.
10 
11 TerraLib is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15 
16 You should have received a copy of the GNU Lesser General Public License
17 along with TerraLib. See COPYING. If not, write to
18 TerraLib Team at <terralib-team@terralib.org>.
19 */
20 
21 /*!
22 \file terralib/mnt/qt/SmoothIsolinesDialog.cpp
23 
24 \brief A dialog for Smooth Isolines
25 */
26 
27 //terralib
28 #include "../../core/filesystem/FileSystem.h"
29 #include "../../core/translator/Translator.h"
30 #include "../../common/progress/ProgressManager.h"
31 #include "../../dataaccess/datasource/DataSourceFactory.h"
32 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
33 #include "../../dataaccess/datasource/DataSourceManager.h"
34 #include "../../dataaccess/utils/Utils.h"
35 #include "../../geometry/GeometryProperty.h"
36 #include "../../maptools/DataSetLayer.h"
37 #include "../../mnt/core/Smooth.h"
38 #include "../../mnt/core/Utils.h"
39 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
40 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
41 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
42 #include "../../qt/widgets/rp/Utils.h"
43 #include "../../qt/widgets/utils/FileDialog.h"
44 #include "../../qt/widgets/srs/SRSManagerDialog.h"
45 #include "../../raster.h"
46 #include "../../srs/SpatialReferenceSystemManager.h"
47 
48 #include "LayerSearchDialog.h"
49 #include "SmoothIsolinesDialog.h"
50 #include "ui_SmoothIsolinesDialogForm.h"
51 
52 // Qt
53 #include <QFileDialog>
54 #include <QMessageBox>
55 
56 // BOOST
57 #include <boost/algorithm/string.hpp>
58 #include <boost/filesystem.hpp>
59 #include <boost/lexical_cast.hpp>
60 #include <boost/uuid/random_generator.hpp>
61 #include <boost/uuid/uuid_io.hpp>
62 
64  : QDialog(parent, f),
65  m_ui(new Ui::SmoothIsolinesDialogForm),
66  m_layers(std::list<te::map::AbstractLayerPtr>())
67 {
68  // add controls
69  m_ui->setupUi(this);
70 
71  //signals
72  connect(m_ui->m_layerSearchToolButton, SIGNAL(clicked()), this, SLOT(onInputLayerToolButtonClicked()));
73  connect(m_ui->m_layersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onInputComboBoxChanged(int)));
74 
75  connect(m_ui->m_scalePushButton, SIGNAL(clicked()), this, SLOT(onScalePushButtonClicked()));
76 
77  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
78  connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()), this, SLOT(onTargetDatasourceToolButtonPressed()));
79  connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()), this, SLOT(onTargetFileToolButtonPressed()));
80 
81  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
82  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
83 
84  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
85  m_ui->m_helpPushButton->setPageReference("plugins/mnt/DTM_SmoothIsolines.html");
86 
87  m_ui->m_srsToolButton->setIcon(QIcon::fromTheme("srs"));
88  connect(m_ui->m_srsToolButton, SIGNAL(clicked()), this, SLOT(onSrsToolButtonClicked()));
89 
90  m_outsrid = 0;
91 }
92 
94 
95 void te::mnt::SmoothIsolinesDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
96 {
97  m_layers = layers;
98 
99  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
100 
101  while (it != m_layers.end())
102  {
103  if (it->get())
104  {
105  if (it->get()->isValid())
106  {
107  std::unique_ptr<te::da::DataSetType> dsType (it->get()->getSchema());
108  mntType type = getMNTType(dsType.get());
109 
110  if (type == ISOLINE)
111  m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
112  }
113  }
114  ++it;
115  }
116 }
117 
119 {
120  LayerSearchDialog search(this->parentWidget());
121  search.setLayers(m_layers);
122  QList<mntType> types;
123  types.append(ISOLINE);
124  search.setActive(types);
125 
126  if (search.exec() != QDialog::Accepted)
127  {
128  return;
129  }
130 
131  int index = m_ui->m_layersComboBox->findText(search.getLayer().get()->getTitle().c_str());
132  m_ui->m_layersComboBox->setCurrentIndex(index);
133 }
134 
136 {
137  m_inputLayer = nullptr;
138  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
139  std::string layerID = m_ui->m_layersComboBox->itemData(index, Qt::UserRole).toString().toUtf8().data();
140 
141  while (it != m_layers.end())
142  {
143  if(layerID == it->get()->getId())
144  {
145  te::map::AbstractLayerPtr selectedLayer = it->get();
146  m_inputLayer = selectedLayer;
147 
148  setSRID(m_inputLayer->getSRID());
149 
150  break;
151  }
152  ++it;
153  }
154 }
155 
156 
158 {
159  m_scale = m_ui->m_scaleLineEdit->text().toDouble();
160  m_factor = (m_scale * 0.4) / 1000;
162 
163  m_ui->m_factorLineEdit->setText(QString::number(m_factor));
164  m_ui->m_distanceLineEdit->setText(QString::number(m_maxdist));
165 
166 }
167 
169 {
170  return m_outputLayer;
171 }
172 
173 
175 {
176  m_ui->m_newLayerNameLineEdit->clear();
177  m_ui->m_newLayerNameLineEdit->setEnabled(true);
179  dlg.exec();
180 
181  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
182 
183  if (dsPtrList.empty())
184  return;
185 
186  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
187 
188  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
189 
190  m_outputDatasource = *it;
191 
192  m_toFile = false;
193 }
194 
196 {
197  m_ui->m_newLayerNameLineEdit->clear();
198  m_ui->m_repositoryLineEdit->clear();
199 
201 
202  try {
203  fileDialog.exec();
204  }
205  catch (const std::exception& e)
206  {
207  QMessageBox::information(this, tr("Smooth Isolines"), e.what());
208  return;
209  }
210 
211  m_ui->m_repositoryLineEdit->setText(fileDialog.getPath().c_str());
212  m_ui->m_newLayerNameLineEdit->setText(fileDialog.getFileName().c_str());
213 
214  m_toFile = true;
215  m_ui->m_newLayerNameLineEdit->setEnabled(false);
216 }
217 
219 {
220  //progress
222 
223  try
224  {
225  QApplication::setOverrideCursor(Qt::WaitCursor);
226 
227  if (!m_inputLayer.get())
228  throw te::common::Exception(TE_TR("Select an input layer!"));
229 
230  te::map::DataSetLayer* indsLayer = dynamic_cast<te::map::DataSetLayer*>(m_inputLayer.get());
231  if (!indsLayer)
232  throw te::common::Exception(TE_TR("Can not execute this operation on this type of layer!"));
233 
234  te::da::DataSourcePtr inDataSource = te::da::GetDataSource(indsLayer->getDataSourceId(), true);
235  if (!inDataSource.get())
236  throw te::common::Exception(TE_TR("The selected input data source can not be accessed!"));
237 
238  std::string inDsetName = indsLayer->getDataSetName();
239  std::unique_ptr<te::da::DataSetType> inDsetType(inDataSource->getDataSetType(inDsetName));
240 
241  // Checking consistency of output paramenters
242  if (m_ui->m_repositoryLineEdit->text().isEmpty())
243  throw te::common::Exception(TE_TR("Select a repository for the resulting layer."));
244 
245  if (m_ui->m_newLayerNameLineEdit->text().isEmpty())
246  throw te::common::Exception(TE_TR("Define a name for the resulting layer."));
247 
248  std::string outputdataset = m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
249  std::string outdsinfo ("file://");
250  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toUtf8().data());
251 
252  if (m_toFile)
253  {
254  if (te::core::FileSystem::exists(uri.string()))
255  throw te::common::Exception(TE_TR("Output file already exists! Remove it or select a new name and try again."));
256 
257  std::size_t idx = outputdataset.find(".");
258  if (idx != std::string::npos)
259  outputdataset = outputdataset.substr(0, idx);
260 
261  outdsinfo += uri.string();
262  }
263 
264  Smooth *iso = new te::mnt::Smooth();
265  iso->setInput(inDataSource, inDsetName, std::move(inDsetType));
266  if (m_toFile)
267  {
268  te::da::DataSourcePtr dsOGR(te::da::DataSourceFactory::make("OGR", outdsinfo).release());
269  dsOGR->open();
270 
271  if (dsOGR->dataSetExists(outputdataset))
272  throw te::common::Exception(TE_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."));
273 
274  iso->setOutput(dsOGR, outputdataset);
275  }
276  else
277  {
279  if (!aux)
280  throw te::common::Exception(TE_TR("The selected output datasource can not be accessed."));
281 
282  if (aux->dataSetExists(outputdataset))
283  throw te::common::Exception(TE_TR("Dataset already exists. Remove it or select a new name and try again. "));
284 
285  iso->setOutput(aux, outputdataset);
286  }
287 
288  bool simpl_out = m_ui->m_simploutCheckBox->isChecked();
289 
290  iso->setSRID(m_outsrid);
291  if (m_outsrid)
292  {
294  if (unitin.get())
295  {
297 
298  if (unitin->getId() != te::common::UOM_Metre)
299  {
300  convertPlanarToAngle(m_factor, unitout);
302  }
303  }
304  }
305 
306  iso->setParams(m_factor, m_maxdist, simpl_out);
307 
308  bool result = iso->run();
309 
310  delete iso;
311 
312  if (result)
313  {
314  if (m_toFile)
315  {
316  // let's include the new datasource in the managers
317  boost::uuids::basic_random_generator<boost::mt19937> gen;
318  boost::uuids::uuid u = gen();
319  std::string id = boost::uuids::to_string(u);
320 
322  ds->setConnInfo(outdsinfo);
323  ds->setTitle(uri.stem().string());
324  ds->setAccessDriver("OGR");
325  ds->setType("OGR");
326  ds->setDescription(uri.string());
327  ds->setId(id);
328 
329  te::da::DataSourcePtr newds = te::da::DataSourceManager::getInstance().get(id, "OGR", ds->getConnInfo());
330  newds->open();
333  }
334 
335  // creating a layer for the result
337 
339 
340  te::da::DataSetTypePtr dt(outDataSource->getDataSetType(outputdataset).release());
341  m_outputLayer = converter(dt);
342  }
343  }
344  catch (const std::exception& e)
345  {
346  QApplication::restoreOverrideCursor();
347  QMessageBox::information(this, tr("Smooth Isolines "), e.what());
348  return;
349  }
350 
351  QApplication::restoreOverrideCursor();
352  accept();
353 }
354 
356 {
357  reject();
358 }
359 
361 {
362  te::qt::widgets::SRSManagerDialog srsDialog(this);
363  srsDialog.setWindowTitle(tr("Choose the SRS"));
364 
365  if (srsDialog.exec() == QDialog::Rejected)
366  return;
367 
368  int newSRID = srsDialog.getSelectedSRS().first;
369 
370  setSRID(newSRID);
371 
372 }
373 
375 {
376  if (newSRID <= 0)
377  {
378  m_ui->m_resSRIDLabel->setText("No SRS defined");
379  }
380  else
381  {
382  std::string name = te::srs::SpatialReferenceSystemManager::getInstance().getName(newSRID);
383  if (name.size())
384  m_ui->m_resSRIDLabel->setText(name.c_str());
385  else
386  m_ui->m_resSRIDLabel->setText(QString("%1").arg(newSRID));
387  }
388  m_outsrid = newSRID;
389 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
te::map::AbstractLayerPtr m_outputLayer
Generated Layer.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
TEMNTEXPORT bool convertPlanarToAngle(double &val, te::common::UnitOfMeasurePtr unit)
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
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
std::unique_ptr< Ui::SmoothIsolinesDialogForm > m_ui
A dialog Layer Search.
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
Definition: Smooth.cpp:31
static te::dt::Date ds(2010, 01, 01)
void setParams(double factor, double max_dist, bool simpl)
Definition: Smooth.cpp:37
#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.
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::unique_ptr< te::da::DataSetType > inDsetType)
Definition: Smooth.cpp:21
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
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
static te::dt::TimeDuration dt(20, 30, 50, 11)
te::map::AbstractLayerPtr m_inputLayer
Input layer.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
void setSRID(int srid)
Definition: Smooth.h:38
SmoothIsolinesDialog(QWidget *parent=0, Qt::WindowFlags f=0)
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
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
void setActive(const QList< mntType > &types)
A dialog for selecting a data source.
A class that represents a data source component.
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
UnitOfMeasurePtr find(unsigned int id) const
Returns a unit of measure identified by its identificaton.
TEMNTEXPORT te::mnt::mntType getMNTType(const te::da::DataSetType *dt)
A dialog used to build a SRSManagerDialog element.
te::da::DataSourceInfoPtr m_outputDatasource
DataSource information.
te::map::AbstractLayerPtr getLayer()
bool run()
Definition: Smooth.cpp:44
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.
A dialog Smooth Isolines.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.