support for GDAL >= 2.0 in ogr plugin - refs #2259

This commit is contained in:
Dane Springmeyer 2014-06-02 19:33:49 -07:00
parent d0a509ccf4
commit cbdd112223
3 changed files with 25 additions and 8 deletions

View file

@ -72,7 +72,11 @@ ogr_datasource::~ogr_datasource()
{ {
// free layer before destroying the datasource // free layer before destroying the datasource
layer_.free_layer(); layer_.free_layer();
#if GDAL_VERSION_NUM >= 2000
GDALClose(( GDALDatasetH) dataset_);
#else
OGRDataSource::DestroyDataSource (dataset_); OGRDataSource::DestroyDataSource (dataset_);
#endif
} }
void ogr_datasource::init(mapnik::parameters const& params) void ogr_datasource::init(mapnik::parameters const& params)
@ -82,6 +86,7 @@ void ogr_datasource::init(mapnik::parameters const& params)
#endif #endif
// initialize ogr formats // initialize ogr formats
// NOTE: in GDAL >= 2.0 this is the same as GDALAllRegister()
OGRRegisterAll(); OGRRegisterAll();
boost::optional<std::string> file = params.get<std::string>("file"); boost::optional<std::string> file = params.get<std::string>("file");
@ -112,17 +117,22 @@ void ogr_datasource::init(mapnik::parameters const& params)
if (! driver.empty()) 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<gdal_dataset_type>(GDALOpenEx(dataset_name_.c_str(),nOpenFlags,papszAllowedDrivers,NULL,NULL));
#else
OGRSFDriver * ogr_driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driver.c_str()); OGRSFDriver * ogr_driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driver.c_str());
if (ogr_driver && ogr_driver != NULL) if (ogr_driver && ogr_driver != NULL)
{ {
dataset_ = ogr_driver->Open((dataset_name_).c_str(), FALSE); dataset_ = ogr_driver->Open((dataset_name_).c_str(), FALSE);
} }
#endif
} }
else else
{ {
// open ogr driver // open ogr driver
dataset_ = OGRSFDriverRegistrar::Open((dataset_name_).c_str(), FALSE); dataset_ = static_cast<gdal_dataset_type>(OGROpen(dataset_name_.c_str(), FALSE, NULL));
} }
if (! dataset_) if (! dataset_)

View file

@ -63,7 +63,7 @@ private:
mapnik::datasource::datasource_t type_; mapnik::datasource::datasource_t type_;
std::string dataset_name_; std::string dataset_name_;
std::string index_name_; std::string index_name_;
OGRDataSource* dataset_; gdal_dataset_type dataset_;
ogr_layer_ptr layer_; ogr_layer_ptr layer_;
std::string layer_name_; std::string layer_name_;
mapnik::layer_descriptor desc_; mapnik::layer_descriptor desc_;

View file

@ -29,9 +29,16 @@
// stl // stl
#include <stdexcept> #include <stdexcept>
// ogr // gdal
#include <gdal_version.h>
#include <ogrsf_frmts.h> #include <ogrsf_frmts.h>
#if GDAL_VERSION_NUM >= 2000
typedef GDALDataset* gdal_dataset_type;
#else
typedef OGRDataSource* gdal_dataset_type;
#endif
class ogr_layer_ptr class ogr_layer_ptr
{ {
public: public:
@ -62,7 +69,7 @@ public:
is_valid_ = false; is_valid_ = false;
} }
void layer_by_name(OGRDataSource* const datasource, void layer_by_name(gdal_dataset_type const datasource,
std::string const& layer_name) std::string const& layer_name)
{ {
free_layer(); free_layer();
@ -84,7 +91,7 @@ public:
#endif #endif
} }
void layer_by_index(OGRDataSource* const datasource, void layer_by_index(gdal_dataset_type const datasource,
int layer_index) int layer_index)
{ {
free_layer(); free_layer();
@ -110,7 +117,7 @@ public:
#endif #endif
} }
void layer_by_sql(OGRDataSource* const datasource, void layer_by_sql(gdal_dataset_type const datasource,
std::string const& layer_sql) std::string const& layer_sql)
{ {
free_layer(); free_layer();
@ -179,7 +186,7 @@ private:
} }
#endif #endif
OGRDataSource* datasource_; gdal_dataset_type datasource_;
OGRLayer* layer_; OGRLayer* layer_;
std::string layer_name_; std::string layer_name_;
bool owns_layer_; bool owns_layer_;