LoadDataSource.cpp
Go to the documentation of this file.
1 #include "QueryExamples.h"
2 
3 // TerraLib
4 #include <terralib/common.h>
5 #include <terralib/dataaccess.h>
6 #include <terralib/maptools.h>
7 
8 // STL
9 #include <cassert>
10 #include <iostream>
11 
12 te::da::DataSourcePtr LoadShapeDataSource(const std::string& fileName, const std::string& dsId)
13 {
14  //shape
15  std::string connInfo ("file://" + fileName);
16 
17  te::da::DataSource* dsShape = te::da::DataSourceFactory::make("OGR", connInfo).get();
18  dsShape->open();
19  dsShape->setId(dsId);
20 
21  te::da::DataSourcePtr dsPtrShape(dsShape);
22 
23  te::da::DataSourceManager::getInstance().insert(dsPtrShape);
24 
25  return dsPtrShape;
26 }
27 
28 te::da::DataSourcePtr LoadPGISDataSource(const std::string& dsId)
29 {
30  // let's give the minimal server connection information needed to connect to the database server
31  std::string aux, user, password, host, port, path, query;
32  std::string strURI = "pgsql://"; // The base of the URI
33 
34  std::cout << "Inform the Host for your postGIS server (ENTER to accept default \'atlas.dpi.inpe.br\'): ";
35  std::getline(std::cin, aux);
36  host = aux.empty() ? "atlas.dpi.inpe.br" : aux;
37 
38  std::cout << "Inform the Port number to access your postGIS server (ENTER to accept default \'5433\'): ";
39  std::getline(std::cin, aux);
40  port = aux.empty() ? "5433" : aux;
41 
42  std::cout << "Inform the User to access your postGIS server (ENTER to accept default \'postgres\'): ";
43  std::getline(std::cin, aux);
44  user = aux.empty() ? "postgres" : aux;
45 
46  std::cout << "Inform the Password to access your postGIS server (ENTER to accept default \'postgres\'): ";
47  std::getline(std::cin, aux);
48  password = aux.empty() ? "postgres" : aux;
49 
50  std::cout << "Inform the Database name to connect to your postGIS server (ENTER to accept default \'terralib4\'): ";
51  std::getline(std::cin, aux);
52  path = aux.empty() ? "terralib4" : aux;
53 
54  std::cout << "Inform the Client enconding to connect to your postGIS server (ENTER to accept default \'UTF-8\'): ";
55  std::getline(std::cin, aux);
56  query = aux.empty() ? "&PG_CLIENT_ENCODING=" + te::core::CharEncoding::getEncodingName(te::core::EncodingType::UTF8) : aux;
57 
58  std::cout << "Inform the Connection Time Out to connect to your postGIS server (ENTER to accept default \'4\'): ";
59  std::getline(std::cin, aux);
60  query += aux.empty() ? "&PG_CONNECT_TIMEOUT=4" : "&PG_CONNECT_TIMEOUT=" + aux;
61 
62  strURI += user + ":";
63  strURI += password + "@";
64  strURI += host + ":";
65  strURI += port + "/";
66  strURI += path + "?";
67  strURI += query;
68 
69  // create a data source using the data source factory
70  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("POSTGIS", strURI);
71 
72  try
73  {
74  ds->open();
75  }
76  catch (const std::exception& e)
77  {
78  std::cout << "Datasource " << host << "/" << path << " can not be used!\nMake sure to have the correct connection parameters\n";
79  std::cout << "Error: " << e.what() << std::endl;
80  }
81  catch (...)
82  {
83  std::cout << "Datasource " << host << "/" << path << " can not be used!\nMake sure to have the correct connection parameters\n";
84  }
85 
86  ds->setId(dsId);
87  te::da::DataSourcePtr dsPtrPGIS(ds.release());
88  te::da::DataSourceManager::getInstance().insert(dsPtrPGIS);
89 
90  return dsPtrPGIS;
91 }
92 
94 {
95  te::da::DataSourceTransactor* transactor = ds->getTransactor().get();
96 
97  te::da::DataSet* dataset = transactor->query(s).get();
98 
99  delete transactor;
100 
101  return dataset;
102 }
103 
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
virtual void open()=0
It opens the data source and makes it ready for using.
boost::shared_ptr< DataSource > DataSourcePtr
te::da::DataSourcePtr LoadPGISDataSource(const std::string &dsId)
virtual std::unique_ptr< DataSet > query(const Select &q, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)=0
It executes a query that may return some data using a generic query. A dataset can be connected or di...
static te::dt::Date ds(2010, 01, 01)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
te::da::DataSourcePtr LoadShapeDataSource(const std::string &fileName, const std::string &dsId)
static std::string getEncodingName(EncodingType et)
Retrive a string from a given character encoding type enum.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
void setId(const std::string &id)
It sets the data source identification.
Several examples declarations.
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
A dataset is the unit of information manipulated by the data access module of TerraLib.
This file contains include headers for the TerraLib Common Runtime module.
This file contains include headers for the Data Access module of TerraLib.
This file contains include headers for the Map Tools module of TerraLib.
te::da::DataSet * GetDataSet(te::da::Select &s, te::da::DataSourcePtr &ds)