SamplePointsGeneratorDialog.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/sa/qt/SamplePointsGeneratorDialog.cpp
22 
23  \brief A dialog to generate sample points inside an area of a dataset.
24 */
25 
26 // TerraLib
27 #include "../../core/logger/Logger.h"
28 #include "../../common/progress/ProgressManager.h"
29 #include "../../core/translator/Translator.h"
30 #include "../../common/STLUtils.h"
31 #include "../../dataaccess/datasource/DataSource.h"
32 #include "../../dataaccess/utils/Utils.h"
33 #include "../../geometry/GeometryProperty.h"
34 #include "../../maptools/DataSetLayer.h"
35 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
36 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
37 #include "../core/SamplePointsGeneratorRandom.h"
38 #include "../core/SamplePointsGeneratorStratified.h"
39 #include "../Enums.h"
40 #include "../Exception.h"
42 #include "Utils.h"
43 #include "ui_SamplePointsGeneratorDialogForm.h"
44 
45 // Qt
46 #include <QFileDialog>
47 #include <QFileInfo>
48 #include <QMessageBox>
49 #include <QValidator>
50 
51 // STL
52 #include <memory>
53 
54 // Boost
55 #include <boost/filesystem.hpp>
56 
58 
59 te::sa::SamplePointsGeneratorDialog::SamplePointsGeneratorDialog(QWidget* parent, Qt::WindowFlags f)
60  : QDialog(parent, f),
61  m_ui(new Ui::SamplePointsGeneratorDialogForm)
62 {
63 // add controls
64  m_ui->setupUi(this);
65 
66  fillGeneratorTypes();
67 
68  m_ui->m_nPointsRandomLineEdit->setValidator(new QIntValidator(this));
69  m_ui->m_nPointsStratifiedLineEdit->setValidator(new QIntValidator(this));
70 
71 // add icons
72  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("sa-samplepoint-hint").pixmap(112,48));
73  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
74 
75 // connectors
76  connect(m_ui->m_inputLayerComboBox, SIGNAL(activated(int)), this, SLOT(onInputLayerComboBoxActivated(int)));
77 
78  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
79  connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()), this, SLOT(onTargetDatasourceToolButtonPressed()));
80  connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()), this, SLOT(onTargetFileToolButtonPressed()));
81 
82 // help info
83  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
84  m_ui->m_helpPushButton->setPageReference("plugins/sa/sa_samplepointsgenerator.html");
85 }
86 
88 
89 void te::sa::SamplePointsGeneratorDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
90 {
91  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
92 
93  while(it != layers.end())
94  {
96 
97  if(l->isValid())
98  {
99  std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
100 
101  if(dsType->hasGeom())
102  m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
103  }
104 
105  ++it;
106  }
107 
108 // fill attributes combo
109  if(m_ui->m_inputLayerComboBox->count() > 0)
111 }
112 
114 {
115  return m_outputLayer;
116 }
117 
119 {
120  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
121 
123 
124  std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
125 
126  std::vector<te::dt::Property*> propVec = dsType->getProperties();
127 
128  m_ui->m_attrStratifiedComboBox->clear();
129 
130  for(std::size_t t = 0; t < propVec.size(); ++t)
131  {
132  int dataType = propVec[t]->getType();
133 
134  if(dataType == te::dt::STRING_TYPE)
135  m_ui->m_attrStratifiedComboBox->addItem(propVec[t]->getName().c_str(), dataType);
136  }
137 }
138 
140 {
141  // check input parameters
142  if(m_ui->m_repositoryLineEdit->text().isEmpty())
143  {
144  QMessageBox::information(this, tr("Warning"), tr("Define a repository for the result."));
145  return;
146  }
147 
148  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
149  {
150  QMessageBox::information(this, tr("Warning"), tr("Define a name for the resulting layer."));
151  return;
152  }
153 
154  //set operation parameters
155  te::sa::SamplePointsGeneratorType spgt = (te::sa::SamplePointsGeneratorType)m_ui->m_generatorTypeComboBox->itemData(m_ui->m_generatorTypeComboBox->currentIndex()).toInt();
156 
157  if(spgt == te::sa::Random)
158  {
159  if(m_ui->m_nPointsRandomLineEdit->text().isEmpty())
160  {
161  QMessageBox::information(this, tr("Warning"), tr("Number of points not defined."));
162  return;
163  }
164  }
165  else if(spgt == te::sa::Stratified)
166  {
167  if(m_ui->m_nPointsStratifiedLineEdit->text().isEmpty())
168  {
169  QMessageBox::information(this, tr("Warning"), tr("Number of points not defined."));
170  return;
171  }
172  }
173 
174  //get selected layer
175  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
177 
178  std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
179 
180  //get srid
182 
183  if(!gmProp)
184  throw;
185 
186  int srid = gmProp->getSRID();
187 
188  te::gm::Envelope env = l->getExtent();
189 
190  //create datasource to save the output information
191  te::da::DataSourcePtr outputDataSource;
192 
193  if(m_toFile)
194  {
195  outputDataSource = te::sa::CreateOGRDataSource(m_ui->m_repositoryLineEdit->text().toUtf8().data());
196  }
197  else
198  {
199  outputDataSource = te::da::GetDataSource(m_outputDatasource->getId());
200  }
201 
202  //get output dataset name
203  std::string dataSetName = m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
204 
205  std::size_t idx = dataSetName.find(".");
206  if (idx != std::string::npos)
207  dataSetName=dataSetName.substr(0,idx);
208 
209  std::vector<std::string> classNames;
210 
211  //progress
213 
214  QApplication::setOverrideCursor(Qt::WaitCursor);
215 
216  try
217  {
219 
220  if(spgt == te::sa::Random)
221  {
223 
224  spga->setOutputDataSetName(dataSetName);
225  spga->setOutputDataSource(outputDataSource);
226  spga->setEnvelope(env);
227  spga->setSRID(srid);
228 
229  ((te::sa::SamplePointsGeneratorRandom*)spga)->setNumberOfPoints(m_ui->m_nPointsRandomLineEdit->text().toInt());
230  }
231  else if(spgt == te::sa::Stratified)
232  {
234 
235  spga->setOutputDataSetName(dataSetName);
236  spga->setOutputDataSource(outputDataSource);
237  spga->setEnvelope(env);
238  spga->setSRID(srid);
239 
240  ((te::sa::SamplePointsGeneratorStratified*)spga)->setNumberOfPoints(m_ui->m_nPointsStratifiedLineEdit->text().toInt());
241  ((te::sa::SamplePointsGeneratorStratified*)spga)->setInputDataSet(l->getData());
242  ((te::sa::SamplePointsGeneratorStratified*)spga)->setInputAttributeName(m_ui->m_attrStratifiedComboBox->currentText().toUtf8().data());
243  ((te::sa::SamplePointsGeneratorStratified*)spga)->isProportionalToArea(m_ui->m_propStratifiedCheckBox->isChecked());
244  }
245 
246  assert(spga);
247 
248  spga->execute();
249 
250  if(spgt == te::sa::Stratified)
251  {
252  classNames = ((te::sa::SamplePointsGeneratorStratified*)spga)->getClassNames();
253  }
254 
255 
256  delete spga;
257  }
258  catch(const std::exception& e)
259  {
260  QMessageBox::warning(this, tr("Warning"), e.what());
261 
262  QApplication::restoreOverrideCursor();
263 
264  return;
265  }
266  catch(...)
267  {
268  QMessageBox::warning(this, tr("Warning"), tr("Internal Error.Sample Points not generated."));
269 
270  QApplication::restoreOverrideCursor();
271 
272  return;
273  }
274 
275  QApplication::restoreOverrideCursor();
276 
277  //save generated information
278  m_outputLayer = te::sa::CreateLayer(outputDataSource, dataSetName);
279 
280  if(spgt == te::sa::Stratified)
281  {
283  }
284 
285  accept();
286 }
287 
289 {
290  m_ui->m_newLayerNameLineEdit->clear();
291  m_ui->m_newLayerNameLineEdit->setEnabled(true);
292 
294  dlg.exec();
295 
296  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
297 
298  if(dsPtrList.empty())
299  return;
300 
301  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
302 
303  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
304 
305  m_outputDatasource = *it;
306 
307  m_toFile = false;
308 }
309 
311 {
312  m_ui->m_newLayerNameLineEdit->clear();
313  m_ui->m_repositoryLineEdit->clear();
314 
315  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."), QString(), tr("Shapefile (*.shp *.SHP);;"),nullptr, QFileDialog::DontConfirmOverwrite);
316 
317  if (fileName.isEmpty())
318  return;
319 
320  boost::filesystem::path outfile(fileName.toUtf8().data());
321 
322  m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
323 
324  m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
325 
326  m_ui->m_newLayerNameLineEdit->setEnabled(false);
327 
328  m_toFile = true;
329 }
330 
332 {
333  m_ui->m_generatorTypeComboBox->clear();
334 
335  m_ui->m_generatorTypeComboBox->addItem("Random", QVariant(te::sa::Random));
336  m_ui->m_generatorTypeComboBox->addItem("Stratified", QVariant(te::sa::Stratified));
337 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Geometric property.
std::unique_ptr< Ui::SamplePointsGeneratorDialogForm > m_ui
boost::shared_ptr< DataSource > DataSourcePtr
TESAEXPORT te::da::DataSourcePtr CreateOGRDataSource(std::string repository)
TESAEXPORT void CreateSampleGeneratorStratifiedGrouping(te::map::AbstractLayerPtr layer, std::vector< std::string > strVec)
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
Class to generate samples points using stratified strategy.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
An Envelope defines a 2D rectangular region.
URI C++ Library.
Definition: Attributes.h:37
te::map::AbstractLayerPtr m_outputLayer
Generated Layer.
Utility functions for the data access module.
Virtual class to generate samples points.
void execute()
Function to execute the kernel operation.
Class to generate samples points using random strategy.
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
TESAEXPORT te::map::AbstractLayerPtr CreateLayer(te::da::DataSourcePtr ds, std::string dataSetName)
A dialog for selecting a data source.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
A dialog to generate sample points inside an area of a dataset.
SamplePointsGeneratorType
Generator types used to create sample of points.