diff --git a/plugins/input/postgis/postgis_datasource.cpp b/plugins/input/postgis/postgis_datasource.cpp index 0c5f083e8..40a3e9097 100644 --- a/plugins/input/postgis/postgis_datasource.cpp +++ b/plugins/input/postgis/postgis_datasource.cpp @@ -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("table","")), + table_(*params.get("table","")), schema_(""), - geometry_table_(*params_.get("geometry_table","")), - geometry_field_(*params_.get("geometry_field","")), - key_field_(*params_.get("key_field","")), - cursor_fetch_size_(*params_.get("cursor_size",0)), - row_limit_(*params_.get("row_limit",0)), + geometry_table_(*params.get("geometry_table","")), + geometry_field_(*params.get("geometry_field","")), + key_field_(*params.get("key_field","")), + cursor_fetch_size_(*params.get("cursor_size",0)), + row_limit_(*params.get("row_limit",0)), type_(datasource::Vector), - srid_(*params_.get("srid",0)), + srid_(*params.get("srid",0)), extent_initialized_(false), - desc_(*params_.get("type"),"utf-8"), + desc_(*params.get("type"),"utf-8"), creator_(params.get("host"), params.get("port"), params.get("dbname"), @@ -75,30 +75,25 @@ postgis_datasource::postgis_datasource(parameters const& params, bool bind) params.get("connect_timeout","4")), bbox_token_("!bbox!"), scale_denom_token_("!scale_denominator!"), - persist_connection_(*params_.get("persist_connection",true)), - extent_from_subquery_(*params_.get("extent_from_subquery",false)), + persist_connection_(*params.get("persist_connection",true)), + extent_from_subquery_(*params.get("extent_from_subquery",false)), + estimate_extent_(*params.get("estimate_extent",false)), // params below are for testing purposes only (will likely be removed at any time) - intersect_min_scale_(*params_.get("intersect_min_scale",0)), - intersect_max_scale_(*params_.get("intersect_max_scale",0)) - //show_queries_(*params_.get("show_queries",false)) + intersect_min_scale_(*params.get("intersect_min_scale",0)), + intersect_max_scale_(*params.get("intersect_max_scale",0)) + //show_queries_(*params.get("show_queries",false)) { if (table_.empty()) throw mapnik::datasource_exception("Postgis Plugin: missing parameter"); - boost::optional ext = params_.get("extent"); + boost::optional ext = params.get("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 initial_size = params_.get("initial_size",1); - boost::optional max_size = params_.get("max_size",10); + boost::optional initial_size = params.get("initial_size",1); + boost::optional max_size = params.get("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 const& env) const { std::ostringstream b; @@ -464,8 +453,6 @@ boost::shared_ptr postgis_datasource::get_resultset(boost::shared_pt featureset_ptr postgis_datasource::features(const query& q) const { - if (!is_bound_) bind(); - box2d 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=mgr->getPool(creator_.id()); if (pool) @@ -607,8 +592,7 @@ featureset_ptr postgis_datasource::features_at_point(coord2d const& pt) const box2d postgis_datasource::envelope() const { if (extent_initialized_) return extent_; - if (!is_bound_) bind(); - + ConnectionManager *mgr=ConnectionManager::instance(); shared_ptr > pool=mgr->getPool(creator_.id()); if (pool) @@ -618,7 +602,6 @@ box2d postgis_datasource::envelope() const { PoolGuard,shared_ptr > > guard(conn,pool); std::ostringstream s; - boost::optional estimate_extent = params_.get("estimate_extent",false); if (!geometryColumn_.length() > 0) { @@ -635,7 +618,7 @@ box2d 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 postgis_datasource::envelope() const boost::optional postgis_datasource::get_geometry_type() const { - if (! is_bound_) bind(); boost::optional result; - + ConnectionManager *mgr=ConnectionManager::instance(); shared_ptr > pool=mgr->getPool(creator_.id()); if (pool) @@ -818,7 +800,7 @@ boost::optional postgis_datasource::get_geometry postgis_datasource::~postgis_datasource() { - if (is_bound_ && !persist_connection_) + if (!persist_connection_) { ConnectionManager *mgr=ConnectionManager::instance(); shared_ptr > pool=mgr->getPool(creator_.id()); diff --git a/plugins/input/postgis/postgis_datasource.hpp b/plugins/input/postgis/postgis_datasource.hpp index 846664de1..a168ad4e5 100644 --- a/plugins/input/postgis/postgis_datasource.hpp +++ b/plugins/input/postgis/postgis_datasource.hpp @@ -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 extent_; - mutable layer_descriptor desc_; + layer_descriptor desc_; ConnectionCreator 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 ¶ms); + ~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 envelope() const; boost::optional get_geometry_type() const; layer_descriptor get_descriptor() const; - postgis_datasource(const parameters ¶ms, bool bind=true); - ~postgis_datasource(); - void bind() const; + void init(parameters const& params); private: std::string sql_bbox(box2d const& env) const; std::string populate_tokens(const std::string& sql, double scale_denom, box2d const& env) const; diff --git a/plugins/input/shape/shape_datasource.cpp b/plugins/input/shape/shape_datasource.cpp index 10d5b3bcb..82dab0120 100644 --- a/plugins/input/shape/shape_datasource.cpp +++ b/plugins/input/shape/shape_datasource.cpp @@ -49,12 +49,12 @@ using mapnik::filter_in_box; using mapnik::filter_at_point; using mapnik::attribute_descriptor; -shape_datasource::shape_datasource(const parameters ¶ms, bool bind) +shape_datasource::shape_datasource(const parameters ¶ms) : datasource (params), type_(datasource::Vector), file_length_(0), indexed_(false), - row_limit_(*params_.get("row_limit",0)), + row_limit_(*params.get("row_limit",0)), desc_(*params.get("type"), *params.get("encoding","utf-8")) { boost::optional file = params.get("file"); @@ -67,17 +67,11 @@ shape_datasource::shape_datasource(const parameters ¶ms, 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_ref = boost::make_shared(shape_name_); - init(*shape_ref); + init_io(*shape_ref); for (int i=0;idbf().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 const& desc_vector = desc_.get_descriptors(); @@ -310,8 +298,6 @@ featureset_ptr shape_datasource::features_at_point(coord2d const& pt) const box2d shape_datasource::envelope() const { - if (!is_bound_) bind(); - return extent_; } diff --git a/plugins/input/shape/shape_datasource.hpp b/plugins/input/shape/shape_datasource.hpp index e8735fc6c..01c6d62ec 100644 --- a/plugins/input/shape/shape_datasource.hpp +++ b/plugins/input/shape/shape_datasource.hpp @@ -42,7 +42,7 @@ using mapnik::coord2d; class shape_datasource : public datasource { public: - shape_datasource(const parameters ¶ms, bool bind=true); + shape_datasource(const parameters ¶ms); virtual ~shape_datasource(); datasource::datasource_t type() const; @@ -52,19 +52,18 @@ public: box2d envelope() const; boost::optional 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_; - mutable shape_io::shapeType shape_type_; - mutable long file_length_; + boost::shared_ptr shape_; + shape_io::shapeType shape_type_; + long file_length_; mutable box2d extent_; - mutable bool indexed_; + bool indexed_; const int row_limit_; mutable layer_descriptor desc_; };