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/GPMBuilder.h" 38 #include "../core/GPMConstructorAdjacencyStrategy.h" 39 #include "../core/GPMWeightsNoWeightsStrategy.h" 40 #include "../core/SpatialStatisticsFunctions.h" 41 #include "../core/SpatialWeightsExchanger.h" 42 #include "../core/StatisticsFunctions.h" 43 #include "../core/Utils.h" 44 #include "../Exception.h" 47 #include "ui_SpatialStatisticsDialogForm.h" 50 #include <QFileDialog> 52 #include <QMessageBox> 59 #include <boost/filesystem.hpp> 63 te::sa::SpatialStatisticsDialog::SpatialStatisticsDialog(
QWidget* parent, Qt::WindowFlags f)
65 m_ui(new
Ui::SpatialStatisticsDialogForm)
71 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"sa-spatialstatistics-hint").pixmap(112,48));
72 m_ui->m_gpmToolButton->setIcon(QIcon::fromTheme(
"folder-open"));
73 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
76 connect(m_ui->m_inputLayerComboBox, SIGNAL(activated(
int)),
this, SLOT(onInputLayerComboBoxActivated(
int)));
77 connect(m_ui->m_gpmToolButton, SIGNAL(clicked()),
this, SLOT(onGPMToolButtonClicked()));
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()));
83 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
84 m_ui->m_helpPushButton->setPageReference(
"plugins/sa/sa_spatialstatistics.html");
91 std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
93 while(it != layers.end())
99 std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
103 if(dsLayer && dsType->hasGeom())
104 m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
111 if(
m_ui->m_inputLayerComboBox->count() > 0)
122 QVariant varLayer =
m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
126 std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
128 std::vector<te::dt::Property*> propVec = dsType->getProperties();
130 m_ui->m_attrIdComboBox->clear();
131 m_ui->m_attrLinkComboBox->clear();
133 for(std::size_t t = 0; t < propVec.size(); ++t)
135 int dataType = propVec[t]->getType();
142 m_ui->m_attrIdComboBox->addItem(propVec[t]->getName().c_str(), dataType);
145 m_ui->m_attrLinkComboBox->addItem(propVec[t]->getName().c_str(), dataType);
151 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Open Generalized Proximity Matrix File"),
"", tr(
"GAL Files (*.gal *.GAL);; GWT Files (*.gwt *.GWT)"));
153 if(fileName.isEmpty())
157 QVariant varLayer =
m_ui->m_inputLayerComboBox->itemData(
m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
162 std::string path = fileName.toUtf8().data();
163 std::string dataSetName;
164 std::string attrName;
168 if(dsLayer->getDataSetName() != dataSetName)
170 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid GPM file for selected layer."));
174 if(
m_ui->m_attrLinkComboBox->currentText().toUtf8().data() != attrName)
176 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid GPM file for selected Attr Link."));
180 m_ui->m_gpmLineEdit->setText(fileName);
186 if(
m_ui->m_repositoryLineEdit->text().isEmpty())
188 QMessageBox::information(
this, tr(
"Warning"), tr(
"Define a repository for the result."));
192 if(
m_ui->m_newLayerNameLineEdit->text().isEmpty())
194 QMessageBox::information(
this, tr(
"Warning"), tr(
"Define a name for the resulting layer."));
198 if(
m_ui->m_attrIdComboBox->currentText().isEmpty())
200 QMessageBox::information(
this, tr(
"Warning"), tr(
"Attribute Name not defined."));
205 std::unique_ptr<te::sa::GeneralizedProximityMatrix> gpm;
213 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal error. GPM not loaded."));
221 QVariant varLayer =
m_ui->m_inputLayerComboBox->itemData(
m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
228 std::unique_ptr<te::da::DataSetType> dataSetType = l->getSchema();
230 std::string attrLink = gpm->getAttributeName();
232 std::string attrName =
m_ui->m_attrIdComboBox->currentText().toUtf8().data();
234 int type =
m_ui->m_attrIdComboBox->itemData(
m_ui->m_attrIdComboBox->currentIndex()).toInt();
242 QApplication::setOverrideCursor(Qt::WaitCursor);
245 if(
m_ui->m_localGStatisticsCheckBox->isChecked())
251 catch(
const std::exception& e)
253 QMessageBox::warning(
this, tr(
"Warning"), e.what());
255 QApplication::restoreOverrideCursor();
261 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal error. G Statistics not calculated."));
263 QApplication::restoreOverrideCursor();
270 if(
m_ui->m_localLocalMeanCheckBox->isChecked())
276 catch(
const std::exception& e)
278 QMessageBox::warning(
this, tr(
"Warning"), e.what());
280 QApplication::restoreOverrideCursor();
286 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal error. Local Mean not calculated."));
288 QApplication::restoreOverrideCursor();
295 if(
m_ui->m_globalMoranIndexCheckBox->isChecked() ||
m_ui->m_localMoranIndex->isChecked())
304 if(
m_ui->m_globalMoranIndexCheckBox->isChecked())
306 m_ui->m_globalMoranIndexValueLineEdit->setText(QString::number(globalMoranIndex));
310 if(
m_ui->m_globalMoranIndexCheckBox->isChecked() && !
m_ui->m_globalEvalNotRadioButton->isChecked())
314 if(
m_ui->m_globalEval99RadioButton->isChecked())
316 else if(
m_ui->m_globalEval999RadioButton->isChecked())
321 m_ui->m_globalMoranIndexPValueLineEdit->setText(QString::number(globalMoranSignificance));
327 if(
m_ui->m_localMoranIndex->isChecked() && !
m_ui->m_localEvalNotRadioButton->isChecked())
331 if(
m_ui->m_localEval99RadioButton->isChecked())
333 else if(
m_ui->m_localEval999RadioButton->isChecked())
335 else if(
m_ui->m_localEval9999RadioButton->isChecked())
345 catch(
const std::exception& e)
347 QMessageBox::warning(
this, tr(
"Warning"), e.what());
349 QApplication::restoreOverrideCursor();
355 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal error. Moran not calculated."));
357 QApplication::restoreOverrideCursor();
364 std::string dataSetName =
m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
366 std::size_t idx = dataSetName.find(
".");
367 if (idx != std::string::npos)
368 dataSetName=dataSetName.substr(0,idx);
384 te::sa::AssociateGPMVertexAttribute(gpm.get(), ds.get(), dsLayer->getDataSetName(), attrLink, gmProp->getName(), gmProp->getType(), gmProp->getSRID(), gmProp->getGeometryType());
388 gpm->toDataSource(outputDataSource, dataSetName);
390 catch(
const std::exception& e)
392 QMessageBox::warning(
this, tr(
"Warning"), e.what());
394 QApplication::restoreOverrideCursor();
400 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error saving GPM into data source."));
402 QApplication::restoreOverrideCursor();
407 QApplication::restoreOverrideCursor();
409 QMessageBox::information(
this, tr(
"Information"), tr(
"Statistics Calculated. Press the 'Close' button to close the dialog."));
416 std::unique_ptr<te::sa::GeneralizedProximityMatrix> gpm;
419 QVariant varLayer =
m_ui->m_inputLayerComboBox->itemData(
m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
424 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid selected layer."));
435 if(!
m_ui->m_gpmGroupBox->isChecked())
438 if(QMessageBox::question(
this, tr(
"Spatial Analysis"), tr(
"GPM not selected. Create default GPM?"), QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
442 std::unique_ptr<te::da::DataSetType> dsType = dsLayer->
getSchema();
444 if(!dsType->getPrimaryKey() || dsType->getPrimaryKey()->getProperties().empty())
446 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid Data Set Primary Key."));
450 std::string attrLink = dsType->getPrimaryKey()->getProperties()[0]->getName();
458 builder.
setGPMInfo(ds, dataSetName, attrLink);
461 gpm.reset(builder.
getGPM());
465 if(
m_ui->m_gpmLineEdit->text().isEmpty())
467 QMessageBox::warning(
this, tr(
"Warning"), tr(
"GPM File not selected."));
472 QFileInfo
file(
m_ui->m_gpmLineEdit->text());
474 std::string extension =
file.suffix().toUtf8().data();
478 if(extension ==
"gal" || extension ==
"GAL")
480 gpm.reset(swe.
importFromGAL(
m_ui->m_gpmLineEdit->text().toUtf8().data(), ds.get()));
482 else if(extension ==
"gwt" || extension ==
"GWT")
484 gpm.reset(swe.
importFromGWT(
m_ui->m_gpmLineEdit->text().toUtf8().data(), ds.get()));
493 m_ui->m_newLayerNameLineEdit->clear();
494 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
499 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
501 if(dsPtrList.empty())
504 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
506 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
515 m_ui->m_newLayerNameLineEdit->clear();
516 m_ui->m_repositoryLineEdit->clear();
518 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."), QString(), tr(
"Shapefile (*.shp *.SHP);;"),
nullptr, QFileDialog::DontConfirmOverwrite);
520 if (fileName.isEmpty())
523 boost::filesystem::path outfile(fileName.toUtf8().data());
525 m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
527 m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
529 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
~SpatialStatisticsDialog()
This class defines a an adjacency strategy class for a GPM constructor.
GeneralizedProximityMatrix * getGPM()
Function used to return the generated gpm.
void onOkPushButtonClicked()
te::map::AbstractLayerPtr m_outputLayer
Generated Layer.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
TESAEXPORT void MoranMap(te::sa::GeneralizedProximityMatrix *gpm)
Function used to calculate the moran map info for a gpm, classifies the objects based in the scatterp...
TESAEXPORT double GlobalMoranSignificance(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx, int permutationsNumber, double moranIndex)
Function used to calculate the global moran significance.
boost::shared_ptr< DataSource > DataSourcePtr
TESAEXPORT te::da::DataSourcePtr CreateOGRDataSource(std::string repository)
bool setGPMInfo(te::da::DataSourcePtr ds, const std::string &dataSetName, const std::string &attributeName)
Function used to create an empty gpm (using a MEMORY DIRECT graph)
This class defines a class to calculates a weight for a GPM using No Weights strategy.
TESAEXPORT void LocalMean(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
Function used to calculate the local mean of each vertex from gpm graph.
std::unique_ptr< LayerSchema > getSchema() const
It returns the layer schema.
static te::dt::Date ds(2010, 01, 01)
te::sa::GeneralizedProximityMatrix * importFromGAL(std::string pathFileName, te::da::DataSource *ds=0)
Function used to import a gpm from a Spatial Weights File GAL Format.
te::sa::GeneralizedProximityMatrix * importFromGWT(std::string pathFileName, te::da::DataSource *ds=0)
Function used to import a gpm from a Spatial Weights File GWT Format.
void onInputLayerComboBoxActivated(int index)
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
TESAEXPORT void LisaStatisticalSignificance(te::sa::GeneralizedProximityMatrix *gpm, int permutationsNumber)
Function used to calculate LISA Statical Significance for each gpm element.
te::da::DataSourceInfoPtr m_outputDatasource
A dialog to calculate spatial statistics from a attribute of a dataset.
te::map::AbstractLayerPtr getOutputLayer()
TESAEXPORT void LISAMap(te::sa::GeneralizedProximityMatrix *gpm, int permutationsNumber)
Function used to calculate the lisa map info for a gpm, classifies the objects based in the statistic...
static void getSpatialWeightsFileInfo(std::string pathFileName, std::string &dataSetName, std::string &attrName)
Function used to get information of how a Spatial Weights was generated.
Utility functions for the data access module.
TESAEXPORT void BoxMap(te::sa::GeneralizedProximityMatrix *gpm, double mean)
Function used to calculate the box map info for a gpm, classifies the objects in quadrants based in t...
TESAEXPORT int AssociateGPMVertexAttribute(te::sa::GeneralizedProximityMatrix *gpm, te::da::DataSource *ds, std::string dataSetName, std::string attrLink, std::string attr, int dataType, int srid=TE_UNKNOWN_SRS, int subType=static_cast< int >(te::gm::UnknownGeometryType))
Function used to set a an attribute valeu from a dataset to the vertex objects from a gpm...
void onGPMToolButtonClicked()
TESAEXPORT void ZAndWZ(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
Function used to calculate the standard deviation Z and local mean of the desviation Z (WZ)...
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.
const std::string & getDataSetName() const
TESAEXPORT double MoranIndex(te::sa::GeneralizedProximityMatrix *gpm)
Function used to calculate the moran index, also calculates the local moran value.
TESAEXPORT te::map::AbstractLayerPtr CreateLayer(te::da::DataSourcePtr ds, std::string dataSetName)
std::unique_ptr< Ui::SpatialStatisticsDialogForm > m_ui
std::unique_ptr< te::sa::GeneralizedProximityMatrix > loadGPM()
void onTargetDatasourceToolButtonPressed()
This class defines functions used to load and save gpm's using GAL and GWT formats, both formats use a ' ' as separator.
void onTargetFileToolButtonPressed()
A layer with reference to a dataset.
This class defines the GPM Builder class.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
TESAEXPORT void GStatistics(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
The local spatial statistic G is calculated for each zone based on the spatial weights object used...
virtual const std::string & getDataSourceId() const
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $