apply modified patch from Mak Kolybabi fixing up postgres schema support in PostGIS plugin - closes #500 - extends #260

This commit is contained in:
Dane Springmeyer 2010-02-19 21:00:35 +00:00
parent 3a80dffb6e
commit 7ed73b3b69
4 changed files with 35 additions and 16 deletions

View file

@ -31,6 +31,7 @@ Patches
- Beau Gunderson - Beau Gunderson
- John Hague - John Hague
- Aubrey Holland - Aubrey Holland
- Mak Kolybabi
- Dennis Luxen - Dennis Luxen
- Tom MacWright - Tom MacWright
- Michal Migurski - Michal Migurski

View file

@ -15,6 +15,8 @@ For a complete change history, see the SVN log.
Mapnik 0.7.1 Release Mapnik 0.7.1 Release
-------------------- --------------------
- PostGIS: Added better support for alterative schemas (#500)
- AGG Renderer - Enforced default gamma function on all symbolizers to ensure proper antialiasing - AGG Renderer - Enforced default gamma function on all symbolizers to ensure proper antialiasing
even when gamma is modified on the PolygonSymbolizer. (#512) even when gamma is modified on the PolygonSymbolizer. (#512)

View file

@ -74,6 +74,7 @@ using mapnik::attribute_descriptor;
postgis_datasource::postgis_datasource(parameters const& params) postgis_datasource::postgis_datasource(parameters const& params)
: datasource(params), : datasource(params),
table_(*params_.get<std::string>("table","")), table_(*params_.get<std::string>("table","")),
schema_(""),
geometry_table_(*params_.get<std::string>("geometry_table","")), geometry_table_(*params_.get<std::string>("geometry_table","")),
geometry_field_(*params_.get<std::string>("geometry_field","")), geometry_field_(*params_.get<std::string>("geometry_field","")),
cursor_fetch_size_(*params_.get<int>("cursor_size",0)), cursor_fetch_size_(*params_.get<int>("cursor_size",0)),
@ -154,11 +155,10 @@ postgis_datasource::postgis_datasource(parameters const& params)
{ {
geometry_table_ = table_from_sql(table_); geometry_table_ = table_from_sql(table_);
} }
std::string schema_name="";
std::string::size_type idx = geometry_table_.find_last_of('.'); std::string::size_type idx = geometry_table_.find_last_of('.');
if (idx!=std::string::npos) 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); geometry_table_ = geometry_table_.substr(idx+1);
} }
else else
@ -179,8 +179,8 @@ postgis_datasource::postgis_datasource(parameters const& params)
s << "SELECT f_geometry_column, srid FROM "; s << "SELECT f_geometry_column, srid FROM ";
s << GEOMETRY_COLUMNS <<" WHERE f_table_name='" << unquote(geometry_table_) <<"'"; s << GEOMETRY_COLUMNS <<" WHERE f_table_name='" << unquote(geometry_table_) <<"'";
if (schema_name.length() > 0) if (schema_.length() > 0)
s << " AND f_table_schema='" << unquote(schema_name) << "'"; s << " AND f_table_schema='" << unquote(schema_) << "'";
if (geometry_field_.length() > 0) if (geometry_field_.length() > 0)
s << " AND f_geometry_column='" << unquote(geometry_field_) << "'"; s << " AND f_geometry_column='" << unquote(geometry_field_) << "'";
@ -473,9 +473,9 @@ featureset_ptr postgis_datasource::features(const query& q) const
if (!geometryColumn_.length() > 0) if (!geometryColumn_.length() > 0)
{ {
std::ostringstream s_error; 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 " << "'. 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()); throw mapnik::datasource_exception(s_error.str());
} }
@ -520,9 +520,9 @@ featureset_ptr postgis_datasource::features_at_point(coord2d const& pt) const
if (!geometryColumn_.length() > 0) if (!geometryColumn_.length() > 0)
{ {
std::ostringstream s_error; 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 " << "'. 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()); throw mapnik::datasource_exception(s_error.str());
} }
@ -573,32 +573,47 @@ Envelope<double> postgis_datasource::envelope() const
{ {
std::ostringstream s_error; std::ostringstream s_error;
s_error << "PostGIS: unable to query the layer extent of table '" 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, " << "\nPlease provide either 1) an 'extent' parameter to skip this query, "
<< "2) a 'geometry_field' and/or 'geometry_table' parameter, or 3) add a " << "2) a 'geometry_field' and/or 'geometry_table' parameter, or 3) add a "
<< "record to the 'geometry_columns' for your table."; << "record to the 'geometry_columns' for your table.";
throw mapnik::datasource_exception(s_error.str()); throw mapnik::datasource_exception(s_error.str());
} }
// TODO - do we need to respect schema here?
if (estimate_extent && *estimate_extent == "true") if (estimate_extent && *estimate_extent == "true")
{ {
s << "select xmin(ext),ymin(ext),xmax(ext),ymax(ext)" s << "select xmin(ext),ymin(ext),xmax(ext),ymax(ext)"
<< " from (select estimated_extent('" << " from (select estimated_extent(";
<< geometry_table_ << "','"
<< geometryColumn_ << "') as ext) as tmp"; 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)" s << "select xmin(ext),ymin(ext),xmax(ext),ymax(ext)"
<< " from (select extent(" <<geometryColumn_<< ") as ext from "; << " from (select extent(" <<geometryColumn_<< ") as ext from ";
if (extent_from_subquery_) if (extent_from_subquery_)
// if a subselect limits records then calculating the extent upon the {
// if a subselect limits records then calculating the extent upon the
// subquery will be faster and the bounds will be more accurate // subquery will be faster and the bounds will be more accurate
s << populate_tokens(table_) << ") as tmp"; s << populate_tokens(table_) << ") as tmp";
}
else else
{
if (schema_.length() > 0)
{
s << schema_ << ".";
}
// but if the subquery does not limit records then querying the // but if the subquery does not limit records then querying the
// actual table will be faster as indexes can be used // actual table will be faster as indexes can be used
s << geometry_table_ << ") as tmp"; s << geometry_table_ << ") as tmp";
}
} }
/*if (show_queries_) /*if (show_queries_)

View file

@ -57,6 +57,7 @@ class postgis_datasource : public datasource
const std::string username_; const std::string username_;
const std::string password_; const std::string password_;
const std::string table_; const std::string table_;
mutable std::string schema_;
mutable std::string geometry_table_; mutable std::string geometry_table_;
const std::string geometry_field_; const std::string geometry_field_;
const int cursor_fetch_size_; const int cursor_fetch_size_;