LineToPolygonDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/vp/LineToPolygonDialog.cpp
22 
23  \brief A dialog for line to polygon operation
24 */
25 
26 // TerraLib
27 #include "../../common/progress/ProgressManager.h"
28 #include "../../core/filesystem/FileSystem.h"
29 #include "../../core/logger/Logger.h"
30 #include "../../core/translator/Translator.h"
31 #include "../../common/STLUtils.h"
32 #include "../../dataaccess/dataset/DataSetType.h"
33 #include "../../dataaccess/dataset/ObjectIdSet.h"
34 #include "../../dataaccess/datasource/DataSourceCapabilities.h"
35 #include "../../dataaccess/datasource/DataSourceInfo.h"
36 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
37 #include "../../dataaccess/datasource/DataSourceFactory.h"
38 #include "../../dataaccess/datasource/DataSourceManager.h"
39 #include "../../dataaccess/utils/Utils.h"
40 #include "../../datatype/Enums.h"
41 #include "../../datatype/Property.h"
42 #include "../../geometry/GeometryProperty.h"
43 #include "../../maptools/AbstractLayer.h"
44 #include "../../qt/af/Utils.h"
45 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
46 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
47 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
48 #include "../../qt/widgets/utils/FileDialog.h"
49 #include "../Config.h"
50 #include "../Exception.h"
51 #include "../LineToPolygonMemory.h"
52 #include "../LineToPolygonOp.h"
53 #include "../LineToPolygonQuery.h"
54 #include "LineToPolygonDialog.h"
55 #include "ui_LineToPolygonDialogForm.h"
56 
57 // Qt
58 #include <QFileDialog>
59 #include <QMessageBox>
60 #include <QSize>
61 
62 // Boost
63 #include <boost/algorithm/string.hpp>
64 #include <boost/filesystem.hpp>
65 #include <boost/uuid/random_generator.hpp>
66 #include <boost/uuid/uuid_io.hpp>
67 
69  : QDialog(parent, f),
70  m_ui(new Ui::LineToPolygonDialogForm),
71  m_layers(std::list<te::map::AbstractLayerPtr>()),
72  m_selectedLayer(nullptr),
73  m_toFile(false)
74 {
75 // add controls
76  m_ui->setupUi(this);
77 
78 // add icons
79  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("vp-line-polygon-hint").pixmap(112,48));
80  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
81 
82 //signals
83 
84  connect(m_ui->m_layersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onLayerComboBoxChanged(int)));
85 
86  connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()), this, SLOT(onTargetDatasourceToolButtonPressed()));
87  connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()), this, SLOT(onTargetFileToolButtonPressed()));
88 
89  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
90  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
91 
92  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
93  m_ui->m_helpPushButton->setPageReference("plugins/vp/vp_lineToPolygon.html");
94 }
95 
97 
98 void te::vp::LineToPolygonDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
99 {
100  m_layers = layers;
101 
102  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
103 
104  while(it != m_layers.end())
105  {
106  std::unique_ptr<te::map::LayerSchema> layerSchema = it->get()->getSchema();
107  te::gm::GeometryProperty* prop = static_cast<te::gm::GeometryProperty*>(layerSchema->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
108 
109  if(!prop)
110  {
111  ++it;
112  continue;
113  }
114 
115  te::gm::GeomType geomType = prop->getGeometryType();
116 
117  if( geomType == te::gm::LineStringMType || geomType == te::gm::LineStringType || geomType == te::gm::LineStringZMType || geomType == te::gm::LineStringZType ||
119  m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
120 
121  ++it;
122  }
123 }
124 
126 {
127  return m_outLayer;
128 }
129 
131 {
132  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
133 
134  std::string layerID = m_ui->m_layersComboBox->itemData(index, Qt::UserRole).toString().toUtf8().data();
135 
136  while(it != m_layers.end())
137  {
138  if(layerID == it->get()->getId())
139  {
140  te::map::AbstractLayerPtr selectedLayer = it->get();
141  m_selectedLayer = selectedLayer;
142 
143  return;
144  }
145  ++it;
146  }
147 }
148 
150 {
151  m_ui->m_newLayerNameLineEdit->clear();
152  m_ui->m_newLayerNameLineEdit->setEnabled(true);
154  dlg.exec();
155 
156  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
157 
158  if(dsPtrList.empty())
159  return;
160 
161  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
162 
163  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
164 
165  m_outputDatasource = *it;
166 
167  m_toFile = false;
168 }
169 
171 {
172  m_ui->m_newLayerNameLineEdit->clear();
173  m_ui->m_repositoryLineEdit->clear();
174 
176 
177  try {
178  fileDialog.exec();
179  }
180  catch (te::common::Exception& ex) {
181  QMessageBox::warning(this, tr("File information"), ex.what());
182  return;
183  }
184 
185  m_ui->m_repositoryLineEdit->setText(fileDialog.getPath().c_str());
186  m_ui->m_newLayerNameLineEdit->setText(fileDialog.getFileName().c_str());
187 
188  m_toFile = true;
189  m_ui->m_newLayerNameLineEdit->setEnabled(false);
190 }
191 
193 {
194  if(m_ui->m_layersComboBox->count() == 0)
195  {
196  QMessageBox::information(this, "Line to polygon", "Select input layer.");
197  return;
198  }
199 
200  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(m_selectedLayer.get());
201  if(!dsLayer)
202  {
203  QMessageBox::information(this, "Line to polygon", "Can not execute this operation on this type of layer.");
204  }
205 
206  const te::da::ObjectIdSet* oidSet = nullptr;
207  if(m_ui->m_onlySelectedCheckBox->isChecked())
208  {
209  oidSet = m_selectedLayer->getSelected();
210  if(!oidSet)
211  {
212  QMessageBox::information(this, "Line to polygon", "Select the layer objects to perform the polygon to line operation.");
213  return;
214  }
215  }
216 
217  te::da::DataSourcePtr inDataSource = te::da::GetDataSource(dsLayer->getDataSourceId(), true);
218  if(!inDataSource.get())
219  {
220  QMessageBox::information(this, "Line to polygon", "The selected input data source can not be accessed.");
221  return;
222  }
223 
224  if(m_ui->m_repositoryLineEdit->text().isEmpty())
225  {
226  QMessageBox::information(this, "Line to polygon", "Define a repository for the result.");
227  return;
228  }
229 
230  std::string outputDataSet = m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
231  if(outputDataSet.empty())
232  {
233  QMessageBox::information(this, "Line to polygon", "Define a name for the resulting layer.");
234  return;
235  }
236 
237  bool res;
238 
239  //progress
241 
242  try
243  {
244  if(m_toFile)
245  {
246  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toUtf8().data());
247  if (te::core::FileSystem::exists(uri.string()))
248  {
249  QMessageBox::information(this, "Line to polygon", "Output file already exists. Remove it or select a new name and try again.");
250  return;
251  }
252 
253  std::size_t idx = outputDataSet.find(".");
254  if (idx != std::string::npos)
255  outputDataSet=outputDataSet.substr(0,idx);
256 
257  std::string dsinfo("file://");
258  dsinfo += uri.string();
259 
260  te::da::DataSourcePtr dsOGR(te::da::DataSourceFactory::make("OGR", dsinfo).release());
261 
262  dsOGR->open();
263  if (dsOGR->dataSetExists(outputDataSet))
264  {
265  QMessageBox::information(this, "Line to polygon", "There is already a dataset with the requested name in the output data source. Remove it or select a new name and try again.");
266  return;
267  }
268 
269  std::unique_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(dsLayer->getSchema().get(), dsOGR->getCapabilities(), dsOGR->getEncoding()));
270 
271  te::da::AssociateDataSetTypeConverterSRID(converter.get(), dsLayer->getSRID());
272 
273  this->setCursor(Qt::WaitCursor);
274 
275  te::vp::LineToPolygonOp* line2polygonOp = nullptr;
276 
277  // select a strategy based on the capabilities of the input datasource
278  const te::da::DataSourceCapabilities dsCapabilities = inDataSource->getCapabilities();
279 
280  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
281  line2polygonOp = new te::vp::LineToPolygonQuery();
282  else
283  line2polygonOp = new te::vp::LineToPolygonMemory();
284 
285  line2polygonOp->setInput(inDataSource, dsLayer->getDataSetName(), std::move(converter), oidSet);
286  line2polygonOp->setParams(m_ui->m_forceCloseCheckBox->isChecked());
287  line2polygonOp->setOutput(dsOGR, outputDataSet);
288 
289  if(!line2polygonOp->paramsAreValid())
290  res = false;
291  else
292  res = line2polygonOp->run();
293 
294  if(!res)
295  {
296  this->setCursor(Qt::ArrowCursor);
297  dsOGR->close();
298  QMessageBox::information(this, "Line to polygon", "Error: Error in operation.");
299  reject();
300  }
301  dsOGR->close();
302 
303  delete line2polygonOp;
304 
305  // let's include the new datasource in the managers
306  boost::uuids::basic_random_generator<boost::mt19937> gen;
307  boost::uuids::uuid u = gen();
308  std::string id_ds = boost::uuids::to_string(u);
309 
311  ds->setConnInfo(dsinfo);
312  ds->setTitle(uri.stem().string());
313  ds->setAccessDriver("OGR");
314  ds->setType("OGR");
315  ds->setDescription(uri.string());
316  ds->setId(id_ds);
317 
318  te::da::DataSourcePtr newds = te::da::DataSourceManager::getInstance().get(id_ds, "OGR", ds->getConnInfo());
319  newds->open();
322  }
323  else
324  {
326  if(!dsOGR)
327  {
328  QMessageBox::information(this, "Line to polygon", "The selected output datasource can not be accessed.");
329  return;
330  }
331 
332  if(dsOGR->dataSetExists(outputDataSet))
333  {
334  QMessageBox::information(this, "Line to polygon", "Dataset already exists. Remove ir or select a new name and try again.");
335  return;
336  }
337 
338  std::unique_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(dsLayer->getSchema().get(), dsOGR->getCapabilities(), dsOGR->getEncoding()));
339 
340  te::da::AssociateDataSetTypeConverterSRID(converter.get(), dsLayer->getSRID());
341 
342  this->setCursor(Qt::WaitCursor);
343 
344  te::vp::LineToPolygonOp* line2polygonOp = nullptr;
345 
346  const te::da::DataSourceCapabilities dsCapabilities = inDataSource->getCapabilities();
347 
348  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
349  line2polygonOp = new te::vp::LineToPolygonQuery();
350  else
351  line2polygonOp = new te::vp::LineToPolygonMemory();
352 
353  line2polygonOp->setInput(inDataSource, dsLayer->getDataSetName(), std::move(converter), oidSet);
354  line2polygonOp->setParams(m_ui->m_forceCloseCheckBox->isChecked());
355  line2polygonOp->setOutput(dsOGR, outputDataSet);
356 
357  if(!line2polygonOp->paramsAreValid())
358  res = false;
359  else
360  res = line2polygonOp->run();
361 
362  delete line2polygonOp;
363 
364  if(!res)
365  {
366  this->setCursor(Qt::ArrowCursor);
367  QMessageBox::information(this, "Line to polygon", "Error in operation.");
368  reject();
369  }
370  }
371 
374  te::da::DataSetTypePtr dt(outDataSource->getDataSetType(outputDataSet).release());
375 
376  m_outLayer = converter(dt);
377  }
378  catch(const std::exception& e)
379  {
380  this->setCursor(Qt::ArrowCursor);
381 
382  QMessageBox::information(this, "Line to polygon", e.what());
383 
384  std::string str = "Vector Processing - Line to polygon - ";
385  str += e.what();
386  TE_LOG_ERROR(str);
387 
388  return;
389  }
390 
391  this->setCursor(Qt::ArrowCursor);
392 
393  accept();
394 }
395 
397 {
398  reject();
399 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
Defines a component for choose a file.
Definition: FileDialog.h:52
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
LineToPolygonDialog(QWidget *parent=0, Qt::WindowFlags f=0)
te::map::AbstractLayerPtr m_outLayer
Resulting layer.
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
std::unique_ptr< Ui::LineToPolygonDialogForm > m_ui
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
virtual const char * what() const
It outputs the exception message.
std::unique_ptr< LayerSchema > getSchema() const
It returns the layer schema.
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
static te::dt::Date ds(2010, 01, 01)
const QueryCapabilities & getQueryCapabilities() const
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::unique_ptr< te::da::DataSetTypeConverter > converter, const te::da::ObjectIdSet *oidSet=0)
void setParams(bool forceClose=false)
An converter for DataSetType.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
te::map::AbstractLayerPtr m_selectedLayer
Layer used for operation.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
void exec()
This method will open the dialog of file selection and populate the class members with the chosen fil...
Definition: FileDialog.cpp:54
URI C++ Library.
Definition: Attributes.h:37
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
bool supportsSpatialSQLDialect() const
std::list< te::map::AbstractLayerPtr > m_layers
List of layers.
virtual bool run()=0
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::string getPath()
This method will return the chosen path.
Definition: FileDialog.cpp:103
const std::string & getDataSetName() const
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
te::da::DataSourceInfoPtr m_outputDatasource
DataSource information.
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
A dialog for selecting a data source.
#define TE_LOG_ERROR(message)
Use this tag in order to log a message to the TerraLib default logger with the ERROR level...
Definition: Logger.h:337
A class that represents a data source component.
virtual int getSRID() const
It returns the Spatial Reference System ID associated to the Layer.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
std::string getFileName()
This method will return the file name.
Definition: FileDialog.cpp:113
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
virtual const std::string & getDataSourceId() const
virtual bool paramsAreValid()
A dialog for line to polygon operation.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
te::map::AbstractLayerPtr getLayer()