All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../common/Logger.h"
28 #include "../../common/progress/ProgressManager.h"
29 #include "../../common/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 
60  : QDialog(parent, f),
61  m_ui(new Ui::SamplePointsGeneratorDialogForm)
62 {
63 // add controls
64  m_ui->setupUi(this);
65 
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 }
90 
91 void te::sa::SamplePointsGeneratorDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
92 {
93  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
94 
95  while(it != layers.end())
96  {
98 
99  if(l->isValid())
100  {
101  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
102 
103  if(dsType->hasGeom())
104  m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
105  }
106 
107  ++it;
108  }
109 
110 // fill attributes combo
111  if(m_ui->m_inputLayerComboBox->count() > 0)
112  onInputLayerComboBoxActivated(0);
113 }
114 
116 {
117  return m_outputLayer;
118 }
119 
121 {
122  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
123 
125 
126  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
127 
128  std::vector<te::dt::Property*> propVec = dsType->getProperties();
129 
130  m_ui->m_attrStratifiedComboBox->clear();
131 
132  for(std::size_t t = 0; t < propVec.size(); ++t)
133  {
134  int dataType = propVec[t]->getType();
135 
136  if(dataType == te::dt::STRING_TYPE)
137  m_ui->m_attrStratifiedComboBox->addItem(propVec[t]->getName().c_str(), dataType);
138  }
139 }
140 
142 {
143  // check input parameters
144  if(m_ui->m_repositoryLineEdit->text().isEmpty())
145  {
146  QMessageBox::information(this, tr("Warning"), tr("Define a repository for the result."));
147  return;
148  }
149 
150  if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
151  {
152  QMessageBox::information(this, tr("Warning"), tr("Define a name for the resulting layer."));
153  return;
154  }
155 
156  //set operation parameters
157  te::sa::SamplePointsGeneratorType spgt = (te::sa::SamplePointsGeneratorType)m_ui->m_generatorTypeComboBox->itemData(m_ui->m_generatorTypeComboBox->currentIndex()).toInt();
158 
159  if(spgt == te::sa::Random)
160  {
161  if(m_ui->m_nPointsRandomLineEdit->text().isEmpty())
162  {
163  QMessageBox::information(this, tr("Warning"), tr("Number of points not defined."));
164  return;
165  }
166  }
167  else if(spgt == te::sa::Stratified)
168  {
169  if(m_ui->m_nPointsStratifiedLineEdit->text().isEmpty())
170  {
171  QMessageBox::information(this, tr("Warning"), tr("Number of points not defined."));
172  return;
173  }
174  }
175 
176  //get selected layer
177  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
179 
180  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
181 
182  //get srid
184 
185  if(!gmProp)
186  throw;
187 
188  int srid = gmProp->getSRID();
189 
190  te::gm::Envelope env = l->getExtent();
191 
192  //create datasource to save the output information
193  te::da::DataSourcePtr outputDataSource;
194 
195  if(m_toFile)
196  {
197  outputDataSource = te::sa::CreateOGRDataSource(m_ui->m_repositoryLineEdit->text().toStdString());
198  }
199  else
200  {
201  outputDataSource = te::da::GetDataSource(m_outputDatasource->getId());
202  }
203 
204  //get output dataset name
205  std::string dataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
206 
207  std::size_t idx = dataSetName.find(".");
208  if (idx != std::string::npos)
209  dataSetName=dataSetName.substr(0,idx);
210 
211  std::vector<std::string> classNames;
212 
213  //progress
216 
217  QApplication::setOverrideCursor(Qt::WaitCursor);
218 
219  try
220  {
222 
223  if(spgt == te::sa::Random)
224  {
226 
227  spga->setOutputDataSetName(dataSetName);
228  spga->setOutputDataSource(outputDataSource);
229  spga->setEnvelope(env);
230  spga->setSRID(srid);
231 
232  ((te::sa::SamplePointsGeneratorRandom*)spga)->setNumberOfPoints(m_ui->m_nPointsRandomLineEdit->text().toInt());
233  }
234  else if(spgt == te::sa::Stratified)
235  {
237 
238  spga->setOutputDataSetName(dataSetName);
239  spga->setOutputDataSource(outputDataSource);
240  spga->setEnvelope(env);
241  spga->setSRID(srid);
242 
243  ((te::sa::SamplePointsGeneratorStratified*)spga)->setNumberOfPoints(m_ui->m_nPointsStratifiedLineEdit->text().toInt());
244  ((te::sa::SamplePointsGeneratorStratified*)spga)->setInputDataSet(l->getData());
245  ((te::sa::SamplePointsGeneratorStratified*)spga)->setInputAttributeName(m_ui->m_attrStratifiedComboBox->currentText().toStdString());
246  ((te::sa::SamplePointsGeneratorStratified*)spga)->isProportionalToArea(m_ui->m_propStratifiedCheckBox->isChecked());
247  }
248 
249  assert(spga);
250 
251  spga->execute();
252 
253  if(spgt == te::sa::Stratified)
254  {
255  classNames = ((te::sa::SamplePointsGeneratorStratified*)spga)->getClassNames();
256  }
257 
258 
259  delete spga;
260  }
261  catch(const std::exception& e)
262  {
263  QMessageBox::warning(this, tr("Warning"), e.what());
264 
265  QApplication::restoreOverrideCursor();
266 
268 
269  return;
270  }
271  catch(...)
272  {
273  QMessageBox::warning(this, tr("Warning"), tr("Internal Error.Sample Points not generated."));
274 
275  QApplication::restoreOverrideCursor();
276 
278 
279  return;
280  }
281 
282  QApplication::restoreOverrideCursor();
283 
285 
286  //save generated information
287  m_outputLayer = te::sa::CreateLayer(outputDataSource, dataSetName);
288 
289  if(spgt == te::sa::Stratified)
290  {
291  te::sa::CreateSampleGeneratorStratifiedGrouping(m_outputLayer, classNames);
292  }
293 
294  accept();
295 }
296 
298 {
299  m_ui->m_newLayerNameLineEdit->clear();
300  m_ui->m_newLayerNameLineEdit->setEnabled(true);
301 
303  dlg.exec();
304 
305  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
306 
307  if(dsPtrList.size() <= 0)
308  return;
309 
310  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
311 
312  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
313 
314  m_outputDatasource = *it;
315 
316  m_toFile = false;
317 }
318 
320 {
321  m_ui->m_newLayerNameLineEdit->clear();
322  m_ui->m_repositoryLineEdit->clear();
323 
324  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."), QString(), tr("Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
325 
326  if (fileName.isEmpty())
327  return;
328 
329  boost::filesystem::path outfile(fileName.toStdString());
330 
331  m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
332 
333  m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
334 
335  m_ui->m_newLayerNameLineEdit->setEnabled(false);
336 
337  m_toFile = true;
338 }
339 
341 {
342  m_ui->m_generatorTypeComboBox->clear();
343 
344  m_ui->m_generatorTypeComboBox->addItem("Random", QVariant(te::sa::Random));
345  m_ui->m_generatorTypeComboBox->addItem("Stratified", QVariant(te::sa::Stratified));
346 }
SamplePointsGeneratorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
Geometric property.
Utility functions for the data access module.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
TESAEXPORT te::da::DataSourcePtr CreateOGRDataSource(std::string repository)
Definition: Utils.cpp:63
TESAEXPORT void CreateSampleGeneratorStratifiedGrouping(te::map::AbstractLayerPtr layer, std::vector< std::string > strVec)
Definition: Utils.cpp:365
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
Class to generate samples points using stratified strategy.
std::auto_ptr< Ui::SamplePointsGeneratorDialogForm > m_ui
int getSRID() const
It returns the spatial reference system identifier associated to this property.
void removeViewer(int viewerId)
Dettach a progress viewer.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
Virtual class to generate samples points.
void execute()
Function to execute the kernel operation.
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
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)
Definition: Utils.cpp:122
A dialog for selecting a data source.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
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.
Definition: Enums.h:115