CopyingData.cpp
Go to the documentation of this file.
1 // Examples
2 #include "DataAccessExamples.h"
3 
4 // TerraLib
5 #include "../Config.h"
6 #include <terralib/dataaccess.h>
7 #include <terralib/geometry.h>
8 
9 // STL
10 #include <iostream>
11 
12 std::unique_ptr<te::da::DataSource> GetPostGISConnection();
13 
15 {
16  try
17  {
18  // let's take the input dataset from a shape file
19  std::string connInfo("file://");
20  std::string data_dir = TERRALIB_DATA_DIR;
21 
22  std::string aux("");
23  std::cout << "Inform the location of your shapefile (ENTER to accept default \'" << (data_dir + "/shape/munic_2001.shp") << "\'): ";
24  std::getline (std::cin, aux);
25  if (!aux.empty())
26  connInfo += aux;
27  else
28  connInfo += data_dir + "/shape/munic_2001.shp";
29 
30  std::unique_ptr<te::da::DataSource> dsOrigin = te::da::DataSourceFactory::make("OGR", connInfo);
31  dsOrigin->open();
32 
33  if (!dsOrigin->isValid())
34  {
35  std::cout << "Can not access the shapefile.\n";
36  return;
37  }
38 
39  // get a transactor to interact to the data source origin
40  std::unique_ptr<te::da::DataSourceTransactor> tOrigin = dsOrigin->getTransactor();
41 
42  std::vector<std::string> datasets = tOrigin->getDataSetNames();
43  std::unique_ptr<te::da::DataSet> datasetOrigin = tOrigin->getDataSet(datasets[0]);
44  std::unique_ptr<te::da::DataSetType> dtOrigin = tOrigin->getDataSetType(datasets[0]);
45 
46  std::unique_ptr<te::da::DataSource> dsDestination = GetPostGISConnection();
47  if (!dsDestination.get())
48  return;
49 
50  dsDestination->open();
51 
52  // get a transactor to interact to the data source
53  std::unique_ptr<te::da::DataSourceTransactor> tDestination = dsDestination->getTransactor();
54 
55  // create and save datasettype in the datasource destination
56  te::da::DataSetType* newDataSet = static_cast<te::da::DataSetType*>(dtOrigin->clone());
57  newDataSet->setName("public.munic_2001_from_shp_to_pgis");
58  GetFirstGeomProperty(newDataSet)->setSRID(4326);
60 
61  std::cout << std::endl << "starting copy..." << std::endl;
62  std::map<std::string, std::string> options;
63  if (tDestination->dataSetExists("public.munic_2001_from_shp_to_pgis"))
64  {
65  tDestination->dropDataSet("public.munic_2001_from_shp_to_pgis");
66  }
67  tDestination->begin();
68  tDestination->createDataSet(newDataSet,options);
69  tDestination->add(newDataSet->getName(), datasetOrigin.get(),options);
70  tDestination->commit();
71  std::cout << std::endl << "Copy finished..." << std::endl;
72  }
73  catch(const std::exception& e)
74  {
75  std::cout << std::endl << "An exception has occurred in the Copy Example: " << e.what() << std::endl;
76  }
77  catch(...)
78  {
79  std::cout << std::endl << "An unexpected exception has occurred in the Copy Example!" << std::endl;
80  }
81 }
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
void setGeometryType(GeomType t)
It sets the geometry subtype.
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::unique_ptr< te::da::DataSource > GetPostGISConnection()
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
void CopyingData()
This example shows how to copy a given dataset from one data source to another one.
Definition: CopyingData.cpp:14
Examples on how to access/manipulate DataSources in TerraLib.
This file contains include headers for the Vector Geometry model of TerraLib.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
This file contains include headers for the Data Access module of TerraLib.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127