gdal: RAII + minor cleanup

This commit is contained in:
Mickey Rose 2016-03-03 01:48:07 +01:00
parent 489631ca34
commit ec2c5ddbdc
4 changed files with 17 additions and 29 deletions

View file

@ -53,7 +53,7 @@ static bool GDALAllRegister_once_()
gdal_datasource::gdal_datasource(parameters const& params) gdal_datasource::gdal_datasource(parameters const& params)
: datasource(params), : datasource(params),
dataset_(nullptr), dataset_(nullptr, &GDALClose),
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))
@ -85,12 +85,14 @@ gdal_datasource::gdal_datasource(parameters const& params)
#if GDAL_VERSION_NUM >= 1600 #if GDAL_VERSION_NUM >= 1600
if (shared_dataset_) if (shared_dataset_)
{ {
dataset_ = reinterpret_cast<GDALDataset*>(GDALOpenShared((dataset_name_).c_str(), GA_ReadOnly)); auto ds = GDALOpenShared(dataset_name_.c_str(), GA_ReadOnly);
dataset_.reset(static_cast<GDALDataset*>(ds));
} }
else else
#endif #endif
{ {
dataset_ = reinterpret_cast<GDALDataset*>(GDALOpen((dataset_name_).c_str(), GA_ReadOnly)); auto ds = GDALOpen(dataset_name_.c_str(), GA_ReadOnly);
dataset_.reset(static_cast<GDALDataset*>(ds));
} }
if (! dataset_) if (! dataset_)
@ -98,7 +100,7 @@ gdal_datasource::gdal_datasource(parameters const& params)
throw datasource_exception(CPLGetLastErrorMsg()); throw datasource_exception(CPLGetLastErrorMsg());
} }
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: opened Dataset=" << dataset_; MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: opened Dataset=" << dataset_.get();
nbands_ = dataset_->GetRasterCount(); nbands_ = dataset_->GetRasterCount();
width_ = dataset_->GetRasterXSize(); width_ = dataset_->GetRasterXSize();
@ -188,8 +190,7 @@ gdal_datasource::gdal_datasource(parameters const& params)
gdal_datasource::~gdal_datasource() gdal_datasource::~gdal_datasource()
{ {
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Closing Dataset=" << dataset_; MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Closing Dataset=" << dataset_.get();
GDALClose(dataset_);
} }
datasource::datasource_t gdal_datasource::type() const datasource::datasource_t gdal_datasource::type() const
@ -223,12 +224,9 @@ featureset_ptr gdal_datasource::features(query const& q) const
mapnik::progress_timer __stats__(std::clog, "gdal_datasource::features"); mapnik::progress_timer __stats__(std::clog, "gdal_datasource::features");
#endif #endif
gdal_query gq = q; return std::make_shared<gdal_featureset>(*dataset_,
// TODO - move to std::make_shared, but must reduce # of args to <= 9
return featureset_ptr(new gdal_featureset(*dataset_,
band_, band_,
gq, gdal_query(q),
extent_, extent_,
width_, width_,
height_, height_,
@ -236,7 +234,7 @@ featureset_ptr gdal_datasource::features(query const& q) const
dx_, dx_,
dy_, dy_,
nodata_value_, nodata_value_,
nodata_tolerance_)); nodata_tolerance_);
} }
featureset_ptr gdal_datasource::features_at_point(coord2d const& pt, double tol) const featureset_ptr gdal_datasource::features_at_point(coord2d const& pt, double tol) const
@ -245,12 +243,9 @@ featureset_ptr gdal_datasource::features_at_point(coord2d const& pt, double tol)
mapnik::progress_timer __stats__(std::clog, "gdal_datasource::features_at_point"); mapnik::progress_timer __stats__(std::clog, "gdal_datasource::features_at_point");
#endif #endif
gdal_query gq = pt; return std::make_shared<gdal_featureset>(*dataset_,
// TODO - move to std::make_shared, but must reduce # of args to <= 9
return featureset_ptr(new gdal_featureset(*dataset_,
band_, band_,
gq, gdal_query(pt),
extent_, extent_,
width_, width_,
height_, height_,
@ -258,5 +253,5 @@ featureset_ptr gdal_datasource::features_at_point(coord2d const& pt, double tol)
dx_, dx_,
dy_, dy_,
nodata_value_, nodata_value_,
nodata_tolerance_)); nodata_tolerance_);
} }

View file

@ -55,8 +55,7 @@ public:
boost::optional<mapnik::datasource_geometry_t> get_geometry_type() const; boost::optional<mapnik::datasource_geometry_t> get_geometry_type() const;
mapnik::layer_descriptor get_descriptor() const; mapnik::layer_descriptor get_descriptor() const;
private: private:
GDALDataset* open_dataset() const; std::unique_ptr<GDALDataset, decltype(&GDALClose)> dataset_;
GDALDataset* dataset_;
mapnik::box2d<double> extent_; mapnik::box2d<double> extent_;
std::string dataset_name_; std::string dataset_name_;
int band_; int band_;

View file

@ -21,7 +21,7 @@
*****************************************************************************/ *****************************************************************************/
// mapnik // mapnik
#include <mapnik/make_unique.hpp> #include <mapnik/datasource.hpp>
#include <mapnik/global.hpp> #include <mapnik/global.hpp>
#include <mapnik/debug.hpp> #include <mapnik/debug.hpp>
#include <mapnik/image.hpp> #include <mapnik/image.hpp>
@ -29,7 +29,6 @@
#include <mapnik/view_transform.hpp> #include <mapnik/view_transform.hpp>
#include <mapnik/feature.hpp> #include <mapnik/feature.hpp>
#include <mapnik/feature_factory.hpp> #include <mapnik/feature_factory.hpp>
#include <mapnik/util/variant.hpp>
// stl // stl
#include <cmath> #include <cmath>
@ -39,8 +38,6 @@
#include "gdal_featureset.hpp" #include "gdal_featureset.hpp"
#include <gdal_priv.h> #include <gdal_priv.h>
using mapnik::query;
using mapnik::coord2d;
using mapnik::box2d; using mapnik::box2d;
using mapnik::feature_ptr; using mapnik::feature_ptr;
using mapnik::view_transform; using mapnik::view_transform;
@ -77,8 +74,6 @@ gdal_featureset::gdal_featureset(GDALDataset& dataset,
gdal_featureset::~gdal_featureset() gdal_featureset::~gdal_featureset()
{ {
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Closing Dataset=" << &dataset_;
} }
feature_ptr gdal_featureset::next() feature_ptr gdal_featureset::next()

View file

@ -24,13 +24,12 @@
#define GDAL_FEATURESET_HPP #define GDAL_FEATURESET_HPP
// mapnik // mapnik
#include <mapnik/feature.hpp> #include <mapnik/featureset.hpp>
#include <mapnik/query.hpp>
#include <mapnik/util/variant.hpp> #include <mapnik/util/variant.hpp>
// boost // boost
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include "gdal_datasource.hpp"
class GDALDataset; class GDALDataset;
class GDALRasterBand; class GDALRasterBand;