All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../common/Logger.h"
28 #include "../../common/progress/ProgressManager.h"
29 #include "../../common/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 
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 }
82 
83 void te::sa::ProximityMatrixCreatorDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
84 {
85  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
86 
87  while(it != layers.end())
88  {
90 
91  if(l->isValid())
92  {
93  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
94 
95  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
96 
97  if(dsLayer && dsType->hasGeom())
98  m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
99  }
100 
101  ++it;
102  }
103 
104 // fill attributes combo
105  if(m_ui->m_inputLayerComboBox->count() > 0)
106  onInputLayerComboBoxActivated(0);
107 }
108 
110 {
111  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
112 
114 
115  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
116 
117  std::vector<te::dt::Property*> propVec = dsType->getProperties();
118 
119  m_ui->m_attrIdComboBox->clear();
120 
121  for(std::size_t t = 0; t < propVec.size(); ++t)
122  {
123  m_ui->m_attrIdComboBox->addItem(propVec[t]->getName().c_str());
124  }
125 
126  m_ui->m_nameLineEdit->setText(l->getTitle().c_str());
127 }
128 
130 {
131  if(!m_ui->m_dsRadioButton->isChecked())
132  {
133  QString fileName = QFileDialog::getExistingDirectory(this, tr("Save To File"), "");
134 
135  if(fileName.isEmpty())
136  return;
137 
138  m_ui->m_locationLineEdit->setText(fileName);
139  }
140 }
141 
143 {
144  // check interface parameters
145  if(m_ui->m_nameLineEdit->text().isEmpty())
146  {
147  QMessageBox::warning(this, tr("Warning"), tr("Output name not defined."));
148  return;
149  }
150 
151  if(m_ui->m_locationLineEdit->text().isEmpty())
152  {
153  QMessageBox::warning(this, tr("Warning"), tr("Output location not defined."));
154  return;
155  }
156 
157  if(m_ui->m_inputLayerComboBox->currentText().isEmpty() || m_ui->m_attrIdComboBox->currentText().isEmpty())
158  {
159  QMessageBox::warning(this, tr("Warning"), tr("Input information not defined."));
160  return;
161  }
162 
163  //get input parameters
164  std::string attrName = m_ui->m_attrIdComboBox->currentText().toStdString();
165 
166  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
167 
169 
170  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
171 
172  if(!dsLayer)
173  {
174  QMessageBox::warning(this, tr("Warning"), tr("Error getting input layer."));
175  return;
176  }
177 
178  te::da::DataSourcePtr ds = te::da::GetDataSource(dsLayer->getDataSourceId(), true);
179 
180  //get constructor strategy
182 
183  if(m_ui->m_buildStratContiguityRadioButton->isChecked())
184  {
185  constructor = new te::sa::GPMConstructorAdjacencyStrategy(m_ui->m_calcDistCheckBox->isChecked());
186  }
187  else if(m_ui->m_buildStratDistanceRadioButton->isChecked())
188  {
189  if(m_ui->m_distanceLineEdit->text().isEmpty())
190  {
191  QMessageBox::warning(this, tr("Warning"), tr("Distance Value not Defined."));
192  return;
193  }
194 
195  constructor = new te::sa::GPMConstructorDistanceStrategy(m_ui->m_distanceLineEdit->text().toDouble());
196  }
197  else if(m_ui->m_buildStratNNRadioButton->isChecked())
198  {
199  if(m_ui->m_nearNeighborLineEdit->text().isEmpty())
200  {
201  QMessageBox::warning(this, tr("Warning"), tr("Number of Neighbors not Defined."));
202  return;
203  }
204 
205  constructor = new te::sa::GPMConstructorNearestNeighborStrategy(m_ui->m_nearNeighborLineEdit->text().toInt());
206  }
207 
208  //get weights strategy
210 
211  if(m_ui->m_weightNoWeightRadioButton->isChecked())
212  {
213  weights = new te::sa::GPMWeightsNoWeightsStrategy(m_ui->m_normalizeWeightCheckBox->isChecked());
214  }
215  else if(m_ui->m_weightInverseDistRadioButton->isChecked())
216  {
217  weights = new te::sa::GPMWeightsInverseDistanceStrategy(m_ui->m_normalizeWeightCheckBox->isChecked()); // using default parameters
218  }
219  else if(m_ui->m_weightSquareInverseDistRadioButton->isChecked())
220  {
221  weights = new te::sa::GPMWeightsSquaredInverseDistanceStrategy(m_ui->m_normalizeWeightCheckBox->isChecked()); // using default parameters
222  }
223 
224  //run operation
225  te::sa::GPMBuilder builder(constructor, weights);
226 
227  std::auto_ptr<te::sa::GeneralizedProximityMatrix> gpm;
228 
229  //progress
232 
233  QApplication::setOverrideCursor(Qt::WaitCursor);
234 
235  try
236  {
237  builder.setGPMInfo(ds, dsLayer->getDataSetName(), attrName);
238 
239  gpm = builder.build();
240  }
241  catch(const std::exception& e)
242  {
243  QMessageBox::warning(this, tr("Warning"), e.what());
244 
245  QApplication::restoreOverrideCursor();
246 
248 
249  return;
250  }
251  catch(...)
252  {
253  QMessageBox::warning(this, tr("Warning"), tr("Internal Error."));
254 
255  QApplication::restoreOverrideCursor();
256 
258 
259  return;
260  }
261 
262  QApplication::restoreOverrideCursor();
263 
265 
266  //save gpm
267  if(!m_ui->m_dsRadioButton->isChecked())
268  {
269  //define the file name
270  std::string ext = "";
271 
272  if(m_ui->m_galRadioButton->isChecked())
273  ext = ".gal";
274  else if(m_ui->m_gwtRadioButton->isChecked())
275  ext = ".gwt";
276 
277  std::string name = m_ui->m_nameLineEdit->text().toStdString();
278 
279  std::string fullFileName = m_ui->m_locationLineEdit->text().toStdString();
280 
281  fullFileName += "/" + name + ext;
282 
283  //export
285 
286  if(m_ui->m_galRadioButton->isChecked())
287  exchanger .exportToGAL(gpm.get(), fullFileName);
288  else if(m_ui->m_gwtRadioButton->isChecked())
289  exchanger .exportToGWT(gpm.get(), fullFileName, 1); // use 1 pos to export the weight information
290  }
291 
292  accept();
293 }
294 
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
This class defines a an adjacency strategy class for a GPM constructor.
std::auto_ptr< GeneralizedProximityMatrix > build()
Definition: GPMBuilder.cpp:78
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
bool setGPMInfo(te::da::DataSourcePtr ds, const std::string &dataSetName, const std::string &attributeName)
Function used to create a empty gpm (using a MEMORY DIRECT graph)
Definition: GPMBuilder.cpp:49
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.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
ProximityMatrixCreatorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
void removeViewer(int viewerId)
Dettach a progress viewer.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
This class defines a class to calculates a weight for a GPM using Inverse Distance strategy...
std::auto_ptr< Ui::ProximityMatrixCreatorDialogForm > m_ui
This class defines a an Abstract class for a GPM constructor.
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
This class defines a an Abstract class to calculates a weight for a GPM.
This class defines functions used to load and save gpm's using GAL and GWT formats, both formats use a ' ' 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
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr