From badbc8706f4fe64878351f069620542f10077080 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Fri, 19 Feb 2010 21:04:45 +0000 Subject: [PATCH] apply modified patch from Mak Kolybabi fixing up postgres schema support in PostGIS plugin - closes #500 - extends #260 --- AUTHORS | 1 + CHANGELOG | 2 ++ plugins/input/postgis/postgis.cpp | 47 ++++++++++++++++++++----------- plugins/input/postgis/postgis.hpp | 1 + 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/AUTHORS b/AUTHORS index 66c821dd6..df5bbba25 100644 --- a/AUTHORS +++ b/AUTHORS @@ -31,6 +31,7 @@ Patches - Beau Gunderson - John Hague - Aubrey Holland + - Mak Kolybabi - Dennis Luxen - Tom MacWright - Michal Migurski diff --git a/CHANGELOG b/CHANGELOG index 8c0295840..8775f6908 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -16,6 +16,8 @@ Mapnik Trunk - Fixed reading of PostGIS data on Big Endian systems (#515) +- PostGIS: Added better support for alterative schemas (#500) + - AGG Renderer - Enforced default gamma function on all symbolizers to ensure proper antialiasing even when gamma is modified on the PolygonSymbolizer. (#512) diff --git a/plugins/input/postgis/postgis.cpp b/plugins/input/postgis/postgis.cpp index ff48532db..914c82129 100644 --- a/plugins/input/postgis/postgis.cpp +++ b/plugins/input/postgis/postgis.cpp @@ -72,6 +72,7 @@ using mapnik::attribute_descriptor; postgis_datasource::postgis_datasource(parameters const& params) : datasource(params), table_(*params_.get("table","")), + schema_(""), geometry_table_(*params_.get("geometry_table","")), geometry_field_(*params_.get("geometry_field","")), cursor_fetch_size_(*params_.get("cursor_size",0)), @@ -152,11 +153,10 @@ postgis_datasource::postgis_datasource(parameters const& params) { geometry_table_ = table_from_sql(table_); } - std::string schema_name=""; std::string::size_type idx = geometry_table_.find_last_of('.'); if (idx!=std::string::npos) { - schema_name = geometry_table_.substr(0,idx); + schema_ = geometry_table_.substr(0,idx); geometry_table_ = geometry_table_.substr(idx+1); } else @@ -177,8 +177,8 @@ postgis_datasource::postgis_datasource(parameters const& params) s << "SELECT f_geometry_column, srid FROM "; s << GEOMETRY_COLUMNS <<" WHERE f_table_name='" << unquote(geometry_table_) <<"'"; - if (schema_name.length() > 0) - s << " AND f_table_schema='" << unquote(schema_name) << "'"; + if (schema_.length() > 0) + s << " AND f_table_schema='" << unquote(schema_) << "'"; if (geometry_field_.length() > 0) s << " AND f_geometry_column='" << unquote(geometry_field_) << "'"; @@ -468,9 +468,9 @@ featureset_ptr postgis_datasource::features(const query& q) const if (!geometryColumn_.length() > 0) { std::ostringstream s_error; - s_error << "PostGIS: geometry name lookup failed for table '" << geometry_table_ + s_error << "PostGIS: geometry name lookup failed for table '" << schema_ << "." << geometry_table_ << "'. Please manually provide the 'geometry_field' parameter or add an entry " - << "in the geometry_columns for '" << geometry_table_ << "'."; + << "in the geometry_columns for '" << schema_ << "." << geometry_table_ << "'."; throw mapnik::datasource_exception(s_error.str()); } @@ -515,9 +515,9 @@ featureset_ptr postgis_datasource::features_at_point(coord2d const& pt) const if (!geometryColumn_.length() > 0) { std::ostringstream s_error; - s_error << "PostGIS: geometry name lookup failed for table '" << geometry_table_ + s_error << "PostGIS: geometry name lookup failed for table '" << schema_ << "." << geometry_table_ << "'. Please manually provide the 'geometry_field' parameter or add an entry " - << "in the geometry_columns for '" << geometry_table_ << "'."; + << "in the geometry_columns for '" << schema_ << "." << geometry_table_ << "'."; throw mapnik::datasource_exception(s_error.str()); } @@ -568,32 +568,47 @@ box2d postgis_datasource::envelope() const { std::ostringstream s_error; s_error << "PostGIS: unable to query the layer extent of table '" - << geometry_table_ << "' because we cannot determine the geometry field name." + << schema_ << "." << geometry_table_ << "' because we cannot determine the geometry field name." << "\nPlease provide either 1) an 'extent' parameter to skip this query, " << "2) a 'geometry_field' and/or 'geometry_table' parameter, or 3) add a " << "record to the 'geometry_columns' for your table."; throw mapnik::datasource_exception(s_error.str()); } - // TODO - do we need to respect schema here? + if (estimate_extent && *estimate_extent == "true") { - s << "select xmin(ext),ymin(ext),xmax(ext),ymax(ext)" - << " from (select estimated_extent('" - << geometry_table_ << "','" - << geometryColumn_ << "') as ext) as tmp"; + s << "select xmin(ext),ymin(ext),xmax(ext),ymax(ext)" + << " from (select estimated_extent("; + + if (schema_.length() > 0) + { + s << schema_ << "."; + } + + s << geometry_table_ << "," + << geometryColumn_ << ") as ext) as tmp"; } - else + else { s << "select xmin(ext),ymin(ext),xmax(ext),ymax(ext)" << " from (select extent(" < 0) + { + s << schema_ << "."; + } + // but if the subquery does not limit records then querying the // actual table will be faster as indexes can be used s << geometry_table_ << ") as tmp"; + } } /*if (show_queries_) diff --git a/plugins/input/postgis/postgis.hpp b/plugins/input/postgis/postgis.hpp index 9b79c9cff..897dae1d7 100644 --- a/plugins/input/postgis/postgis.hpp +++ b/plugins/input/postgis/postgis.hpp @@ -57,6 +57,7 @@ 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_; const std::string geometry_field_; const int cursor_fetch_size_;