otbOGRVersionProxy2x.cxx 3.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "otbOGRVersionProxy.h"

20
#if defined(__GNUC__) || defined(__clang__)
21 22
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
23
#include "gdal_priv.h"
24 25 26 27
#pragma GCC diagnostic pop
#else
#include "gdal_priv.h"
#endif
28

29

30 31
namespace otb
{
32 33 34 35
namespace ogr
{
namespace version_proxy
{
36

37 38 39 40 41
OTBGdalAdapters_EXPORT bool IsOFTInteger64(OGRFieldType type)
{
  return type == OFTInteger64;
}

42
GDALDatasetType * Open(const char * filename, bool readOnly)
43 44 45 46
{
  return (GDALDatasetType *)GDALOpenEx(filename, (readOnly? GDAL_OF_READONLY : GDAL_OF_UPDATE) | GDAL_OF_VECTOR,NULL,NULL,NULL);
}

47
void Close(GDALDatasetType * dataset)
48 49 50
{
  GDALClose(dataset);
}
51

52
GDALDatasetType * Create(GDALDriverType * driver, const char * name)
53 54 55 56
{
  return driver->Create(name,0,0,0,GDT_Unknown,NULL);
}

57
bool Delete(const char * name)
58
{
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  // Open dataset
  GDALDatasetType * poDS = otb::ogr::version_proxy::Open(name,false);
  GDALDriverType * poDriver = NULL;
  if(poDS)
    {
    poDriver = poDS->GetDriver();
    Close(poDS);
    }

  if(poDriver)
    {
    OGRErr ret = poDriver->Delete(name);

    return (ret == OGRERR_NONE);
    }
  return false;
75 76
}

77
GDALDriverType *  GetDriverByName(const char * name)
78 79 80 81
{
  return GetGDALDriverManager()->GetDriverByName(name);
}

82
std::string GetDatasetClassName()
83 84 85 86
{
  return std::string("GDALDataset");
}

87
std::string GetDriverClassName()
88 89 90 91
{
  return std::string("GDALDriver");
}

92 93 94
namespace raii
{
// This class is used in the next function, so as to prevent any
95
// resource leak on char ** returned by dataset->GetFileList()
96 97 98 99
class CharPPCapsule
{
public:
  CharPPCapsule(char ** in)
100 101 102 103
    : m_P(in)
  {}

  const char ** P() const
104
  {
105
    return const_cast<const char **>(m_P);
106
  }
107

108 109 110 111 112 113
  ~CharPPCapsule()
  {
    if(m_P)
      CSLDestroy(m_P);
  }

114
private:
115 116 117 118
  char ** m_P;
};
}

119
std::vector<std::string> GetFileListAsStringVector(GDALDatasetType * dataset)
120
{
121
  std::vector<std::string> ret;
122

123
  raii::CharPPCapsule capsule(dataset->GetFileList());
124 125

  std::string files_str="";
126

127
  if(capsule.P())
128 129
    {
    unsigned int i = 0;
130
    while(capsule.P()[i]!=NULL)
131
      {
132
      ret.push_back(std::string(capsule.P()[i]));
133
      ++i;
134
      }
135
    }
136
  return ret;
137 138
}

139
bool SyncToDisk(GDALDatasetType * dataset)
140 141 142 143 144 145
{
  dataset->FlushCache();

  return true;
}

146
std::vector<std::string> GetAvailableDriversAsStringVector()
147 148
{
  std::vector<std::string> ret;
149

150
  int nbDrivers = GetGDALDriverManager()->GetDriverCount();
151

152 153 154 155 156 157 158 159
  for(int i = 0; i < nbDrivers;++i)
    {
    ret.push_back(GDALGetDriverShortName(GetGDALDriverManager()->GetDriver(i)));
    }

  return ret;
}

160 161 162 163 164
std::string GetDriverNameFromDataSource(const GDALDatasetType * ds)
{
  return std::string(const_cast<GDALDatasetType *>(ds)->GetDriverName());
}

165 166
}
}
167
} // end namespace