All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSource.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 terralib/gdal/DataSource.cpp
22 
23  \brief The implementation of a DataSource that consists of datasets that can be decoded by the GDAL Library.
24 */
25 
26 // TerraLib
27 #include "../common/StringUtils.h"
28 #include "../common/Translator.h"
29 #include "../geometry/Envelope.h"
30 #include "../dataaccess/dataset/DataSetType.h"
31 #include "../raster/Grid.h"
32 #include "../raster/Raster.h"
33 #include "../raster/RasterProperty.h"
34 #include "DataSet.h"
35 #include "DataSource.h"
36 #include "Exception.h"
37 #include "Transactor.h"
38 #include "Utils.h"
39 #include "DataSetUseCounter.h"
40 
41 // GDAL
42 #include <gdal_priv.h>
43 
44 // Boost
45 #include <boost/filesystem.hpp>
46 #include <boost/format.hpp>
47 
49 
51  m_straccess(""),
52  m_isOpened(false)
53 {
54 }
55 
57 {}
58 
59 std::string te::gdal::DataSource::getType() const
60 {
62 }
63 
64 const std::map<std::string, std::string>& te::gdal::DataSource::getConnectionInfo() const
65 {
66  return m_connectionInfo;
67 }
68 
69 void te::gdal::DataSource::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
70 {
71  m_connectionInfo = connInfo;
72 }
73 
74 // open methods retrieves the names of the datasets in the data source
76 {
77  if (m_isOpened)
78  return;
79 
80  if (m_connectionInfo.empty())
81  throw Exception((boost::format(TE_TR("Empty data source connection information"))).str());
82 
83  std::map<std::string, std::string>::const_iterator it = m_connectionInfo.find("SOURCE");
84  if (it != m_connectionInfo.end())
85  m_straccess = it->second;
86  else
87  {
88  it = m_connectionInfo.find("URI");
89  if (it != m_connectionInfo.end())
90  m_straccess = it->second;
91  else
92  throw Exception((boost::format(TE_TR("Invalid data source connection information"))).str());
93  }
94  m_isOpened = true;
95 }
96 
98 {
99 }
100 
102 {
103  return m_isOpened;
104 }
105 
107 {
108  if(m_connectionInfo.empty())
109  return false;
110 
111  std::map<std::string, std::string>::const_iterator it = m_connectionInfo.find("SOURCE");
112 
113  // if URI used, check if it is a valid directory or file name
114  if(it != m_connectionInfo.end())
115  {
116  if(boost::filesystem::is_directory(it->second))
117  return true;
118  }
119  else // if it is another GDAL string let's check it
120  {
121  it = m_connectionInfo.find("URI");
122 
123  if(it == m_connectionInfo.end())
124  return false;
125 
126  DataSetUseCounter dsUseCounter( GetParentDataSetName( it->second ),
128 
129  GDALDataset* gds = static_cast<GDALDataset*>(GDALOpen(it->second.c_str(), GA_ReadOnly));
130 
131  if(gds)
132  {
133  GDALClose(gds);
134 
135  return true;
136  }
137  }
138  return false;
139 }
140 
142 {
143  return sm_capabilities;
144 }
145 
146 
147 std::auto_ptr<te::da::DataSourceTransactor> te::gdal::DataSource::getTransactor()
148 {
149  if (!m_isOpened)
150  throw Exception((boost::format(TE_TR("Data source is not open."))).str());
151 
152  return std::auto_ptr<te::da::DataSourceTransactor>(new te::gdal::Transactor(m_straccess));
153 }
154 
156 {
157  sm_capabilities = capabilities;
158 }
159 
160 void te::gdal::DataSource::create(const std::map<std::string, std::string>& dsInfo)
161 {
162  m_connectionInfo = dsInfo;
163 
164  // create the needed directory
165  std::map<std::string, std::string>::const_iterator it = m_connectionInfo.find("SOURCE");
166  if(it != m_connectionInfo.end())
167  {
168  try
169  {
170  if(!boost::filesystem::is_directory(it->second))
171  boost::filesystem::create_directory(it->second);
172  }
173  catch(const boost::filesystem::filesystem_error& e)
174  {
175  throw Exception((boost::format(TE_TR("Could not create the data source due to the following error: %1%.")) % e.what()).str());
176  }
177  }
178  else
179  {
180  throw Exception((boost::format(TE_TR("Data source creation is supported only for directory data sources"))).str());
181  }
182 }
183 
184 bool te::gdal::DataSource::exists(const std::map<std::string, std::string>& dsInfo)
185 {
186  std::map<std::string, std::string>::const_iterator it = dsInfo.find("SOURCE"); // expects a directory
187  if(it != dsInfo.end())
188  {
189  if (boost::filesystem::exists(it->second) && boost::filesystem::is_directory(it->second))
190  return true;
191 
192  return false;
193  }
194 
195  it = dsInfo.find("URI"); // expects a file?
196  if(it != dsInfo.end())
197  {
199 
200  GDALDataset* gds = static_cast<GDALDataset*>(GDALOpen(it->second.c_str(), GA_ReadOnly));
201 
202  if (gds)
203  {
204  GDALClose(gds);
205 
206  return true;
207  }
208  }
209 
210  return false; // it is an invalid file
211 }
212 
213 void te::gdal::DataSource::drop(const std::map<std::string, std::string>& dsInfo)
214 {
215  std::map<std::string, std::string>::const_iterator it = dsInfo.find("URI");
216  if (it == dsInfo.end())
217  it = dsInfo.find("SOURCE");
218 
219  if (it == dsInfo.end())
220  return; // nothing to be done
221 
222  try
223  {
224  boost::filesystem::remove(it->second);
225  }
226  catch(const boost::filesystem::filesystem_error& /*e*/)
227  {}
228 }
229 
230 std::vector<std::string> te::gdal::DataSource::getDataSourceNames(const std::map<std::string, std::string>& dsInfo)
231 {
232  std::vector<std::string> dsnames;
233 
234  std::map<std::string, std::string>::const_iterator it = dsInfo.find("URI");
235  if (it != dsInfo.end())
236  dsnames.push_back(it->second);
237  else
238  {
239  it = dsInfo.find("SOURCE");
240  if (it != dsInfo.end())
241  dsnames.push_back(it->second);
242  else
243  throw Exception((boost::format(TE_TR("Empty ou invalid data source connection information"))).str());
244  }
245  return dsnames;
246 }
247 
248 std::vector<te::common::CharEncoding> te::gdal::DataSource::getEncodings(const std::map<std::string, std::string>& dsInfo)
249 {
250  return std::vector<te::common::CharEncoding>();
251 }
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
static void setCapabilities(const te::da::DataSourceCapabilities &capabilities)
Definition: DataSource.cpp:155
const std::map< std::string, std::string > & getConnectionInfo() const
It returns the set of parameters used to set up the access channel to the underlying repository...
Definition: DataSource.cpp:64
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
static te::da::DataSourceCapabilities sm_capabilities
Definition: DataSource.h:246
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
~DataSource()
Virtual destructor.
Definition: DataSource.cpp:56
void create(const std::map< std::string, std::string > &dsInfo)
It creates a new data source.
Definition: DataSource.cpp:160
#define TE_GDAL_DRIVER_IDENTIFIER
Definition: Config.h:38
An exception class for the GDAL module.
std::vector< te::common::CharEncoding > getEncodings(const std::map< std::string, std::string > &dsInfo)
It gets the encodings for the data source.
Definition: DataSource.cpp:248
te::da::DataSourceCapabilities capabilities
std::string GetParentDataSetName(const std::string &subDataSetName)
It returns the parent dataset name from a Sub DataSet name.
Definition: Utils.cpp:882
void drop(const std::map< std::string, std::string > &dsInfo)
It removes the data source with the connection information from a driver.
Definition: DataSource.cpp:213
bool exists(const std::map< std::string, std::string > &dsInfo)
Check the existence of a data source in a driver.
Definition: DataSource.cpp:184
void close()
It closes the data source and clears all the resources used by its internal communication channel...
Definition: DataSource.cpp:97
std::auto_ptr< te::da::DataSourceTransactor > getTransactor()
It returns an object that can execute transactions in the context of a data source.
Definition: DataSource.cpp:147
bool isValid() const
It checks if the data source is valid (available for using).
Definition: DataSource.cpp:106
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
Definition: DataSource.cpp:101
GDAL data set use counter.
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
Definition: DataSource.cpp:59
std::vector< std::string > getDataSourceNames(const std::map< std::string, std::string > &dsInfo)
It gets the data source names available in a driver.
Definition: DataSource.cpp:230
void open()
It opens the data source and makes it ready for using.
Definition: DataSource.cpp:75
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
Definition: DataSource.cpp:141
The implementation of a DataSource that consists of datasets that can be decoded by the GDAL Library...
void setConnectionInfo(const std::map< std::string, std::string > &connInfo)
It sets the connection information to be used when connecting to the data source. ...
Definition: DataSource.cpp:69