examples/vp/Dissolve.cpp
Go to the documentation of this file.
1 #include "../Config.h"
2 #include <terralib/common.h>
3 #include <terralib/dataaccess.h>
15 #include <terralib/vp/Dissolve.h>
16 #include <terralib/vp/Utils.h>
17 
18 // Boost
19 #include <boost/uuid/random_generator.hpp>
20 #include <boost/uuid/uuid_io.hpp>
21 
22 // STL
23 #include <iostream>
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 //OGR to OGR
31 {
32  std::string data_dir = TERRALIB_DATA_DIR;
33 
34  std::string filename(data_dir + "/shape/SP_cities.shp");
35 
36  std::string srcInfo ("file://" + filename);
37 
39  srcDs->open();
40 
41  std::string inDsetName = "SP_cities";
42  if (!srcDs->dataSetExists(inDsetName))
43  {
44  std::cout << "Input dataset not found: " << inDsetName << std::endl;
45  return false;
46  }
47 
48  te::da::DataSet* inDset = srcDs->getDataSet(inDsetName).release();
49  te::da::DataSetType* inDsetType = srcDs->getDataSetType(inDsetName).release();
50 
51  // Set specific parameters.
52  std::vector<std::string> selProperties;
53  selProperties.push_back("NOMEMESO");
54 
55  std::map<std::string, te::dt::AbstractData*> specificParams;
56  specificParams["DISSOLVE"] =
58 
59  specificParams["IS_COLLECTION"] =
61 
62  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > stats;
63 
64  std::unique_ptr<te::dt::Property> prop1 =
65  srcDs->getProperty(inDsetName, "POPULACA");
66 
67  std::vector<te::stat::StatisticalSummary> stat1;
68  stat1.push_back(te::stat::SUM);
69  stat1.push_back(te::stat::MIN_VALUE);
70  stat1.push_back(te::stat::MODE);
71  stats.insert(std::make_pair(prop1.get(), stat1));
72 
73  specificParams["SUMMARY"] = new te::vp::ComplexData<
74  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > >(
75  stats);
76 
77  te::gm::GeometryProperty* geomInputProp =
78  te::da::GetFirstGeomProperty(inDsetType);
79 
80  int inputSRID = geomInputProp->getSRID();
81 
82  std::string filename2(data_dir + "/shape/dissolve_ogrtoogr.shp");
83  std::string tgrInfo ("file://" + filename2);
84 
86  trgDs->open();
87 
88  std::string outDS = "dissolve_ogrtoogr";
89 
90  if (trgDs->dataSetExists(outDS))
91  {
92  std::cout << "A dataset with the same requested output dataset name "
93  "already exists: " << outDS << std::endl;
94  return false;
95  }
96 
97  // Can be added more than one input parameter.
98  std::vector<te::vp::InputParams> inputParamsVec;
99 
100  // Declare the input parameters
101  te::vp::InputParams structInputParams;
102 
103  // Set the inputLayer parameters
104  structInputParams.m_inputDataSource = srcDs;
105  structInputParams.m_inputDataSetType = inDsetType;
106  structInputParams.m_inputDataSet = inDset;
107 
108  inputParamsVec.push_back(structInputParams);
109 
110  // Set parameters (Input/Output).
112  params->setInputParams(inputParamsVec);
113  params->setOutputDataSource(trgDs);
114  params->setOutputDataSetName(outDS);
115  params->setOutputSRID(inputSRID);
116  params->setSpecificParams(specificParams);
117 
118  te::vp::Dissolve dissolve;
119  bool result = dissolve.executeMemory(params);
120 
121  delete params;
122 
123  return result;
124 }
125 
126 //OGR to Postgis
128 {
129  std::string data_dir = TERRALIB_DATA_DIR;
130 
131  std::string filename(data_dir + "/shape/SP_cities.shp");
132 
133  std::string srcInfo("file://" + filename);
134 
136  srcDs->open();
137 
138  std::string inDsetName = "SP_cities";
139  if (!srcDs->dataSetExists(inDsetName))
140  {
141  std::cout << "Input dataset not found: " << inDsetName << std::endl;
142  return false;
143  }
144 
145  te::da::DataSet* inDset = srcDs->getDataSet(inDsetName).release();
146  te::da::DataSetType* inDsetType = srcDs->getDataSetType(inDsetName).release();
147 
148  // Set specific parameters.
149  std::vector<std::string> selProperties;
150  selProperties.push_back("NOMEMESO");
151 
152  std::map<std::string, te::dt::AbstractData*> specificParams;
153  specificParams["DISSOLVE"] =
155 
156  specificParams["IS_COLLECTION"] =
158 
159  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > stats;
160 
161  std::unique_ptr<te::dt::Property> prop1 =
162  srcDs->getProperty(inDsetName, "POPULACA");
163 
164  std::vector<te::stat::StatisticalSummary> stat1;
165  stat1.push_back(te::stat::SUM);
166  stat1.push_back(te::stat::MIN_VALUE);
167  stat1.push_back(te::stat::MODE);
168  stats.insert(std::make_pair(prop1.get(), stat1));
169 
170  specificParams["SUMMARY"] = new te::vp::ComplexData<
171  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > >(
172  stats);
173 
174  te::gm::GeometryProperty* geomInputProp =
175  te::da::GetFirstGeomProperty(inDsetType);
176 
177  int inputSRID = geomInputProp->getSRID();
178 
179  std::string connInfo(
180  "ppgsql://postgres:postgres@atlas.dpi.inpe.br:5433/testPostGIS");
181 
182  te::da::DataSourcePtr trgDs(
183  te::da::DataSourceFactory::make("POSTGIS", connInfo));
184  trgDs->open();
185 
186  std::string outDS = "dissolve_ogrtopgis";
187 
188  if (trgDs->dataSetExists(outDS))
189  {
190  std::cout << "A dataset with the same requested output dataset name "
191  "already exists: " << outDS << std::endl;
192  return false;
193  }
194 
195  // Can be added more than one input parameter.
196  std::vector<te::vp::InputParams> inputParamsVec;
197 
198  // Declare the input parameters
199  te::vp::InputParams structInputParams;
200 
201  // Set the inputLayer parameters
202  structInputParams.m_inputDataSource = srcDs;
203  structInputParams.m_inputDataSetType = inDsetType;
204  structInputParams.m_inputDataSet = inDset;
205 
206  inputParamsVec.push_back(structInputParams);
207 
208  // Set parameters (Input/Output).
210  params->setInputParams(inputParamsVec);
211  params->setOutputDataSource(trgDs);
212  params->setOutputDataSetName(outDS);
213  params->setOutputSRID(inputSRID);
214  params->setSpecificParams(specificParams);
215 
216  te::vp::Dissolve dissolve;
217  bool result = dissolve.executeMemory(params);
218 
219  delete params;
220 
221  return result;
222 }
223 
224 //Postgis to Postgis
226 {
227  boost::uuids::basic_random_generator<boost::mt19937> gen;
228  boost::uuids::uuid u = gen();
229  std::string dsId = boost::uuids::to_string(u);
230 
231  std::string connInfo(
232  "ppgsql://postgres:postgres@atlas.dpi.inpe.br:5433/testPostGIS");
233 
234  te::da::DataSourcePtr srcDs(
235  te::da::DataSourceFactory::make("POSTGIS", connInfo));
236  srcDs->setId(dsId);
237 
239  dsInfoPtr->setId(dsId);
240  dsInfoPtr->setConnInfo(connInfo);
241  dsInfoPtr->setType("POSTGIS");
242 
244 
245  srcDs->open();
246 
247  std::string inDsetName = "sp_cities";
248  if (!srcDs->dataSetExists(inDsetName))
249  {
250  std::cout << "Input dataset not found: " << inDsetName << std::endl;
251  return false;
252  }
253 
254  te::da::DataSet* inDset = srcDs->getDataSet(inDsetName).release();
255  te::da::DataSetType* inDsetType = srcDs->getDataSetType(inDsetName).release();
256 
257  std::vector<std::string> selProperties;
258  selProperties.push_back("nomemeso");
259 
260  std::map<std::string, te::dt::AbstractData*> specificParams;
261  specificParams["DISSOLVE"] =
263 
264  specificParams["IS_COLLECTION"] =
266 
267  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > stats;
268 
269  std::unique_ptr<te::dt::Property> prop1 =
270  srcDs->getProperty(inDsetName, "populaca");
271 
272  std::vector<te::stat::StatisticalSummary> stat1;
273  stat1.push_back(te::stat::SUM);
274  stat1.push_back(te::stat::MIN_VALUE);
275  stat1.push_back(te::stat::MODE);
276  stats.insert(std::make_pair(prop1.get(), stat1));
277 
278  specificParams["SUMMARY"] = new te::vp::ComplexData<
279  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > >(
280  stats);
281 
282  te::gm::GeometryProperty* geomInputProp =
283  te::da::GetFirstGeomProperty(inDsetType);
284 
285  int inputSRID = geomInputProp->getSRID();
286 
287  te::da::Select* inputSelect = new te::da::Select();
288  te::da::Fields* fields = new te::da::Fields();
289 
290  te::da::Field* f_all = new te::da::Field("*");
291  fields->push_back(f_all);
292  inputSelect->setFields(fields);
293 
294  te::da::From* from = new te::da::From;
295 
296  te::da::FromItem* fromItem = new te::da::DataSetName(inDsetName);
297  from->push_back(fromItem);
298  inputSelect->setFrom(from);
299 
300  std::string outDS = "dissolve_pgistopgis";
301 
302  if (srcDs->dataSetExists(outDS))
303  {
304  std::cout << "A dataset with the same requested output dataset name "
305  "already exists: " << outDS << std::endl;
306  return false;
307  }
308 
309  // Can be added more than one input parameter.
310  std::vector<te::vp::InputParams> inputParamsVec;
311 
312  // Declare the input parameters
313 
314  // Set the inputLayer parameters
315  te::vp::InputParams structInputParams;
316 
317  structInputParams.m_inputDataSource = srcDs;
318  structInputParams.m_inputDataSetType = inDsetType;
319  structInputParams.m_inputDataSet = inDset;
320  structInputParams.m_inputQuery = inputSelect;
321 
322  inputParamsVec.push_back(structInputParams);
323 
324  // Set parameters (Input/Output).
326  params->setInputParams(inputParamsVec);
327  params->setOutputDataSource(srcDs);
328  params->setOutputDataSetName(outDS);
329  params->setOutputSRID(inputSRID);
330  params->setSpecificParams(specificParams);
331 
332  te::vp::Dissolve dissolve;
333  bool result = dissolve.executeQuery(params);
334 
335  delete params;
336 
337  return result;
338 }
339 
340 //Postgis to OGR
342 {
343  boost::uuids::basic_random_generator<boost::mt19937> gen;
344  boost::uuids::uuid u = gen();
345  std::string dsId = boost::uuids::to_string(u);
346 
347  std::string connInfo(
348  "ppgsql://postgres:postgres@atlas.dpi.inpe.br:5433/testPostGIS");
349 
350  te::da::DataSourcePtr srcDs(
351  te::da::DataSourceFactory::make("POSTGIS", connInfo));
352  srcDs->setId(dsId);
353 
355  dsInfoPtr->setId(dsId);
356  dsInfoPtr->setConnInfo(connInfo);
357  dsInfoPtr->setType("POSTGIS");
358 
360 
361  srcDs->open();
362 
363  std::string inDsetName = "sp_cities";
364  if (!srcDs->dataSetExists(inDsetName))
365  {
366  std::cout << "Input dataset not found: " << inDsetName << std::endl;
367  return false;
368  }
369 
370  te::da::DataSet* inDset = srcDs->getDataSet(inDsetName).release();
371  te::da::DataSetType* inDsetType = srcDs->getDataSetType(inDsetName).release();
372 
373  std::vector<std::string> selProperties;
374  selProperties.push_back("nomemeso");
375 
376  std::map<std::string, te::dt::AbstractData*> specificParams;
377  specificParams["DISSOLVE"] =
379 
380  specificParams["IS_COLLECTION"] =
382 
383  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > stats;
384 
385  std::unique_ptr<te::dt::Property> prop1 =
386  srcDs->getProperty(inDsetName, "populaca");
387 
388  std::vector<te::stat::StatisticalSummary> stat1;
389  stat1.push_back(te::stat::SUM);
390  stat1.push_back(te::stat::MIN_VALUE);
391  stat1.push_back(te::stat::MODE);
392  stats.insert(std::make_pair(prop1.get(), stat1));
393 
394  specificParams["SUMMARY"] = new te::vp::ComplexData<
395  std::map<te::dt::Property*, std::vector<te::stat::StatisticalSummary> > >(
396  stats);
397 
398  te::gm::GeometryProperty* geomInputProp =
399  te::da::GetFirstGeomProperty(inDsetType);
400 
401  int inputSRID = geomInputProp->getSRID();
402 
403  te::da::Select* inputSelect = new te::da::Select();
404  te::da::Fields* fields = new te::da::Fields();
405 
406  te::da::Field* f_all = new te::da::Field("*");
407  fields->push_back(f_all);
408  inputSelect->setFields(fields);
409 
410  te::da::From* from = new te::da::From;
411 
412  te::da::FromItem* fromItem = new te::da::DataSetName(inDsetName);
413  from->push_back(fromItem);
414  inputSelect->setFrom(from);
415 
416  std::string data_dir = TERRALIB_DATA_DIR;
417 
418  std::string uriResult(data_dir + "/shape/dissolve_pgistoogr.shp");
419  std::string tgrInfo("file://" + uriResult);
420 
422  trgDs->open();
423 
424  std::string outDS = "dissolve_pgistoogr";
425 
426  if (trgDs->dataSetExists(outDS))
427  {
428  std::cout << "A dataset with the same requested output dataset name "
429  "already exists: " << outDS << std::endl;
430  return false;
431  }
432 
433  // Can be added more than one input parameter.
434  std::vector<te::vp::InputParams> inputParamsVec;
435 
436  // Declare the input parameters
437  te::vp::InputParams structInputParams;
438 
439  // Set the inputLayer parameters
440  structInputParams.m_inputDataSource = srcDs;
441  structInputParams.m_inputDataSetType = inDsetType;
442  structInputParams.m_inputDataSet = inDset;
443  structInputParams.m_inputQuery = inputSelect;
444 
445  inputParamsVec.push_back(structInputParams);
446 
447  // Set parameters (Input/Output).
449  params->setInputParams(inputParamsVec);
450  params->setOutputDataSource(trgDs);
451  params->setOutputDataSetName(outDS);
452  params->setOutputSRID(inputSRID);
453  params->setSpecificParams(specificParams);
454 
455  te::vp::Dissolve dissolve;
456  bool result = dissolve.executeQuery(params);
457 
458  delete params;
459 
460  return result;
461 }
462 
463 
bool DissolveOGRToOGR()
void setOutputSRID(const int &outputSRID)
Dissolve operation.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
A structure to hold the input parameters of vector processing.
Definition: InputParams.h:50
An abstract class that models a source of data in a query.
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the description of a dataset.
Definition: DataSetType.h:72
bool DissolveOGRToPGIS()
bool executeQuery(te::vp::AlgorithmParams *mainParams)
void setOutputDataSetName(const std::string &outputDataSetName)
bool DissolvePGISToOGR()
te::da::DataSourcePtr m_inputDataSource
Is Required.
Definition: InputParams.h:81
te::da::Select * m_inputQuery
Is required for operations in spatial database.
Definition: InputParams.h:85
void setOutputDataSource(te::da::DataSourcePtr outputDataSource)
A singleton to keep all the registered data sources.
bool DissolvePGISToPGIS()
Utility functions for Vector Processing.
A template for complex data types.
Definition: ComplexData.h:52
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
bool executeMemory(te::vp::AlgorithmParams *mainParams)
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A factory for data sources.
Geometric property.
te::da::DataSetType * m_inputDataSetType
Is required.
Definition: InputParams.h:82
A Select models a query to be used when retrieving data from a data source.
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
A conteiner class for keeping information about a data source.
A dataset is the unit of information manipulated by the data access module of TerraLib.
te::da::DataSet * m_inputDataSet
Is required for operations in memory.
Definition: InputParams.h:84
The Field class can be used to model an expression that takes part of the output items of a SELECT...
It models the FROM clause for a query.
A class that represents a data source component.
This file contains include headers for the TerraLib Common Runtime module.
void setInputParams(const std::vector< te::vp::InputParams > &setInputParams)
The Fields class can be used to model a set of expressions that form the output items of a SELECT...
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
This file contains several implementations for atomic data types (integers, floats, strings and others).
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
This file contains include headers for the Data Access module of TerraLib.
void setSpecificParams(const std::map< std::string, te::dt::AbstractData * > &specificParams)
void setFields(Fields *f)
It sets the list of output expressions used to form the result set.
Definition: Select.cpp:779
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void setFrom(From *f)
It sets the list of source information.
Definition: Select.cpp:789