examples/vp/Identity.cpp
Go to the documentation of this file.
1 #include "../Config.h"
2 #include <terralib/common.h>
6 #include <terralib/vp/Identity.h>
7 
8 // Boost
9 #include <boost/uuid/random_generator.hpp>
10 #include <boost/uuid/uuid_io.hpp>
11 
12 // STL
13 #include <iostream>
14 
15 //OGR to OGR
17 {
18  std::string data_dir = TERRALIB_DATA_DIR;
19 
20  std::string input_filename(data_dir + "/shape/munic_goias.shp");
21  std::string srcInputInfo ("file://" + input_filename);
22 
23  std::string intersection_filename(data_dir +
24  "/shape/munic_goias_selected.shp");
25  std::string srcIntersectionInfo ("file://" + intersection_filename);
26 
27 
28  te::da::DataSourcePtr srcInputDs(
29  te::da::DataSourceFactory::make("OGR", srcInputInfo));
30  srcInputDs->open();
31 
32  te::da::DataSourcePtr srcIntersectionDs(
33  te::da::DataSourceFactory::make("OGR", srcIntersectionInfo));
34  srcIntersectionDs->open();
35 
36 
37  std::string input_DsetName = "munic_goias";
38  std::string intersection_DsetName = "munic_goias_selected";
39 
40  if (!srcInputDs->dataSetExists(input_DsetName))
41  {
42  std::cout << "Input dataset not found: " << input_DsetName << std::endl;
43  return false;
44  }
45 
46  std::unique_ptr<te::da::DataSet> input_Dset =
47  srcInputDs->getDataSet(input_DsetName);
48 
49  std::unique_ptr<te::da::DataSetType> input_DsetType =
50  srcInputDs->getDataSetType(input_DsetName);
51 
52  if (!srcIntersectionDs->dataSetExists(intersection_DsetName))
53  {
54  std::cout << "Input dataset not found: " << intersection_DsetName
55  << std::endl;
56  return false;
57  }
58 
59  std::unique_ptr<te::da::DataSet> intersection_Dset =
60  srcIntersectionDs->getDataSet(intersection_DsetName);
61 
62  std::unique_ptr<te::da::DataSetType> intersection_DsetType =
63  srcIntersectionDs->getDataSetType(intersection_DsetName);
64 
65  std::vector<std::string> attributes;
66  attributes.push_back("nome");
67  attributes.push_back("nomemeso");
68 
69  std::map<std::string, te::dt::AbstractData*> specificParams;
70  specificParams["munic_goias"] =
72  specificParams["munic_goias_selected"] =
74 
75  std::string output_filename(data_dir + "/shape/identity_shptoshp_ex.shp");
76  std::string tgrInfo ("file://" + output_filename);
77 
79  trgDs->open();
80 
81  std::string outDSName = "identity_shptoshp_ex";
82 
83  if (trgDs->dataSetExists(outDSName))
84  {
85  std::cout << "A dataset with the same requested output dataset name "
86  "already exists:" << outDSName << std::endl;
87  return false;
88  }
89 
90  std::vector<te::vp::InputParams> inputParamsVec;
91  te::vp::InputParams structInputParams;
92  te::vp::InputParams structIntersectionParams;
93 
94  structInputParams.m_inputDataSource = srcInputDs;
95  structInputParams.m_inputDataSetType = input_DsetType.release();
96  structInputParams.m_inputDataSet = input_Dset.release();
97 
98  structIntersectionParams.m_inputDataSource = srcIntersectionDs;
99  structIntersectionParams.m_inputDataSetType = intersection_DsetType.release();
100  structIntersectionParams.m_inputDataSet = intersection_Dset.release();
101 
102  inputParamsVec.push_back(structInputParams);
103  inputParamsVec.push_back(structIntersectionParams);
104 
106  params->setInputParams(inputParamsVec);
107  params->setOutputDataSource(trgDs);
108  params->setOutputDataSetName(outDSName);
109  params->setSpecificParams(specificParams);
110 
111  te::vp::Identity identity;
112  bool result = identity.executeMemory(params);
113 
114  delete params;
115 
116  return result;
117 }
118 
119 //OGR to Postgis
121 {
122  std::string data_dir = TERRALIB_DATA_DIR;
123 
124  std::string input_filename(data_dir + "/shape/munic_goias.shp");
125  std::string srcInputInfo ("file://" + input_filename);
126 
127  std::string intersection_filename(data_dir +
128  "/shape/munic_goias_selected.shp");
129  std::string srcIntersectionInfo ("file://" + intersection_filename);
130 
131 
132  te::da::DataSourcePtr srcInputDs(
133  te::da::DataSourceFactory::make("OGR", srcInputInfo));
134  srcInputDs->open();
135 
136  te::da::DataSourcePtr srcIntersectionDs(
137  te::da::DataSourceFactory::make("OGR", srcIntersectionInfo));
138  srcIntersectionDs->open();
139 
140 
141  std::string input_DsetName = "munic_goias";
142  std::string intersection_DsetName = "munic_goias_selected";
143 
144  if (!srcInputDs->dataSetExists(input_DsetName))
145  {
146  std::cout << "Input dataset not found: " << input_DsetName << std::endl;
147  return false;
148  }
149 
150  std::unique_ptr<te::da::DataSet> input_Dset =
151  srcInputDs->getDataSet(input_DsetName);
152 
153  std::unique_ptr<te::da::DataSetType> input_DsetType =
154  srcInputDs->getDataSetType(input_DsetName);
155 
156  if (!srcIntersectionDs->dataSetExists(intersection_DsetName))
157  {
158  std::cout << "Input dataset not found: " << intersection_DsetName
159  << std::endl;
160  return false;
161  }
162 
163  std::unique_ptr<te::da::DataSet> intersection_Dset =
164  srcIntersectionDs->getDataSet(intersection_DsetName);
165 
166  std::unique_ptr<te::da::DataSetType> intersection_DsetType =
167  srcIntersectionDs->getDataSetType(intersection_DsetName);
168 
169  std::vector<std::string> attributes;
170  attributes.push_back("nome");
171  attributes.push_back("nomemeso");
172 
173  std::map<std::string, te::dt::AbstractData*> specificParams;
174  specificParams["munic_goias"] =
176  specificParams["munic_goias_selected"] =
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 outDSName = "identity_shptopgis_ex";
187 
188  if (trgDs->dataSetExists(outDSName))
189  {
190  std::cout << "A dataset with the same requested output dataset name "
191  "already exists:" << outDSName << std::endl;
192  return false;
193  }
194 
195  std::vector<te::vp::InputParams> inputParamsVec;
196  te::vp::InputParams structInputParams;
197  te::vp::InputParams structIntersectionParams;
198 
199  structInputParams.m_inputDataSource = srcInputDs;
200  structInputParams.m_inputDataSetType = input_DsetType.release();
201  structInputParams.m_inputDataSet = input_Dset.release();
202 
203  structIntersectionParams.m_inputDataSource = srcIntersectionDs;
204  structIntersectionParams.m_inputDataSetType = intersection_DsetType.release();
205  structIntersectionParams.m_inputDataSet = intersection_Dset.release();
206 
207  inputParamsVec.push_back(structInputParams);
208  inputParamsVec.push_back(structIntersectionParams);
209 
211  params->setInputParams(inputParamsVec);
212  params->setOutputDataSource(trgDs);
213  params->setOutputDataSetName(outDSName);
214  params->setSpecificParams(specificParams);
215 
216  te::vp::Identity identity;
217  bool result = identity.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 input_DsetName = "munic_goias";
248  std::string intersection_DsetName = "munic_goias_selected";
249 
250  if (!srcDs->dataSetExists(input_DsetName))
251  {
252  std::cout << "Input dataset not found: " << input_DsetName << std::endl;
253  return false;
254  }
255 
256  std::unique_ptr<te::da::DataSet> input_Dset =
257  srcDs->getDataSet(input_DsetName);
258 
259  std::unique_ptr<te::da::DataSetType> input_DsetType =
260  srcDs->getDataSetType(input_DsetName);
261 
262  if (!srcDs->dataSetExists(intersection_DsetName))
263  {
264  std::cout << "Input dataset not found: " << intersection_DsetName
265  << std::endl;
266  return false;
267  }
268 
269  std::unique_ptr<te::da::DataSet> intersection_Dset =
270  srcDs->getDataSet(intersection_DsetName);
271 
272  std::unique_ptr<te::da::DataSetType> intersection_DsetType =
273  srcDs->getDataSetType(intersection_DsetName);
274 
275  std::vector<std::string> attributes;
276  attributes.push_back("nome");
277  attributes.push_back("nomemeso");
278 
279  std::map<std::string, te::dt::AbstractData*> specificParams;
280  specificParams["public.munic_goias"] =
282  specificParams["public.munic_goias_selected"] =
284 
285  te::da::DataSourcePtr trgDs(
286  te::da::DataSourceFactory::make("POSTGIS", connInfo));
287  trgDs->open();
288 
289  std::string outDSName = "identity_pgistopgis_ex";
290 
291  if (trgDs->dataSetExists(outDSName))
292  {
293  std::cout << "A dataset with the same requested output dataset name "
294  "already exists:" << outDSName << std::endl;
295  return false;
296  }
297 
298  std::vector<te::vp::InputParams> inputParamsVec;
299  te::vp::InputParams structInputParams;
300  te::vp::InputParams structIntersectionParams;
301 
302  structInputParams.m_inputDataSource = srcDs;
303  structInputParams.m_inputDataSetType = input_DsetType.release();
304  structInputParams.m_inputDataSet = input_Dset.release();
305 
306  structIntersectionParams.m_inputDataSource = srcDs;
307  structIntersectionParams.m_inputDataSetType = intersection_DsetType.release();
308  structIntersectionParams.m_inputDataSet = intersection_Dset.release();
309 
310  inputParamsVec.push_back(structInputParams);
311  inputParamsVec.push_back(structIntersectionParams);
312 
314  params->setInputParams(inputParamsVec);
315  params->setOutputDataSource(trgDs);
316  params->setOutputDataSetName(outDSName);
317  params->setSpecificParams(specificParams);
318 
319  te::vp::Identity identity;
320  bool result = identity.executeMemory(params);
321 
322  delete params;
323 
324  return result;
325 }
326 
327 //Postgis to OGR
329 {
330  boost::uuids::basic_random_generator<boost::mt19937> gen;
331  boost::uuids::uuid u = gen();
332  std::string dsId = boost::uuids::to_string(u);
333 
334  std::string connInfo(
335  "ppgsql://postgres:postgres@atlas.dpi.inpe.br:5433/testPostGIS");
336 
337  te::da::DataSourcePtr srcDs(
338  te::da::DataSourceFactory::make("POSTGIS", connInfo));
339  srcDs->setId(dsId);
340 
342  dsInfoPtr->setId(dsId);
343  dsInfoPtr->setConnInfo(connInfo);
344  dsInfoPtr->setType("POSTGIS");
345 
347 
348  srcDs->open();
349 
350  std::string input_DsetName = "munic_goias";
351  std::string intersection_DsetName = "munic_goias_selected";
352 
353  if (!srcDs->dataSetExists(input_DsetName))
354  {
355  std::cout << "Input dataset not found: " << input_DsetName << std::endl;
356  return false;
357  }
358 
359  std::unique_ptr<te::da::DataSet> input_Dset =
360  srcDs->getDataSet(input_DsetName);
361 
362  std::unique_ptr<te::da::DataSetType> input_DsetType =
363  srcDs->getDataSetType(input_DsetName);
364 
365 
366  if (!srcDs->dataSetExists(intersection_DsetName))
367  {
368  std::cout << "Input dataset not found: " << intersection_DsetName
369  << std::endl;
370  return false;
371  }
372 
373  std::unique_ptr<te::da::DataSet> intersection_Dset =
374  srcDs->getDataSet(intersection_DsetName);
375 
376  std::unique_ptr<te::da::DataSetType> intersection_DsetType =
377  srcDs->getDataSetType(intersection_DsetName);
378 
379  std::vector<std::string> attributes;
380  attributes.push_back("nome");
381  attributes.push_back("nomemeso");
382 
383  std::map<std::string, te::dt::AbstractData*> specificParams;
384  specificParams["public.munic_goias"] =
386  specificParams["public.munic_goias_selected"] =
388 
389  std::string data_dir = TERRALIB_DATA_DIR;
390  std::string output_filename(data_dir + "/shape/identity_pgistoshp_ex.shp");
391  std::string tgrInfo ("file://" + output_filename);
392 
394  trgDs->open();
395 
396  std::string outDSName = "identity_pgistoshp_ex";
397 
398  if (trgDs->dataSetExists(outDSName))
399  {
400  std::cout << "A dataset with the same requested output dataset name "
401  "already exists:" << outDSName << std::endl;
402  return false;
403  }
404 
405  std::vector<te::vp::InputParams> inputParamsVec;
406  te::vp::InputParams structInputParams;
407  te::vp::InputParams structIntersectionParams;
408 
409  structInputParams.m_inputDataSource = srcDs;
410  structInputParams.m_inputDataSetType = input_DsetType.release();
411  structInputParams.m_inputDataSet = input_Dset.release();
412 
413  structIntersectionParams.m_inputDataSource = srcDs;
414  structIntersectionParams.m_inputDataSetType = intersection_DsetType.release();
415  structIntersectionParams.m_inputDataSet = intersection_Dset.release();
416 
417  inputParamsVec.push_back(structInputParams);
418  inputParamsVec.push_back(structIntersectionParams);
419 
421  params->setInputParams(inputParamsVec);
422  params->setOutputDataSource(trgDs);
423  params->setOutputDataSetName(outDSName);
424  params->setSpecificParams(specificParams);
425 
426  te::vp::Identity identity;
427  bool result = identity.executeMemory(params);
428 
429  delete params;
430 
431  return result;
432 }
433 
434 
Identity operation.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
A structure to hold the input parameters of vector processing.
Definition: InputParams.h:50
boost::shared_ptr< DataSource > DataSourcePtr
void setOutputDataSetName(const std::string &outputDataSetName)
te::da::DataSourcePtr m_inputDataSource
Is Required.
Definition: InputParams.h:81
void setOutputDataSource(te::da::DataSourcePtr outputDataSource)
A singleton to keep all the registered data sources.
bool IdentityOGRToOGR()
bool executeMemory(te::vp::AlgorithmParams *mainParams)
bool IdentityOGRToPGIS()
A template for complex data types.
Definition: ComplexData.h:52
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
A factory for data sources.
te::da::DataSetType * m_inputDataSetType
Is required.
Definition: InputParams.h:82
te::da::DataSet * m_inputDataSet
Is required for operations in memory.
Definition: InputParams.h:84
bool IdentityPGISToPGIS()
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)
void setSpecificParams(const std::map< std::string, te::dt::AbstractData * > &specificParams)
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
bool IdentityPGISToOGR()