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)
: datasource(params),
dataset_(nullptr),
dataset_(nullptr, &GDALClose),
desc_(gdal_datasource::name(), "utf-8"),
nodata_value_(params.get<double>("nodata")),
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 (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
#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_)
@ -98,7 +100,7 @@ gdal_datasource::gdal_datasource(parameters const& params)
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();
width_ = dataset_->GetRasterXSize();
@ -188,8 +190,7 @@ gdal_datasource::gdal_datasource(parameters const& params)
gdal_datasource::~gdal_datasource()
{
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Closing Dataset=" << dataset_;
GDALClose(dataset_);
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Closing Dataset=" << dataset_.get();
}
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");
#endif
gdal_query gq = q;
// TODO - move to std::make_shared, but must reduce # of args to <= 9
return featureset_ptr(new gdal_featureset(*dataset_,
return std::make_shared<gdal_featureset>(*dataset_,
band_,
gq,
gdal_query(q),
extent_,
width_,
height_,
@ -236,7 +234,7 @@ featureset_ptr gdal_datasource::features(query const& q) const
dx_,
dy_,
nodata_value_,
nodata_tolerance_));
nodata_tolerance_);
}
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");
#endif
gdal_query gq = pt;
// TODO - move to std::make_shared, but must reduce # of args to <= 9
return featureset_ptr(new gdal_featureset(*dataset_,
return std::make_shared<gdal_featureset>(*dataset_,
band_,
gq,
gdal_query(pt),
extent_,
width_,
height_,
@ -258,5 +253,5 @@ featureset_ptr gdal_datasource::features_at_point(coord2d const& pt, double tol)
dx_,
dy_,
nodata_value_,
nodata_tolerance_));
nodata_tolerance_);
}

View file

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

View file

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

View file

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