diff --git a/plugins/input/csv/csv_datasource.cpp b/plugins/input/csv/csv_datasource.cpp index b2848509f..2fca63198 100644 --- a/plugins/input/csv/csv_datasource.cpp +++ b/plugins/input/csv/csv_datasource.cpp @@ -29,23 +29,23 @@ using namespace boost::spirit; DATASOURCE_PLUGIN(csv_datasource) -csv_datasource::csv_datasource(parameters const& params, bool bind) +csv_datasource::csv_datasource(parameters const& params) : datasource(params), - desc_(*params_.get("type"), *params_.get("encoding", "utf-8")), + desc_(*params.get("type"), *params.get("encoding", "utf-8")), extent_(), filename_(), inline_string_(), file_length_(0), - row_limit_(*params_.get("row_limit", 0)), + row_limit_(*params.get("row_limit", 0)), features_(), - escape_(*params_.get("escape", "")), - separator_(*params_.get("separator", "")), - quote_(*params_.get("quote", "")), + escape_(*params.get("escape", "")), + separator_(*params.get("separator", "")), + quote_(*params.get("quote", "")), headers_(), - manual_headers_(boost::trim_copy(*params_.get("headers", ""))), - strict_(*params_.get("strict", false)), - quiet_(*params_.get("quiet", false)), - filesize_max_(*params_.get("filesize_max", 20.0)) // MB + manual_headers_(boost::trim_copy(*params.get("headers", ""))), + strict_(*params.get("strict", false)), + quiet_(*params.get("quiet", false)), + filesize_max_(*params.get("filesize_max", 20.0)) // MB { /* TODO: general: @@ -70,36 +70,31 @@ csv_datasource::csv_datasource(parameters const& params, bool bind) http://boost-spirit.com/home/articles/qi-example/tracking-the-input-position-while-parsing/ */ - boost::optional inline_string = params_.get("inline"); + boost::optional inline_string = params.get("inline"); if (inline_string) { inline_string_ = *inline_string; } else { - boost::optional file = params_.get("file"); + boost::optional file = params.get("file"); if (!file) throw mapnik::datasource_exception("CSV Plugin: missing parameter"); - boost::optional base = params_.get("base"); + boost::optional base = params.get("base"); if (base) filename_ = *base + "/" + *file; else filename_ = *file; } - if (bind) - { - this->bind(); - } + this->init(params); } csv_datasource::~csv_datasource() { } -void csv_datasource::bind() const +void csv_datasource::init(mapnik::parameters const& params) { - if (is_bound_) return; - if (!inline_string_.empty()) { std::istringstream in(inline_string_); @@ -113,7 +108,6 @@ void csv_datasource::bind() const parse_csv(in,escape_, separator_, quote_); in.close(); } - is_bound_ = true; } template @@ -245,7 +239,7 @@ void csv_datasource::parse_csv(T& stream, // grammer = boost::escaped_list_separator('\\', ',', '\"'); grammer = boost::escaped_list_separator(esc, sep, quo); } - catch(const std::exception & ex) + catch(std::exception const& ex) { std::ostringstream s; s << "CSV Plugin: " << ex.what(); @@ -371,7 +365,7 @@ void csv_datasource::parse_csv(T& stream, break; } } - catch(const std::exception & ex) + catch(std::exception const& ex) { std::ostringstream s; s << "CSV Plugin: error parsing headers: " << ex.what(); @@ -812,7 +806,7 @@ void csv_datasource::parse_csv(T& stream, if (!quiet_) std::clog << ex.what() << "\n"; } } - catch(const std::exception & ex) + catch(std::exception const& ex) { std::ostringstream s; s << "CSV Plugin: unexpected error parsing line: " << line_number @@ -846,14 +840,11 @@ datasource::datasource_t csv_datasource::type() const mapnik::box2d csv_datasource::envelope() const { - if (!is_bound_) bind(); - return extent_; } boost::optional csv_datasource::get_geometry_type() const { - if (! is_bound_) bind(); boost::optional result; int multi_type = 0; unsigned num_features = features_.size(); @@ -876,15 +867,11 @@ boost::optional csv_datasource::get_geometry_typ mapnik::layer_descriptor csv_datasource::get_descriptor() const { - if (!is_bound_) bind(); - return desc_; } mapnik::featureset_ptr csv_datasource::features(mapnik::query const& q) const { - if (!is_bound_) bind(); - const std::set& attribute_names = q.property_names(); std::set::const_iterator pos = attribute_names.begin(); while (pos != attribute_names.end()) @@ -916,7 +903,5 @@ mapnik::featureset_ptr csv_datasource::features(mapnik::query const& q) const mapnik::featureset_ptr csv_datasource::features_at_point(mapnik::coord2d const& pt) const { - if (!is_bound_) bind(); - throw mapnik::datasource_exception("CSV Plugin: features_at_point is not supported yet"); } diff --git a/plugins/input/csv/csv_datasource.hpp b/plugins/input/csv/csv_datasource.hpp index 985d73242..5253c5a5c 100644 --- a/plugins/input/csv/csv_datasource.hpp +++ b/plugins/input/csv/csv_datasource.hpp @@ -10,7 +10,7 @@ class csv_datasource : public mapnik::datasource { public: - csv_datasource(mapnik::parameters const& params, bool bind=true); + csv_datasource(mapnik::parameters const& params); virtual ~csv_datasource (); mapnik::datasource::datasource_t type() const; static std::string name(); @@ -19,13 +19,13 @@ public: mapnik::box2d envelope() const; boost::optional get_geometry_type() const; mapnik::layer_descriptor get_descriptor() const; - void bind() const; template void parse_csv(T& stream, std::string const& escape, std::string const& separator, std::string const& quote) const; private: + void init(mapnik::parameters const& params); mutable mapnik::layer_descriptor desc_; mutable mapnik::box2d extent_; mutable std::string filename_; diff --git a/plugins/input/kismet/kismet_datasource.cpp b/plugins/input/kismet/kismet_datasource.cpp index 7780b078d..899690a3a 100644 --- a/plugins/input/kismet/kismet_datasource.cpp +++ b/plugins/input/kismet/kismet_datasource.cpp @@ -71,7 +71,7 @@ boost::mutex knd_list_mutex; std::list knd_list; const unsigned int queue_size = 20; -kismet_datasource::kismet_datasource(parameters const& params, bool bind) +kismet_datasource::kismet_datasource(parameters const& params) : datasource(params), extent_(), extent_initialized_(false), @@ -79,7 +79,7 @@ kismet_datasource::kismet_datasource(parameters const& params, bool bind) srs_("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"), desc_(*params.get("type"), *params.get("encoding","utf-8")) { - boost::optional host = params_.get("host"); + boost::optional host = params.get("host"); if (host) { host_ = *host; @@ -89,37 +89,25 @@ kismet_datasource::kismet_datasource(parameters const& params, bool bind) throw datasource_exception("Kismet Plugin: missing parameter"); } - boost::optional port = params_.get("port", 2501); + boost::optional port = params.get("port", 2501); if (port) { port_ = *port; } - boost::optional srs = params_.get("srs"); + boost::optional srs = params.get("srs"); if (srs) { srs_ = *srs; } - boost::optional ext = params_.get("extent"); + boost::optional ext = params.get("extent"); if (ext) { extent_initialized_ = extent_.from_string(*ext); } kismet_thread.reset(new boost::thread(boost::bind(&kismet_datasource::run, this, host_, port_))); - - if (bind) - { - this->bind(); - } -} - -void kismet_datasource::bind() const -{ - if (is_bound_) return; - - is_bound_ = true; } kismet_datasource::~kismet_datasource() @@ -138,7 +126,6 @@ mapnik::datasource::datasource_t kismet_datasource::type() const box2d kismet_datasource::envelope() const { - if (! is_bound_) bind(); return extent_; } @@ -154,7 +141,6 @@ layer_descriptor kismet_datasource::get_descriptor() const featureset_ptr kismet_datasource::features(query const& q) const { - if (! is_bound_) bind(); // std::clog << "kismet_datasource::features()" << endl; @@ -172,7 +158,6 @@ featureset_ptr kismet_datasource::features(query const& q) const featureset_ptr kismet_datasource::features_at_point(coord2d const& pt) const { - if (! is_bound_) bind(); // std::clog << "kismet_datasource::features_at_point()" << endl; diff --git a/plugins/input/kismet/kismet_datasource.hpp b/plugins/input/kismet/kismet_datasource.hpp index 308246dde..af28e6540 100644 --- a/plugins/input/kismet/kismet_datasource.hpp +++ b/plugins/input/kismet/kismet_datasource.hpp @@ -43,7 +43,7 @@ class kismet_datasource : public mapnik::datasource { public: - kismet_datasource(mapnik::parameters const& params, bool bind = true); + kismet_datasource(mapnik::parameters const& params); virtual ~kismet_datasource (); datasource::datasource_t type() const; static std::string name(); @@ -52,11 +52,10 @@ public: mapnik::box2d envelope() const; boost::optional get_geometry_type() const; mapnik::layer_descriptor get_descriptor() const; - void bind() const; private: + void init(mapnik::parameters const& params); void run (const std::string& host, const unsigned int port); - mapnik::box2d extent_; bool extent_initialized_; std::string host_; diff --git a/plugins/input/occi/occi_datasource.cpp b/plugins/input/occi/occi_datasource.cpp index b99698d22..0354f8f39 100644 --- a/plugins/input/occi/occi_datasource.cpp +++ b/plugins/input/occi/occi_datasource.cpp @@ -67,24 +67,25 @@ const std::string occi_datasource::METADATA_TABLE = "USER_SDO_GEOM_METADATA"; DATASOURCE_PLUGIN(occi_datasource) -occi_datasource::occi_datasource(parameters const& params, bool bind) +occi_datasource::occi_datasource(parameters const& params) : datasource (params), + params_(params), type_(datasource::Vector), - fields_(*params_.get("fields", "*")), - geometry_field_(*params_.get("geometry_field", "")), + fields_(*params.get("fields", "*")), + geometry_field_(*params.get("geometry_field", "")), srid_initialized_(false), extent_initialized_(false), - desc_(*params_.get("type"), *params_.get("encoding", "utf-8")), - row_limit_(*params_.get("row_limit", 0)), - row_prefetch_(*params_.get("row_prefetch", 100)), + desc_(*params.get("type"), *params.get("encoding", "utf-8")), + row_limit_(*params.get("row_limit", 0)), + row_prefetch_(*params.get("row_prefetch", 100)), pool_(0), conn_(0) { - if (! params_.get("user")) throw datasource_exception("OCCI Plugin: no specified"); - if (! params_.get("password")) throw datasource_exception("OCCI Plugin: no specified"); - if (! params_.get("host")) throw datasource_exception("OCCI Plugin: no string specified"); + if (! params.get("user")) throw datasource_exception("OCCI Plugin: no specified"); + if (! params.get("password")) throw datasource_exception("OCCI Plugin: no specified"); + if (! params.get("host")) throw datasource_exception("OCCI Plugin: no string specified"); - boost::optional table = params_.get("table"); + boost::optional table = params.get("table"); if (! table) { throw datasource_exception("OCCI Plugin: no parameter specified"); @@ -94,52 +95,44 @@ occi_datasource::occi_datasource(parameters const& params, bool bind) table_ = *table; } - use_spatial_index_ = *params_.get("use_spatial_index",true); - use_connection_pool_ = *params_.get("use_connection_pool",true); + use_spatial_index_ = *params.get("use_spatial_index",true); + use_connection_pool_ = *params.get("use_connection_pool",true); - boost::optional ext = params_.get("extent"); + boost::optional ext = params.get("extent"); if (ext) extent_initialized_ = extent_.from_string(*ext); - boost::optional srid = params_.get("srid"); + boost::optional srid = params.get("srid"); if (srid) { srid_ = *srid; srid_initialized_ = true; } - if (bind) - { - this->bind(); - } + this->init(params); } occi_datasource::~occi_datasource() { - if (is_bound_) - { - Environment* env = occi_environment::get_environment(); + Environment* env = occi_environment::get_environment(); - if (use_connection_pool_) + if (use_connection_pool_) + { + if (pool_ != 0) { - if (pool_ != 0) - { - env->terminateStatelessConnectionPool(pool_, StatelessConnectionPool::SPD_FORCE); - } + env->terminateStatelessConnectionPool(pool_, StatelessConnectionPool::SPD_FORCE); } - else + } + else + { + if (conn_ != 0) { - if (conn_ != 0) - { - env->terminateConnection(conn_); - } + env->terminateConnection(conn_); } } } -void occi_datasource::bind() const +void occi_datasource::init(mapnik::parameters const& params) { - if (is_bound_) return; - // connect to environment if (use_connection_pool_) { @@ -148,11 +141,11 @@ void occi_datasource::bind() const Environment* env = occi_environment::get_environment(); pool_ = env->createStatelessConnectionPool( - *params_.get("user"), - *params_.get("password"), - *params_.get("host"), - *params_.get("max_size", 10), - *params_.get("initial_size", 1), + *params.get("user"), + *params.get("password"), + *params.get("host"), + *params.get("max_size", 10), + *params.get("initial_size", 1), 1, StatelessConnectionPool::HOMOGENEOUS); } @@ -168,9 +161,9 @@ void occi_datasource::bind() const Environment* env = occi_environment::get_environment(); conn_ = env->createConnection( - *params_.get("user"), - *params_.get("password"), - *params_.get("host")); + *params.get("user"), + *params.get("password"), + *params.get("host")); } catch (SQLException& ex) { @@ -342,8 +335,6 @@ void occi_datasource::bind() const throw datasource_exception(ex.getMessage()); } } - - is_bound_ = true; } std::string occi_datasource::name() @@ -359,7 +350,6 @@ mapnik::datasource::datasource_t occi_datasource::type() const box2d occi_datasource::envelope() const { if (extent_initialized_) return extent_; - if (! is_bound_) bind(); double lox = 0.0, loy = 0.0, hix = 0.0, hiy = 0.0; @@ -478,20 +468,17 @@ box2d occi_datasource::envelope() const boost::optional occi_datasource::get_geometry_type() const { // FIXME - //if (! is_bound_) bind(); return boost::optional(); } layer_descriptor occi_datasource::get_descriptor() const { - if (! is_bound_) bind(); return desc_; } featureset_ptr occi_datasource::features(query const& q) const { - if (! is_bound_) bind(); box2d const& box = q.get_bbox(); @@ -540,15 +527,18 @@ featureset_ptr occi_datasource::features(query const& q) const if (row_limit_ > 0) { - std::string row_limit_string = "rownum < " + row_limit_; + std::ostringstream row_limit_string; + + row_limit_string << "rownum < " << row_limit_; if (boost::algorithm::ifind_first(query, "WHERE")) { - boost::algorithm::ireplace_first(query, "WHERE", row_limit_string + " AND "); + row_limit_string << " AND "; + boost::algorithm::ireplace_first(query, "WHERE", row_limit_string.str()); } else if (boost::algorithm::ifind_first(query, table_name_)) { - boost::algorithm::ireplace_first(query, table_name_, table_name_ + " " + row_limit_string); + boost::algorithm::ireplace_first(query, table_name_, table_name_ + " " + row_limit_string.str()); } else { @@ -575,7 +565,6 @@ featureset_ptr occi_datasource::features(query const& q) const featureset_ptr occi_datasource::features_at_point(coord2d const& pt) const { - if (! is_bound_) bind(); std::ostringstream s; s << "SELECT " << geometry_field_; @@ -621,15 +610,18 @@ featureset_ptr occi_datasource::features_at_point(coord2d const& pt) const if (row_limit_ > 0) { - std::string row_limit_string = "rownum < " + row_limit_; + std::ostringstream row_limit_string; + + row_limit_string << "rownum < " << row_limit_; if (boost::algorithm::ifind_first(query, "WHERE")) { - boost::algorithm::ireplace_first(query, "WHERE", row_limit_string + " AND "); + row_limit_string << " AND "; + boost::algorithm::ireplace_first(query, "WHERE", row_limit_string.str()); } else if (boost::algorithm::ifind_first(query, table_name_)) { - boost::algorithm::ireplace_first(query, table_name_, table_name_ + " " + row_limit_string); + boost::algorithm::ireplace_first(query, table_name_, table_name_ + " " + row_limit_string.str()); } else { diff --git a/plugins/input/occi/occi_datasource.hpp b/plugins/input/occi/occi_datasource.hpp index ed235305c..0c78890c7 100644 --- a/plugins/input/occi/occi_datasource.hpp +++ b/plugins/input/occi/occi_datasource.hpp @@ -38,7 +38,7 @@ class occi_datasource : public mapnik::datasource { public: - occi_datasource(mapnik::parameters const& params, bool bind = true); + occi_datasource(mapnik::parameters const& params); virtual ~occi_datasource (); mapnik::datasource::datasource_t type() const; static std::string name(); @@ -47,9 +47,10 @@ public: mapnik::box2d envelope() const; boost::optional get_geometry_type() const; mapnik::layer_descriptor get_descriptor() const; - void bind() const; private: + void init(mapnik::parameters const& params); + mapnik::parameters const& params_; mapnik::datasource::datasource_t type_; mutable std::string table_; mutable std::string table_name_; diff --git a/plugins/input/postgis/postgis_featureset.cpp b/plugins/input/postgis/postgis_featureset.cpp index f4bbfe4b2..2518931f4 100644 --- a/plugins/input/postgis/postgis_featureset.cpp +++ b/plugins/input/postgis/postgis_featureset.cpp @@ -72,7 +72,7 @@ feature_ptr postgis_featureset::next() int oid = rs_->getTypeOID(pos); const char* buf = rs_->getValue(pos); std::string name = rs_->getFieldName(pos); - // validation happens of this type at bind() + // validation happens of this type at init() int val; if (oid == 20) { diff --git a/plugins/input/rasterlite/rasterlite_datasource.cpp b/plugins/input/rasterlite/rasterlite_datasource.cpp index e9cd0bf69..043b84b98 100644 --- a/plugins/input/rasterlite/rasterlite_datasource.cpp +++ b/plugins/input/rasterlite/rasterlite_datasource.cpp @@ -70,7 +70,7 @@ inline void *rasterlite_datasource::open_dataset() const -rasterlite_datasource::rasterlite_datasource(parameters const& params, bool bind) +rasterlite_datasource::rasterlite_datasource(parameters const& params) : datasource(params), desc_(*params.get("type"),"utf-8") { @@ -92,16 +92,11 @@ rasterlite_datasource::rasterlite_datasource(parameters const& params, bool bind else dataset_name_ = *file; - if (bind) - { - this->bind(); - } + this->init(params); } -void rasterlite_datasource::bind() const +void rasterlite_datasource::init(mapnik::parameters const& params) { - if (is_bound_) return; - if (!boost::filesystem::exists(dataset_name_)) throw datasource_exception(dataset_name_ + " does not exist"); void *dataset = open_dataset(); @@ -157,8 +152,6 @@ void rasterlite_datasource::bind() const #endif rasterliteClose(dataset); - - is_bound_ = true; } rasterlite_datasource::~rasterlite_datasource() @@ -177,8 +170,6 @@ mapnik::datasource::datasource_t rasterlite_datasource::type() const box2d rasterlite_datasource::envelope() const { - if (!is_bound_) bind(); - return extent_; } @@ -194,16 +185,12 @@ layer_descriptor rasterlite_datasource::get_descriptor() const featureset_ptr rasterlite_datasource::features(query const& q) const { - if (!is_bound_) bind(); - rasterlite_query gq = q; return boost::make_shared(open_dataset(), gq); } featureset_ptr rasterlite_datasource::features_at_point(coord2d const& pt) const { - if (!is_bound_) bind(); - rasterlite_query gq = pt; return boost::make_shared(open_dataset(), gq); } diff --git a/plugins/input/rasterlite/rasterlite_datasource.hpp b/plugins/input/rasterlite/rasterlite_datasource.hpp index 6c7d56072..85f9c4c8c 100644 --- a/plugins/input/rasterlite/rasterlite_datasource.hpp +++ b/plugins/input/rasterlite/rasterlite_datasource.hpp @@ -34,7 +34,7 @@ class rasterlite_datasource : public mapnik::datasource { public: - rasterlite_datasource(mapnik::parameters const& params, bool bind = true); + rasterlite_datasource(mapnik::parameters const& params); virtual ~rasterlite_datasource (); mapnik::datasource::datasource_t type() const; static std::string name(); @@ -43,9 +43,9 @@ public: mapnik::box2d envelope() const; boost::optional get_geometry_type() const; mapnik::layer_descriptor get_descriptor() const; - void bind() const; private: + void init(mapnik::parameters const& params); inline void* open_dataset() const; mutable mapnik::box2d extent_; std::string dataset_name_; diff --git a/plugins/input/templates/helloworld/hello_datasource.cpp b/plugins/input/templates/helloworld/hello_datasource.cpp index 8bcd2283c..08dca10bd 100644 --- a/plugins/input/templates/helloworld/hello_datasource.cpp +++ b/plugins/input/templates/helloworld/hello_datasource.cpp @@ -11,29 +11,22 @@ using mapnik::parameters; DATASOURCE_PLUGIN(hello_datasource) -hello_datasource::hello_datasource(parameters const& params, bool bind) +hello_datasource::hello_datasource(parameters const& params) : datasource(params), - desc_(*params_.get("type"), *params_.get("encoding","utf-8")), + desc_(*params.get("type"), *params.get("encoding","utf-8")), extent_() { - if (bind) - { - this->bind(); - } + this->init(params); } -void hello_datasource::bind() const +void hello_datasource::init(mapnik::parameters const& params) { - if (is_bound_) return; - // every datasource must have some way of reporting its extent // in this case we are not actually reading from any data so for fun // let's just create a world extent in Mapnik's default srs: // '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' (equivalent to +init=epsg:4326) // see http://spatialreference.org/ref/epsg/4326/ for more details extent_.init(-180,-90,180,90); - - is_bound_ = true; } hello_datasource::~hello_datasource() { } @@ -53,8 +46,6 @@ mapnik::datasource::datasource_t hello_datasource::type() const mapnik::box2d hello_datasource::envelope() const { - if (!is_bound_) bind(); - return extent_; } @@ -65,15 +56,11 @@ boost::optional hello_datasource::get_geometry_t mapnik::layer_descriptor hello_datasource::get_descriptor() const { - if (!is_bound_) bind(); - return desc_; } mapnik::featureset_ptr hello_datasource::features(mapnik::query const& q) const { - if (!is_bound_) bind(); - // if the query box intersects our world extent then query for features if (extent_.intersects(q.get_bbox())) { @@ -86,8 +73,6 @@ mapnik::featureset_ptr hello_datasource::features(mapnik::query const& q) const mapnik::featureset_ptr hello_datasource::features_at_point(mapnik::coord2d const& pt) const { - if (!is_bound_) bind(); - // features_at_point is rarely used - only by custom applications, // so for this sample plugin let's do nothing... return mapnik::featureset_ptr(); diff --git a/plugins/input/templates/helloworld/hello_datasource.hpp b/plugins/input/templates/helloworld/hello_datasource.hpp index a2c136246..f6cd2b50c 100644 --- a/plugins/input/templates/helloworld/hello_datasource.hpp +++ b/plugins/input/templates/helloworld/hello_datasource.hpp @@ -9,7 +9,7 @@ class hello_datasource : public mapnik::datasource public: // constructor // arguments must not change - hello_datasource(mapnik::parameters const& params, bool bind=true); + hello_datasource(mapnik::parameters const& params); // destructor virtual ~hello_datasource (); @@ -38,10 +38,10 @@ public: // mandatory: return the layer descriptor mapnik::layer_descriptor get_descriptor() const; - // mandatory: will bind the datasource given params - void bind() const; - private: + // recommended - do intialization in a so-named init function + // to reduce code in constructor + void init(mapnik::parameters const& params); // recommended naming convention of datasource members: // name_, type_, extent_, and desc_ static const std::string name_;