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/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>
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"));
83 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
84 m_ui->m_helpPushButton->setPageReference(
"plugins/sa/sa_spatialstatistics.html");
93 std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
95 while(it != layers.end())
101 std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
105 if(dsLayer && dsType->hasGeom())
106 m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
113 if(m_ui->m_inputLayerComboBox->count() > 0)
114 onInputLayerComboBoxActivated(0);
119 return m_outputLayer;
124 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
128 std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
130 std::vector<te::dt::Property*> propVec = dsType->getProperties();
132 m_ui->m_attrIdComboBox->clear();
133 m_ui->m_attrLinkComboBox->clear();
135 for(std::size_t t = 0; t < propVec.size(); ++t)
137 int dataType = propVec[t]->getType();
144 m_ui->m_attrIdComboBox->addItem(propVec[t]->getName().c_str(), dataType);
147 m_ui->m_attrLinkComboBox->addItem(propVec[t]->getName().c_str(), dataType);
153 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Open Generalized Proximity Matrix File"),
"", tr(
"GAL Files (*.gal *.GAL);; GWT Files (*.gwt *.GWT)"));
155 if(fileName.isEmpty())
159 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
164 std::string path = fileName.toStdString();
165 std::string dataSetName =
"";
166 std::string attrName =
"";
170 if(dsLayer->getDataSetName() != dataSetName)
172 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid GPM file for selected layer."));
176 if(m_ui->m_attrLinkComboBox->currentText().toStdString() != attrName)
178 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid GPM file for selected Attr Link."));
182 m_ui->m_gpmLineEdit->setText(fileName);
188 if(m_ui->m_repositoryLineEdit->text().isEmpty())
190 QMessageBox::information(
this, tr(
"Warning"), tr(
"Define a repository for the result."));
194 if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
196 QMessageBox::information(
this, tr(
"Warning"), tr(
"Define a name for the resulting layer."));
200 if(m_ui->m_attrIdComboBox->currentText().isEmpty())
202 QMessageBox::information(
this, tr(
"Warning"), tr(
"Attribute Name not defined."));
207 std::auto_ptr<te::sa::GeneralizedProximityMatrix> gpm;
215 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal error. GPM not loaded."));
223 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
230 std::auto_ptr<te::da::DataSetType> dataSetType = l->getSchema();
232 std::string attrLink = gpm->getAttributeName();
234 std::string attrName = m_ui->m_attrIdComboBox->currentText().toStdString();
236 int type = m_ui->m_attrIdComboBox->itemData(m_ui->m_attrIdComboBox->currentIndex()).toInt();
245 QApplication::setOverrideCursor(Qt::WaitCursor);
248 if(m_ui->m_localGStatisticsCheckBox->isChecked())
254 catch(
const std::exception& e)
256 QMessageBox::warning(
this, tr(
"Warning"), e.what());
258 QApplication::restoreOverrideCursor();
266 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal error. G Statistics not calculated."));
268 QApplication::restoreOverrideCursor();
277 if(m_ui->m_localLocalMeanCheckBox->isChecked())
283 catch(
const std::exception& e)
285 QMessageBox::warning(
this, tr(
"Warning"), e.what());
287 QApplication::restoreOverrideCursor();
295 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal error. Local Mean not calculated."));
297 QApplication::restoreOverrideCursor();
306 if(m_ui->m_globalMoranIndexCheckBox->isChecked() || m_ui->m_localMoranIndex->isChecked())
315 if(m_ui->m_globalMoranIndexCheckBox->isChecked())
317 m_ui->m_globalMoranIndexValueLineEdit->setText(QString::number(globalMoranIndex));
321 if(m_ui->m_globalMoranIndexCheckBox->isChecked() && !m_ui->m_globalEvalNotRadioButton->isChecked())
325 if(m_ui->m_globalEval99RadioButton->isChecked())
327 else if(m_ui->m_globalEval999RadioButton->isChecked())
332 m_ui->m_globalMoranIndexPValueLineEdit->setText(QString::number(globalMoranSignificance));
338 if(m_ui->m_localMoranIndex->isChecked() && !m_ui->m_localEvalNotRadioButton->isChecked())
342 if(m_ui->m_localEval99RadioButton->isChecked())
344 else if(m_ui->m_localEval999RadioButton->isChecked())
346 else if(m_ui->m_localEval9999RadioButton->isChecked())
356 catch(
const std::exception& e)
358 QMessageBox::warning(
this, tr(
"Warning"), e.what());
360 QApplication::restoreOverrideCursor();
368 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal error. Moran not calculated."));
370 QApplication::restoreOverrideCursor();
379 std::string dataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
381 std::size_t idx = dataSetName.find(
".");
382 if (idx != std::string::npos)
383 dataSetName=dataSetName.substr(0,idx);
402 gpm->toDataSource(outputDataSource, dataSetName);
404 catch(
const std::exception& e)
406 QMessageBox::warning(
this, tr(
"Warning"), e.what());
408 QApplication::restoreOverrideCursor();
416 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Error saving GPM into data source."));
418 QApplication::restoreOverrideCursor();
425 QApplication::restoreOverrideCursor();
429 QMessageBox::information(
this, tr(
"Information"), tr(
"Statistics Calculated. Press the 'Close' button to close the dialog."));
436 std::auto_ptr<te::sa::GeneralizedProximityMatrix> gpm;
439 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
444 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid selected layer."));
455 if(!m_ui->m_gpmGroupBox->isChecked())
458 if(QMessageBox::question(
this, tr(
"Spatial Analysis"), tr(
"GPM not selected. Create default GPM?"), QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
462 std::auto_ptr<te::da::DataSetType> dsType = dsLayer->
getSchema();
464 if(!dsType->getPrimaryKey() || dsType->getPrimaryKey()->getProperties().empty())
466 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid Data Set Primary Key."));
470 std::string attrLink = dsType->getPrimaryKey()->getProperties()[0]->getName();
478 builder.
setGPMInfo(ds, dataSetName, attrLink);
480 gpm = builder.
build();
484 if(m_ui->m_gpmLineEdit->text().isEmpty())
486 QMessageBox::warning(
this, tr(
"Warning"), tr(
"GPM File not selected."));
491 QFileInfo file(m_ui->m_gpmLineEdit->text());
493 std::string extension = file.suffix().toStdString();
497 if(extension ==
"gal" || extension ==
"GAL")
499 gpm.reset(swe.
importFromGAL(m_ui->m_gpmLineEdit->text().toStdString(), ds.get()));
501 else if(extension ==
"gwt" || extension ==
"GWT")
503 gpm.reset(swe.
importFromGWT(m_ui->m_gpmLineEdit->text().toStdString(), ds.get()));
512 m_ui->m_newLayerNameLineEdit->clear();
513 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
518 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
520 if(dsPtrList.size() <= 0)
523 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
525 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
527 m_outputDatasource = *it;
534 m_ui->m_newLayerNameLineEdit->clear();
535 m_ui->m_repositoryLineEdit->clear();
537 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."), QString(), tr(
"Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
539 if (fileName.isEmpty())
542 boost::filesystem::path outfile(fileName.toStdString());
544 m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
546 m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
548 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.
void onOkPushButtonClicked()
std::auto_ptr< GeneralizedProximityMatrix > build()
Utility functions for the data access module.
const std::string & getDataSetName() const
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 a 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.
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.
std::auto_ptr< Ui::SpatialStatisticsDialogForm > m_ui
void onInputLayerComboBoxActivated(int index)
TESAEXPORT void LisaStatisticalSignificance(te::sa::GeneralizedProximityMatrix *gpm, int permutationsNumber)
Function used to calculate LISA Statical Significance for each gpm element.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
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.
A dialog to calculate spatial statistics from a attribute of a dataset.
const std::string & getDataSourceId() const
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
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.
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...
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)...
int getType() const
It returns the property data type.
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.
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)
SpatialStatisticsDialog(QWidget *parent=0, Qt::WindowFlags f=0)
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.
std::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
This class defines the GPM Builder class.
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=te::gm::UnknownGeometryType)
Function used to set a an attribute valeu from a dataset to the vertex objects from a gpm...
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
std::auto_ptr< te::sa::GeneralizedProximityMatrix > loadGPM()
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...
const std::string & getName() const
It returns the property name.