SegmenterAdvancedOptionsWizardPage.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/qt/widgets/rp/SegmenterAdvancedOptionsWizardPage.h
22 
23  \brief This file defines a class for a Segmenter Advanced Options Wizard page.
24 */
25 
26 // TerraLib
27 #include "../../../common/Exception.h"
28 #include "../../../core/translator/Translator.h"
29 #include "../../../rp/Segmenter.h"
30 #include "../../../maptools/Utils.h"
32 #include "ui_SegmenterAdvancedOptionsWizardPageForm.h"
33 
34 // BOOST
35 #include <boost/algorithm/string.hpp>
36 #include <boost/filesystem.hpp>
37 #include <boost/lexical_cast.hpp>
38 #include <boost/uuid/random_generator.hpp>
39 #include <boost/uuid/uuid_io.hpp>
40 
42  : QWizardPage(parent),
43  m_ui(new Ui::SegmenterAdvancedOptionsWizardPageForm)
44 {
45 // setup controls
46  m_ui->setupUi(this);
47 
48 //configure page
49  this->setTitle(tr("Segmenter Advanced Options"));
50  this->setSubTitle(tr("Used to set the segmenter advanced options."));
51 
53 
54  QString threadNumber;
55  threadNumber.setNum(input.m_maxSegThreads);
56 
57  QString maxBlockSize;
58  maxBlockSize.setNum(input.m_maxBlockSize);
59 
60  m_ui->m_enableThreadedProcessingcheckBox->setChecked(input.m_enableThreadedProcessing);
61  m_ui->m_maximumThreadsNumberLineEdit->setText(threadNumber);
62 // m_ui->m_enableBlockProcessingcheckBox->setChecked(input.m_enableBlockProcessing);
63  m_ui->m_blockOverlapPercentSpinBox->setValue(input.m_blocksOverlapPercent);
64  m_ui->m_maximumBlockSizeLineEdit->setText(maxBlockSize);
65 }
66 
69 
70 Ui::SegmenterAdvancedOptionsWizardPageForm* te::qt::widgets::SegmenterAdvancedOptionsWizardPage::getForm() const
71 {
72  return m_ui.get();
73 }
74 
76 {
77  //check output datasource parameters
78  te::da::DataSourceInfoPtr outDSInfo;
79 
80  std::string outputdataset = m_name.append(".shp");
81 
82  boost::filesystem::path uri(m_path.append("/"+outputdataset));
83 
84  std::size_t idx = outputdataset.find(".");
85  if(idx != std::string::npos)
86  outputdataset = outputdataset.substr(0,idx);
87 
88  const std::string connInfo("file://" + uri.string());
89 
90  // let's include the new datasource in the managers
91  boost::uuids::basic_random_generator<boost::mt19937> gen;
92  boost::uuids::uuid u = gen();
93  std::string id = boost::uuids::to_string(u);
94 
96  ds->setConnInfo(connInfo);
97  ds->setTitle(uri.stem().string());
98  ds->setAccessDriver("OGR");
99  ds->setType("OGR");
100  ds->setDescription(uri.string());
101  ds->setId(id);
102 
104  outDSInfo = ds;
105 
106  //input parameters
107  te::rst::Raster* raster = te::map::GetRaster(m_layer.get());
108  unsigned int band = 0;
109  unsigned int maxGeom = 0;
110 
111  //output parameters
112  std::vector<te::gm::Geometry*> geomVec;
113  std::vector< double > geomsValues;
114 
115  //progress
117 
118  QApplication::setOverrideCursor(Qt::WaitCursor);
119 
120  try
121  {
122  //run operation
123  raster->vectorize(geomVec, band, maxGeom, &geomsValues);
124  }
125  catch(const std::exception& e)
126  {
127  throw te::common::Exception(TE_TR(e.what()));
128 
129  QApplication::restoreOverrideCursor();
130 
131  return;
132  }
133  catch(...)
134  {
135  throw te::common::Exception(TE_TR("An exception has occurred!"));
136 
137  QApplication::restoreOverrideCursor();
138 
139  return;
140  }
141 
142  try
143  {
144  //save data
145  std::unique_ptr<te::da::DataSetType> dsType = createDataSetType(outputdataset, raster->getSRID());
146 
147  std::unique_ptr<te::mem::DataSet> dsMem = createDataSet(dsType.get(), geomVec, geomsValues);
148 
149  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(outDSInfo->getId(), outDSInfo->getType(), outDSInfo->getConnInfo());
150 
151  saveDataSet(dsMem.get(), dsType.get(), ds, outputdataset);
152 
153  //create output layer
154  te::da::DataSourcePtr outDataSource = te::da::GetDataSource(outDSInfo->getId());
155 
156  te::qt::widgets::DataSet2Layer converter(outDSInfo->getId());
157 
158  te::da::DataSetTypePtr dt(outDataSource->getDataSetType(outputdataset).release());
159 
160  m_outputLayer = converter(dt);
161  }
162  catch(const std::exception& e)
163  {
164  throw te::common::Exception(TE_TR(e.what()));
165 
166  QApplication::restoreOverrideCursor();
167 
168  return;
169  }
170  catch(...)
171  {
172  throw te::common::Exception(TE_TR("An exception has occurred saving geometries!"));
173 
174  QApplication::restoreOverrideCursor();
175 
176  return;
177  }
178 
179  QApplication::restoreOverrideCursor();
180 }
181 
183 {
184  m_layer = layer;
185 }
186 
188 {
189  m_path = path;
190 }
191 
193 {
194  m_name = name;
195 }
196 
198 {
199  return m_outputLayer;
200 }
201 
202 std::unique_ptr<te::da::DataSetType> te::qt::widgets::SegmenterAdvancedOptionsWizardPage::createDataSetType(std::string dataSetName, int srid)
203 {
204  std::unique_ptr<te::da::DataSetType> dsType(new te::da::DataSetType(dataSetName));
205 
206  //create id property
208  dsType->add(idProperty);
209 
210  te::dt::SimpleProperty* valueProperty = new te::dt::SimpleProperty("value", te::dt::DOUBLE_TYPE, true);
211  dsType->add(valueProperty);
212 
213  //create geometry property
214  te::gm::GeometryProperty* geomProperty = new te::gm::GeometryProperty("geom", srid, te::gm::PolygonType);
215  dsType->add(geomProperty);
216 
217  //create primary key
218  std::string pkName = "pk_id";
219  pkName+= "_" + dataSetName;
220  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pkName, dsType.get());
221  pk->add(idProperty);
222 
223  return dsType;
224 }
225 
226 std::unique_ptr<te::mem::DataSet> te::qt::widgets::SegmenterAdvancedOptionsWizardPage::createDataSet(te::da::DataSetType* dsType, std::vector<te::gm::Geometry*>& geoms, std::vector< double >& geomsValues)
227 {
228  std::unique_ptr<te::mem::DataSet> ds(new te::mem::DataSet(dsType));
229 
230  for(std::size_t t = 0; t < geoms.size(); ++t)
231  {
232  //create dataset item
233  te::mem::DataSetItem* item = new te::mem::DataSetItem(ds.get());
234 
235  //set id
236  item->setInt32(0, (int)t);
237 
238  //set Value
239  item->setDouble( 1, geomsValues[ t ] );
240 
241  //set geometry
242  item->setGeometry(2, geoms[t]);
243 
244  ds->add(item);
245  }
246 
247  return ds;
248 }
249 
251 {
252  //save dataset
253  dataSet->moveBeforeFirst();
254 
255  std::map<std::string, std::string> options;
256 
257  ds->createDataSet(dsType, options);
258 
259  ds->add(dataSetName, dataSet, options);
260 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
unsigned int band
virtual void vectorize(std::vector< te::gm::Geometry * > &g, std::size_t b, unsigned int mp=0, std::vector< double > *const polygonsValues=0)
Vectorizes a given raster band, using GDALPolygonize function.
Geometric property.
unsigned char m_blocksOverlapPercent
The percentage of blocks overlapped area (valid range:0-25, defaul:10).
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.
void setDouble(std::size_t i, double 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
A class that models the description of a dataset.
Definition: DataSetType.h:72
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
This file defines a class for a Segmenter Advanced Options Wizard page.
unsigned int m_maxBlockSize
The input image will be split into blocks with this width for processing, this parameter tells the ma...
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
std::unique_ptr< Ui::SegmenterAdvancedOptionsWizardPageForm > m_ui
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Ui::SegmenterAdvancedOptionsWizardPageForm * getForm() const
std::unique_ptr< te::da::DataSetType > createDataSetType(std::string dataSetName, int srid)
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
An abstract class for raster data strucutures.
static te::dt::TimeDuration dt(20, 30, 50, 11)
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
bool moveBeforeFirst()
It moves the internal pointer to a position before the first item in the collection.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
int getSRID() const
Returns the raster spatial reference system identifier.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
void saveDataSet(te::mem::DataSet *dataSet, te::da::DataSetType *dsType, te::da::DataSourcePtr ds, std::string dataSetName)
std::unique_ptr< te::mem::DataSet > createDataSet(te::da::DataSetType *dsType, std::vector< te::gm::Geometry * > &geoms, std::vector< double > &geomsValues)
A class that represents a data source component.
bool m_enableThreadedProcessing
If true, threaded processing will be performed (best with multi-core or multi-processor systems (defa...
unsigned int m_maxSegThreads
The maximum number of concurrent segmenter threads (default:0 - automatically found).
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr