From dcbedc007a3583f3b6c1f3e3658ee6713e015908 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Mon, 2 Jun 2014 19:33:49 -0700 Subject: [PATCH] support for GDAL >= 2.0 in ogr plugin - refs #2259 --- plugins/input/ogr/ogr_datasource.cpp | 14 ++++++++++++-- plugins/input/ogr/ogr_datasource.hpp | 2 +- plugins/input/ogr/ogr_layer_ptr.hpp | 17 ++++++++++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/plugins/input/ogr/ogr_datasource.cpp b/plugins/input/ogr/ogr_datasource.cpp index 319e02713..59fc239b5 100644 --- a/plugins/input/ogr/ogr_datasource.cpp +++ b/plugins/input/ogr/ogr_datasource.cpp @@ -71,7 +71,11 @@ ogr_datasource::~ogr_datasource() { // free layer before destroying the datasource layer_.free_layer(); +#if GDAL_VERSION_NUM >= 2000 + GDALClose(( GDALDatasetH) dataset_); +#else OGRDataSource::DestroyDataSource (dataset_); +#endif } void ogr_datasource::init(mapnik::parameters const& params) @@ -81,6 +85,7 @@ void ogr_datasource::init(mapnik::parameters const& params) #endif // initialize ogr formats + // NOTE: in GDAL >= 2.0 this is the same as GDALAllRegister() OGRRegisterAll(); boost::optional file = params.get("file"); @@ -111,17 +116,22 @@ void ogr_datasource::init(mapnik::parameters const& params) if (! driver.empty()) { +#if GDAL_VERSION_NUM >= 2000 + unsigned int nOpenFlags = GDAL_OF_READONLY | GDAL_OF_VECTOR; + const char* papszAllowedDrivers[] = { driver.c_str(), NULL }; + dataset_ = static_cast(GDALOpenEx(dataset_name_.c_str(),nOpenFlags,papszAllowedDrivers,NULL,NULL)); +#else OGRSFDriver * ogr_driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driver.c_str()); if (ogr_driver && ogr_driver != nullptr) { dataset_ = ogr_driver->Open((dataset_name_).c_str(), FALSE); } - +#endif } else { // open ogr driver - dataset_ = OGRSFDriverRegistrar::Open((dataset_name_).c_str(), FALSE); + dataset_ = static_cast(OGROpen(dataset_name_.c_str(), FALSE, NULL)); } if (! dataset_) diff --git a/plugins/input/ogr/ogr_datasource.hpp b/plugins/input/ogr/ogr_datasource.hpp index 7be996c4d..5691fd845 100644 --- a/plugins/input/ogr/ogr_datasource.hpp +++ b/plugins/input/ogr/ogr_datasource.hpp @@ -63,7 +63,7 @@ private: mapnik::datasource::datasource_t type_; std::string dataset_name_; std::string index_name_; - OGRDataSource* dataset_; + gdal_dataset_type dataset_; ogr_layer_ptr layer_; std::string layer_name_; mapnik::layer_descriptor desc_; diff --git a/plugins/input/ogr/ogr_layer_ptr.hpp b/plugins/input/ogr/ogr_layer_ptr.hpp index d1f4b6317..5fbb57526 100644 --- a/plugins/input/ogr/ogr_layer_ptr.hpp +++ b/plugins/input/ogr/ogr_layer_ptr.hpp @@ -29,9 +29,16 @@ // stl #include -// ogr +// gdal +#include #include +#if GDAL_VERSION_NUM >= 2000 +typedef GDALDataset* gdal_dataset_type; +#else +typedef OGRDataSource* gdal_dataset_type; +#endif + class ogr_layer_ptr { public: @@ -62,7 +69,7 @@ public: is_valid_ = false; } - void layer_by_name(OGRDataSource* const datasource, + void layer_by_name(gdal_dataset_type const datasource, std::string const& layer_name) { free_layer(); @@ -84,7 +91,7 @@ public: #endif } - void layer_by_index(OGRDataSource* const datasource, + void layer_by_index(gdal_dataset_type const datasource, int layer_index) { free_layer(); @@ -110,7 +117,7 @@ public: #endif } - void layer_by_sql(OGRDataSource* const datasource, + void layer_by_sql(gdal_dataset_type const datasource, std::string const& layer_sql) { free_layer(); @@ -179,7 +186,7 @@ private: } #endif - OGRDataSource* datasource_; + gdal_dataset_type datasource_; OGRLayer* layer_; std::string layer_name_; bool owns_layer_;