GDALExample.cpp
Go to the documentation of this file.
1 
2 #include "../Config.h"
3 #include "DataAccessExamples.h"
4 
5 #include <iostream>
6 
7 // This example shows how to instantiate a single raster data file as a GDAL data source.
8 void OpenFile()
9 {
10  try
11  {
12  // let's take the input dataset from a raster file
13  std::string connInfo("file://");
14  std::string data_dir = TERRALIB_DATA_DIR;
15 
16  std::string aux("");
17  std::cout << "Inform the location of your tiff file (ENTER to accept default \'" << (data_dir + "/geotiff/cbers2b_rgb342_crop.tif") << "\'): ";
18  std::getline (std::cin, aux);
19  if (!aux.empty())
20  connInfo += aux;
21  else
22  connInfo += data_dir + "/geotiff/cbers2b_rgb342_crop.tif";
23 
24  std::unique_ptr<te::da::DataSource> dsGDAL = te::da::DataSourceFactory::make("GDAL", connInfo);
25  std::cout << "Datasource is opened? " << std::boolalpha << dsGDAL->isOpened() << '\n' << '\n';
26 
27  dsGDAL->open();
28 
29  std::cout << "Datasource is opened? " << std::boolalpha << dsGDAL->isOpened() << '\n' << '\n';
30  std::cout << "Datasource is valid? " << std::boolalpha << dsGDAL->isValid() << '\n' << '\n';
31 
32  if (!dsGDAL->isOpened() || !dsGDAL->isValid())
33  std::cout << "Datasource " << aux << " can not be used!\n";
34 
35  PrintDataSets(dsGDAL.get());
36  }
37  catch(const std::exception& e)
38  {
39  std::cout << std::endl << "An exception has occurred in GDALExample OpenFile(): " << e.what() << std::endl;
40  }
41  catch(...)
42  {
43  std::cout << std::endl << "An unexpected exception has occurred in GDALExample OpenFile()!" << std::endl;
44  }
45 }
46 
47 // This example shows how to open a directory, with multiple raster data files, as a GDAL data source.
49 {
50  try
51  {
52  std::string connInfo("file://");
53  std::string data_dir = TERRALIB_DATA_DIR;
54 
55  std::string aux("");
56  std::cout << "Inform the location of your folder of images (ENTER to accept default \'" << (data_dir + "/geotiff") << "\'): ";
57  std::getline (std::cin, aux);
58  if (!aux.empty())
59  connInfo += aux;
60  else
61  connInfo += data_dir + "/geotiff";
62 
63  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("GDAL", connInfo);
64 
65  bool res;
66  res = ds->isOpened(); // expect false
67 
68  ds->open();
69 
70  std::cout << "Datasource is opened? " << std::boolalpha << ds->isOpened() << '\n' << '\n';
71  std::cout << "Datasource is valid? " << std::boolalpha << ds->isValid() << '\n' << '\n';
72 
73  if (!ds->isOpened() || !ds->isValid())
74  std::cout << "Datasource " << aux << " can not be used!\n";
75 
76  PrintDataSets(ds.get());
77 
78 
79  }
80  catch(const std::exception& e)
81  {
82  std::cout << std::endl << "An exception has occurred in GDALExample OpenDirectory(): " << e.what() << std::endl;
83  }
84  catch(...)
85  {
86  std::cout << std::endl << "An unexpected exception has occurred in GDALExample OpenDirectory()!" << std::endl;
87  }
88 
89 }
90 
91 // This examples shows how to use a Data Source Transactor to retrieve information about a raster data file.
93 {
94  try
95  {
96  std::string connInfo("file://");
97  std::string data_dir = TERRALIB_DATA_DIR;
98 
99  std::string aux("");
100  std::cout << "Inform the location of your folder of images (ENTER to accept default \'" << (data_dir + "/geotiff") << "\'): ";
101  std::getline (std::cin, aux);
102  if (!aux.empty())
103  connInfo += aux;
104  else
105  connInfo += data_dir + "/geotiff";
106 
107  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("GDAL", connInfo);
108  ds->open();
109 
110  std::unique_ptr<te::da::DataSourceTransactor> tr = ds->getTransactor(); // caller gets the pointer ownership, delete it later
111 
112  std::string dsName("cbers2b_rgb342_crop.tif");
113  if (tr->dataSetExists(dsName))
114  {
115  std::unique_ptr<te::da::DataSet> dtset = tr->getDataSet("cbers2b_rgb342_crop.tif"); // caller gets the pointer ownership, delete it later
116  PrintDataSet(dsName,dtset.get());
117  }
118  }
119  catch(const std::exception& e)
120  {
121  std::cout << std::endl << "An exception has occurred in GDALExample DataSourceTransactor(): " << e.what() << std::endl;
122  }
123  catch(...)
124  {
125  std::cout << std::endl << "An unexpected exception has occurred in GDALExample DataSourceTransactor()!" << std::endl;
126  }
127 }
128 
130 {
131  OpenFile();
132  OpenDirectory();
134 }
void DataSourceTransactor()
Definition: GDALExample.cpp:92
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
void GDALExample()
An example using GDAL data source driver.
void PrintDataSets(te::da::DataSource *ds)
It prints datasets in a given data source.
static te::dt::Date ds(2010, 01, 01)
Examples on how to access/manipulate DataSources in TerraLib.
void OpenDirectory()
Definition: GDALExample.cpp:48
void OpenFile()
Definition: GDALExample.cpp:8
void PrintDataSet(std::string datasetName, te::da::DataSet *dataset)
It prints the data in a given dataset.
Definition: PrintDataSet.cpp:7