PolygonToLineDialog.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/PolygonToLineDialog.cpp
22 
23  \brief A dialog for polygon to line operation
24 */
25 
26 // TerraLib
27 #include "../../common/progress/ProgressManager.h"
28 #include "../../common/Logger.h"
29 #include "../../common/Translator.h"
30 #include "../../common/STLUtils.h"
31 #include "../../dataaccess/dataset/DataSetType.h"
32 #include "../../dataaccess/dataset/DataSetTypeConverter.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 "../Config.h"
49 #include "../Exception.h"
50 #include "../PolygonToLineMemory.h"
51 #include "../PolygonToLineOp.h"
52 #include "../PolygonToLineQuery.h"
53 #include "PolygonToLineDialog.h"
54 #include "ui_PolygonToLineDialogForm.h"
55 
56 // Qt
57 #include <QFileDialog>
58 #include <QMessageBox>
59 #include <QSize>
60 
61 // Boost
62 #include <boost/algorithm/string.hpp>
63 #include <boost/filesystem.hpp>
64 #include <boost/uuid/random_generator.hpp>
65 #include <boost/uuid/uuid_io.hpp>
66 
67 te::vp::PolygonToLineDialog::PolygonToLineDialog(QWidget* parent, Qt::WindowFlags f)
68  : QDialog(parent, f),
69  m_ui(new Ui::PolygonToLineDialogForm),
70  m_layers(std::list<te::map::AbstractLayerPtr>()),
71  m_selectedLayer(0),
72  m_toFile(false)
73 {
74 // add controls
75  m_ui->setupUi(this);
76 
77 // add icons
78  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("vp-polygon-line-hint").pixmap(112,48));
79  m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme("datasource"));
80 
81 //signals
82 
83  connect(m_ui->m_layersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onLayerComboBoxChanged(int)));
84 
85  connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()), this, SLOT(onTargetDatasourceToolButtonPressed()));
86  connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()), this, SLOT(onTargetFileToolButtonPressed()));
87 
88  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
89  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
90 
91  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
92  m_ui->m_helpPushButton->setPageReference("plugins/vp/vp_polygonToLine.html");
93 }
94 
96 {
97 }
98 
99 void te::vp::PolygonToLineDialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
100 {
101  m_layers = layers;
102 
103  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
104 
105  while(it != m_layers.end())
106  {
107  std::auto_ptr<te::map::LayerSchema> layerSchema = it->get()->getSchema();
108  te::gm::GeometryProperty* prop = static_cast<te::gm::GeometryProperty*>(layerSchema->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
109 
110  if(!prop)
111  {
112  ++it;
113  continue;
114  }
115 
116  te::gm::GeomType geomType = prop->getGeometryType();
117 
118  if( geomType == te::gm::PolygonMType || geomType == te::gm::PolygonType || geomType == te::gm::PolygonZMType || geomType == te::gm::PolygonZType ||
119  geomType == te::gm::MultiPolygonMType || geomType == te::gm::MultiPolygonType || geomType == te::gm::MultiPolygonZMType || geomType == te::gm::MultiPolygonZType)
120  m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant(it->get()->getId().c_str()));
121 
122  ++it;
123  }
124 }
125 
127 {
128  return m_outLayer;
129 }
130 
132 {
133  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
134 
135  std::string layerID = m_ui->m_layersComboBox->itemData(index, Qt::UserRole).toString().toStdString();
136 
137  while(it != m_layers.end())
138  {
139  if(layerID == it->get()->getId().c_str())
140  {
141  te::map::AbstractLayerPtr selectedLayer = it->get();
142  m_selectedLayer = selectedLayer;
143 
144  return;
145  }
146  ++it;
147  }
148 }
149 
151 {
152  m_ui->m_newLayerNameLineEdit->clear();
153  m_ui->m_newLayerNameLineEdit->setEnabled(true);
155  dlg.exec();
156 
157  std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.getSelecteds();
158 
159  if(dsPtrList.empty())
160  return;
161 
162  std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
163 
164  m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
165 
166  m_outputDatasource = *it;
167 
168  m_toFile = false;
169 }
170 
172 {
173  m_ui->m_newLayerNameLineEdit->clear();
174  m_ui->m_repositoryLineEdit->clear();
175 
176  QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."),
177  QString(), tr("Shapefile (*.shp *.SHP);;"),0, QFileDialog::DontConfirmOverwrite);
178 
179  if (fileName.isEmpty())
180  return;
181 
182  boost::filesystem::path outfile(fileName.toStdString());
183  std::string aux = outfile.leaf().string();
184  m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
185  aux = outfile.string();
186  m_ui->m_repositoryLineEdit->setText(aux.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, "Polygon to line", "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, "Polygon to line", "Can not execute this operation on this type of layer.");
204  }
205 
206  const te::da::ObjectIdSet* oidSet = 0;
207  if(m_ui->m_onlySelectedCheckBox->isChecked())
208  {
209  oidSet = m_selectedLayer->getSelected();
210  if(!oidSet)
211  {
212  QMessageBox::information(this, "Polygon to line", "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, "Polygon to line", "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, "Polygon to line", "Define a repository for the result.");
227  return;
228  }
229 
230  std::string outputDataSet = m_ui->m_newLayerNameLineEdit->text().toStdString();
231  if(outputDataSet.empty())
232  {
233  QMessageBox::information(this, "Polygon to line", "Define a name for the resulting layer.");
234  return;
235  }
236 
237  bool res;
238 
239  //progress
242 
243  try
244  {
245  if(m_toFile)
246  {
247  boost::filesystem::path uri(m_ui->m_repositoryLineEdit->text().toStdString());
248  if (boost::filesystem::exists(uri))
249  {
250  QMessageBox::information(this, "Polygon to line", "Output file already exists. Remove it or select a new name and try again.");
251  return;
252  }
253 
254  std::size_t idx = outputDataSet.find(".");
255  if (idx != std::string::npos)
256  outputDataSet=outputDataSet.substr(0,idx);
257 
258  std::map<std::string, std::string> dsinfo;
259  dsinfo["URI"] = uri.string();
260 
262  dsOGR->setConnectionInfo(dsinfo);
263  dsOGR->open();
264  if (dsOGR->dataSetExists(outputDataSet))
265  {
266  QMessageBox::information(this, "Polygon to Line", "There is already a dataset with the requested name in the output data source. Remove it or select a new name and try again.");
267  return;
268  }
269 
270  std::auto_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(dsLayer->getSchema().get(), dsOGR->getCapabilities(), dsOGR->getEncoding()));
271 
272  te::da::AssociateDataSetTypeConverterSRID(converter.get(), dsLayer->getSRID());
273 
274  this->setCursor(Qt::WaitCursor);
275 
276  te::vp::PolygonToLineOp* pol2LineOp = 0;
277 
278  // select a strategy based on the capabilities of the input datasource
279  const te::da::DataSourceCapabilities dsCapabilities = inDataSource->getCapabilities();
280 
281  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
282  pol2LineOp = new te::vp::PolygonToLineQuery();
283  else
284  pol2LineOp = new te::vp::PolygonToLineMemory();
285 
286  pol2LineOp->setInput(inDataSource, dsLayer->getDataSetName(), converter, oidSet);
287  pol2LineOp->setOutput(dsOGR, outputDataSet);
288 
289  if(!pol2LineOp->paramsAreValid())
290  res = false;
291  else
292  res = pol2LineOp->run();
293 
294  if(!res)
295  {
296  this->setCursor(Qt::ArrowCursor);
297  dsOGR->close();
298  QMessageBox::information(this, "Polygon to Line", "Error: Error in operation.");
299  reject();
300  }
301  dsOGR->close();
302 
303  delete pol2LineOp;
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();
321  m_outputDatasource = ds;
322 
323  }
324  else
325  {
326  te::da::DataSourcePtr dsOGR = te::da::GetDataSource(m_outputDatasource->getId());
327  if(!dsOGR)
328  {
329  QMessageBox::information(this, "Polygon to Line", "The selected output datasource can not be accessed.");
330  return;
331  }
332 
333  if(dsOGR->dataSetExists(outputDataSet))
334  {
335  QMessageBox::information(this, "Polygon to Line", "Dataset already exists. Remove ir or select a new name and try again.");
336  return;
337  }
338 
339  std::auto_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(dsLayer->getSchema().get(), dsOGR->getCapabilities(), dsOGR->getEncoding()));
340 
341  te::da::AssociateDataSetTypeConverterSRID(converter.get(), dsLayer->getSRID());
342 
343  this->setCursor(Qt::WaitCursor);
344 
345  te::vp::PolygonToLineOp* pol2LineOp = 0;
346 
347  const te::da::DataSourceCapabilities dsCapabilities = inDataSource->getCapabilities();
348 
349  if(dsCapabilities.supportsPreparedQueryAPI() && dsCapabilities.getQueryCapabilities().supportsSpatialSQLDialect())
350  pol2LineOp = new te::vp::PolygonToLineQuery();
351  else
352  pol2LineOp = new te::vp::PolygonToLineMemory();
353 
354  pol2LineOp->setInput(inDataSource, dsLayer->getDataSetName(), converter, oidSet);
355  pol2LineOp->setOutput(dsOGR, outputDataSet);
356 
357  if(!pol2LineOp->paramsAreValid())
358  res = false;
359  else
360  res = pol2LineOp->run();
361 
362  delete pol2LineOp;
363 
364  if(!res)
365  {
366  this->setCursor(Qt::ArrowCursor);
367  QMessageBox::information(this, "Polygon to Line", "Error in operation.");
368  reject();
369  }
370  }
371 
372  te::da::DataSourcePtr outDataSource = te::da::GetDataSource(m_outputDatasource->getId());
373  te::qt::widgets::DataSet2Layer converter(m_outputDatasource->getId());
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, "Polygon to Line", e.what());
383 
384 #ifdef TERRALIB_LOGGER_ENABLED
385  std::string str = "Polygon to Line - ";
386  str += e.what();
387  te::common::Logger::logDebug("vp", str.c_str());
388 #endif
389 
391  return;
392  }
393 
395  this->setCursor(Qt::ArrowCursor);
396 
397  accept();
398 }
399 
401 {
402  reject();
403 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
Geometric property.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
const std::string & getDataSetName() const
A dialog for polygon to line operation.
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
Definition: Utils.cpp:670
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::auto_ptr< te::da::DataSetTypeConverter > converter, const te::da::ObjectIdSet *oidSet=0)
virtual bool run()=0
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
std::auto_ptr< Ui::PolygonToLineDialogForm > m_ui
const QueryCapabilities & getQueryCapabilities() const
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
An converter for DataSetType.
PolygonToLineDialog(QWidget *parent=0, Qt::WindowFlags f=0)
void removeViewer(int viewerId)
Dettach a progress viewer.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
const std::string & getDataSourceId() const
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
static std::auto_ptr< DataSource > make(const std::string &dsType)
URI C++ Library.
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
bool supportsSpatialSQLDialect() const
te::map::AbstractLayerPtr getLayer()
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
const std::list< te::da::DataSourceInfoPtr > & getSelecteds() const
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
A dialog for selecting a data source.
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::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr