2010-11-10 11:46:09 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2010-11-10 11:46:09 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-23 21:19:56 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2010-11-10 11:46:09 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-10-23 21:19:56 +02:00
|
|
|
#include "geos_datasource.hpp"
|
|
|
|
#include "geos_featureset.hpp"
|
|
|
|
|
|
|
|
// stl
|
2010-11-10 11:46:09 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <cstdarg>
|
|
|
|
|
|
|
|
// mapnik
|
|
|
|
#include <mapnik/ptree_helpers.hpp>
|
|
|
|
#include <mapnik/geom_util.hpp>
|
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2010-11-12 23:11:13 +01:00
|
|
|
#include <boost/limits.hpp>
|
2010-11-10 12:35:19 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2011-05-17 01:41:34 +02:00
|
|
|
#include <boost/make_shared.hpp>
|
2010-11-10 11:46:09 +01:00
|
|
|
|
|
|
|
// geos
|
|
|
|
#include <geos_c.h>
|
|
|
|
|
2010-11-10 12:35:19 +01:00
|
|
|
using boost::lexical_cast;
|
|
|
|
using boost::bad_lexical_cast;
|
|
|
|
|
2010-11-10 11:46:09 +01:00
|
|
|
using mapnik::box2d;
|
|
|
|
using mapnik::coord2d;
|
|
|
|
using mapnik::query;
|
|
|
|
using mapnik::featureset_ptr;
|
|
|
|
using mapnik::layer_descriptor;
|
|
|
|
using mapnik::attribute_descriptor;
|
|
|
|
using mapnik::datasource_exception;
|
2011-10-23 21:19:56 +02:00
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::parameters;
|
2010-11-10 11:46:09 +01:00
|
|
|
using mapnik::filter_in_box;
|
|
|
|
using mapnik::filter_at_point;
|
|
|
|
|
2011-10-23 21:19:56 +02:00
|
|
|
DATASOURCE_PLUGIN(geos_datasource)
|
2010-11-10 11:46:09 +01:00
|
|
|
|
2010-11-18 23:12:39 +01:00
|
|
|
void geos_notice(const char* fmt, ...)
|
2010-11-10 11:46:09 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
2010-11-18 23:12:39 +01:00
|
|
|
fprintf( stdout, "GEOS Plugin: (GEOS NOTICE) ");
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-10 11:46:09 +01:00
|
|
|
va_start (ap, fmt);
|
|
|
|
vfprintf( stdout, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf( stdout, "\n" );
|
|
|
|
}
|
|
|
|
|
2010-11-18 23:12:39 +01:00
|
|
|
void geos_error(const char* fmt, ...)
|
2010-11-10 11:46:09 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
2010-11-18 23:12:39 +01:00
|
|
|
fprintf( stdout, "GEOS Plugin: (GEOS ERROR) ");
|
2010-11-10 11:46:09 +01:00
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
vfprintf( stdout, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf( stdout, "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
geos_datasource::geos_datasource(parameters const& params, bool bind)
|
2011-11-14 04:37:50 +01:00
|
|
|
: datasource(params),
|
|
|
|
extent_(),
|
|
|
|
extent_initialized_(false),
|
|
|
|
type_(datasource::Vector),
|
|
|
|
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding", "utf-8")),
|
|
|
|
geometry_data_(""),
|
|
|
|
geometry_data_name_("name"),
|
|
|
|
geometry_id_(1)
|
2010-11-10 11:46:09 +01:00
|
|
|
{
|
2010-11-10 12:35:19 +01:00
|
|
|
boost::optional<std::string> geometry = params.get<std::string>("wkt");
|
2011-10-23 21:19:56 +02:00
|
|
|
if (! geometry) throw datasource_exception("missing <wkt> parameter");
|
2010-11-12 22:18:53 +01:00
|
|
|
geometry_string_ = *geometry;
|
2012-02-02 02:37:35 +01:00
|
|
|
|
2010-11-14 16:02:01 +01:00
|
|
|
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");
|
2010-11-18 23:12:39 +01:00
|
|
|
if (gdata_name) geometry_data_name_ = *gdata_name;
|
2010-11-14 16:02:01 +01:00
|
|
|
|
|
|
|
desc_.add_descriptor(attribute_descriptor(geometry_data_name_, mapnik::String));
|
|
|
|
|
2010-11-10 11:46:09 +01:00
|
|
|
if (bind)
|
|
|
|
{
|
|
|
|
this->bind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
geos_datasource::~geos_datasource()
|
|
|
|
{
|
2011-11-14 04:37:50 +01:00
|
|
|
if (is_bound_)
|
2010-11-10 11:46:09 +01:00
|
|
|
{
|
2010-11-12 22:18:53 +01:00
|
|
|
geometry_.set_feature(0);
|
|
|
|
|
2010-11-10 11:46:09 +01:00
|
|
|
finishGEOS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void geos_datasource::bind() const
|
|
|
|
{
|
2011-11-14 04:37:50 +01:00
|
|
|
if (is_bound_) return;
|
2010-11-10 12:35:19 +01:00
|
|
|
|
2010-11-18 23:12:39 +01:00
|
|
|
// open geos driver
|
|
|
|
initGEOS(geos_notice, geos_error);
|
2010-11-12 22:18:53 +01:00
|
|
|
|
|
|
|
// parse the string into geometry
|
|
|
|
geometry_.set_feature(GEOSGeomFromWKT(geometry_string_.c_str()));
|
|
|
|
if (*geometry_ == NULL || ! GEOSisValid(*geometry_))
|
|
|
|
{
|
2010-11-18 23:12:39 +01:00
|
|
|
throw datasource_exception("GEOS Plugin: invalid <wkt> geometry specified");
|
2010-11-12 22:18:53 +01:00
|
|
|
}
|
|
|
|
|
2010-11-14 16:02:01 +01:00
|
|
|
// try to obtain the extent from the geometry itself
|
|
|
|
if (! extent_initialized_)
|
2010-11-10 12:35:19 +01:00
|
|
|
{
|
2010-11-12 23:11:13 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-23 21:19:56 +02:00
|
|
|
std::clog << "GEOS Plugin: initializing extent from geometry" << std::endl;
|
2010-11-12 23:11:13 +01:00
|
|
|
#endif
|
|
|
|
|
2010-11-18 23:12:39 +01:00
|
|
|
if (GEOSGeomTypeId(*geometry_) == GEOS_POINT)
|
|
|
|
{
|
|
|
|
double x, y;
|
|
|
|
unsigned int size;
|
|
|
|
|
|
|
|
const GEOSCoordSequence* cs = GEOSGeom_getCoordSeq(*geometry_);
|
|
|
|
|
|
|
|
GEOSCoordSeq_getSize(cs, &size);
|
|
|
|
GEOSCoordSeq_getX(cs, 0, &x);
|
|
|
|
GEOSCoordSeq_getY(cs, 0, &y);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-23 21:19:56 +02:00
|
|
|
extent_.init(x, y, x, y);
|
2010-11-18 23:12:39 +01:00
|
|
|
extent_initialized_ = true;
|
|
|
|
}
|
|
|
|
else
|
2010-11-12 22:18:53 +01:00
|
|
|
{
|
2010-11-18 23:12:39 +01:00
|
|
|
geos_feature_ptr envelope (GEOSEnvelope(*geometry_));
|
|
|
|
if (*envelope != NULL && GEOSisValid(*envelope))
|
|
|
|
{
|
2010-11-12 23:11:13 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 23:12:39 +01:00
|
|
|
char* wkt = GEOSGeomToWKT(*envelope);
|
2011-10-23 21:19:56 +02:00
|
|
|
std::clog << "GEOS Plugin: getting coord sequence from: " << wkt << std::endl;
|
2010-11-18 23:12:39 +01:00
|
|
|
GEOSFree(wkt);
|
2010-11-12 23:11:13 +01:00
|
|
|
#endif
|
2010-11-12 22:18:53 +01:00
|
|
|
|
2010-11-18 23:12:39 +01:00
|
|
|
const GEOSGeometry* exterior = GEOSGetExteriorRing(*envelope);
|
|
|
|
if (exterior != NULL && GEOSisValid(exterior))
|
2010-11-12 22:18:53 +01:00
|
|
|
{
|
2010-11-18 23:12:39 +01:00
|
|
|
const GEOSCoordSequence* cs = GEOSGeom_getCoordSeq(exterior);
|
|
|
|
if (cs != NULL)
|
|
|
|
{
|
2010-11-12 23:11:13 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-23 21:19:56 +02:00
|
|
|
std::clog << "GEOS Plugin: iterating boundary points" << std::endl;
|
2010-11-12 23:11:13 +01:00
|
|
|
#endif
|
2010-11-12 22:18:53 +01:00
|
|
|
|
2010-11-18 23:12:39 +01:00
|
|
|
double x, y;
|
|
|
|
double minx = std::numeric_limits<float>::max(),
|
2011-11-14 04:37:50 +01:00
|
|
|
miny = std::numeric_limits<float>::max(),
|
|
|
|
maxx = -std::numeric_limits<float>::max(),
|
|
|
|
maxy = -std::numeric_limits<float>::max();
|
2010-11-18 23:12:39 +01:00
|
|
|
|
2011-10-23 21:19:56 +02:00
|
|
|
unsigned int num_points;
|
2010-11-18 23:12:39 +01:00
|
|
|
GEOSCoordSeq_getSize(cs, &num_points);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < num_points; ++i)
|
|
|
|
{
|
|
|
|
GEOSCoordSeq_getX(cs, i, &x);
|
|
|
|
GEOSCoordSeq_getY(cs, i, &y);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-18 23:12:39 +01:00
|
|
|
if (x < minx) minx = x;
|
|
|
|
if (x > maxx) maxx = x;
|
|
|
|
if (y < miny) miny = y;
|
|
|
|
if (y > maxy) maxy = y;
|
|
|
|
}
|
|
|
|
|
2011-10-23 21:19:56 +02:00
|
|
|
extent_.init(minx, miny, maxx, maxy);
|
2010-11-18 23:12:39 +01:00
|
|
|
extent_initialized_ = true;
|
2010-11-12 23:11:13 +01:00
|
|
|
}
|
|
|
|
}
|
2010-11-12 22:18:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-11-10 12:35:19 +01:00
|
|
|
}
|
2010-11-12 23:11:13 +01:00
|
|
|
|
|
|
|
if (! extent_initialized_)
|
2011-10-23 21:19:56 +02:00
|
|
|
{
|
2010-11-18 23:12:39 +01:00
|
|
|
throw datasource_exception("GEOS Plugin: cannot determine extent for <wkt> geometry");
|
2011-10-23 21:19:56 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-10 12:35:19 +01:00
|
|
|
is_bound_ = true;
|
2010-11-10 11:46:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string geos_datasource::name()
|
|
|
|
{
|
|
|
|
return "geos";
|
|
|
|
}
|
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
mapnik::datasource::datasource_t geos_datasource::type() const
|
2010-11-10 11:46:09 +01:00
|
|
|
{
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
box2d<double> geos_datasource::envelope() const
|
|
|
|
{
|
2011-10-23 21:19:56 +02:00
|
|
|
if (! is_bound_) bind();
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-10 11:46:09 +01:00
|
|
|
return extent_;
|
|
|
|
}
|
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
boost::optional<mapnik::datasource::geometry_t> geos_datasource::get_geometry_type() const
|
|
|
|
{
|
|
|
|
if (! is_bound_) bind();
|
|
|
|
boost::optional<mapnik::datasource::geometry_t> result;
|
|
|
|
|
|
|
|
// get geometry type
|
|
|
|
const int type = GEOSGeomTypeId(*geometry_);
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case GEOS_POINT:
|
|
|
|
case GEOS_MULTIPOINT:
|
|
|
|
result.reset(mapnik::datasource::Point);
|
|
|
|
break;
|
|
|
|
case GEOS_LINESTRING:
|
|
|
|
case GEOS_LINEARRING:
|
|
|
|
case GEOS_MULTILINESTRING:
|
|
|
|
result.reset(mapnik::datasource::LineString);
|
|
|
|
break;
|
|
|
|
case GEOS_POLYGON:
|
|
|
|
case GEOS_MULTIPOLYGON:
|
|
|
|
result.reset(mapnik::datasource::Polygon);
|
|
|
|
break;
|
|
|
|
case GEOS_GEOMETRYCOLLECTION:
|
|
|
|
result.reset(mapnik::datasource::Collection);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-02-02 02:37:35 +01:00
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-11-10 11:46:09 +01:00
|
|
|
layer_descriptor geos_datasource::get_descriptor() const
|
|
|
|
{
|
2011-10-23 21:19:56 +02:00
|
|
|
if (! is_bound_) bind();
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-10 11:46:09 +01:00
|
|
|
return desc_;
|
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr geos_datasource::features(query const& q) const
|
|
|
|
{
|
2011-10-23 21:19:56 +02:00
|
|
|
if (! is_bound_) bind();
|
2010-11-10 11:46:09 +01:00
|
|
|
|
|
|
|
const mapnik::box2d<double> extent = q.get_bbox();
|
|
|
|
|
|
|
|
std::ostringstream s;
|
2010-11-12 23:11:13 +01:00
|
|
|
s << "POLYGON(("
|
2010-11-10 11:46:09 +01:00
|
|
|
<< extent.minx() << " " << extent.miny() << ","
|
|
|
|
<< extent.maxx() << " " << extent.miny() << ","
|
2010-11-12 22:18:53 +01:00
|
|
|
<< extent.maxx() << " " << extent.maxy() << ","
|
|
|
|
<< extent.minx() << " " << extent.maxy() << ","
|
2010-11-10 11:46:09 +01:00
|
|
|
<< extent.minx() << " " << extent.miny()
|
2010-11-12 23:11:13 +01:00
|
|
|
<< "))";
|
2010-11-10 11:46:09 +01:00
|
|
|
|
2010-11-12 22:18:53 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-23 21:19:56 +02:00
|
|
|
std::clog << "GEOS Plugin: using extent: " << s.str() << std::endl;
|
2010-11-12 22:18:53 +01:00
|
|
|
#endif
|
|
|
|
|
2011-05-17 01:41:34 +02:00
|
|
|
return boost::make_shared<geos_featureset>(*geometry_,
|
2010-11-12 22:18:53 +01:00
|
|
|
GEOSGeomFromWKT(s.str().c_str()),
|
2010-11-14 16:02:01 +01:00
|
|
|
geometry_id_,
|
|
|
|
geometry_data_,
|
|
|
|
geometry_data_name_,
|
2011-12-06 13:53:16 +01:00
|
|
|
desc_.get_encoding());
|
2010-11-10 11:46:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr geos_datasource::features_at_point(coord2d const& pt) const
|
|
|
|
{
|
2011-10-23 21:19:56 +02:00
|
|
|
if (! is_bound_) bind();
|
2010-11-10 11:46:09 +01:00
|
|
|
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "POINT(" << pt.x << " " << pt.y << ")";
|
|
|
|
|
2010-11-12 22:18:53 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-23 21:19:56 +02:00
|
|
|
std::clog << "GEOS Plugin: using point: " << s.str() << std::endl;
|
2010-11-12 22:18:53 +01:00
|
|
|
#endif
|
|
|
|
|
2011-05-17 01:41:34 +02:00
|
|
|
return boost::make_shared<geos_featureset>(*geometry_,
|
2010-11-12 22:18:53 +01:00
|
|
|
GEOSGeomFromWKT(s.str().c_str()),
|
2010-11-14 16:02:01 +01:00
|
|
|
geometry_id_,
|
|
|
|
geometry_data_,
|
|
|
|
geometry_data_name_,
|
2011-12-06 13:53:16 +01:00
|
|
|
desc_.get_encoding());
|
2010-11-10 11:46:09 +01:00
|
|
|
}
|