PostGISExample.cpp
Go to the documentation of this file.
1 
2 // Examples
3 #include "DataAccessExamples.h"
4 
5 // TerraLib
7 #include <terralib/datatype.h>
8 
9 // STL
10 #include <iostream>
11 #include <exception>
12 
13 std::unique_ptr<te::da::DataSource> GetPostGISConnection()
14 {
15  // let's give the minimal server connection information needed to connect to the database server
16  std::string aux, user, password, host, port, path, query;
17  std::string strURI = "pgsql://"; // The base of the URI
18 
19  std::cout << "Inform the Host for your postGIS server (ENTER to accept default \'atlas.dpi.inpe.br\'): ";
20  std::getline (std::cin, aux);
21  host = aux.empty() ? "atlas.dpi.inpe.br" : aux;
22 
23  std::cout << "Inform the Port number to access your postGIS server (ENTER to accept default \'5433\'): ";
24  std::getline (std::cin, aux);
25  port = aux.empty() ? "5433" : aux;
26 
27  std::cout << "Inform the User to access your postGIS server (ENTER to accept default \'postgres\'): ";
28  std::getline (std::cin, aux);
29  user = aux.empty() ? "postgres" : aux;
30 
31  std::cout << "Inform the Password to access your postGIS server (ENTER to accept default \'postgres\'): ";
32  std::getline (std::cin, aux);
33  password = aux.empty() ? "postgres" : aux;
34 
35  std::cout << "Inform the Database name to connect to your postGIS server (ENTER to accept default \'terralib4\'): ";
36  std::getline (std::cin, aux);
37  path = aux.empty() ? "terralib4" : aux;
38 
39  std::cout << "Inform the Client enconding to connect to your postGIS server (ENTER to accept default \'UTF-8\'): ";
40  std::getline(std::cin, aux);
41  query = aux.empty() ? "&PG_CLIENT_ENCODING=" + te::core::CharEncoding::getEncodingName(te::core::EncodingType::UTF8) : aux;
42 
43  std::cout << "Inform the Connection Time Out to connect to your postGIS server (ENTER to accept default \'4\'): ";
44  std::getline (std::cin, aux);
45  query += aux.empty() ? "&PG_CONNECT_TIMEOUT=4" : "&PG_CONNECT_TIMEOUT=" + aux;
46 
47  strURI += user + ":";
48  strURI += password + "@";
49  strURI += host + ":";
50  strURI += port + "/";
51  strURI += path + "?";
52  strURI += query;
53 
54  // create a data source using the data source factory
55  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("POSTGIS", strURI);
56 
57  try
58  {
59  ds->open();
60  }
61  catch(const std::exception& e)
62  {
63  std::cout << "Datasource " << host << "/" << path << " can not be used!\nMake sure to have the correct connection parameters\n";
64  std::cout << "Error: " << e.what() << std::endl;
65  ds.reset();
66  return ds;
67  }
68  catch(...)
69  {
70  std::cout << "Datasource " << host << "/" << path << " can not be used!\nMake sure to have the correct connection parameters\n";
71  ds.reset();
72  return ds;
73  }
74 
75  std::cout << "Using datasource " << host << "/" << path << std::endl;
76  ds->close();
77  return ds;
78 }
79 
81 {
82  try
83  {
84  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
85  if (!ds.get())
86  return;
87 
88  ds->open();
89 
90  std::cout << "Datasource is opened? " << std::boolalpha << ds->isOpened() << '\n';
91  std::cout << "Datasource is valid? " << std::boolalpha << ds->isValid() << '\n';
92 
93 // retrieve the data source capabilities and print it
94  std::cout << std::endl;
96 
97 // printing some datasets from the datasource
98  std::vector<std::string> dsets = ds->getDataSetNames();
99 
100  if (dsets.empty())
101  {
102  std::cout << "Datasource has no datasets.\n";
103  return;
104  }
105 
106 // let's check one of them (or all)
107  std::cout << "\nThere is(are) " << dsets.size() << " dataset(s) in the datasource: \n";
108  for (size_t i=0; i<dsets.size(); ++i)
109  std::cout << '\t' << i+1 << ':' << dsets[i] << std::endl;
110 
111  // check point: retrieving the data from a dataset of the datasource
112  while (true)
113  {
114  std::cout << "\nSelect a dataset from 1 to " << ds->getNumberOfDataSets() << " to see its data (0 to none): ";
115  int n;
116  std::cin >> n;
117  if (n<1 || n>ds->getNumberOfDataSets())
118  break;
119  PrintDataSet(dsets[n-1], ds->getDataSet(dsets[n-1]).get());
120  }
121 
122 // shows how to use a spatial filter
123  std::vector<std::string>::iterator it;
124  it = std::find(dsets.begin(),dsets.end(),"public.munic_2001");
125  if (it != dsets.end())
126  {
127  std::cout << "\nHow to do spatial filtering using the dataset \'public.munic_2001\'\n";
128  RetrieveUsingSpatialFilter(ds.get());
129  }
130 
131 // it creates a DataSetType called 'our_country' using the schema 'public' in the given data source
132  it = std::find(dsets.begin(),dsets.end(),"public.our_country");
133  if (it != dsets.end())
134  return;
135 
136  std::cout << "\nHow to create a new dataset the dataset: ex 'public.our_country'\n";
137  std::unique_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
138  std::string dt_name = "our_country";
139  std::cout << std::endl << "Creating dataSet= " << dt_name << std::endl;
140  te::da::DataSetType* dtype = new te::da::DataSetType(dt_name);
141  dtype->add(new te::dt::SimpleProperty("gid", te::dt::INT32_TYPE, true));
142  dtype->add(new te::dt::StringProperty("country_name", te::dt::STRING));
143  dtype->add(new te::dt::StringProperty("city_name", te::dt::VAR_STRING, 50, true));
144  dtype->add(new te::gm::GeometryProperty("spatial_data", 4326, te::gm::GeometryType, true));
145 
146  te::da::DataSetType* datasetType = CreateDataSetType(dt_name,dtype,transactor.get());
147 
148 // it adds a primary key to the given dataset type
149  std::cout << std::endl << "Adding Primary Key to " << dt_name << std::endl;
150  te::da::PrimaryKey* pk = AddPrimaryKey(datasetType->getName(), transactor.get());
151  assert(pk);
152 
153 // it adds an Unique Key to the given dataset type
154  std::cout << std::endl << "Adding Unique Key to " << dt_name << std::endl;
155  te::da::UniqueKey* uk = AddUniqueKey(datasetType->getName(), transactor.get());
156  assert(uk);
157 
158 // it adds a spatial index to the given dataset type
159  std::cout << std::endl << "Adding spatial index to " << dt_name << std::endl;
160  te::da::Index* idx = AddSpatialIndex(datasetType->getName(), transactor.get());
161  assert(idx);
162 
163 // it adds an integer property called 'population' to the given dataset type
164  std::cout << std::endl << "Adding new Property population to " << dt_name << std::endl;
165  AddProperty(datasetType->getName(), transactor.get());
166 
167 // Now, lets remove things from the data source using transactor or function DroppingDataSetTypeProperty
168 // first, drop the recently added property
169  std::cout << std::endl << "Droping Property population of " << dt_name <<" using transactor or ds"<< std::endl;
170  (transactor.get())->dropProperty(datasetType->getName(), "population");
171 
172 // Now, lets it add again an integer property called 'population' to the given dataset type and drop it using ds
173  std::cout << std::endl << "Adding new Property population to " << dt_name << std::endl;
174  AddProperty(datasetType->getName(), transactor.get());
175 
176 // Dropping using ds api
177  ds->dropProperty(dt_name, "population");
178 
179 // Now, lets drop a geom column
180  ds->dropProperty(dt_name, "spatial_data"); //check the view geometry_columns
181 
182 // finally, drop the dataset we have created above via ds or via transactor
183  std::cout << std::endl << "Droping dataSet " << dt_name << std::endl;
184  ds->dropDataSet(dt_name);
185 
186 // Create again the dataset and drop it using transactor
187  datasetType = CreateDataSetType(dt_name,dtype,transactor.get());
188 
189  DroppingDataSetType(datasetType->getName(),transactor.get());
190 
191  if (transactor->isInTransaction())
192  std::cout << std::endl << "Transactor in transaction! "<< std::endl;
193  delete transactor.release();
194  ds->close();
195  }
196  catch(const std::exception& e)
197  {
198  std::cout << std::endl << "An exception has occurred in the PostGIS Example: " << e.what() << std::endl;
199  }
200  catch(...)
201  {
202  std::cout << std::endl << "An unexpected exception has occurred in the PostGIS Example!" << std::endl;
203  }
204 }
205 
This file contains include headers for the Data Type module of TerraLib.
te::dt::SimpleProperty * AddProperty(const std::string &datasetname, te::da::DataSourceTransactor *transactor)
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
An atomic property like an integer or double.
A class that models the description of a dataset.
Definition: DataSetType.h:72
void RetrieveUsingSpatialFilter(te::da::DataSource *ds)
An example showing how to retrieve data using a spatial filter.
te::da::Index * AddSpatialIndex(const std::string &datasetname, te::da::DataSourceTransactor *transactor)
void PostGISExample()
An example using PostGIS data source driver.
static te::dt::Date ds(2010, 01, 01)
void PrintDataSourceCapabilities(te::da::DataSource *ds)
It retrieves and prints the capabilities of a given data source.
te::da::UniqueKey * AddUniqueKey(const std::string &datasetname, te::da::DataSourceTransactor *transactor)
te::da::DataSetType * CreateDataSetType(const std::string &datasetname, te::da::DataSetType *dt, te::da::DataSourceTransactor *transactor)
static std::string getEncodingName(EncodingType et)
Retrive a string from a given character encoding type enum.
te::da::PrimaryKey * AddPrimaryKey(const std::string &datasetname, te::da::DataSourceTransactor *transactor)
Examples on how to access/manipulate DataSources in TerraLib.
A factory for data sources.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
std::unique_ptr< te::da::DataSource > GetPostGISConnection()
It returns the union of a geometry vector.
void add(Constraint *c)
It adds a new constraint.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
void DroppingDataSetType(const std::string &datasetname, te::da::DataSourceTransactor *trans)
It removes a data set type from the associated data source.
It describes an index associated to a DataSetType.
void PrintDataSet(std::string datasetName, te::da::DataSet *dataset)
It prints the data in a given dataset.
Definition: PrintDataSet.cpp:7
const std::string & getName() const
It returns the property name.
Definition: Property.h:127