OGRExample.cpp
Go to the documentation of this file.
1 // Examples
2 #include "../Config.h"
3 #include "DataAccessExamples.h"
4 
5 // TerraLib
6 #include <terralib/dataaccess.h>
9 
10 // STL
11 #include <iostream>
12 
14 {
15  try
16  {
17  // let's take the input dataset from a shape file
18  std::string connInfo("file://");
19  std::string data_dir = TERRALIB_DATA_DIR;
20 
21  std::string aux("");
22  std::cout << "Inform the location of your data source (ENTER to accept default \'" << (data_dir + "/shape/poligono_unico.shp") << "\'): ";
23  std::getline (std::cin, aux);
24  if (!aux.empty())
25  connInfo += aux;
26  else
27  connInfo += data_dir + "/shape/poligono_unico.shp";
28 
29  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR", connInfo);
30  ds->open();
31 
32  // check point: the datasource exists and is opened to be used
33  std::cout << "Datasource is opened? " << std::boolalpha << ds->isOpened() << '\n' << '\n';
34 
35  // check point: what can be done with this datasource
37 
38  // check point: retrieving data from the datasource
39  std::cout << "\nDatasource has " << ds->getNumberOfDataSets() << " datasources\n";
40  std::vector<std::string> dsets = ds->getDataSetNames();
41  for (size_t i=0; i<ds->getNumberOfDataSets(); ++i)
42  std::cout << '[' << i+1 << "]: " << dsets[i] << std::endl;
43 
44  // check point: retrieving the data from a dataset of the datasource
45  while (true)
46  {
47  std::cout << "\nSelect a dataset from 1 to " << ds->getNumberOfDataSets() << " to see its data (0 to none): ";
48  int n;
49  std::cin >> n;
50  if (n<1 || n>ds->getNumberOfDataSets())
51  break;
52  PrintDataSet(dsets[n-1], ds->getDataSet(dsets[n-1]).get());
53  }
54  }
55  catch(const std::exception& e)
56  {
57  std::cout << std::endl << "An exception has occurred in the OGR Example: " << e.what() << std::endl;
58  }
59  catch(...)
60  {
61  std::cout << std::endl << "An unexpected exception has occurred in the OGR Example!" << std::endl;
62  }
63 }
64 
66 {
67  try
68  {
69  // create a dataset in memory with some data
70  std::string dsName;
71  std::string aux("");
72  std::cout << "Inform a name to shapefile being created (ENTER to accept default \'soilmeasures\'): ";
73  std::getline (std::cin, aux);
74  if (!aux.empty())
75  dsName = aux;
76  else
77  dsName = "soilmeasures";
78 
81  dSet->moveBeforeFirst();
82 
83  // create a datasource using OGR
84  std::string connInfo("file://");
85  std::string data_dir = TERRALIB_DATA_DIR;
86  aux.clear();
87  std::cout << "Inform a location to write your shapefile (ENTER to accept default \'" << (data_dir + "/shape") << "\'): ";
88  std::getline(std::cin, aux);
89 
90  if (!aux.empty())
91  connInfo += aux;
92  else
93  connInfo += data_dir + "/shape";
94 
95  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR", connInfo);
96  ds->open();
97 
98  // check point: the datasource exists and is opened to be used
99  std::cout << "Datasource is opened? " << std::boolalpha << ds->isOpened() << '\n' << '\n';
100 
101  // persist the dataset from memory to OGR
102  std::map<std::string, std::string> options; //No specific options
103  ds->createDataSet(dType, options);
104  ds->add(dsName,dSet, options);
105  ds->close();
106 
107  // check point: reopening the data source
108  std::cout << "Datasource is opened? " << std::boolalpha << ds->isOpened() << '\n' << '\n';
109  ds->open();
110  std::cout << "Datasource is opened? " << std::boolalpha << ds->isOpened() << '\n' << '\n';
111 
112  // check point: reading the data inserted
113  PrintDataSet(dsName, ds->getDataSet(dsName).get());
114 
115  delete dSet;
116  }
117  catch(const std::exception& e)
118  {
119  std::cout << std::endl << "An exception has occurred in the OGR Example: " << e.what() << std::endl;
120  }
121  catch(...)
122  {
123  std::cout << std::endl << "An unexpected exception has occurred in the OGR Example!" << std::endl;
124  }
125 }
126 
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
A class that models the description of a dataset.
Definition: DataSetType.h:72
It models a property definition.
te::da::DataSetType * CreateDataSetTypeInMemory(const std::string &datasettypename)
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::DataSet * CreatingDataSetInMemoryGivingDt(te::da::DataSetType *dt)
Examples on how to access/manipulate DataSources in TerraLib.
A factory for data sources.
void ORGExampleWrite()
An example using OGR data source driver to persist data to a datafile.
Definition: OGRExample.cpp:65
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
void OGRExampleRead()
An example using OGR data source driver to retrieve data from a datafile.
Definition: OGRExample.cpp:13
This file contains include headers for the Data Access module of TerraLib.
void PrintDataSet(std::string datasetName, te::da::DataSet *dataset)
It prints the data in a given dataset.
Definition: PrintDataSet.cpp:7