27 #include "terralib_config.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)))
192 return vecStatistics;
197 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
199 std::string layerID = m_ui->m_inRasterComboBox->itemData(index, Qt::UserRole).toString().toStdString();
201 while(it != m_layers.end())
203 if(layerID == it->get()->getId().c_str())
204 m_rasterLayer = it->get();
209 m_ui->m_bandsListWidget->clear();
211 std::auto_ptr<te::da::DataSetType> dsType = m_rasterLayer->getSchema();
213 std::auto_ptr<te::da::DataSet> dsRaster = m_rasterLayer->getData();
214 std::auto_ptr<te::rst::Raster> raster = dsRaster->getRaster(rasterProp->
getName());
215 std::size_t n_bands = raster->getNumberOfBands();
217 for(std::size_t b = 0; b < n_bands; ++b)
218 m_ui->m_bandsListWidget->addItem(boost::lexical_cast<std::string>(b).c_str());
221 m_ui->m_statisticsListWidget->clear();
223 m_ui->m_statisticsListWidget->addItem(
"Minimum value");
224 m_ui->m_statisticsListWidget->addItem(
"Maximum value");
225 m_ui->m_statisticsListWidget->addItem(
"Mean");
226 m_ui->m_statisticsListWidget->addItem(
"Sum of values");
227 m_ui->m_statisticsListWidget->addItem(
"Total number of values");
228 m_ui->m_statisticsListWidget->addItem(
"Total not null values");
229 m_ui->m_statisticsListWidget->addItem(
"Standard deviation");
230 m_ui->m_statisticsListWidget->addItem(
"Variance");
231 m_ui->m_statisticsListWidget->addItem(
"Skewness");
232 m_ui->m_statisticsListWidget->addItem(
"Kurtosis");
233 m_ui->m_statisticsListWidget->addItem(
"Amplitude");
234 m_ui->m_statisticsListWidget->addItem(
"Median");
235 m_ui->m_statisticsListWidget->addItem(
"Coefficient variation");
236 m_ui->m_statisticsListWidget->addItem(
"Mode");
242 std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
244 std::string layerID = m_ui->m_inVectorComboBox->itemData(index, Qt::UserRole).toString().toStdString();
246 while(it != m_layers.end())
248 if(layerID == it->get()->getId().c_str())
249 m_vectorLayer = it->get();
257 m_ui->m_newLayerNameLineEdit->clear();
258 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
262 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
264 if(dsPtrList.size() <= 0)
267 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
269 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
271 m_outputDatasource = *it;
278 m_ui->m_newLayerNameLineEdit->clear();
279 m_ui->m_repositoryLineEdit->clear();
281 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
282 QString(), tr(
"Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
284 if (fileName.isEmpty())
287 boost::filesystem::path outfile(fileName.toStdString());
288 std::string aux = outfile.leaf().string();
289 m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
290 aux = outfile.string();
291 m_ui->m_repositoryLineEdit->setText(aux.c_str());
294 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
299 QMessageBox::information(
this,
"Help",
"Under development");
304 if(m_ui->m_inRasterComboBox->count() == 0)
306 QMessageBox::information(
this,
"Fill",
"Select an input raster layer.");
310 if(m_ui->m_inVectorComboBox->count() == 0)
312 QMessageBox::information(
this,
"Fill",
"Select an input vector layer.");
321 QMessageBox::information(
this,
"Fill",
"Can not execute this operation on this type of layer.");
329 QMessageBox::information(
this,
"Fill",
"Can not execute this operation on this type of layer.");
334 if (!inRasterDataSource.get())
336 QMessageBox::information(
this,
"Fill",
"The selected raster data source can not be accessed.");
341 if (!inVectorDataSource.get())
343 QMessageBox::information(
this,
"Fill",
"The selected vector data source can not be accessed.");
347 std::vector<unsigned int> vecBands = getSelectedBands();
350 QMessageBox::information(
this,
"Fill",
"Select at least one band.");
355 std::vector<te::stat::StatisticalSummary> vecStatistics = getSelectedStatistics();
356 m_texture = m_ui->m_textureCheckBox->isChecked();
358 if(vecStatistics.empty() && m_texture ==
false)
360 QMessageBox::information(
this,
"Fill",
"Select at least one statistic operation or select the texture checkbox.");
364 if(m_ui->m_repositoryLineEdit->text().isEmpty())
366 QMessageBox::information(
this,
"Fill",
"Define a repository for the result.");
370 if(m_ui->m_newLayerNameLineEdit->text().isEmpty())
372 QMessageBox::information(
this,
"Fill",
"Define a name for the resulting layer.");
376 std::string outputdataset = m_ui->m_newLayerNameLineEdit->text().toStdString();
388 boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
390 if (boost::filesystem::exists(uri))
392 QMessageBox::information(
this,
"Fill",
"Output file already exists. Remove it or select a new name and try again.");
396 std::size_t idx = outputdataset.find(
".");
397 if (idx != std::string::npos)
398 outputdataset=outputdataset.substr(0,idx);
400 std::map<std::string, std::string> dsinfo;
401 dsinfo[
"URI"] = uri.string();
404 dsOGR->setConnectionInfo(dsinfo);
406 if (dsOGR->dataSetExists(outputdataset))
408 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.");
412 this->setCursor(Qt::WaitCursor);
415 rst2vec->
setInput(inRasterDataSource,
422 rst2vec->
setParams(vecBands, vecStatistics, m_texture);
424 rst2vec->
setOutput(dsOGR, outputdataset);
429 res = rst2vec->
run();
433 this->setCursor(Qt::ArrowCursor);
435 QMessageBox::information(
this,
"Fill",
"Error: could not generate the operation.");
443 boost::uuids::basic_random_generator<boost::mt19937> gen;
444 boost::uuids::uuid u = gen();
445 std::string id_ds = boost::uuids::to_string(u);
448 ds->setConnInfo(dsinfo);
449 ds->setTitle(uri.stem().string());
450 ds->setAccessDriver(
"OGR");
452 ds->setDescription(uri.string());
458 m_outputDatasource = ds;
465 QMessageBox::information(
this,
"Fill",
"The selected output datasource can not be accessed.");
469 if (aux->dataSetExists(outputdataset))
471 QMessageBox::information(
this,
"Fill",
"Dataset already exists. Remove it or select a new name and try again.");
474 this->setCursor(Qt::WaitCursor);
478 rst2vec->
setInput(inRasterDataSource,
485 rst2vec->
setParams(vecBands, vecStatistics, m_texture);
492 res = rst2vec->
run();
498 this->setCursor(Qt::ArrowCursor);
499 QMessageBox::information(
this,
"Fill",
"Error: could not generate the operation.");
512 m_outLayer = converter(dt);
514 catch(
const std::exception& e)
516 this->setCursor(Qt::ArrowCursor);
518 QMessageBox::information(
this,
"Fill", e.what());
520 #ifdef TERRALIB_LOGGER_ENABLED
521 te::common::Logger::logDebug(
"attributefill", e.what());
522 #endif // TERRALIB_LOGGER_ENABLED
530 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)
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)
void onHelpPushButtonClicked()
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.