Skip to content

Commit

Permalink
Merge pull request #1192 from strk/master-pixelsize-tokens
Browse files Browse the repository at this point in the history
Provide !pixel_width! and !pixel_height! tokens (#1181)
  • Loading branch information
Dane Springmeyer committed Apr 30, 2012
2 parents 0ccabf2 + ceffa76 commit 635b245
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
43 changes: 37 additions & 6 deletions plugins/input/postgis/postgis_datasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ postgis_datasource::postgis_datasource(parameters const& params, bool bind)
params.get<std::string>("connect_timeout", "4")),
bbox_token_("!bbox!"),
scale_denom_token_("!scale_denominator!"),
pixel_width_token_("!pixel_width!"),
pixel_height_token_("!pixel_height!"),
persist_connection_(*params_.get<mapnik::boolean>("persist_connection", true)),
extent_from_subquery_(*params_.get<mapnik::boolean>("extent_from_subquery", false)),
// params below are for testing purposes only (will likely be removed at any time)
Expand Down Expand Up @@ -497,10 +499,24 @@ std::string postgis_datasource::populate_tokens(const std::string& sql) const
boost::algorithm::replace_all(populated_sql, scale_denom_token_, ss.str());
}

if (boost::algorithm::icontains(sql, pixel_width_token_))
{
std::ostringstream ss;
ss << 0;
boost::algorithm::replace_all(populated_sql, pixel_width_token_, ss.str());
}

if (boost::algorithm::icontains(sql, pixel_height_token_))
{
std::ostringstream ss;
ss << 0;
boost::algorithm::replace_all(populated_sql, pixel_height_token_, ss.str());
}

return populated_sql;
}

std::string postgis_datasource::populate_tokens(const std::string& sql, double scale_denom, box2d<double> const& env) const
std::string postgis_datasource::populate_tokens(const std::string& sql, double scale_denom, box2d<double> const& env, double pixel_width, double pixel_height) const
{
std::string populated_sql = sql;
std::string box = sql_bbox(env);
Expand All @@ -512,6 +528,20 @@ std::string postgis_datasource::populate_tokens(const std::string& sql, double s
boost::algorithm::replace_all(populated_sql, scale_denom_token_, ss.str());
}

if (boost::algorithm::icontains(sql, pixel_width_token_))
{
std::ostringstream ss;
ss << pixel_width;
boost::algorithm::replace_all(populated_sql, pixel_width_token_, ss.str());
}

if (boost::algorithm::icontains(sql, pixel_height_token_))
{
std::ostringstream ss;
ss << pixel_height;
boost::algorithm::replace_all(populated_sql, pixel_height_token_, ss.str());
}

if (boost::algorithm::icontains(populated_sql, bbox_token_))
{
boost::algorithm::replace_all(populated_sql, bbox_token_, box);
Expand Down Expand Up @@ -611,6 +641,10 @@ featureset_ptr postgis_datasource::features(const query& q) const
}

std::ostringstream s;

const double px_gw = 1.0 / boost::get<0>(q.resolution());
const double px_gh = 1.0 / boost::get<1>(q.resolution());

s << "SELECT ST_AsBinary(";

if (simplify_geometries_) {
Expand All @@ -620,9 +654,6 @@ featureset_ptr postgis_datasource::features(const query& q) const
s << "\"" << geometryColumn_ << "\"";

if (simplify_geometries_) {
const double px_gw = 1.0 / boost::get<0>(q.resolution());
const double px_gh = 1.0 / boost::get<1>(q.resolution());

const double tolerance = std::min(px_gw, px_gh) / 2.0;
s << ", " << tolerance << ")";
}
Expand Down Expand Up @@ -657,7 +688,7 @@ featureset_ptr postgis_datasource::features(const query& q) const
}
}

std::string table_with_bbox = populate_tokens(table_, scale_denom, box);
std::string table_with_bbox = populate_tokens(table_, scale_denom, box, px_gw, px_gh);

s << " FROM " << table_with_bbox;

Expand Down Expand Up @@ -750,7 +781,7 @@ featureset_ptr postgis_datasource::features_at_point(coord2d const& pt) const
}

box2d<double> box(pt.x, pt.y, pt.x, pt.y);
std::string table_with_bbox = populate_tokens(table_, FMAX, box);
std::string table_with_bbox = populate_tokens(table_, FMAX, box, 0, 0);

s << " FROM " << table_with_bbox;

Expand Down
4 changes: 3 additions & 1 deletion plugins/input/postgis/postgis_datasource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class postgis_datasource : public datasource

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;
std::string populate_tokens(const std::string& sql, double scale_denom, box2d<double> const& env, double pixel_width, double pixel_height) const;
std::string populate_tokens(const std::string& sql) const;
static std::string unquote(const std::string& sql);
boost::shared_ptr<IResultSet> get_resultset(boost::shared_ptr<Connection> const &conn, std::string const& sql) const;
Expand Down Expand Up @@ -90,6 +90,8 @@ class postgis_datasource : public datasource
ConnectionCreator<Connection> creator_;
const std::string bbox_token_;
const std::string scale_denom_token_;
const std::string pixel_width_token_;
const std::string pixel_height_token_;
bool persist_connection_;
bool extent_from_subquery_;
// params below are for testing purposes only (will likely be removed at any time)
Expand Down

0 comments on commit 635b245

Please sign in to comment.