All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
VectorizationWizard.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 //Terralib
21 #include "../../../common/progress/ProgressManager.h"
22 #include "../../../dataaccess/datasource/DataSource.h"
23 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
24 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
25 #include "../../../dataaccess/datasource/DataSourceManager.h"
26 #include "../../../dataaccess/utils/Utils.h"
27 #include "../../../datatype/SimpleProperty.h"
28 #include "../../../geometry/GeometryProperty.h"
29 #include "../../../memory/DataSet.h"
30 #include "../../../memory/DataSetItem.h"
31 #include "../../widgets/layer/utils/DataSet2Layer.h"
32 #include "../help/HelpPushButton.h"
33 #include "../layer/search/LayerSearchWidget.h"
34 #include "../layer/search/LayerSearchWizardPage.h"
35 #include "../progress/ProgressViewerDialog.h"
36 #include "VectorizationWizard.h"
38 
39 // BOOST
40 #include <boost/algorithm/string.hpp>
41 #include <boost/filesystem.hpp>
42 #include <boost/lexical_cast.hpp>
43 #include <boost/uuid/random_generator.hpp>
44 #include <boost/uuid/uuid_io.hpp>
45 
46 // Qt
47 #include <QApplication>
48 #include <QMessageBox>
49 
51 QWizard(parent)
52 {
53  this->setWizardStyle(QWizard::ModernStyle);
54  this->setWindowTitle(tr("Vectorization"));
55 
56  this->setOption(QWizard::HaveHelpButton, true);
57  this->setOption(QWizard::HelpButtonOnRight, false);
58 
60 
61  this->setButton(QWizard::HelpButton, helpButton);
62 
63  helpButton->setPageReference("plugins/rp/rp_vectorization.html");
64 
65  addPages();
66 }
67 
69 {
70 }
71 
73 {
74  if(currentPage() == m_layerSearchPage.get())
75  {
76  std::list<te::map::AbstractLayerPtr> list = m_layerSearchPage->getSearchWidget()->getSelecteds();
77 
78  if(list.empty() == false)
79  {
80  te::map::AbstractLayerPtr l = *list.begin();
81 
82  m_vectorPage->setLayer(l);
83  }
84 
85  return m_layerSearchPage->isComplete();
86  }
87  else if(currentPage() == m_vectorPage.get())
88  {
89  return execute();
90  }
91 
92  return true;
93 }
94 
95 void te::qt::widgets::VectorizationWizard::setList(std::list<te::map::AbstractLayerPtr> &layerList)
96 {
97  m_layerSearchPage->getSearchWidget()->setList(layerList);
98  m_layerSearchPage->getSearchWidget()->filterOnlyByRaster();
99 }
100 
102 {
103  return m_outputLayer;
104 }
105 
107 {
108  m_layerSearchPage.reset(new LayerSearchWizardPage(this));
109  addPage(m_layerSearchPage.get());
110 
111  m_vectorPage.reset(new VectorizationWizardPage(this));
112  addPage(m_vectorPage.get());
113 }
114 
116 {
117  //check output parameters
118  std::string outputdataset = m_vectorPage->getLayerName();
119 
120  if(outputdataset.empty())
121  {
122  QMessageBox::warning(this, tr("Vectorizer"), tr("Output dataset name not defined."));
123  return false;
124  }
125 
126  std::string uriStr = m_vectorPage->getRepositoryName();
127 
128  if(uriStr.empty())
129  {
130  QMessageBox::warning(this, tr("Vectorizer"), tr("Output repository name not defined."));
131  return false;
132  }
133 
134  //check output datasource parameters
135  te::da::DataSourceInfoPtr outDSInfo;
136 
137  bool toFile = m_vectorPage->outputDataSourceToFile();
138 
139  if(toFile)
140  {
141  boost::filesystem::path uri(uriStr);
142 
143  std::size_t idx = outputdataset.find(".");
144  if(idx != std::string::npos)
145  outputdataset = outputdataset.substr(0,idx);
146 
147  std::map<std::string, std::string> dsinfo;
148  dsinfo["URI"] = uri.string();
149 
150  // let's include the new datasource in the managers
151  boost::uuids::basic_random_generator<boost::mt19937> gen;
152  boost::uuids::uuid u = gen();
153  std::string id = boost::uuids::to_string(u);
154 
156  ds->setConnInfo(dsinfo);
157  ds->setTitle(uri.stem().string());
158  ds->setAccessDriver("OGR");
159  ds->setType("OGR");
160  ds->setDescription(uri.string());
161  ds->setId(id);
162 
164  outDSInfo = ds;
165  }
166  else
167  {
168  outDSInfo = m_vectorPage->getDataSourceInfo();
169  }
170 
171  //input parameters
172  std::auto_ptr<te::rst::Raster> raster = m_vectorPage->getRaster();
173  unsigned int band = m_vectorPage->getBand();
174  unsigned int maxGeom = 0;
175  m_vectorPage->hasMaxGeom(maxGeom);
176 
177  //output parameters
178  std::vector<te::gm::Geometry*> geomVec;
179 
180  //progress
183 
184  QApplication::setOverrideCursor(Qt::WaitCursor);
185 
186  try
187  {
188  //run operation
189  raster->vectorize(geomVec, band, maxGeom);
190  }
191  catch(const std::exception& e)
192  {
193  QMessageBox::warning(this, tr("Vectorizer"), e.what());
194 
196 
197  QApplication::restoreOverrideCursor();
198 
199  return false;
200  }
201  catch(...)
202  {
203  QMessageBox::warning(this, tr("Vectorizer"), tr("An exception has occurred!"));
204 
206 
207  QApplication::restoreOverrideCursor();
208 
209  return false;
210  }
211 
213 
214  QApplication::restoreOverrideCursor();
215 
216  try
217  {
218  //save data
219  std::auto_ptr<te::da::DataSetType> dsType = createDataSetType(outputdataset, raster->getSRID());
220 
221  std::auto_ptr<te::mem::DataSet> dsMem = createDataSet(dsType.get(), geomVec);
222 
223  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(outDSInfo->getId(), outDSInfo->getType(), outDSInfo->getConnInfo());
224 
225  saveDataSet(dsMem.get(), dsType.get(), ds, outputdataset);
226 
227  //create output layer
228  te::da::DataSourcePtr outDataSource = te::da::GetDataSource(outDSInfo->getId());
229 
230  te::qt::widgets::DataSet2Layer converter(outDSInfo->getId());
231 
232  te::da::DataSetTypePtr dt(outDataSource->getDataSetType(outputdataset).release());
233 
234  m_outputLayer = converter(dt);
235  }
236  catch(const std::exception& e)
237  {
238  QMessageBox::warning(this, tr("Vectorizer"), e.what());
239 
240  return false;
241  }
242  catch(...)
243  {
244  QMessageBox::warning(this, tr("Vectorizer"), tr("An exception has occurred saving geometries!"));
245 
246  return false;
247  }
248 
249  return true;
250 }
251 
252 std::auto_ptr<te::da::DataSetType> te::qt::widgets::VectorizationWizard::createDataSetType(std::string dataSetName, int srid)
253 {
254  std::auto_ptr<te::da::DataSetType> dsType(new te::da::DataSetType(dataSetName));
255 
256  //create id property
258  dsType->add(idProperty);
259 
260  //create geometry property
261  te::gm::GeometryProperty* geomProperty = new te::gm::GeometryProperty("geom", srid, te::gm::PolygonType);
262  dsType->add(geomProperty);
263 
264  //create primary key
265  std::string pkName = "pk_id";
266  pkName+= "_" + dataSetName;
267  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pkName, dsType.get());
268  pk->add(idProperty);
269 
270  return dsType;
271 }
272 
273 std::auto_ptr<te::mem::DataSet> te::qt::widgets::VectorizationWizard::createDataSet(te::da::DataSetType* dsType, std::vector<te::gm::Geometry*>& geoms)
274 {
275  std::auto_ptr<te::mem::DataSet> ds(new te::mem::DataSet(dsType));
276 
277  for(std::size_t t = 0; t < geoms.size(); ++t)
278  {
279  //create dataset item
280  te::mem::DataSetItem* item = new te::mem::DataSetItem(ds.get());
281 
282  //set id
283  item->setInt32("id", (int)t);
284 
285  //set geometry
286  item->setGeometry("geom", geoms[t]);
287 
288  ds->add(item);
289  }
290 
291  return ds;
292 }
293 
295 {
296  //save dataset
297  dataSet->moveBeforeFirst();
298 
299  std::map<std::string, std::string> options;
300 
301  ds->createDataSet(dsType, options);
302 
303  ds->add(dataSetName, dataSet, options);
304 }
305 
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
void saveDataSet(te::mem::DataSet *dataSet, te::da::DataSetType *dsType, te::da::DataSourcePtr ds, std::string dataSetName)
Geometric property.
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
An atomic property like an integer or double.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A class that models the description of a dataset.
Definition: DataSetType.h:72
void setPageReference(const QString &ref)
Sets the documentation page reference.
std::auto_ptr< te::mem::DataSet > createDataSet(te::da::DataSetType *dsType, std::vector< te::gm::Geometry * > &geoms)
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
void removeViewer(int viewerId)
Dettach a progress viewer.
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
bool moveBeforeFirst()
It moves the internal pointer to a position before the first item in the collection.
Definition: DataSet.cpp:328
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
std::auto_ptr< te::da::DataSetType > createDataSetType(std::string dataSetName, int srid)
A class that represents a data source component.
te::map::AbstractLayerPtr getOutputLayer()
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr