otbOGRVersionProxy.cxx 5.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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.
 */

#include "otbOGRVersionProxy.h"
22
#include "otbOGRHelpers.h"
23 24 25 26 27 28 29 30 31 32 33

#include "itkMacro.h"

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#if GDAL_VERSION_NUM<2000000
#include "ogrsf_frmts.h"
#else
#include "gdal_priv.h"
#endif
34
#include "ogr_feature.h"
35 36 37 38 39 40 41
#pragma GCC diagnostic pop
#else
#if GDAL_VERSION_NUM<2000000
#include "ogrsf_frmts.h"
#else
#include "gdal_priv.h"
#endif
42
#include "ogr_feature.h"
43 44 45 46 47 48 49 50 51 52
#endif // __GNUC__ || __clang__

namespace otb
{
namespace ogr
{
namespace version_proxy
{

/*-------------------------[GDAL v2 API change]-------------------------------*/
53
OTBGdalAdapters_EXPORT bool IsOFTInteger64(OGRFieldType type)
54 55
{
#if GDAL_VERSION_NUM<2000000
56
  (void)type;
57 58 59 60 61 62 63
  return false;
#else
  return type == OFTInteger64;
#endif
}


64
GDALDatasetType * Open(const char * filename, bool readOnly , std::vector< std::string > const & options )
65 66 67 68
{
#if GDAL_VERSION_NUM<2000000
  return OGRSFDriverRegistrar::Open(filename,!readOnly);
#else
69 70 71 72 73 74
  return (GDALDatasetType *)GDALOpenEx(
      filename, 
      (readOnly? GDAL_OF_READONLY : GDAL_OF_UPDATE) | GDAL_OF_VECTOR,
      NULL,
      otb::ogr::StringListConverter( options ).to_ogr(),
      NULL);
75 76 77 78 79 80 81 82 83 84 85 86
#endif
}

void Close(GDALDatasetType * dataset)
{
#if GDAL_VERSION_NUM<2000000
  OGRDataSource::DestroyDataSource(dataset);
#else
  GDALClose(dataset);
#endif
}

87
GDALDatasetType * Create(GDALDriverType * driver, const char * name ,  std::vector< std::string > const & options )
88 89 90 91 92 93 94 95 96
{
#if GDAL_VERSION_NUM<2000000
  GDALDatasetType * ds = driver->CreateDataSource(name);

  if(ds)
    ds->SetDriver(driver);

  return ds;
#else
97 98 99 100 101 102
  return driver->Create( name ,
                         0 ,
                         0 ,
                         0 ,
                         GDT_Unknown ,
                         otb::ogr::StringListConverter( options ).to_ogr() );
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
#endif
}

bool Delete(const char * name)
{
  // Open dataset
  GDALDatasetType * poDS = otb::ogr::version_proxy::Open(name,false);
  GDALDriverType * poDriver = NULL;
  if(poDS)
    {
    poDriver = poDS->GetDriver();
    Close(poDS);
    }
#if GDAL_VERSION_NUM<2000000
  if(poDriver && poDriver->TestCapability(ODrCDeleteDataSource))
    {
    OGRErr ret = poDriver->DeleteDataSource(name);
    return (ret == OGRERR_NONE);
    }
#else
  if(poDriver)
    {
    OGRErr ret = poDriver->Delete(name);
    return (ret == OGRERR_NONE);
    }
#endif
  return false;
}

GDALDriverType *  GetDriverByName(const char * name)
{
#if GDAL_VERSION_NUM<2000000
  return OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(name);
#else
  return GetGDALDriverManager()->GetDriverByName(name);
#endif
}

std::string GetDatasetClassName()
{
#if GDAL_VERSION_NUM<2000000
  return std::string("OGRDataSource");
#else
  return std::string("GDALDataset");
#endif
}

std::string GetDriverClassName()
{
#if GDAL_VERSION_NUM<2000000
  return std::string("OGRSFDriver");
#else
  return std::string("GDALDriver");
#endif
}

#if GDAL_VERSION_NUM>=2000000
namespace raii
{
// This class is used in the next function, so as to prevent any
// resource leak on char ** returned by dataset->GetFileList()
class CharPPCapsule
{
public:
  CharPPCapsule(char ** in)
    : m_P(in)
  {}

  const char ** P() const
  {
    return const_cast<const char **>(m_P);
  }

  ~CharPPCapsule()
  {
    if(m_P)
      CSLDestroy(m_P);
  }

private:
  char ** m_P;
};
}
#endif

std::vector<std::string> GetFileListAsStringVector(GDALDatasetType * dataset)
{
  std::vector<std::string> ret;
#if GDAL_VERSION_NUM<2000000
  ret.push_back(std::string(dataset->GetName()));
#else
  raii::CharPPCapsule capsule(dataset->GetFileList());

  std::string files_str="";

  if(capsule.P())
    {
    unsigned int i = 0;
    while(capsule.P()[i]!=NULL)
      {
      ret.push_back(std::string(capsule.P()[i]));
      ++i;
      }
    }
#endif
  return ret;
}

bool SyncToDisk(GDALDatasetType * dataset)
{
#if GDAL_VERSION_NUM<2000000
  const OGRErr res= dataset->SyncToDisk();
  return (res == OGRERR_NONE);
#else
  dataset->FlushCache();
  return true;
#endif
}

std::vector<std::string> GetAvailableDriversAsStringVector()
{
  std::vector<std::string> ret;
#if GDAL_VERSION_NUM<2000000
  int nbDrivers = OGRSFDriverRegistrar::GetRegistrar()->GetDriverCount();

  for(int i = 0; i < nbDrivers;++i)
    {
    ret.push_back(OGRSFDriverRegistrar::GetRegistrar()->GetDriver(i)->GetName());
    }
#else
  int nbDrivers = GetGDALDriverManager()->GetDriverCount();

  for(int i = 0; i < nbDrivers;++i)
    {
    ret.push_back(GDALGetDriverShortName(GetGDALDriverManager()->GetDriver(i)));
    }
#endif
  return ret;
}

std::string GetDriverNameFromDataSource(const GDALDatasetType * ds)
{
#if GDAL_VERSION_NUM<2000000
  return std::string(const_cast<GDALDatasetType *>(ds)->GetDriver()->GetName());
#else
  return std::string(const_cast<GDALDatasetType *>(ds)->GetDriverName());
#endif
}

/*----------------------[GDAL 2.2 change on IsFieldSet()]---------------------*/
bool IsFieldSetAndNotNull(OGRFeature *feat, int index)
{
#if GDAL_VERSION_NUM<2020000
  return feat->IsFieldSet(index);
#else
  return feat->IsFieldSetAndNotNull(index);
#endif
}

}
}
} // end namespace