examples/dataaccess/main.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file examples/dataaccess/main.cpp
22 
23  \brief A list of examples for the TerraLib DataAccess Module.
24 */
25 #include "DataAccessExamples.h"
26 
27 // TerraLib
29 #include <terralib/core/plugin.h>
31 #include <terralib/core/uri/URI.h>
42 
43 // Examples
44 
45 // STL
46 #include <cassert>
47 #include <cstdlib>
48 #include <exception>
49 #include <iostream>
50 #include <map>
51 
52 void LoadModules();
53 
54 int main(int /*argc*/, char** /*argv*/)
55 {
56  try
57  {
58  // Initialize the Terralib support
60 
61  LoadModules();
62 
63  MemoryExample();
66  ExportingOGR();
67  GDALExample();
69  QueryExample();
71  CopyingData();
72 
74 
76  }
77  catch(const std::exception& e)
78  {
79  std::cout << std::endl << "An exception has occurred: " << e.what() << std::endl;
80 
81  return EXIT_FAILURE;
82  }
83  catch(...)
84  {
85  std::cout << std::endl << "An unexpected exception has occurred!" << std::endl;
86 
87  return EXIT_FAILURE;
88  }
89 
90  return EXIT_SUCCESS;
91 }
92 
93 void PrintDataSourceNames(const std::string& dsType, const std::string& connInfo)
94 {
95  std::cout << "===== Data Source Names available: \n";
96 
97  std::vector<std::string> dataSourceNames = te::da::DataSource::getDataSourceNames(dsType, connInfo);
98  for(std::size_t i = 0; i < dataSourceNames.size(); ++i)
99  std::cout << dataSourceNames[i] << std::endl;
100 }
101 
102 std::unique_ptr<te::da::DataSource> CreateDataSource(const std::string& dsType, const std::string& connInfo)
103 {
104  std::unique_ptr<te::da::DataSource> ds = te::da::DataSource::create(dsType, connInfo);
105 
106  return ds;
107 }
108 
109 void DropDataSource(const std::string& dsType, const std::string& connInfo)
110 {
111  te::da::DataSource::drop(dsType, connInfo);
112 }
113 
114 bool CheckDataSourceExistence(const std::string& dsType, const std::string& connInfo)
115 {
116  return te::da::DataSource::exists(dsType, connInfo);
117 }
118 
119 
121 {
122  // Get the database name
123  const te::core::URI& uri = ds->getConnectionInfo();
124  std::string dbName = uri.path().substr(1, uri.path().length());
125 
126  std::cout << "\n===== Dataset Names in the data source \"" << dbName << "\":\n";
127 
128  std::vector<std::string> datasetNames = ds->getDataSetNames();
129  for(std::size_t i = 0; i < datasetNames.size(); ++i)
130  std::cout << datasetNames[i] << std::endl;
131 }
132 
133 void PrintDataSetPropertyNames(te::da::DataSource* ds, const std::string& datasetName)
134 {
135  std::cout << "\n===== Property Names of the dataset \"" << datasetName << "\":\n";
136 
137  std::vector<std::string> pNames = ds->getPropertyNames(datasetName);
138  for(std::size_t i = 0; i < pNames.size(); ++i)
139  std::cout << pNames[i] << std::endl;
140 }
141 
142 void PrintDataSetConstraints(te::da::DataSource* ds, const std::string& datasetName)
143 {
144  std::cout << "\n===== Primary Key Name of the dataset \"" << datasetName << "\": ";
145 
146  std::unique_ptr<te::da::PrimaryKey> pk = ds->getPrimaryKey(datasetName);
147  std::cout << pk->getName() << std::endl;
148 
149  std::cout << "\n===== Property Names of the Primary Key \"" << pk->getName() << "\": ";
150 
151  const std::vector<te::dt::Property*> pkProperties = pk->getProperties();
152  std::size_t numPkProperties = pkProperties.size();
153  for(std::size_t i = 0; i < numPkProperties; ++i)
154  std::cout << pkProperties[i]->getName() << std::endl;
155 }
static std::unique_ptr< DataSource > create(const std::string &dsType, const std::string &connInfo)
It creates a new repository for a data source.
std::string path() const
Retrieving the path.
Definition: URI.cpp:118
An utility class to control the startup and cleanup of the TerraLib Platform and its resources...
Include files for Core Plugin Library.
void OGRExampleRead()
An example using OGR data source driver to retrieve data from a datafile.
Definition: OGRExample.cpp:13
It describes a primary key (pk) constraint.
void PrintDataSourceNames(const std::string &dsType, const std::string &connInfo)
void DropDataSource(const std::string &dsType, const std::string &connInfo)
int main(int, char **)
void PostGISExample()
An example using PostGIS data source driver.
void QueryExample()
Quering a dataset.
static te::dt::Date ds(2010, 01, 01)
static void drop(const std::string &dsType, const std::string &connInfo)
It removes a data source identified by its connection information and the driver type.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
static std::vector< std::string > getDataSourceNames(const std::string &dsType, const std::string &connInfo)
It returns the data source names available in the driver.
It describes an index associated to a DataSetType.
static PluginManager & instance()
Access the singleton.
void CopyingData()
This example shows how to copy a given dataset from one data source to another one.
Definition: CopyingData.cpp:14
void finalize()
It finalizes the TerraLib Platform.
static TerraLib & getInstance()
It returns a reference to the singleton instance.
Examples on how to access/manipulate DataSources in TerraLib.
A factory for data sources.
void PrintDataSetPropertyNames(te::da::DataSource *ds, const std::string &datasetName)
virtual std::vector< std::string > getDataSetNames()
It gets the dataset names available in the data source.
A class for handling character enconding/decoding.
void ORGExampleWrite()
An example using OGR data source driver to persist data to a datafile.
Definition: OGRExample.cpp:65
void initialize()
It initializes the TerraLib Platform.
void GDALExample()
An example using GDAL data source driver.
A class that describes a check constraint.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
void ExportingOGR()
An example using OGR data source driver to export some ;.
const te::core::URI & getConnectionInfo() const
An Uniform Resource Identifier used to describe the datasource connection.
virtual std::unique_ptr< te::da::PrimaryKey > getPrimaryKey(const std::string &datasetName)
It retrieves the primary key of the dataset.
An atomic property like an integer or double.
A class for representing an Uniform Resource Identifier (URI).
void LoadModules()
Load terralib modules.
static bool exists(const std::string &dsType, const std::string &connInfo)
It checks if the data source exists with the connection information and the driver type...
void clear()
Stop and unload all plugins, then clear the internal list of plugins.
This is the factory for PostGIS data sources.
std::unique_ptr< te::da::DataSource > CreateDataSource(const std::string &dsType, const std::string &connInfo)
void PrintDataSetNames(te::da::DataSource *ds)
A class that implements a prepared query for PostgreSQL data access driver.
Implementation of the data source for the PostGIS driver.
void MemoryExample()
This example shows how to create DataSet and DataSetType in memory.
virtual std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
void QueryInsertExample()
Quering Insert clause.
void PrintDataSetConstraints(te::da::DataSource *ds, const std::string &datasetName)
bool CheckDataSourceExistence(const std::string &dsType, const std::string &connInfo)