12#ifndef CCGL_DB_GDAL_H_
13#define CCGL_DB_GDAL_H_
20#include <ogrsf_frmts.h>
21#include "cpl_string.h"
22#include "ogr_spatialref.h"
26#if GDAL_VERSION_MAJOR >= 2
27typedef GDALDataset GDALVectorDS;
29typedef OGRDataSource GDALVectorDS;
33typedef GDALDataset GDALRasterDS;
37inline GDALVectorDS* OpenVector(
const char* path,
int read_only=
true) {
38#if GDAL_VERSION_MAJOR >= 2
40 int flags = GDAL_OF_VECTOR | (read_only ? GDAL_OF_READONLY : GDAL_OF_UPDATE);
41 return static_cast<GDALVectorDS*
>(GDALOpenEx(path, flags,
nullptr,
nullptr,
nullptr));
44 return OGRSFDriverRegistrar::Open(path, read_only ? FALSE : TRUE);
48inline void CloseVector(GDALVectorDS* ds) {
50#if GDAL_VERSION_MAJOR >= 2
53 OGRDataSource::DestroyDataSource(ds);
58 void operator()(GDALVectorDS* ds)
const { CloseVector(ds); }
60typedef std::unique_ptr<GDALVectorDS, GDALVectorDSDeleter> GDALVectorDSHandle;
64inline GDALRasterDS* OpenRaster(
const char* path,
int read_only=
true,
65 const char*
const* open_options=
nullptr) {
66#if GDAL_VERSION_MAJOR >= 2
68 int flags = GDAL_OF_RASTER | (read_only ? GDAL_OF_READONLY : GDAL_OF_UPDATE);
69 return static_cast<GDALRasterDS*
>(GDALOpenEx(path, flags,
nullptr, open_options,
nullptr));
73 return static_cast<GDALRasterDS*
>(GDALOpen(path, read_only ? GA_ReadOnly : GA_Update));
77inline GDALRasterDS* CreateRaster(
const char* driverName,
const char* filename,
79 int bands, GDALDataType dtype,
80 char** papszOptions=
nullptr) {
81 if (!driverName || !driverName[0] || !filename)
return nullptr;
83 GDALDriver* drv = GetGDALDriverManager()->GetDriverByName(driverName);
84 if (!drv)
return nullptr;
85 return drv->Create(filename, xsize, ysize, bands, dtype, papszOptions);
88inline void CloseRaster(GDALRasterDS* ds) {
94 void operator()(GDALRasterDS* ds)
const { CloseRaster(ds); }
96typedef std::unique_ptr<GDALRasterDS, GDALRasterDSDeleter> GDALRasterDSHandle;
Definition: gdal_handler.h:93
Definition: gdal_handler.h:57