27 #include "../../BuildConfig.h"
28 #include "../../common/Logger.h"
29 #include "../../common/progress/ProgressManager.h"
30 #include "../../common/Translator.h"
31 #include "../../common/STLUtils.h"
32 #include "../../dataaccess/dataset/DataSetType.h"
33 #include "../../dataaccess/datasource/DataSourceCapabilities.h"
34 #include "../../dataaccess/datasource/DataSourceInfo.h"
35 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
36 #include "../../dataaccess/datasource/DataSourceFactory.h"
37 #include "../../dataaccess/datasource/DataSourceManager.h"
38 #include "../../dataaccess/utils/Utils.h"
39 #include "../../datatype/Enums.h"
40 #include "../../datatype/Property.h"
41 #include "../../maptools/AbstractLayer.h"
42 #include "../../qt/af/Utils.h"
43 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
44 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
45 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
46 #include "../../qt/widgets/Utils.h"
47 #include "../../raster/RasterProperty.h"
48 #include "../../statistics/core/Utils.h"
49 #include "../Config.h"
50 #include "../Exception.h"
52 #include "../RasterToVector.h"
53 #include "ui_RasterToVectorDialogForm.h"
56 #include <QFileDialog>
58 #include <QListWidget>
59 #include <QListWidgetItem>
60 #include <QMessageBox>
63 #include <boost/algorithm/string.hpp>
64 #include <boost/filesystem.hpp>
65 #include <boost/lexical_cast.hpp>
66 #include <boost/uuid/random_generator.hpp>
67 #include <boost/uuid/uuid_io.hpp>
71 m_ui(new
Ui::RasterToVectorDialogForm),
78 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"raster-vector-hint").pixmap(112,48));
79 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
90 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
91 m_ui->m_helpPushButton->setPageReference(
"plugins/attributefill/attrfill_raster_to_vector.html");
103 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
105 while(it != m_layers.end())
107 std::auto_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
108 if(dsType->hasRaster())
109 m_ui->m_inRasterComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
110 if(dsType->hasGeom())
111 m_ui->m_inVectorComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
123 std::vector<unsigned int> vecBands;
125 for(
int i = 0; i < m_ui->m_bandsListWidget->count(); ++i)
127 if(m_ui->m_bandsListWidget->isItemSelected(m_ui->m_bandsListWidget->item(i)))
129 vecBands.push_back(i);
137 std::vector<te::stat::StatisticalSummary> vecStatistics;
139 for(
int i = 0; i < m_ui->m_statisticsListWidget->count(); ++i)
141 if(m_ui->m_statisticsListWidget->isItemSelected(m_ui->m_statisticsListWidget->item(i)))
195 return vecStatistics;
200 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
202 std::string layerID = m_ui->m_inRasterComboBox->itemData(index, Qt::UserRole).toString().toStdString();
204 while(it != m_layers.end())
206 if(layerID == it->get()->getId().c_str())
207 m_rasterLayer = it->get();
212 m_ui->m_bandsListWidget->clear();
214 std::auto_ptr<te::da::DataSetType> dsType = m_rasterLayer->getSchema();
216 std::auto_ptr<te::da::DataSet> dsRaster = m_rasterLayer->getData();
217 std::auto_ptr<te::rst::Raster> raster = dsRaster->getRaster(rasterProp->
getName());
218 std::size_t n_bands = raster->getNumberOfBands();
220 for(std::size_t b = 0; b < n_bands; ++b)
221 m_ui->m_bandsListWidget->addItem(boost::lexical_cast<std::string>(b).c_str());
224 m_ui->m_statisticsListWidget->clear();
226 m_ui->m_statisticsListWidget->addItem(
"Minimum value");
227 m_ui->m_statisticsListWidget->addItem(
"Maximum value");
228 m_ui->m_statisticsListWidget->addItem(
"Mean");
229 m_ui->m_statisticsListWidget->addItem(
"Sum of values");
230 m_ui->m_statisticsListWidget->addItem(
"Total number of values");
231 m_ui->m_statisticsListWidget->addItem(
"Total not null values");
232 m_ui->m_statisticsListWidget->addItem(
"Standard deviation");
233 m_ui->m_statisticsListWidget->addItem(
"Variance");
234 m_ui->m_statisticsListWidget->addItem(
"Skewness");
235 m_ui->m_statisticsListWidget->addItem(
"Kurtosis");
236 m_ui->m_statisticsListWidget->addItem(
"Amplitude");
237 m_ui->m_statisticsListWidget->addItem(
"Median");
238 m_ui->m_statisticsListWidget->addItem(
"Coefficient variation");
239 m_ui->m_statisticsListWidget->addItem(
"Mode");
240 m_ui->m_statisticsListWidget->addItem(
"Percent of each class by area");
246 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
248 std::string layerID = m_ui->m_inVectorComboBox->itemData(index, Qt::UserRole).toString().toStdString();
250 while(it != m_layers.end())
252 if(layerID == it->get()->getId().c_str())
253 m_vectorLayer = it->get();
261 m_ui->m_newLayerNameLineEdit->clear();
262 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
266 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
268 if(dsPtrList.empty())
271 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
273 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
275 m_outputDatasource = *it;
282 m_ui->m_newLayerNameLineEdit->clear();
283 m_ui->m_repositoryLineEdit->clear();
285 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
286 QString(), tr(
"Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
288 if (fileName.isEmpty())
291 boost::filesystem::path outfile(fileName.toStdString());
292 std::string aux = outfile.leaf().string();
293 m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
294 aux = outfile.string();
295 m_ui->m_repositoryLineEdit->setText(aux.c_str());
298 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
303 if(m_ui->m_inRasterComboBox->count() == 0)
305 QMessageBox::information(
this,
"Fill",
"Select an input raster layer.");
309 if(m_ui->m_inVectorComboBox->count() == 0)
311 QMessageBox::information(
this,
"Fill",
"Select an input vector layer.");
320 QMessageBox::information(
this,
"Fill",
"Can not execute this operation on this type of layer.");
328 QMessageBox::information(
this,
"Fill",
"Can not execute this operation on this type of layer.");
333 if (!inRasterDataSource.get())
335 QMessageBox::information(
this,
"Fill",
"The selected raster data source can not be accessed.");
340 if (!inVectorDataSource.get())
342 QMessageBox::information(
this,
"Fill",
"The selected vector data source can not be accessed.");
346 std::vector<unsigned int> vecBands = getSelectedBands();
349 QMessageBox::information(
this,
"Fill",
"Select at least one band.");
354 std::vector<te::stat::StatisticalSummary> vecStatistics = getSelectedStatistics();
355 m_texture = m_ui->m_textureCheckBox->isChecked();
357 if(vecStatistics.empty() && m_texture ==
false)
359 QMessageBox::information(
this,
"Fill",
"Select at least one statistic operation or select the texture checkbox.");
363 if(m_ui->m_repositoryLineEdit->text().isEmpty())
365 QMessageBox::information(
this,
"Fill",
"Define a repository for the result.");
369 if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
371 QMessageBox::information(
this,
"Fill",
"Define a name for the resulting layer.");
375 std::string outputdataset = m_ui->m_newLayerNameLineEdit->text().toStdString();
387 boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
389 if (boost::filesystem::exists(uri))
391 QMessageBox::information(
this,
"Fill",
"Output file already exists. Remove it or select a new name and try again.");
395 std::size_t idx = outputdataset.find(
".");
396 if (idx != std::string::npos)
397 outputdataset=outputdataset.substr(0,idx);
399 std::map<std::string, std::string> dsinfo;
400 dsinfo[
"URI"] = uri.string();
403 dsOGR->setConnectionInfo(dsinfo);
405 if (dsOGR->dataSetExists(outputdataset))
407 QMessageBox::information(
this,
"Fill",
"There is already a dataset with the requested name in the output data source. Remove it or select a new name and try again.");
411 this->setCursor(Qt::WaitCursor);
414 rst2vec->
setInput(inRasterDataSource,
421 rst2vec->
setParams(vecBands, vecStatistics, m_texture);
423 rst2vec->
setOutput(dsOGR, outputdataset);
428 res = rst2vec->
run();
432 this->setCursor(Qt::ArrowCursor);
434 QMessageBox::information(
this,
"Fill",
"Error: could not generate the operation.");
442 boost::uuids::basic_random_generator<boost::mt19937> gen;
443 boost::uuids::uuid u = gen();
444 std::string id_ds = boost::uuids::to_string(u);
447 ds->setConnInfo(dsinfo);
448 ds->setTitle(uri.stem().string());
449 ds->setAccessDriver(
"OGR");
451 ds->setDescription(uri.string());
457 m_outputDatasource = ds;
464 QMessageBox::information(
this,
"Fill",
"The selected output datasource can not be accessed.");
468 if (aux->dataSetExists(outputdataset))
470 QMessageBox::information(
this,
"Fill",
"Dataset already exists. Remove it or select a new name and try again.");
473 this->setCursor(Qt::WaitCursor);
477 rst2vec->
setInput(inRasterDataSource,
484 rst2vec->
setParams(vecBands, vecStatistics, m_texture);
491 res = rst2vec->
run();
497 this->setCursor(Qt::ArrowCursor);
498 QMessageBox::information(
this,
"Fill",
"Error: could not generate the operation.");
511 m_outLayer = converter(dt);
513 catch(
const std::exception& e)
515 this->setCursor(Qt::ArrowCursor);
517 QMessageBox::information(
this,
"Fill", e.what());
519 #ifdef TERRALIB_LOGGER_ENABLED
520 te::common::Logger::logDebug(
"attributefill", e.what());
521 #endif // TERRALIB_LOGGER_ENABLED
529 this->setCursor(Qt::ArrowCursor);
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Raster to vector attributefill dialog.
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
std::vector< unsigned int > getSelectedBands()
Get the selected bands based on selected QListWidgetItem.
const std::string & getDataSetName() const
boost::shared_ptr< DataSetType > DataSetTypePtr
boost::shared_ptr< DataSource > DataSourcePtr
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName)
void setParams(std::vector< unsigned int > bands, std::vector< te::stat::StatisticalSummary > statSum, bool texture)
Percente of each class by area.
void onTargetDatasourceToolButtonPressed()
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
void removeViewer(int viewerId)
Dettach a progress viewer.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
std::auto_ptr< Ui::RasterToVectorDialogForm > m_ui
User interface.
const std::string & getDataSourceId() const
static std::auto_ptr< DataSource > make(const std::string &dsType)
void onTargetFileToolButtonPressed()
te::map::AbstractLayerPtr getLayer()
Get the generated layer.
void setInput(te::da::DataSourcePtr inRasterDsrc, std::string inRasterName, std::auto_ptr< te::da::DataSetType > inRasterDsType, te::da::DataSourcePtr inVectorDsrc, std::string inVectorName, std::auto_ptr< te::da::DataSetType > inVectorDsType)
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
void onVectorComboBoxChanged(int index)
A class that represents a data source component.
A layer with reference to a dataset.
std::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
void onOkPushButtonClicked()
std::vector< te::stat::StatisticalSummary > getSelectedStatistics()
Get the selected statistics based on selected QListWidgetItem.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void onRasterComboBoxChanged(int index)
void onCancelPushButtonClicked()
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
RasterToVectorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
const std::string & getName() const
It returns the property name.