otbOGRVersionProxy.h 6.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20

21 22
#ifndef otbOGRVersionProxy_h
#define otbOGRVersionProxy_h
23 24

#include <string>
25
#include <vector>
26
#include "otbConfigure.h"
27
#include "itkMacro.h"
28

29 30 31 32 33 34 35 36 37 38
#if defined(_MSC_VER)
#pragma warning ( push )
#pragma warning ( disable: 4251 )
#include "ogr_core.h" // OGR enums
#pragma warning ( pop )
#else
#include "ogr_core.h" // OGR enums
#endif


39 40 41 42 43 44 45 46
#ifdef OTB_USE_GDAL_20
class GDALDataset;
class GDALDriver;
#else
class OGRDataSource;
class OGRSFDriver;
#endif

47 48
class OGRFeature;

49 50
#include "OTBGdalAdaptersExport.h"

51 52
namespace otb
{
53 54 55 56 57
namespace ogr
{
namespace version_proxy
{

58 59 60 61 62 63
/**
* With Gdal >= 2.0, this function will test equality between type and
* OFTInteger64 enum. Otherwise, it returns false. 
*/
OTBGdalAdapters_EXPORT bool IsOFTInteger64(OGRFieldType type);

64
/** 
65
 * This namespace holds proxy functions hiding interface changes in gdal 2.0 
66
 *
67
 * This namespace holds proxy functions hiding interface changes in OGR
68 69
 * dataset between gdal 1.x (x>10) and gdal 2.x. It defines a common
 * interface that should be used in place of calling directly the
70
 * wrapped gdal functions.
71 72 73
 * 
 * Whenever GDALDataset and GDALDriver have to be used to open a
 * vector dataset (or OGRDataSource an OGRSFDriver for gdal 1.x), one
74
 * should use ogr::version_proxy types GDALDatasetType and
75 76
 * GDALDriverType.
 * 
77
 * See function documentation for details.
78 79
 */

80 81 82 83 84 85 86
  #ifdef OTB_USE_GDAL_20
  typedef GDALDataset GDALDatasetType;
  typedef GDALDriver GDALDriverType;
  #else
  typedef OGRDataSource GDALDatasetType;
  typedef OGRSFDriver   GDALDriverType;
#endif
87 88

  /** 
89
   * This function opens a file, possibly in read-only mode, and returns
90 91 92 93 94 95 96 97
   * a dataset.
   *
   * Calls OGRSFDriverRegistrar::Open for gdal 1.x implementation and GDALopenEx for
   * gdal 2.x implementation.

   * \param filename Filename of the file to open
   * \param readOnly: If true, dataset is open in read-only mode.
   * \return NULL if file could not be open.
98 99
   */
  OTBGdalAdapters_EXPORT   
100
  GDALDatasetType * Open(const char * filename, bool readOnly = true , std::vector< std::string > const & options = std::vector< std::string >() );
101 102

  /**
103
   * This function closes a dataset.
104 105 106 107 108 109 110
   *
   * Calls OGRDataSource::DestroyDataSource for gdal 1.x
   * implementation and GDALClose for gdal 2.x implementation.
   *
   * \param dataset Pointer to the dataset to close. Will not be
   * checked for null pointer.
   */
111
  OTBGdalAdapters_EXPORT
112
  void Close(GDALDatasetType * dataset);
113 114

  /**
115
   * This function creates a new dataset.
116 117 118 119 120 121 122 123 124 125 126 127
   *
   * Calls OGRSFDriver::CreateDataSource for gdal 1.x implementation
   * and GDALDriver::Create with (0,0) raster size for gdal 2.x
   * implementation
   * 
   * \param driver Pointer to the driver used for creation. Will not
   * be checked for null pointer.
   *
   * \param name Name of the dataset to create.
   * 
   * \return NULL if dataset could not be created.
   */
128
  OTBGdalAdapters_EXPORT
129
  GDALDatasetType * Create(GDALDriverType * driver, const char * name , std::vector< std::string > const & options = std::vector< std::string >() );
130

131 132

  /**
133
   * This function physically deletes an existing dataset.
134 135 136 137 138
   * 
   * Calls OGRDataSource::DeleteDataSource for gdal 1.x implementation
   * and GDALDriver::Delete for gdal 2.x implementation.
   *
   * \param name Name of the dataset to destroy.
139 140
   */
  OTBGdalAdapters_EXPORT   
141
  bool Delete(const char * name);
142

143
  /**
144
   * This function returns a pointer to the driver from its name.
145 146 147 148 149 150 151 152 153 154
   * 
   * Calls OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName() for
   * gdal 1.x implementation and
   * GetGDALDriverManager()->GetDriverByName() for gdal 2.x
   * implementation.
   * 
   * \param name Name of the driver to retrieve
   * 
   * \return NULL if no driver could be retrieved.
   */
155
  OTBGdalAdapters_EXPORT 
156
  GDALDriverType *  GetDriverByName(const char * name);
157

158
  OTBGdalAdapters_EXPORT
159 160
  std::string GetDriverNameFromDataSource(const GDALDatasetType * ds);

161 162 163 164 165 166 167 168 169 170 171
  /**
   * Sync dataset to disk.
   *
   * Calls OGRDataSource::SyncToDisk() for gdal 1.x implementation and
   * GDALDataset::FlushCache() for gdal 2.x  implementation.
   *
   * \param dataset Pointer to the dataset to sync. Will not be
   * checked for null pointer.
   *
   * \return True if sync went on without any error.
   */
172 173
   OTBGdalAdapters_EXPORT
   bool SyncToDisk(GDALDatasetType * dataset);
174

175 176 177 178
  /**
   * \return The name of the dataset class behind the implementation
   * (OGRDataSource for gdal 1.x and GdalDataset for gdal 2.x)
   */
179
  OTBGdalAdapters_EXPORT
180
  std::string GetDatasetClassName();
181

182 183 184 185
  /**
   * \return The name of the driver class behind the implementation
   * (OGRSFDriver for gdal 1.x and GDALDriver for gdal 2.x)
   */
186
  OTBGdalAdapters_EXPORT 
187
  std::string GetDriverClassName();
188

189 190 191 192 193 194 195 196 197 198 199 200
  /**
   * Return the list of files composing the dataset.
   * 
   * Calls OGRDataSource::GetName() and wrap in string vector for gdal
   * 1.x implementation, and GDALDataset::GetFileList and wrap in
   * string vector for gdal 2.x implementation.
   *  
   * \param dataset Pointer to the dataset to get the file list from. Will not be
   * checked for null pointer.
   * 
   * \return A vector of string containing the list of files.
   */
201 202
   
  OTBGdalAdapters_EXPORT 
203
  std::vector<std::string> GetFileListAsStringVector(GDALDatasetType * dataset);
204 205 206 207 208 209 210 211 212 213

  /** 
   * Return the list of available drivers.
   *
   * Calls OGRSFDriverRegistrar::GetRegistrar() for gdal 1.x
   * implementation and GetGDALDriverManager() for gdal 2.x
   * implementation.
   *
   * \return A vector of string containing the list of available drivers.
   */  
214
  OTBGdalAdapters_EXPORT
215
  std::vector<std::string> GetAvailableDriversAsStringVector();
216

217 218 219 220 221 222 223 224 225
  /**
   * Returns true if the field 'index' is set and not-null in the given feature
   *
   * Before gdal 2.2, it calls OGRFeature::IsFieldSet().
   * After gdal 2.2, it calls OGRFeature::IsFieldSetAndNotNull()
   */
   OTBGdalAdapters_EXPORT
   bool IsFieldSetAndNotNull(OGRFeature *feat, int index);

226 227
}
}
228 229 230
} // end namespace otb

#endif