+ remove bind() logic from shape and postgis plug-ins

This commit is contained in:
Artem Pavlenko 2012-03-05 14:09:19 +00:00
parent 9d24ed04f5
commit accd81192b
4 changed files with 49 additions and 79 deletions

View file

@ -54,19 +54,19 @@ using boost::shared_ptr;
using mapnik::PoolGuard;
using mapnik::attribute_descriptor;
postgis_datasource::postgis_datasource(parameters const& params, bool bind)
postgis_datasource::postgis_datasource(parameters const& params)
: datasource(params),
table_(*params_.get<std::string>("table","")),
table_(*params.get<std::string>("table","")),
schema_(""),
geometry_table_(*params_.get<std::string>("geometry_table","")),
geometry_field_(*params_.get<std::string>("geometry_field","")),
key_field_(*params_.get<std::string>("key_field","")),
cursor_fetch_size_(*params_.get<int>("cursor_size",0)),
row_limit_(*params_.get<int>("row_limit",0)),
geometry_table_(*params.get<std::string>("geometry_table","")),
geometry_field_(*params.get<std::string>("geometry_field","")),
key_field_(*params.get<std::string>("key_field","")),
cursor_fetch_size_(*params.get<int>("cursor_size",0)),
row_limit_(*params.get<int>("row_limit",0)),
type_(datasource::Vector),
srid_(*params_.get<int>("srid",0)),
srid_(*params.get<int>("srid",0)),
extent_initialized_(false),
desc_(*params_.get<std::string>("type"),"utf-8"),
desc_(*params.get<std::string>("type"),"utf-8"),
creator_(params.get<std::string>("host"),
params.get<std::string>("port"),
params.get<std::string>("dbname"),
@ -75,30 +75,25 @@ postgis_datasource::postgis_datasource(parameters const& params, bool bind)
params.get<std::string>("connect_timeout","4")),
bbox_token_("!bbox!"),
scale_denom_token_("!scale_denominator!"),
persist_connection_(*params_.get<mapnik::boolean>("persist_connection",true)),
extent_from_subquery_(*params_.get<mapnik::boolean>("extent_from_subquery",false)),
persist_connection_(*params.get<mapnik::boolean>("persist_connection",true)),
extent_from_subquery_(*params.get<mapnik::boolean>("extent_from_subquery",false)),
estimate_extent_(*params.get<mapnik::boolean>("estimate_extent",false)),
// params below are for testing purposes only (will likely be removed at any time)
intersect_min_scale_(*params_.get<int>("intersect_min_scale",0)),
intersect_max_scale_(*params_.get<int>("intersect_max_scale",0))
//show_queries_(*params_.get<mapnik::boolean>("show_queries",false))
intersect_min_scale_(*params.get<int>("intersect_min_scale",0)),
intersect_max_scale_(*params.get<int>("intersect_max_scale",0))
//show_queries_(*params.get<mapnik::boolean>("show_queries",false))
{
if (table_.empty()) throw mapnik::datasource_exception("Postgis Plugin: missing <table> parameter");
boost::optional<std::string> ext = params_.get<std::string>("extent");
boost::optional<std::string> ext = params.get<std::string>("extent");
if (ext) extent_initialized_ = extent_.from_string(*ext);
if (bind)
{
this->bind();
}
this->init(params);
}
void postgis_datasource::bind() const
void postgis_datasource::init(parameters const& params)
{
if (is_bound_) return;
boost::optional<int> initial_size = params_.get<int>("initial_size",1);
boost::optional<int> max_size = params_.get<int>("max_size",10);
boost::optional<int> initial_size = params.get<int>("initial_size",1);
boost::optional<int> max_size = params.get<int>("max_size",10);
ConnectionManager *mgr=ConnectionManager::instance();
mgr->registerPool(creator_, *initial_size, *max_size);
@ -327,11 +322,7 @@ void postgis_datasource::bind() const
}
}
}
rs->close();
is_bound_ = true;
}
}
}
@ -348,11 +339,9 @@ mapnik::datasource::datasource_t postgis_datasource::type() const
layer_descriptor postgis_datasource::get_descriptor() const
{
if (!is_bound_) bind();
return desc_;
}
std::string postgis_datasource::sql_bbox(box2d<double> const& env) const
{
std::ostringstream b;
@ -464,8 +453,6 @@ boost::shared_ptr<IResultSet> postgis_datasource::get_resultset(boost::shared_pt
featureset_ptr postgis_datasource::features(const query& q) const
{
if (!is_bound_) bind();
box2d<double> const& box = q.get_bbox();
double scale_denom = q.scale_denominator();
ConnectionManager *mgr=ConnectionManager::instance();
@ -538,8 +525,6 @@ featureset_ptr postgis_datasource::features(const query& q) const
featureset_ptr postgis_datasource::features_at_point(coord2d const& pt) const
{
if (!is_bound_) bind();
ConnectionManager *mgr=ConnectionManager::instance();
shared_ptr<Pool<Connection,ConnectionCreator> > pool=mgr->getPool(creator_.id());
if (pool)
@ -607,8 +592,7 @@ featureset_ptr postgis_datasource::features_at_point(coord2d const& pt) const
box2d<double> postgis_datasource::envelope() const
{
if (extent_initialized_) return extent_;
if (!is_bound_) bind();
ConnectionManager *mgr=ConnectionManager::instance();
shared_ptr<Pool<Connection,ConnectionCreator> > pool=mgr->getPool(creator_.id());
if (pool)
@ -618,7 +602,6 @@ box2d<double> postgis_datasource::envelope() const
{
PoolGuard<shared_ptr<Connection>,shared_ptr<Pool<Connection,ConnectionCreator> > > guard(conn,pool);
std::ostringstream s;
boost::optional<mapnik::boolean> estimate_extent = params_.get<mapnik::boolean>("estimate_extent",false);
if (!geometryColumn_.length() > 0)
{
@ -635,7 +618,7 @@ box2d<double> postgis_datasource::envelope() const
throw mapnik::datasource_exception("Postgis Plugin: " + s_error.str());
}
if (estimate_extent && *estimate_extent)
if (estimate_extent_)
{
s << "SELECT ST_XMin(ext),ST_YMin(ext),ST_XMax(ext),ST_YMax(ext)"
<< " FROM (SELECT ST_Estimated_Extent('";
@ -706,9 +689,8 @@ box2d<double> postgis_datasource::envelope() const
boost::optional<mapnik::datasource::geometry_t> postgis_datasource::get_geometry_type() const
{
if (! is_bound_) bind();
boost::optional<mapnik::datasource::geometry_t> result;
ConnectionManager *mgr=ConnectionManager::instance();
shared_ptr<Pool<Connection,ConnectionCreator> > pool=mgr->getPool(creator_.id());
if (pool)
@ -818,7 +800,7 @@ boost::optional<mapnik::datasource::geometry_t> postgis_datasource::get_geometry
postgis_datasource::~postgis_datasource()
{
if (is_bound_ && !persist_connection_)
if (!persist_connection_)
{
ConnectionManager *mgr=ConnectionManager::instance();
shared_ptr<Pool<Connection,ConnectionCreator> > pool=mgr->getPool(creator_.id());

View file

@ -55,28 +55,33 @@ class postgis_datasource : public datasource
const std::string username_;
const std::string password_;
const std::string table_;
mutable std::string schema_;
mutable std::string geometry_table_;
std::string schema_;
std::string geometry_table_;
const std::string geometry_field_;
const std::string key_field_;
const int cursor_fetch_size_;
const int row_limit_;
mutable std::string geometryColumn_;
std::string geometryColumn_;
mapnik::datasource::datasource_t type_;
mutable int srid_;
int srid_;
mutable bool extent_initialized_;
mutable mapnik::box2d<double> extent_;
mutable layer_descriptor desc_;
layer_descriptor desc_;
ConnectionCreator<Connection> creator_;
const std::string bbox_token_;
const std::string scale_denom_token_;
bool persist_connection_;
bool extent_from_subquery_;
bool estimate_extent_;
// params below are for testing purposes only (will likely be removed at any time)
int intersect_min_scale_;
int intersect_max_scale_;
//bool show_queries_;
public:
// ctor/dtor
postgis_datasource(const parameters &params);
~postgis_datasource();
static std::string name();
mapnik::datasource::datasource_t type() const;
featureset_ptr features(const query& q) const;
@ -84,9 +89,7 @@ public:
mapnik::box2d<double> envelope() const;
boost::optional<mapnik::datasource::geometry_t> get_geometry_type() const;
layer_descriptor get_descriptor() const;
postgis_datasource(const parameters &params, bool bind=true);
~postgis_datasource();
void bind() const;
void init(parameters const& params);
private:
std::string sql_bbox(box2d<double> const& env) const;
std::string populate_tokens(const std::string& sql, double scale_denom, box2d<double> const& env) const;

View file

@ -49,12 +49,12 @@ using mapnik::filter_in_box;
using mapnik::filter_at_point;
using mapnik::attribute_descriptor;
shape_datasource::shape_datasource(const parameters &params, bool bind)
shape_datasource::shape_datasource(const parameters &params)
: datasource (params),
type_(datasource::Vector),
file_length_(0),
indexed_(false),
row_limit_(*params_.get<int>("row_limit",0)),
row_limit_(*params.get<int>("row_limit",0)),
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8"))
{
boost::optional<std::string> file = params.get<std::string>("file");
@ -67,17 +67,11 @@ shape_datasource::shape_datasource(const parameters &params, bool bind)
shape_name_ = *file;
boost::algorithm::ireplace_last(shape_name_,".shp","");
if (bind)
{
this->bind();
}
this->init(params);
}
void shape_datasource::bind() const
void shape_datasource::init(parameters const& p)
{
if (is_bound_) return;
if (!boost::filesystem::exists(shape_name_ + ".shp"))
{
throw datasource_exception("Shape Plugin: shapefile '" + shape_name_ + ".shp' does not exist");
@ -97,7 +91,7 @@ void shape_datasource::bind() const
try
{
boost::shared_ptr<shape_io> shape_ref = boost::make_shared<shape_io>(shape_name_);
init(*shape_ref);
init_io(*shape_ref);
for (int i=0;i<shape_ref->dbf().num_fields();++i)
{
field_descriptor const& fd=shape_ref->dbf().descriptor(i);
@ -157,12 +151,11 @@ void shape_datasource::bind() const
throw;
}
is_bound_ = true;
}
shape_datasource::~shape_datasource() {}
void shape_datasource::init(shape_io& shape) const
void shape_datasource::init_io(shape_io& shape)
{
//first read header from *.shp
int file_code=shape.shp().read_xdr_integer();
@ -236,14 +229,11 @@ datasource::datasource_t shape_datasource::type() const
layer_descriptor shape_datasource::get_descriptor() const
{
if (!is_bound_) bind();
return desc_;
}
featureset_ptr shape_datasource::features(const query& q) const
{
if (!is_bound_) bind();
filter_in_box filter(q.get_bbox());
if (indexed_)
{
@ -270,8 +260,6 @@ featureset_ptr shape_datasource::features(const query& q) const
featureset_ptr shape_datasource::features_at_point(coord2d const& pt) const
{
if (!is_bound_) bind();
filter_at_point filter(pt);
// collect all attribute names
std::vector<attribute_descriptor> const& desc_vector = desc_.get_descriptors();
@ -310,8 +298,6 @@ featureset_ptr shape_datasource::features_at_point(coord2d const& pt) const
box2d<double> shape_datasource::envelope() const
{
if (!is_bound_) bind();
return extent_;
}

View file

@ -42,7 +42,7 @@ using mapnik::coord2d;
class shape_datasource : public datasource
{
public:
shape_datasource(const parameters &params, bool bind=true);
shape_datasource(const parameters &params);
virtual ~shape_datasource();
datasource::datasource_t type() const;
@ -52,19 +52,18 @@ public:
box2d<double> envelope() const;
boost::optional<mapnik::datasource::geometry_t> get_geometry_type() const;
layer_descriptor get_descriptor() const;
void bind() const;
private:
shape_datasource(const shape_datasource&);
shape_datasource& operator=(const shape_datasource&);
void init(shape_io& shape) const;
void init(parameters const& params);
void init_io(shape_io& shape);
private:
datasource::datasource_t type_;
std::string shape_name_;
mutable boost::shared_ptr<shape_io> shape_;
mutable shape_io::shapeType shape_type_;
mutable long file_length_;
boost::shared_ptr<shape_io> shape_;
shape_io::shapeType shape_type_;
long file_length_;
mutable box2d<double> extent_;
mutable bool indexed_;
bool indexed_;
const int row_limit_;
mutable layer_descriptor desc_;
};