cache GDALDataset object for re-use

This commit is contained in:
Dane Springmeyer 2015-02-02 22:50:17 -08:00
parent a60a3855a1
commit 5551cae0be
3 changed files with 27 additions and 39 deletions

View file

@ -45,37 +45,9 @@ using mapnik::layer_descriptor;
using mapnik::datasource_exception; using mapnik::datasource_exception;
/*
* Opens a GDALDataset and returns a pointer to it.
* Caller is responsible for calling GDALClose on it
*/
inline GDALDataset* gdal_datasource::open_dataset() const
{
MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource: Opening " << dataset_name_;
GDALDataset *dataset;
#if GDAL_VERSION_NUM >= 1600
if (shared_dataset_)
{
dataset = reinterpret_cast<GDALDataset*>(GDALOpenShared((dataset_name_).c_str(), GA_ReadOnly));
}
else
#endif
{
dataset = reinterpret_cast<GDALDataset*>(GDALOpen((dataset_name_).c_str(), GA_ReadOnly));
}
if (! dataset)
{
throw datasource_exception(CPLGetLastErrorMsg());
}
return dataset;
}
gdal_datasource::gdal_datasource(parameters const& params) gdal_datasource::gdal_datasource(parameters const& params)
: datasource(params), : datasource(params),
dataset_(nullptr),
desc_(gdal_datasource::name(), "utf-8"), desc_(gdal_datasource::name(), "utf-8"),
nodata_value_(params.get<double>("nodata")), nodata_value_(params.get<double>("nodata")),
nodata_tolerance_(*params.get<double>("nodata_tolerance",1e-12)) nodata_tolerance_(*params.get<double>("nodata_tolerance",1e-12))
@ -104,11 +76,27 @@ gdal_datasource::gdal_datasource(parameters const& params)
shared_dataset_ = *params.get<mapnik::boolean_type>("shared", false); shared_dataset_ = *params.get<mapnik::boolean_type>("shared", false);
band_ = *params.get<mapnik::value_integer>("band", -1); band_ = *params.get<mapnik::value_integer>("band", -1);
GDALDataset *dataset = open_dataset(); #if GDAL_VERSION_NUM >= 1600
if (shared_dataset_)
{
dataset_ = reinterpret_cast<GDALDataset*>(GDALOpenShared((dataset_name_).c_str(), GA_ReadOnly));
}
else
#endif
{
dataset_ = reinterpret_cast<GDALDataset*>(GDALOpen((dataset_name_).c_str(), GA_ReadOnly));
}
nbands_ = dataset->GetRasterCount(); if (! dataset_)
width_ = dataset->GetRasterXSize(); {
height_ = dataset->GetRasterYSize(); throw datasource_exception(CPLGetLastErrorMsg());
}
MAPNIK_LOG_ERROR(gdal) << "gdal_featureset: opened Dataset=" << dataset_;
nbands_ = dataset_->GetRasterCount();
width_ = dataset_->GetRasterXSize();
height_ = dataset_->GetRasterYSize();
desc_.add_descriptor(mapnik::attribute_descriptor("nodata", mapnik::Double)); desc_.add_descriptor(mapnik::attribute_descriptor("nodata", mapnik::Double));
double tr[6]; double tr[6];
@ -140,7 +128,7 @@ gdal_datasource::gdal_datasource(parameters const& params)
} }
else else
{ {
if (dataset->GetGeoTransform(tr) != CPLE_None) if (dataset_->GetGeoTransform(tr) != CPLE_None)
{ {
MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource GetGeotransform failure gives=" MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource GetGeotransform failure gives="
<< tr[0] << "," << tr[1] << "," << tr[0] << "," << tr[1] << ","
@ -187,8 +175,6 @@ gdal_datasource::gdal_datasource(parameters const& params)
extent_.init(x0, y0, x1, y1); extent_.init(x0, y0, x1, y1);
} }
GDALClose(dataset);
MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource: Raster Size=" << width_ << "," << height_; MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource: Raster Size=" << width_ << "," << height_;
MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource: Raster Extent=" << extent_; MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource: Raster Extent=" << extent_;
@ -196,6 +182,8 @@ gdal_datasource::gdal_datasource(parameters const& params)
gdal_datasource::~gdal_datasource() gdal_datasource::~gdal_datasource()
{ {
MAPNIK_LOG_ERROR(gdal) << "gdal_featureset: Closing Dataset=" << dataset_;
GDALClose(dataset_);
} }
datasource::datasource_t gdal_datasource::type() const datasource::datasource_t gdal_datasource::type() const
@ -232,7 +220,7 @@ featureset_ptr gdal_datasource::features(query const& q) const
gdal_query gq = q; gdal_query gq = q;
// TODO - move to std::make_shared, but must reduce # of args to <= 9 // TODO - move to std::make_shared, but must reduce # of args to <= 9
return featureset_ptr(new gdal_featureset(*open_dataset(), return featureset_ptr(new gdal_featureset(*dataset_,
band_, band_,
gq, gq,
extent_, extent_,
@ -254,7 +242,7 @@ featureset_ptr gdal_datasource::features_at_point(coord2d const& pt, double tol)
gdal_query gq = pt; gdal_query gq = pt;
// TODO - move to std::make_shared, but must reduce # of args to <= 9 // TODO - move to std::make_shared, but must reduce # of args to <= 9
return featureset_ptr(new gdal_featureset(*open_dataset(), return featureset_ptr(new gdal_featureset(*dataset_,
band_, band_,
gq, gq,
extent_, extent_,

View file

@ -56,6 +56,7 @@ public:
mapnik::layer_descriptor get_descriptor() const; mapnik::layer_descriptor get_descriptor() const;
private: private:
GDALDataset* open_dataset() const; GDALDataset* open_dataset() const;
GDALDataset* dataset_;
mapnik::box2d<double> extent_; mapnik::box2d<double> extent_;
std::string dataset_name_; std::string dataset_name_;
int band_; int band_;

View file

@ -80,7 +80,6 @@ gdal_featureset::~gdal_featureset()
{ {
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Closing Dataset=" << &dataset_; MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Closing Dataset=" << &dataset_;
GDALClose(&dataset_);
} }
feature_ptr gdal_featureset::next() feature_ptr gdal_featureset::next()