+ aligned postgis to use the new box2d::from_string

+ added some new parameters toallow setting "gid" (geometry id), "field_data" (additional text data with the geometry), "field_name" (name of the additional text data field)
+ added proper usage of extent and coord2d selections in spatial queries
+ cosmetics
This commit is contained in:
Lucio Asnaghi 2010-11-14 15:02:01 +00:00
parent 4d04901c0b
commit 128a7ddc5b
6 changed files with 120 additions and 76 deletions

View file

@ -105,6 +105,7 @@ void geos_converter::convert_point (const GEOSGeometry* geom, feature_ptr featur
geometry_type* point = new geometry_type(mapnik::Point);
point->move_to (x, y);
feature->add_geometry (point);
}
@ -133,6 +134,7 @@ void geos_converter::convert_linestring (const GEOSGeometry* geom, feature_ptr f
GEOSCoordSeq_getY(cs, i, &y);
line->line_to (x, y);
}
feature->add_geometry (line);
}
@ -193,6 +195,7 @@ void geos_converter::convert_polygon (const GEOSGeometry* geom, feature_ptr feat
poly->line_to(x, y);
}
}
feature->add_geometry (poly);
}

View file

@ -32,19 +32,18 @@
class geos_converter
{
public:
static void convert_geometry (const GEOSGeometry* geom, mapnik::feature_ptr feature, bool multiple_geometries);
static void convert_collection (const GEOSGeometry* geom, mapnik::feature_ptr feature, bool multiple_geometries);
static void convert_point (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_linestring (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_polygon (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multipoint (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multipoint_2 (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multilinestring (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multilinestring_2 (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multipolygon (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multipolygon_2 (const GEOSGeometry* geom, mapnik::feature_ptr feature);
public:
static void convert_geometry (const GEOSGeometry* geom, mapnik::feature_ptr feature, bool multiple_geometries);
static void convert_collection (const GEOSGeometry* geom, mapnik::feature_ptr feature, bool multiple_geometries);
static void convert_point (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_linestring (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_polygon (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multipoint (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multipoint_2 (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multilinestring (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multilinestring_2 (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multipolygon (const GEOSGeometry* geom, mapnik::feature_ptr feature);
static void convert_multipolygon_2 (const GEOSGeometry* geom, mapnik::feature_ptr feature);
};
#endif // GEOS_FEATURESET_HPP

View file

@ -93,7 +93,10 @@ geos_datasource::geos_datasource(parameters const& params, bool bind)
extent_(),
extent_initialized_(false),
type_(datasource::Vector),
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")),
geometry_id_(0),
geometry_data_(""),
geometry_data_name_("name")
{
boost::optional<std::string> geometry = params.get<std::string>("wkt");
if (!geometry) throw datasource_exception("missing <wkt> parameter");
@ -101,6 +104,20 @@ geos_datasource::geos_datasource(parameters const& params, bool bind)
multiple_geometries_ = *params_.get<mapnik::boolean>("multiple_geometries",false);
boost::optional<std::string> ext = params_.get<std::string>("extent");
if (ext) extent_initialized_ = extent_.from_string(*ext);
boost::optional<int> id = params_.get<int>("gid");
if (id) geometry_id_ = *id;
boost::optional<std::string> gdata = params_.get<std::string>("field_data");
if (gdata) geometry_data_ = *gdata;
boost::optional<std::string> gdata_name = params_.get<std::string>("field_name");
if (gdata) geometry_data_name_ = *gdata_name;
desc_.add_descriptor(attribute_descriptor(geometry_data_name_, mapnik::String));
if (bind)
{
this->bind();
@ -145,50 +162,15 @@ void geos_datasource::bind() const
if (*geometry_ == NULL || ! GEOSisValid(*geometry_))
{
throw datasource_exception("invalid <wkt> geometry specified");
throw datasource_exception("invalid <wkt> geometry specified");
}
#ifdef MAPNIK_DEBUG
clog << "geometry correctly parsed" << endl;
#endif
// initialize envelope
boost::optional<std::string> ext = params_.get<std::string>("extent");
if (ext)
{
boost::char_separator<char> sep(",");
boost::tokenizer<boost::char_separator<char> > tok(*ext,sep);
unsigned i = 0;
bool success = false;
double d[4];
for (boost::tokenizer<boost::char_separator<char> >::iterator beg=tok.begin();
beg!=tok.end();++beg)
{
try
{
d[i] = boost::lexical_cast<double>(*beg);
}
catch (boost::bad_lexical_cast & ex)
{
std::clog << ex.what() << "\n";
break;
}
if (i==3)
{
success = true;
break;
}
++i;
}
if (success)
{
extent_.init(d[0],d[1],d[2],d[3]);
extent_initialized_ = true;
}
}
if (!extent_initialized_)
// try to obtain the extent from the geometry itself
if (! extent_initialized_)
{
#ifdef MAPNIK_DEBUG
clog << "initializing extent from geometry" << endl;
@ -261,12 +243,14 @@ int geos_datasource::type() const
box2d<double> geos_datasource::envelope() const
{
if (!is_bound_) bind();
return extent_;
}
layer_descriptor geos_datasource::get_descriptor() const
{
if (!is_bound_) bind();
return desc_;
}
@ -291,6 +275,9 @@ featureset_ptr geos_datasource::features(query const& q) const
return featureset_ptr(new geos_featureset (*geometry_,
GEOSGeomFromWKT(s.str().c_str()),
geometry_id_,
geometry_data_,
geometry_data_name_,
desc_.get_encoding(),
multiple_geometries_));
}
@ -308,6 +295,9 @@ featureset_ptr geos_datasource::features_at_point(coord2d const& pt) const
return featureset_ptr(new geos_featureset (*geometry_,
GEOSGeomFromWKT(s.str().c_str()),
geometry_id_,
geometry_data_,
geometry_data_name_,
desc_.get_encoding(),
multiple_geometries_));
}

View file

@ -36,24 +36,27 @@
class geos_datasource : public mapnik::datasource
{
public:
geos_datasource(mapnik::parameters const& params, bool bind=true);
virtual ~geos_datasource ();
int type() const;
static std::string name();
mapnik::featureset_ptr features(mapnik::query const& q) const;
mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt) const;
mapnik::box2d<double> envelope() const;
mapnik::layer_descriptor get_descriptor() const;
void bind() const;
private:
mutable mapnik::box2d<double> extent_;
mutable bool extent_initialized_;
int type_;
mutable mapnik::layer_descriptor desc_;
mutable geos_feature_ptr geometry_;
std::string geometry_string_;
bool multiple_geometries_;
public:
geos_datasource(mapnik::parameters const& params, bool bind=true);
virtual ~geos_datasource ();
int type() const;
static std::string name();
mapnik::featureset_ptr features(mapnik::query const& q) const;
mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt) const;
mapnik::box2d<double> envelope() const;
mapnik::layer_descriptor get_descriptor() const;
void bind() const;
private:
mutable mapnik::box2d<double> extent_;
mutable bool extent_initialized_;
int type_;
mutable mapnik::layer_descriptor desc_;
mutable geos_feature_ptr geometry_;
mutable std::string geometry_data_;
mutable std::string geometry_data_name_;
mutable int geometry_id_;
std::string geometry_string_;
bool multiple_geometries_;
};

View file

@ -53,17 +53,25 @@ using mapnik::transcoder;
geos_featureset::geos_featureset(GEOSGeometry* geometry,
GEOSGeometry* extent,
int identifier,
const std::string& field,
const std::string& field_name,
const std::string& encoding,
const bool multiple_geometries)
bool multiple_geometries)
: geometry_(geometry),
tr_(new transcoder(encoding)),
extent_(extent),
identifier_(identifier),
field_(field),
field_name_(field_name),
multiple_geometries_(multiple_geometries),
already_rendered_(false)
{
}
geos_featureset::~geos_featureset() {}
geos_featureset::~geos_featureset()
{
}
feature_ptr geos_featureset::next()
{
@ -73,11 +81,47 @@ feature_ptr geos_featureset::next()
if (GEOSisValid(geometry_) && ! GEOSisEmpty(geometry_))
{
//if (*extent_ != NULL && GEOSWithin(geometry_, *extent_))
bool render_geometry = true;
if (*extent_ != NULL && GEOSisValid(*extent_) && !GEOSisEmpty(*extent_))
{
feature_ptr feature(new Feature(0));
const int type = GEOSGeomTypeId(*extent_);
render_geometry = false;
switch ( type )
{
case GEOS_POINT:
if (GEOSIntersects(*extent_, geometry_))
{
render_geometry = true;
}
break;
case GEOS_POLYGON:
if (GEOSContains(*extent_, geometry_)
|| GEOSWithin(geometry_, *extent_)
|| GEOSEquals(geometry_, *extent_))
{
render_geometry = true;
}
break;
default:
#ifdef MAPNIK_DEBUG
clog << "unknown extent geometry_type=" << type << endl;
#endif
break;
}
}
if (render_geometry)
{
feature_ptr feature(new Feature(identifier_));
geos_converter::convert_geometry (geometry_, feature, multiple_geometries_);
if (field_ != "")
{
boost::put(*feature, field_name_, tr_->transcode(field_.c_str()));
}
return feature;
}

View file

@ -42,8 +42,11 @@ class geos_featureset : public mapnik::Featureset
public:
geos_featureset(GEOSGeometry* geometry,
GEOSGeometry* extent,
int identifier,
const std::string& field,
const std::string& field_name,
const std::string& encoding,
const bool multiple_geometries);
bool multiple_geometries);
virtual ~geos_featureset();
mapnik::feature_ptr next();
@ -51,10 +54,12 @@ private:
GEOSGeometry* geometry_;
boost::scoped_ptr<mapnik::transcoder> tr_;
geos_feature_ptr extent_;
int identifier_;
std::string field_;
std::string field_name_;
bool multiple_geometries_;
bool already_rendered_;
geos_featureset(const geos_featureset&);
const geos_featureset& operator=(const geos_featureset&);
};