From 72344c391856cad6ca4f7731fed0207c8b213a84 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Wed, 27 Jun 2012 17:44:51 +0100 Subject: [PATCH] + cache GDALDataset object for re-use --- plugins/input/gdal/gdal_datasource.cpp | 29 ++++++++++++++++---------- plugins/input/gdal/gdal_datasource.hpp | 1 + plugins/input/gdal/gdal_featureset.cpp | 4 +--- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/plugins/input/gdal/gdal_datasource.cpp b/plugins/input/gdal/gdal_datasource.cpp index 05fe41626..da2ca101d 100644 --- a/plugins/input/gdal/gdal_datasource.cpp +++ b/plugins/input/gdal/gdal_datasource.cpp @@ -52,29 +52,35 @@ inline GDALDataset* gdal_datasource::open_dataset() const { MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource: Opening " << dataset_name_; - GDALDataset *dataset; + if (dataset_) + { + return dataset_; + } + + //GDALDataset *dataset; #if GDAL_VERSION_NUM >= 1600 if (shared_dataset_) { - dataset = reinterpret_cast(GDALOpenShared((dataset_name_).c_str(), GA_ReadOnly)); + dataset_ = reinterpret_cast(GDALOpenShared((dataset_name_).c_str(), GA_ReadOnly)); } else #endif { - dataset = reinterpret_cast(GDALOpen((dataset_name_).c_str(), GA_ReadOnly)); + dataset_ = reinterpret_cast(GDALOpen((dataset_name_).c_str(), GA_ReadOnly)); } - if (! dataset) + if (! dataset_) { throw datasource_exception(CPLGetLastErrorMsg()); } - return dataset; + return dataset_; } gdal_datasource::gdal_datasource(parameters const& params, bool bind) : datasource(params), + dataset_(0), desc_(*params.get("type"), "utf-8"), filter_factor_(*params_.get("filter_factor", 0.0)), nodata_value_(params_.get("nodata")) @@ -114,10 +120,9 @@ void gdal_datasource::bind() const band_ = *params_.get("band", -1); GDALDataset *dataset = open_dataset(); - - nbands_ = dataset->GetRasterCount(); - width_ = dataset->GetRasterXSize(); - height_ = dataset->GetRasterYSize(); + nbands_ = dataset_->GetRasterCount(); + width_ = dataset_->GetRasterXSize(); + height_ = dataset_->GetRasterYSize(); double tr[6]; bool bbox_override = false; @@ -144,7 +149,7 @@ void gdal_datasource::bind() const } else { - dataset->GetGeoTransform(tr); + dataset_->GetGeoTransform(tr); } MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource Geotransform=" @@ -183,7 +188,7 @@ void gdal_datasource::bind() const extent_.init(x0, y0, x1, y1); } - GDALClose(dataset); + //GDALClose(dataset); MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource: Raster Size=" << width_ << "," << height_; MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource: Raster Extent=" << extent_; @@ -193,6 +198,8 @@ void gdal_datasource::bind() const gdal_datasource::~gdal_datasource() { + MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Closing Dataset=" << dataset_; + GDALClose(dataset_); } datasource::datasource_t gdal_datasource::type() const diff --git a/plugins/input/gdal/gdal_datasource.hpp b/plugins/input/gdal/gdal_datasource.hpp index eb196ee28..1410824ff 100644 --- a/plugins/input/gdal/gdal_datasource.hpp +++ b/plugins/input/gdal/gdal_datasource.hpp @@ -47,6 +47,7 @@ public: void bind() const; private: GDALDataset* open_dataset() const; + mutable GDALDataset* dataset_; mutable mapnik::box2d extent_; std::string dataset_name_; mutable int band_; diff --git a/plugins/input/gdal/gdal_featureset.cpp b/plugins/input/gdal/gdal_featureset.cpp index 959b1d009..2f81fa29a 100644 --- a/plugins/input/gdal/gdal_featureset.cpp +++ b/plugins/input/gdal/gdal_featureset.cpp @@ -76,9 +76,7 @@ gdal_featureset::gdal_featureset(GDALDataset& dataset, gdal_featureset::~gdal_featureset() { - MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Closing Dataset=" << &dataset_; - - GDALClose(&dataset_); + // } feature_ptr gdal_featureset::next()