update rasterlite, occi, csv, hello_world, and rundemo

This commit is contained in:
Dane Springmeyer 2012-03-08 22:31:43 -08:00 committed by Artem Pavlenko
parent 514ec14cc9
commit ab7f6ee75c
11 changed files with 91 additions and 157 deletions

View file

@ -29,23 +29,23 @@ using namespace boost::spirit;
DATASOURCE_PLUGIN(csv_datasource) DATASOURCE_PLUGIN(csv_datasource)
csv_datasource::csv_datasource(parameters const& params, bool bind) csv_datasource::csv_datasource(parameters const& params)
: datasource(params), : datasource(params),
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding", "utf-8")), desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding", "utf-8")),
extent_(), extent_(),
filename_(), filename_(),
inline_string_(), inline_string_(),
file_length_(0), file_length_(0),
row_limit_(*params_.get<int>("row_limit", 0)), row_limit_(*params.get<int>("row_limit", 0)),
features_(), features_(),
escape_(*params_.get<std::string>("escape", "")), escape_(*params.get<std::string>("escape", "")),
separator_(*params_.get<std::string>("separator", "")), separator_(*params.get<std::string>("separator", "")),
quote_(*params_.get<std::string>("quote", "")), quote_(*params.get<std::string>("quote", "")),
headers_(), headers_(),
manual_headers_(boost::trim_copy(*params_.get<std::string>("headers", ""))), manual_headers_(boost::trim_copy(*params.get<std::string>("headers", ""))),
strict_(*params_.get<mapnik::boolean>("strict", false)), strict_(*params.get<mapnik::boolean>("strict", false)),
quiet_(*params_.get<mapnik::boolean>("quiet", false)), quiet_(*params.get<mapnik::boolean>("quiet", false)),
filesize_max_(*params_.get<float>("filesize_max", 20.0)) // MB filesize_max_(*params.get<float>("filesize_max", 20.0)) // MB
{ {
/* TODO: /* TODO:
general: 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/ http://boost-spirit.com/home/articles/qi-example/tracking-the-input-position-while-parsing/
*/ */
boost::optional<std::string> inline_string = params_.get<std::string>("inline"); boost::optional<std::string> inline_string = params.get<std::string>("inline");
if (inline_string) if (inline_string)
{ {
inline_string_ = *inline_string; inline_string_ = *inline_string;
} }
else else
{ {
boost::optional<std::string> file = params_.get<std::string>("file"); boost::optional<std::string> file = params.get<std::string>("file");
if (!file) throw mapnik::datasource_exception("CSV Plugin: missing <file> parameter"); if (!file) throw mapnik::datasource_exception("CSV Plugin: missing <file> parameter");
boost::optional<std::string> base = params_.get<std::string>("base"); boost::optional<std::string> base = params.get<std::string>("base");
if (base) if (base)
filename_ = *base + "/" + *file; filename_ = *base + "/" + *file;
else else
filename_ = *file; filename_ = *file;
} }
if (bind) this->init(params);
{
this->bind();
}
} }
csv_datasource::~csv_datasource() { } 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()) if (!inline_string_.empty())
{ {
std::istringstream in(inline_string_); std::istringstream in(inline_string_);
@ -113,7 +108,6 @@ void csv_datasource::bind() const
parse_csv(in,escape_, separator_, quote_); parse_csv(in,escape_, separator_, quote_);
in.close(); in.close();
} }
is_bound_ = true;
} }
template <typename T> template <typename T>
@ -245,7 +239,7 @@ void csv_datasource::parse_csv(T& stream,
// grammer = boost::escaped_list_separator<char>('\\', ',', '\"'); // grammer = boost::escaped_list_separator<char>('\\', ',', '\"');
grammer = boost::escaped_list_separator<char>(esc, sep, quo); grammer = boost::escaped_list_separator<char>(esc, sep, quo);
} }
catch(const std::exception & ex) catch(std::exception const& ex)
{ {
std::ostringstream s; std::ostringstream s;
s << "CSV Plugin: " << ex.what(); s << "CSV Plugin: " << ex.what();
@ -371,7 +365,7 @@ void csv_datasource::parse_csv(T& stream,
break; break;
} }
} }
catch(const std::exception & ex) catch(std::exception const& ex)
{ {
std::ostringstream s; std::ostringstream s;
s << "CSV Plugin: error parsing headers: " << ex.what(); 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"; if (!quiet_) std::clog << ex.what() << "\n";
} }
} }
catch(const std::exception & ex) catch(std::exception const& ex)
{ {
std::ostringstream s; std::ostringstream s;
s << "CSV Plugin: unexpected error parsing line: " << line_number s << "CSV Plugin: unexpected error parsing line: " << line_number
@ -846,14 +840,11 @@ datasource::datasource_t csv_datasource::type() const
mapnik::box2d<double> csv_datasource::envelope() const mapnik::box2d<double> csv_datasource::envelope() const
{ {
if (!is_bound_) bind();
return extent_; return extent_;
} }
boost::optional<mapnik::datasource::geometry_t> csv_datasource::get_geometry_type() const boost::optional<mapnik::datasource::geometry_t> csv_datasource::get_geometry_type() const
{ {
if (! is_bound_) bind();
boost::optional<mapnik::datasource::geometry_t> result; boost::optional<mapnik::datasource::geometry_t> result;
int multi_type = 0; int multi_type = 0;
unsigned num_features = features_.size(); unsigned num_features = features_.size();
@ -876,15 +867,11 @@ boost::optional<mapnik::datasource::geometry_t> csv_datasource::get_geometry_typ
mapnik::layer_descriptor csv_datasource::get_descriptor() const mapnik::layer_descriptor csv_datasource::get_descriptor() const
{ {
if (!is_bound_) bind();
return desc_; return desc_;
} }
mapnik::featureset_ptr csv_datasource::features(mapnik::query const& q) const mapnik::featureset_ptr csv_datasource::features(mapnik::query const& q) const
{ {
if (!is_bound_) bind();
const std::set<std::string>& attribute_names = q.property_names(); const std::set<std::string>& attribute_names = q.property_names();
std::set<std::string>::const_iterator pos = attribute_names.begin(); std::set<std::string>::const_iterator pos = attribute_names.begin();
while (pos != attribute_names.end()) 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 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"); throw mapnik::datasource_exception("CSV Plugin: features_at_point is not supported yet");
} }

View file

@ -10,7 +10,7 @@
class csv_datasource : public mapnik::datasource class csv_datasource : public mapnik::datasource
{ {
public: public:
csv_datasource(mapnik::parameters const& params, bool bind=true); csv_datasource(mapnik::parameters const& params);
virtual ~csv_datasource (); virtual ~csv_datasource ();
mapnik::datasource::datasource_t type() const; mapnik::datasource::datasource_t type() const;
static std::string name(); static std::string name();
@ -19,13 +19,13 @@ public:
mapnik::box2d<double> envelope() const; mapnik::box2d<double> envelope() const;
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;
void bind() const;
template <typename T> template <typename T>
void parse_csv(T& stream, void parse_csv(T& stream,
std::string const& escape, std::string const& escape,
std::string const& separator, std::string const& separator,
std::string const& quote) const; std::string const& quote) const;
private: private:
void init(mapnik::parameters const& params);
mutable mapnik::layer_descriptor desc_; mutable mapnik::layer_descriptor desc_;
mutable mapnik::box2d<double> extent_; mutable mapnik::box2d<double> extent_;
mutable std::string filename_; mutable std::string filename_;

View file

@ -71,7 +71,7 @@ boost::mutex knd_list_mutex;
std::list<kismet_network_data> knd_list; std::list<kismet_network_data> knd_list;
const unsigned int queue_size = 20; const unsigned int queue_size = 20;
kismet_datasource::kismet_datasource(parameters const& params, bool bind) kismet_datasource::kismet_datasource(parameters const& params)
: datasource(params), : datasource(params),
extent_(), extent_(),
extent_initialized_(false), 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"), srs_("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"),
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8")) desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8"))
{ {
boost::optional<std::string> host = params_.get<std::string>("host"); boost::optional<std::string> host = params.get<std::string>("host");
if (host) if (host)
{ {
host_ = *host; host_ = *host;
@ -89,37 +89,25 @@ kismet_datasource::kismet_datasource(parameters const& params, bool bind)
throw datasource_exception("Kismet Plugin: missing <host> parameter"); throw datasource_exception("Kismet Plugin: missing <host> parameter");
} }
boost::optional<unsigned int> port = params_.get<unsigned int>("port", 2501); boost::optional<unsigned int> port = params.get<unsigned int>("port", 2501);
if (port) if (port)
{ {
port_ = *port; port_ = *port;
} }
boost::optional<std::string> srs = params_.get<std::string>("srs"); boost::optional<std::string> srs = params.get<std::string>("srs");
if (srs) if (srs)
{ {
srs_ = *srs; srs_ = *srs;
} }
boost::optional<std::string> ext = params_.get<std::string>("extent"); boost::optional<std::string> ext = params.get<std::string>("extent");
if (ext) if (ext)
{ {
extent_initialized_ = extent_.from_string(*ext); extent_initialized_ = extent_.from_string(*ext);
} }
kismet_thread.reset(new boost::thread(boost::bind(&kismet_datasource::run, this, host_, port_))); 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() kismet_datasource::~kismet_datasource()
@ -138,7 +126,6 @@ mapnik::datasource::datasource_t kismet_datasource::type() const
box2d<double> kismet_datasource::envelope() const box2d<double> kismet_datasource::envelope() const
{ {
if (! is_bound_) bind();
return extent_; return extent_;
} }
@ -154,7 +141,6 @@ layer_descriptor kismet_datasource::get_descriptor() const
featureset_ptr kismet_datasource::features(query const& q) const featureset_ptr kismet_datasource::features(query const& q) const
{ {
if (! is_bound_) bind();
// std::clog << "kismet_datasource::features()" << endl; // 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 featureset_ptr kismet_datasource::features_at_point(coord2d const& pt) const
{ {
if (! is_bound_) bind();
// std::clog << "kismet_datasource::features_at_point()" << endl; // std::clog << "kismet_datasource::features_at_point()" << endl;

View file

@ -43,7 +43,7 @@
class kismet_datasource : public mapnik::datasource class kismet_datasource : public mapnik::datasource
{ {
public: public:
kismet_datasource(mapnik::parameters const& params, bool bind = true); kismet_datasource(mapnik::parameters const& params);
virtual ~kismet_datasource (); virtual ~kismet_datasource ();
datasource::datasource_t type() const; datasource::datasource_t type() const;
static std::string name(); static std::string name();
@ -52,11 +52,10 @@ public:
mapnik::box2d<double> envelope() const; mapnik::box2d<double> envelope() const;
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;
void bind() const;
private: private:
void init(mapnik::parameters const& params);
void run (const std::string& host, const unsigned int port); void run (const std::string& host, const unsigned int port);
mapnik::box2d<double> extent_; mapnik::box2d<double> extent_;
bool extent_initialized_; bool extent_initialized_;
std::string host_; std::string host_;

View file

@ -67,24 +67,25 @@ const std::string occi_datasource::METADATA_TABLE = "USER_SDO_GEOM_METADATA";
DATASOURCE_PLUGIN(occi_datasource) DATASOURCE_PLUGIN(occi_datasource)
occi_datasource::occi_datasource(parameters const& params, bool bind) occi_datasource::occi_datasource(parameters const& params)
: datasource (params), : datasource (params),
params_(params),
type_(datasource::Vector), type_(datasource::Vector),
fields_(*params_.get<std::string>("fields", "*")), fields_(*params.get<std::string>("fields", "*")),
geometry_field_(*params_.get<std::string>("geometry_field", "")), geometry_field_(*params.get<std::string>("geometry_field", "")),
srid_initialized_(false), srid_initialized_(false),
extent_initialized_(false), extent_initialized_(false),
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding", "utf-8")), desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding", "utf-8")),
row_limit_(*params_.get<int>("row_limit", 0)), row_limit_(*params.get<int>("row_limit", 0)),
row_prefetch_(*params_.get<int>("row_prefetch", 100)), row_prefetch_(*params.get<int>("row_prefetch", 100)),
pool_(0), pool_(0),
conn_(0) conn_(0)
{ {
if (! params_.get<std::string>("user")) throw datasource_exception("OCCI Plugin: no <user> specified"); if (! params.get<std::string>("user")) throw datasource_exception("OCCI Plugin: no <user> specified");
if (! params_.get<std::string>("password")) throw datasource_exception("OCCI Plugin: no <password> specified"); if (! params.get<std::string>("password")) throw datasource_exception("OCCI Plugin: no <password> specified");
if (! params_.get<std::string>("host")) throw datasource_exception("OCCI Plugin: no <host> string specified"); if (! params.get<std::string>("host")) throw datasource_exception("OCCI Plugin: no <host> string specified");
boost::optional<std::string> table = params_.get<std::string>("table"); boost::optional<std::string> table = params.get<std::string>("table");
if (! table) if (! table)
{ {
throw datasource_exception("OCCI Plugin: no <table> parameter specified"); throw datasource_exception("OCCI Plugin: no <table> parameter specified");
@ -94,52 +95,44 @@ occi_datasource::occi_datasource(parameters const& params, bool bind)
table_ = *table; table_ = *table;
} }
use_spatial_index_ = *params_.get<mapnik::boolean>("use_spatial_index",true); use_spatial_index_ = *params.get<mapnik::boolean>("use_spatial_index",true);
use_connection_pool_ = *params_.get<mapnik::boolean>("use_connection_pool",true); use_connection_pool_ = *params.get<mapnik::boolean>("use_connection_pool",true);
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 (ext) extent_initialized_ = extent_.from_string(*ext);
boost::optional<int> srid = params_.get<int>("srid"); boost::optional<int> srid = params.get<int>("srid");
if (srid) if (srid)
{ {
srid_ = *srid; srid_ = *srid;
srid_initialized_ = true; srid_initialized_ = true;
} }
if (bind) this->init(params);
{
this->bind();
}
} }
occi_datasource::~occi_datasource() 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 // connect to environment
if (use_connection_pool_) if (use_connection_pool_)
{ {
@ -148,11 +141,11 @@ void occi_datasource::bind() const
Environment* env = occi_environment::get_environment(); Environment* env = occi_environment::get_environment();
pool_ = env->createStatelessConnectionPool( pool_ = env->createStatelessConnectionPool(
*params_.get<std::string>("user"), *params.get<std::string>("user"),
*params_.get<std::string>("password"), *params.get<std::string>("password"),
*params_.get<std::string>("host"), *params.get<std::string>("host"),
*params_.get<int>("max_size", 10), *params.get<int>("max_size", 10),
*params_.get<int>("initial_size", 1), *params.get<int>("initial_size", 1),
1, 1,
StatelessConnectionPool::HOMOGENEOUS); StatelessConnectionPool::HOMOGENEOUS);
} }
@ -168,9 +161,9 @@ void occi_datasource::bind() const
Environment* env = occi_environment::get_environment(); Environment* env = occi_environment::get_environment();
conn_ = env->createConnection( conn_ = env->createConnection(
*params_.get<std::string>("user"), *params.get<std::string>("user"),
*params_.get<std::string>("password"), *params.get<std::string>("password"),
*params_.get<std::string>("host")); *params.get<std::string>("host"));
} }
catch (SQLException& ex) catch (SQLException& ex)
{ {
@ -342,8 +335,6 @@ void occi_datasource::bind() const
throw datasource_exception(ex.getMessage()); throw datasource_exception(ex.getMessage());
} }
} }
is_bound_ = true;
} }
std::string occi_datasource::name() std::string occi_datasource::name()
@ -359,7 +350,6 @@ mapnik::datasource::datasource_t occi_datasource::type() const
box2d<double> occi_datasource::envelope() const box2d<double> occi_datasource::envelope() const
{ {
if (extent_initialized_) return extent_; if (extent_initialized_) return extent_;
if (! is_bound_) bind();
double lox = 0.0, loy = 0.0, hix = 0.0, hiy = 0.0; double lox = 0.0, loy = 0.0, hix = 0.0, hiy = 0.0;
@ -478,20 +468,17 @@ box2d<double> occi_datasource::envelope() const
boost::optional<mapnik::datasource::geometry_t> occi_datasource::get_geometry_type() const boost::optional<mapnik::datasource::geometry_t> occi_datasource::get_geometry_type() const
{ {
// FIXME // FIXME
//if (! is_bound_) bind();
return boost::optional<mapnik::datasource::geometry_t>(); return boost::optional<mapnik::datasource::geometry_t>();
} }
layer_descriptor occi_datasource::get_descriptor() const layer_descriptor occi_datasource::get_descriptor() const
{ {
if (! is_bound_) bind();
return desc_; return desc_;
} }
featureset_ptr occi_datasource::features(query const& q) const featureset_ptr occi_datasource::features(query const& q) const
{ {
if (! is_bound_) bind();
box2d<double> const& box = q.get_bbox(); box2d<double> const& box = q.get_bbox();
@ -540,15 +527,18 @@ featureset_ptr occi_datasource::features(query const& q) const
if (row_limit_ > 0) 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")) 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_)) 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 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 featureset_ptr occi_datasource::features_at_point(coord2d const& pt) const
{ {
if (! is_bound_) bind();
std::ostringstream s; std::ostringstream s;
s << "SELECT " << geometry_field_; s << "SELECT " << geometry_field_;
@ -621,15 +610,18 @@ featureset_ptr occi_datasource::features_at_point(coord2d const& pt) const
if (row_limit_ > 0) 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")) 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_)) 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 else
{ {

View file

@ -38,7 +38,7 @@
class occi_datasource : public mapnik::datasource class occi_datasource : public mapnik::datasource
{ {
public: public:
occi_datasource(mapnik::parameters const& params, bool bind = true); occi_datasource(mapnik::parameters const& params);
virtual ~occi_datasource (); virtual ~occi_datasource ();
mapnik::datasource::datasource_t type() const; mapnik::datasource::datasource_t type() const;
static std::string name(); static std::string name();
@ -47,9 +47,10 @@ public:
mapnik::box2d<double> envelope() const; mapnik::box2d<double> envelope() const;
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;
void bind() const;
private: private:
void init(mapnik::parameters const& params);
mapnik::parameters const& params_;
mapnik::datasource::datasource_t type_; mapnik::datasource::datasource_t type_;
mutable std::string table_; mutable std::string table_;
mutable std::string table_name_; mutable std::string table_name_;

View file

@ -72,7 +72,7 @@ feature_ptr postgis_featureset::next()
int oid = rs_->getTypeOID(pos); int oid = rs_->getTypeOID(pos);
const char* buf = rs_->getValue(pos); const char* buf = rs_->getValue(pos);
std::string name = rs_->getFieldName(pos); std::string name = rs_->getFieldName(pos);
// validation happens of this type at bind() // validation happens of this type at init()
int val; int val;
if (oid == 20) if (oid == 20)
{ {

View file

@ -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), : datasource(params),
desc_(*params.get<std::string>("type"),"utf-8") desc_(*params.get<std::string>("type"),"utf-8")
{ {
@ -92,16 +92,11 @@ rasterlite_datasource::rasterlite_datasource(parameters const& params, bool bind
else else
dataset_name_ = *file; dataset_name_ = *file;
if (bind) this->init(params);
{
this->bind();
}
} }
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"); if (!boost::filesystem::exists(dataset_name_)) throw datasource_exception(dataset_name_ + " does not exist");
void *dataset = open_dataset(); void *dataset = open_dataset();
@ -157,8 +152,6 @@ void rasterlite_datasource::bind() const
#endif #endif
rasterliteClose(dataset); rasterliteClose(dataset);
is_bound_ = true;
} }
rasterlite_datasource::~rasterlite_datasource() rasterlite_datasource::~rasterlite_datasource()
@ -177,8 +170,6 @@ mapnik::datasource::datasource_t rasterlite_datasource::type() const
box2d<double> rasterlite_datasource::envelope() const box2d<double> rasterlite_datasource::envelope() const
{ {
if (!is_bound_) bind();
return extent_; return extent_;
} }
@ -194,16 +185,12 @@ layer_descriptor rasterlite_datasource::get_descriptor() const
featureset_ptr rasterlite_datasource::features(query const& q) const featureset_ptr rasterlite_datasource::features(query const& q) const
{ {
if (!is_bound_) bind();
rasterlite_query gq = q; rasterlite_query gq = q;
return boost::make_shared<rasterlite_featureset>(open_dataset(), gq); return boost::make_shared<rasterlite_featureset>(open_dataset(), gq);
} }
featureset_ptr rasterlite_datasource::features_at_point(coord2d const& pt) const featureset_ptr rasterlite_datasource::features_at_point(coord2d const& pt) const
{ {
if (!is_bound_) bind();
rasterlite_query gq = pt; rasterlite_query gq = pt;
return boost::make_shared<rasterlite_featureset>(open_dataset(), gq); return boost::make_shared<rasterlite_featureset>(open_dataset(), gq);
} }

View file

@ -34,7 +34,7 @@
class rasterlite_datasource : public mapnik::datasource class rasterlite_datasource : public mapnik::datasource
{ {
public: public:
rasterlite_datasource(mapnik::parameters const& params, bool bind = true); rasterlite_datasource(mapnik::parameters const& params);
virtual ~rasterlite_datasource (); virtual ~rasterlite_datasource ();
mapnik::datasource::datasource_t type() const; mapnik::datasource::datasource_t type() const;
static std::string name(); static std::string name();
@ -43,9 +43,9 @@ public:
mapnik::box2d<double> envelope() const; mapnik::box2d<double> envelope() const;
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;
void bind() const;
private: private:
void init(mapnik::parameters const& params);
inline void* open_dataset() const; inline void* open_dataset() const;
mutable mapnik::box2d<double> extent_; mutable mapnik::box2d<double> extent_;
std::string dataset_name_; std::string dataset_name_;

View file

@ -11,29 +11,22 @@ using mapnik::parameters;
DATASOURCE_PLUGIN(hello_datasource) DATASOURCE_PLUGIN(hello_datasource)
hello_datasource::hello_datasource(parameters const& params, bool bind) hello_datasource::hello_datasource(parameters const& params)
: datasource(params), : datasource(params),
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding","utf-8")), desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8")),
extent_() extent_()
{ {
if (bind) this->init(params);
{
this->bind();
}
} }
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 // every datasource must have some way of reporting its extent
// in this case we are not actually reading from any data so for fun // 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: // 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) // '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' (equivalent to +init=epsg:4326)
// see http://spatialreference.org/ref/epsg/4326/ for more details // see http://spatialreference.org/ref/epsg/4326/ for more details
extent_.init(-180,-90,180,90); extent_.init(-180,-90,180,90);
is_bound_ = true;
} }
hello_datasource::~hello_datasource() { } hello_datasource::~hello_datasource() { }
@ -53,8 +46,6 @@ mapnik::datasource::datasource_t hello_datasource::type() const
mapnik::box2d<double> hello_datasource::envelope() const mapnik::box2d<double> hello_datasource::envelope() const
{ {
if (!is_bound_) bind();
return extent_; return extent_;
} }
@ -65,15 +56,11 @@ boost::optional<mapnik::datasource::geometry_t> hello_datasource::get_geometry_t
mapnik::layer_descriptor hello_datasource::get_descriptor() const mapnik::layer_descriptor hello_datasource::get_descriptor() const
{ {
if (!is_bound_) bind();
return desc_; return desc_;
} }
mapnik::featureset_ptr hello_datasource::features(mapnik::query const& q) const 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 the query box intersects our world extent then query for features
if (extent_.intersects(q.get_bbox())) 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 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, // features_at_point is rarely used - only by custom applications,
// so for this sample plugin let's do nothing... // so for this sample plugin let's do nothing...
return mapnik::featureset_ptr(); return mapnik::featureset_ptr();

View file

@ -9,7 +9,7 @@ class hello_datasource : public mapnik::datasource
public: public:
// constructor // constructor
// arguments must not change // arguments must not change
hello_datasource(mapnik::parameters const& params, bool bind=true); hello_datasource(mapnik::parameters const& params);
// destructor // destructor
virtual ~hello_datasource (); virtual ~hello_datasource ();
@ -38,10 +38,10 @@ public:
// mandatory: return the layer descriptor // mandatory: return the layer descriptor
mapnik::layer_descriptor get_descriptor() const; mapnik::layer_descriptor get_descriptor() const;
// mandatory: will bind the datasource given params
void bind() const;
private: 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: // recommended naming convention of datasource members:
// name_, type_, extent_, and desc_ // name_, type_, extent_, and desc_
static const std::string name_; static const std::string name_;