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/SkaterOperation.h"
41 #include "../core/SkaterParams.h"
42 #include "../core/SpatialWeightsExchanger.h"
43 #include "../core/Utils.h"
44 #include "../Exception.h"
47 #include "ui_SkaterDialogForm.h"
50 #include <QFileDialog>
52 #include <QGridLayout>
53 #include <QMessageBox>
60 #include <boost/filesystem.hpp>
66 m_ui(new Ui::SkaterDialogForm)
75 QGridLayout* layout =
new QGridLayout(
m_ui->m_widget);
77 layout->setContentsMargins(0, 0, 0, 0);
80 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"sa-skater-hint").pixmap(112,48));
81 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
91 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
92 m_ui->m_helpPushButton->setPageReference(
"plugins/sa/sa_skater.html");
101 std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
103 while(it != layers.end())
109 std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
112 if(dsLayer && dsType->hasGeom())
113 m_ui->m_inputLayerComboBox->addItem(it->get()->getTitle().c_str(), QVariant::fromValue(l));
120 if(m_ui->m_inputLayerComboBox->count() > 0)
121 onInputLayerComboBoxActivated(0);
126 return m_outputLayer;
131 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
135 std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
137 std::vector<te::dt::Property*> propVec = dsType->getProperties();
139 m_ui->m_popComboBox->clear();
140 m_ui->m_attrLinkComboBox->clear();
142 m_doubleListWidget->clearInputValues();
143 m_doubleListWidget->clearOutputValues();
145 std::vector<std::string> vec;
147 for(std::size_t t = 0; t < propVec.size(); ++t)
149 int dataType = propVec[t]->getType();
156 m_ui->m_popComboBox->addItem(propVec[t]->getName().c_str(), dataType);
158 vec.push_back(propVec[t]->getName());
161 m_ui->m_attrLinkComboBox->addItem(propVec[t]->getName().c_str(), dataType);
164 std::sort(vec.begin(), vec.end());
166 m_doubleListWidget->setInputValues(vec);
172 if(m_ui->m_repositoryLineEdit->text().isEmpty())
174 QMessageBox::information(
this, tr(
"Warning"), tr(
"Define a repository for the result."));
178 if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
180 QMessageBox::information(
this, tr(
"Warning"), tr(
"Define a name for the resulting layer."));
184 if(!m_ui->m_clustersCheckBox->isChecked() && !m_ui->m_popCheckBox->isChecked())
186 QMessageBox::information(
this, tr(
"Warning"), tr(
"Select the aggregation type."));
190 if(m_ui->m_clustersCheckBox->isChecked())
192 if(m_ui->m_nClustersLineEdit->text().isEmpty())
194 QMessageBox::information(
this, tr(
"Warning"), tr(
"Number of clusters not defined."));
199 if(m_ui->m_popCheckBox->isChecked())
201 if(m_ui->m_minPopLineEdit->text().isEmpty())
203 QMessageBox::information(
this, tr(
"Warning"), tr(
"Minimum population not defined."));
208 std::vector<std::string> attrs = m_doubleListWidget->getOutputValues();
212 QMessageBox::information(
this, tr(
"Warning"), tr(
"Select the attributes."));
217 std::auto_ptr<te::sa::GeneralizedProximityMatrix> gpm;
225 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal error. GPM not loaded."));
233 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
236 std::auto_ptr<te::da::DataSetType> dataSetType = l->getSchema();
237 std::auto_ptr<te::da::DataSet> dataSet = l->getData();
240 std::string dataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
242 std::size_t idx = dataSetName.find(
".");
243 if (idx != std::string::npos)
244 dataSetName=dataSetName.substr(0,idx);
260 inParams->
m_ds = dataSet;
263 inParams->
m_gpm = gpm;
266 if(m_ui->m_clustersCheckBox->isChecked() && m_ui->m_popCheckBox->isChecked())
269 inParams->
m_nClusters = (std::size_t)m_ui->m_nClustersLineEdit->text().toInt();
270 inParams->
m_minPop = (std::size_t)m_ui->m_minPopLineEdit->text().toInt();
271 inParams->
m_attrPop = m_ui->m_popComboBox->currentText().toStdString();
273 else if(m_ui->m_clustersCheckBox->isChecked())
276 inParams->
m_nClusters = (std::size_t)m_ui->m_nClustersLineEdit->text().toInt();
278 else if(m_ui->m_popCheckBox->isChecked())
281 inParams->
m_minPop = (std::size_t)m_ui->m_minPopLineEdit->text().toInt();
282 inParams->
m_attrPop = m_ui->m_popComboBox->currentText().toStdString();
298 QApplication::setOverrideCursor(Qt::WaitCursor);
310 catch(
const std::exception& e)
312 QMessageBox::warning(
this, tr(
"Warning"), e.what());
314 QApplication::restoreOverrideCursor();
322 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Internal Error. Skater not calculated."));
324 QApplication::restoreOverrideCursor();
331 QApplication::restoreOverrideCursor();
346 m_ui->m_newLayerNameLineEdit->clear();
347 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
352 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
354 if(dsPtrList.size() <= 0)
357 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
359 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
361 m_outputDatasource = *it;
368 m_ui->m_newLayerNameLineEdit->clear();
369 m_ui->m_repositoryLineEdit->clear();
371 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."), QString(), tr(
"Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
373 if (fileName.isEmpty())
376 boost::filesystem::path outfile(fileName.toStdString());
378 m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
380 m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
382 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
389 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Open Generalized Proximity Matrix File"),
"", tr(
"GAL Files (*.gal *.GAL);; GWT Files (*.gwt *.GWT)"));
391 if(fileName.isEmpty())
395 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
400 std::string path = fileName.toStdString();
401 std::string dataSetName =
"";
402 std::string attrName =
"";
406 if(dsLayer->getDataSetName() != dataSetName)
408 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid GPM file for selected layer."));
412 if(m_ui->m_attrLinkComboBox->currentText().toStdString() != attrName)
414 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid GPM file for selected Attr Link."));
418 m_ui->m_gpmLineEdit->setText(fileName);
423 std::auto_ptr<te::sa::GeneralizedProximityMatrix> gpm;
426 QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(m_ui->m_inputLayerComboBox->currentIndex(), Qt::UserRole);
431 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid selected layer."));
442 if(!m_ui->m_gpmGroupBox->isChecked())
445 if(QMessageBox::question(
this, tr(
"Spatial Analysis"), tr(
"GPM not selected. Create default GPM?"), QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
449 std::auto_ptr<te::da::DataSetType> dsType = dsLayer->
getSchema();
451 if(!dsType->getPrimaryKey() || dsType->getPrimaryKey()->getProperties().empty())
453 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Invalid Data Set Primary Key."));
457 std::string attrLink = dsType->getPrimaryKey()->getProperties()[0]->getName();
465 builder.
setGPMInfo(ds, dataSetName, attrLink);
467 gpm = builder.
build();
471 if(m_ui->m_gpmLineEdit->text().isEmpty())
473 QMessageBox::warning(
this, tr(
"Warning"), tr(
"GPM File not selected."));
478 QFileInfo file(m_ui->m_gpmLineEdit->text());
480 std::string extension = file.suffix().toStdString();
484 if(extension ==
"gal" || extension ==
"GAL")
486 gpm.reset(swe.
importFromGAL(m_ui->m_gpmLineEdit->text().toStdString(), ds.get()));
488 else if(extension ==
"gwt" || extension ==
"GWT")
490 gpm.reset(swe.
importFromGWT(m_ui->m_gpmLineEdit->text().toStdString(), ds.get()));
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
void execute()
Function to execute the skater operation.
This class defines a an adjacency strategy class for a GPM constructor.
std::auto_ptr< GeneralizedProximityMatrix > build()
Utility functions for the data access module.
const std::string & getDataSetName() const
SkaterDialog(QWidget *parent=0, Qt::WindowFlags f=0)
te::map::AbstractLayerPtr getOutputLayer()
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.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
te::qt::widgets::DoubleListWidget * m_doubleListWidget
Widget used to select attributes.
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.
Class that represents the skater output parameters.
Class used to execute the skater operations.
void onTargetDatasourceToolButtonPressed()
A dialog for Spatial 'K'luster Analysis by Tree Edge Removal.
void removeViewer(int viewerId)
Dettach a progress viewer.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
const std::string & getDataSourceId() const
void setParameters(te::sa::SkaterInputParams *inParams, te::sa::SkaterOutputParams *outParams)
void onGPMToolButtonClicked()
static void getSpatialWeightsFileInfo(std::string pathFileName, std::string &dataSetName, std::string &attrName)
Function used to get information of how a Spatial Weights was generated.
std::string m_outputDataSetName
Attribute that defines the output dataset name.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
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.
te::da::DataSourcePtr m_dataSource
Pointer to the output data source.
TESAEXPORT te::map::AbstractLayerPtr CreateLayer(te::da::DataSourcePtr ds, std::string dataSetName)
void onOkPushButtonClicked()
std::auto_ptr< Ui::SkaterDialogForm > m_ui
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.
std::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
This class defines the GPM Builder class.
TESAEXPORT void CreateSkaterGrouping(te::map::AbstractLayerPtr layer, int nClasses)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::auto_ptr< te::sa::GeneralizedProximityMatrix > loadGPM()
void onInputLayerComboBoxActivated(int index)
void onTargetFileToolButtonPressed()