ProximityMatrixCreatorDialog.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/ProximityMatrixCreatorDialog.cpp
22 
23  \brief A dialog for Proximity Matrix Creation
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/utils/Utils.h"
32 #include "../../maptools/DataSetLayer.h"
33 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
34 #include "../core/GPMBuilder.h"
35 #include "../core/GPMConstructorAbstractStrategy.h"
36 #include "../core/GPMConstructorAdjacencyStrategy.h"
37 #include "../core/GPMConstructorDistanceStrategy.h"
38 #include "../core/GPMConstructorNearestNeighborStrategy.h"
39 #include "../core/GPMWeightsAbstractStrategy.h"
40 #include "../core/GPMWeightsInverseDistanceStrategy.h"
41 #include "../core/GPMWeightsNoWeightsStrategy.h"
42 #include "../core/GPMWeightsSquaredInverseDistanceStrategy.h"
43 #include "../core/SpatialWeightsExchanger.h"
44 #include "../Exception.h"
46 #include "ui_ProximityMatrixCreatorDialogForm.h"
47 
48 // Qt
49 #include <QFileDialog>
50 #include <QMessageBox>
51 #include <QValidator>
52 
54 
55 te::sa::ProximityMatrixCreatorDialog::ProximityMatrixCreatorDialog(QWidget* parent, Qt::WindowFlags f)
56  : QDialog(parent, f),
57  m_ui(new Ui::ProximityMatrixCreatorDialogForm)
58 {
59 // add controls
60  m_ui->setupUi(this);
61 
62  m_ui->m_distanceLineEdit->setValidator(new QDoubleValidator(this));
63  m_ui->m_nearNeighborLineEdit->setValidator(new QIntValidator(this));
64 
65 // add icons
66  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("sa-proxmatrixcreator-hint").pixmap(112,48));
67  m_ui->m_fileToolButton->setIcon(QIcon::fromTheme("folder-open"));
68 
69 // connectors
70  connect(m_ui->m_inputLayerComboBox, SIGNAL(activated(int)), this, SLOT(onInputLayerComboBoxActivated(int)));
71  connect(m_ui->m_fileToolButton, SIGNAL(clicked()), this, SLOT(onFileToolButtonClicked()));
72  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
73 
74 // help info
75  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
76  m_ui->m_helpPushButton->setPageReference("plugins/sa/sa_proxmatrixcreator.html");
77 }
78 
80 
81 void te::sa::ProximityMatrixCreatorDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
82 {
83  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
84 
85  while(it != layers.end())
86  {
88 
89  if(l->isValid())
90  {
91  std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
92 
93  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
94 
95  if(dsLayer && dsType->hasGeom())
96  m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
97  }
98 
99  ++it;
100  }
101 
102 // fill attributes combo
103  if(m_ui->m_inputLayerComboBox->count() > 0)
105 }
106 
108 {
109  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
110 
112 
113  std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
114 
115  std::vector<te::dt::Property*> propVec = dsType->getProperties();
116 
117  m_ui->m_attrIdComboBox->clear();
118 
119  for(std::size_t t = 0; t < propVec.size(); ++t)
120  {
121  m_ui->m_attrIdComboBox->addItem(propVec[t]->getName().c_str());
122  }
123 
124  m_ui->m_nameLineEdit->setText(l->getTitle().c_str());
125 }
126 
128 {
129  if(!m_ui->m_dsRadioButton->isChecked())
130  {
131  QString fileName = QFileDialog::getExistingDirectory(this, tr("Save To File"), "");
132 
133  if(fileName.isEmpty())
134  return;
135 
136  m_ui->m_locationLineEdit->setText(fileName);
137  }
138 }
139 
141 {
142  // check interface parameters
143  if(m_ui->m_nameLineEdit->text().isEmpty())
144  {
145  QMessageBox::warning(this, tr("Warning"), tr("Output name not defined."));
146  return;
147  }
148 
149  if(m_ui->m_locationLineEdit->text().isEmpty())
150  {
151  QMessageBox::warning(this, tr("Warning"), tr("Output location not defined."));
152  return;
153  }
154 
155  if(m_ui->m_inputLayerComboBox->currentText().isEmpty() || m_ui->m_attrIdComboBox->currentText().isEmpty())
156  {
157  QMessageBox::warning(this, tr("Warning"), tr("Input information not defined."));
158  return;
159  }
160 
161  //get input parameters
162  std::string attrName = m_ui->m_attrIdComboBox->currentText().toUtf8().data();
163 
164  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
165 
167 
168  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
169 
170  if(!dsLayer)
171  {
172  QMessageBox::warning(this, tr("Warning"), tr("Error getting input layer."));
173  return;
174  }
175 
176  te::da::DataSourcePtr ds = te::da::GetDataSource(dsLayer->getDataSourceId(), true);
177 
178  //get constructor strategy
179  te::sa::GPMConstructorAbstractStrategy* constructor = nullptr;
180 
181  if(m_ui->m_buildStratContiguityRadioButton->isChecked())
182  {
183  constructor = new te::sa::GPMConstructorAdjacencyStrategy(m_ui->m_calcDistCheckBox->isChecked());
184  }
185  else if(m_ui->m_buildStratDistanceRadioButton->isChecked())
186  {
187  if(m_ui->m_distanceLineEdit->text().isEmpty())
188  {
189  QMessageBox::warning(this, tr("Warning"), tr("Distance Value not Defined."));
190  return;
191  }
192 
193  constructor = new te::sa::GPMConstructorDistanceStrategy(m_ui->m_distanceLineEdit->text().toDouble());
194  }
195  else if(m_ui->m_buildStratNNRadioButton->isChecked())
196  {
197  if(m_ui->m_nearNeighborLineEdit->text().isEmpty())
198  {
199  QMessageBox::warning(this, tr("Warning"), tr("Number of Neighbors not Defined."));
200  return;
201  }
202 
203  constructor = new te::sa::GPMConstructorNearestNeighborStrategy(m_ui->m_nearNeighborLineEdit->text().toInt());
204  }
205 
206  //get weights strategy
207  te::sa::GPMWeightsAbstractStrategy* weights = nullptr;
208 
209  if(m_ui->m_weightNoWeightRadioButton->isChecked())
210  {
211  weights = new te::sa::GPMWeightsNoWeightsStrategy(m_ui->m_normalizeWeightCheckBox->isChecked());
212  }
213  else if(m_ui->m_weightInverseDistRadioButton->isChecked())
214  {
215  weights = new te::sa::GPMWeightsInverseDistanceStrategy(m_ui->m_normalizeWeightCheckBox->isChecked()); // using default parameters
216  }
217  else if(m_ui->m_weightSquareInverseDistRadioButton->isChecked())
218  {
219  weights = new te::sa::GPMWeightsSquaredInverseDistanceStrategy(m_ui->m_normalizeWeightCheckBox->isChecked()); // using default parameters
220  }
221 
222  //run operation
223  te::sa::GPMBuilder builder(constructor, weights);
224 
226 
227  //progress
229 
230  QApplication::setOverrideCursor(Qt::WaitCursor);
231 
232  try
233  {
234  builder.setGPMInfo(ds, dsLayer->getDataSetName(), attrName);
235 
236  builder.build();
237 
238  gpm = builder.getGPM();
239  }
240  catch(const std::exception& e)
241  {
242  QMessageBox::warning(this, tr("Warning"), e.what());
243 
244  QApplication::restoreOverrideCursor();
245 
246  return;
247  }
248  catch(...)
249  {
250  QMessageBox::warning(this, tr("Warning"), tr("Internal Error."));
251 
252  QApplication::restoreOverrideCursor();
253 
254  return;
255  }
256 
257  QApplication::restoreOverrideCursor();
258 
259  //save gpm
260  if(!m_ui->m_dsRadioButton->isChecked())
261  {
262  //define the file name
263  std::string ext;
264 
265  if(m_ui->m_galRadioButton->isChecked())
266  ext = ".gal";
267  else if(m_ui->m_gwtRadioButton->isChecked())
268  ext = ".gwt";
269 
270  std::string name = m_ui->m_nameLineEdit->text().toUtf8().data();
271 
272  std::string fullFileName = m_ui->m_locationLineEdit->text().toUtf8().data();
273 
274  fullFileName += "/" + name + ext;
275 
276  //export
278 
279  if(m_ui->m_galRadioButton->isChecked())
280  exchanger .exportToGAL(gpm, fullFileName);
281  else if(m_ui->m_gwtRadioButton->isChecked())
282  exchanger .exportToGWT(gpm, fullFileName, 1); // use 1 pos to export the weight information
283  }
284 
285  accept();
286 }
287 
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
This class defines a an adjacency strategy class for a GPM constructor.
boost::shared_ptr< DataSource > DataSourcePtr
This class defines a Generalized Proximity Matrix.
This class defines a nearest neighbor class for a GPM constructor.
This class defines a class to calculates a weight for a GPM using No Weights strategy.
This class defines a class to calculates a weight for a GPM using Inverse Distance strategy...
A dialog for Proximity Matrix Creation.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
static te::dt::Date ds(2010, 01, 01)
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
URI C++ Library.
Definition: Attributes.h:37
This class defines a class to calculates a weight for a GPM using Inverse Distance strategy...
This class defines a an Abstract class for a GPM constructor.
This class defines a an Abstract class to calculates a weight for a GPM.
This class defines functions used to load and save gpm&#39;s using GAL and GWT formats, both formats use a &#39; &#39; as separator.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
This class defines a an distance strategy class for a GPM constructor.
This class defines the GPM Builder class.
Definition: GPMBuilder.h:54
std::unique_ptr< Ui::ProximityMatrixCreatorDialogForm > m_ui
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr