otbOGRVersionProxy.cxx 4.53 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
#endif // __GNUC__ || __clang__

namespace otb
{
namespace ogr
{
namespace version_proxy
{

52
GDALDataset * Open(const char * filename, bool readOnly , std::vector< std::string > const & options )
53 54
{
#if GDAL_VERSION_NUM<2000000
55
  (void)options;
56 57
  return OGRSFDriverRegistrar::Open(filename,!readOnly);
#else
58
  return (GDALDataset *)GDALOpenEx(
59 60 61 62 63
      filename, 
      (readOnly? GDAL_OF_READONLY : GDAL_OF_UPDATE) | GDAL_OF_VECTOR,
      NULL,
      otb::ogr::StringListConverter( options ).to_ogr(),
      NULL);
64 65 66
#endif
}

67
void Close(GDALDataset * dataset)
68 69 70 71 72 73 74 75
{
#if GDAL_VERSION_NUM<2000000
  OGRDataSource::DestroyDataSource(dataset);
#else
  GDALClose(dataset);
#endif
}

76
GDALDataset * Create(GDALDriver * driver, const char * name ,  std::vector< std::string > const & options )
77 78
{
#if GDAL_VERSION_NUM<2000000
79
  (void)options;
80
  GDALDataset * ds = driver->CreateDataSource(name);
81 82 83 84 85 86

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

  return ds;
#else
87 88 89 90 91 92
  return driver->Create( name ,
                         0 ,
                         0 ,
                         0 ,
                         GDT_Unknown ,
                         otb::ogr::StringListConverter( options ).to_ogr() );
93 94 95 96 97 98
#endif
}

bool Delete(const char * name)
{
  // Open dataset
99
  GDALDataset * poDS = otb::ogr::version_proxy::Open(name,false);
100
  GDALDriver * poDriver = NULL;
101 102 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
  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;
}

#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

151
std::vector<std::string> GetFileListAsStringVector(GDALDataset * dataset)
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
{
  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;
}

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;
}

/*----------------------[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