2009-01-13 01:56:09 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:54:32 +01:00
|
|
|
*
|
2009-01-13 01:56:09 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/python.hpp>
|
|
|
|
#include <boost/python/def.hpp>
|
2011-02-28 13:40:59 +01:00
|
|
|
#include <boost/python/exception_translator.hpp>
|
|
|
|
#include <boost/python/manage_new_object.hpp>
|
2011-09-08 13:52:59 +02:00
|
|
|
#include <boost/python/iterator.hpp>
|
|
|
|
#include <boost/ptr_container/ptr_vector.hpp>
|
2011-12-09 02:13:41 +01:00
|
|
|
#include <boost/make_shared.hpp>
|
2011-09-08 13:52:59 +02:00
|
|
|
|
2009-01-13 01:56:09 +01:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/geometry.hpp>
|
2011-02-28 13:40:59 +01:00
|
|
|
#include <mapnik/wkt/wkt_factory.hpp>
|
2011-09-13 13:54:12 +02:00
|
|
|
#include <mapnik/wkb.hpp>
|
2012-08-17 13:48:41 +02:00
|
|
|
#include <mapnik/json/geometry_parser.hpp>
|
2012-02-20 13:05:11 +01:00
|
|
|
#include <mapnik/json/geojson_generator.hpp>
|
2011-12-09 02:13:41 +01:00
|
|
|
|
|
|
|
#include <boost/version.hpp>
|
|
|
|
#if BOOST_VERSION >= 104700
|
2011-12-03 04:22:04 +01:00
|
|
|
#include <mapnik/util/geometry_to_wkb.hpp>
|
2011-12-08 17:08:15 +01:00
|
|
|
#include <mapnik/util/geometry_to_wkt.hpp>
|
2011-12-09 02:13:41 +01:00
|
|
|
#endif
|
2011-02-28 13:40:59 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using mapnik::from_wkt;
|
|
|
|
using mapnik::geometry_type;
|
|
|
|
|
2011-09-08 13:52:59 +02:00
|
|
|
typedef boost::ptr_vector<geometry_type> path_type;
|
|
|
|
|
|
|
|
geometry_type const& getitem_impl(path_type & p, int key)
|
|
|
|
{
|
2012-06-26 20:23:26 +02:00
|
|
|
if (key >=0 && key < static_cast<int>(p.size()))
|
2011-09-08 13:52:59 +02:00
|
|
|
return p[key];
|
|
|
|
PyErr_SetString(PyExc_IndexError, "Index is out of range");
|
|
|
|
throw boost::python::error_already_set();
|
|
|
|
}
|
|
|
|
|
2011-12-03 04:22:04 +01:00
|
|
|
void add_wkt_impl(path_type& p, std::string const& wkt)
|
2011-02-28 13:40:59 +01:00
|
|
|
{
|
2012-08-17 13:48:41 +02:00
|
|
|
if (!mapnik::from_wkt(wkt , p))
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
2011-02-28 13:40:59 +01:00
|
|
|
}
|
|
|
|
|
2012-08-17 13:48:41 +02:00
|
|
|
void add_wkb_impl(path_type& p, std::string const& wkb)
|
2011-09-13 13:54:12 +02:00
|
|
|
{
|
2012-08-17 13:48:41 +02:00
|
|
|
if (!mapnik::geometry_utils::from_wkb(p, wkb.c_str(), wkb.size()))
|
|
|
|
throw std::runtime_error("Failed to parse WKB");
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_geojson_impl(path_type& p, std::string const& json)
|
|
|
|
{
|
|
|
|
if (!mapnik::json::from_geojson(json, p))
|
|
|
|
throw std::runtime_error("Failed to parse geojson geometry");
|
2011-09-13 13:54:12 +02:00
|
|
|
}
|
|
|
|
|
2011-12-03 04:22:04 +01:00
|
|
|
boost::shared_ptr<path_type> from_wkt_impl(std::string const& wkt)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<path_type> paths = boost::make_shared<path_type>();
|
2012-08-17 13:48:41 +02:00
|
|
|
if (!mapnik::from_wkt(wkt, *paths))
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
2011-12-03 04:22:04 +01:00
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<path_type> from_wkb_impl(std::string const& wkb)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<path_type> paths = boost::make_shared<path_type>();
|
2012-08-17 13:48:41 +02:00
|
|
|
if (!mapnik::geometry_utils::from_wkb(*paths, wkb.c_str(), wkb.size()))
|
|
|
|
throw std::runtime_error("Failed to parse WKB");
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<path_type> from_geojson_impl(std::string const& json)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<path_type> paths = boost::make_shared<path_type>();
|
|
|
|
if (! mapnik::json::from_geojson(json, *paths))
|
|
|
|
throw std::runtime_error("Failed to parse geojson geometry");
|
2011-12-03 04:22:04 +01:00
|
|
|
return paths;
|
2011-02-28 13:40:59 +01:00
|
|
|
}
|
2009-01-13 01:56:09 +01:00
|
|
|
|
2011-12-03 04:22:04 +01:00
|
|
|
}
|
|
|
|
|
2012-01-05 12:56:38 +01:00
|
|
|
inline std::string boost_version()
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << BOOST_VERSION/100000 << "." << BOOST_VERSION/100 % 1000 << "." << BOOST_VERSION % 100;
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
2012-01-06 01:45:12 +01:00
|
|
|
#if BOOST_VERSION >= 104700
|
2012-01-05 12:56:38 +01:00
|
|
|
PyObject* to_wkb( geometry_type const& geom, mapnik::util::wkbByteOrder byte_order)
|
2011-12-03 04:22:04 +01:00
|
|
|
{
|
2012-01-05 12:56:38 +01:00
|
|
|
mapnik::util::wkb_buffer_ptr wkb = mapnik::util::to_wkb(geom,byte_order);
|
2012-08-07 15:47:28 +02:00
|
|
|
if (wkb)
|
|
|
|
{
|
|
|
|
return
|
2011-12-03 04:22:04 +01:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
2012-08-07 15:47:28 +02:00
|
|
|
::PyBytes_FromStringAndSize
|
2011-12-03 04:22:04 +01:00
|
|
|
#else
|
2012-08-07 15:47:28 +02:00
|
|
|
::PyString_FromStringAndSize
|
2011-12-03 04:22:04 +01:00
|
|
|
#endif
|
2012-08-07 15:47:28 +02:00
|
|
|
((const char*)wkb->buffer(),wkb->size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-07 16:43:40 +02:00
|
|
|
Py_RETURN_NONE;
|
2012-08-07 15:47:28 +02:00
|
|
|
}
|
2012-01-06 01:45:12 +01:00
|
|
|
}
|
2011-12-09 02:13:41 +01:00
|
|
|
#else
|
2012-01-06 01:45:12 +01:00
|
|
|
PyObject* to_wkb( geometry_type const& geom)
|
|
|
|
{
|
2012-02-02 02:30:26 +01:00
|
|
|
throw std::runtime_error("mapnik::to_wkb() requires at least boost 1.47 while your build was compiled against boost "
|
2012-01-05 12:56:38 +01:00
|
|
|
+ boost_version());
|
2011-12-03 04:22:04 +01:00
|
|
|
}
|
2012-01-06 01:45:12 +01:00
|
|
|
#endif
|
2011-12-03 04:22:04 +01:00
|
|
|
|
2012-01-05 12:56:38 +01:00
|
|
|
|
2012-01-06 01:45:12 +01:00
|
|
|
#if BOOST_VERSION >= 104700
|
2012-01-05 12:56:38 +01:00
|
|
|
PyObject* to_wkb2( path_type const& p, mapnik::util::wkbByteOrder byte_order)
|
|
|
|
{
|
|
|
|
mapnik::util::wkb_buffer_ptr wkb = mapnik::util::to_wkb(p,byte_order);
|
2012-08-07 15:47:28 +02:00
|
|
|
if (wkb)
|
|
|
|
{
|
|
|
|
return
|
2012-01-05 12:56:38 +01:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
2012-08-07 15:47:28 +02:00
|
|
|
::PyBytes_FromStringAndSize
|
2012-01-05 12:56:38 +01:00
|
|
|
#else
|
2012-08-07 15:47:28 +02:00
|
|
|
::PyString_FromStringAndSize
|
2012-01-05 12:56:38 +01:00
|
|
|
#endif
|
2012-08-07 15:47:28 +02:00
|
|
|
((const char*)wkb->buffer(),wkb->size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-07 16:43:40 +02:00
|
|
|
Py_RETURN_NONE;
|
2012-08-07 15:47:28 +02:00
|
|
|
}
|
2012-01-06 01:45:12 +01:00
|
|
|
}
|
2012-01-05 12:56:38 +01:00
|
|
|
#else
|
2012-01-06 01:45:12 +01:00
|
|
|
PyObject* to_wkb2( path_type const& p)
|
|
|
|
{
|
2012-02-02 02:30:26 +01:00
|
|
|
throw std::runtime_error("mapnik::to_wkb() requires at least boost 1.47 while your build was compiled against boost "
|
2012-01-05 12:56:38 +01:00
|
|
|
+ boost_version());
|
|
|
|
}
|
2012-01-06 01:45:12 +01:00
|
|
|
#endif
|
2012-01-05 12:56:38 +01:00
|
|
|
|
|
|
|
|
2011-12-08 17:08:15 +01:00
|
|
|
std::string to_wkt( geometry_type const& geom)
|
|
|
|
{
|
2011-12-09 02:13:41 +01:00
|
|
|
#if BOOST_VERSION >= 104700
|
2011-12-08 17:08:15 +01:00
|
|
|
std::string wkt; // Use Python String directly ?
|
|
|
|
bool result = mapnik::util::to_wkt(wkt,geom);
|
2012-02-02 02:30:26 +01:00
|
|
|
if (!result)
|
2011-12-08 17:08:15 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Generate WKT failed");
|
|
|
|
}
|
|
|
|
return wkt;
|
2011-12-09 02:13:41 +01:00
|
|
|
#else
|
2012-02-02 02:30:26 +01:00
|
|
|
throw std::runtime_error("mapnik::to_wkt() requires at least boost 1.47 while your build was compiled against boost "
|
2012-01-05 12:56:38 +01:00
|
|
|
+ boost_version());
|
2011-12-09 02:13:41 +01:00
|
|
|
#endif
|
2011-12-08 17:08:15 +01:00
|
|
|
}
|
2011-12-03 04:22:04 +01:00
|
|
|
|
2011-12-12 19:19:52 +01:00
|
|
|
std::string to_wkt2( path_type const& geom)
|
|
|
|
{
|
|
|
|
#if BOOST_VERSION >= 104700
|
|
|
|
std::string wkt; // Use Python String directly ?
|
|
|
|
bool result = mapnik::util::to_wkt(wkt,geom);
|
2012-02-02 02:30:26 +01:00
|
|
|
if (!result)
|
2011-12-12 19:19:52 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Generate WKT failed");
|
|
|
|
}
|
|
|
|
return wkt;
|
|
|
|
#else
|
2012-02-02 02:30:26 +01:00
|
|
|
throw std::runtime_error("mapnik::to_wkt() requires at least boost 1.47 while your build was compiled against boost "
|
2012-01-05 12:56:38 +01:00
|
|
|
+ boost_version());
|
2011-12-12 19:19:52 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-20 13:05:11 +01:00
|
|
|
std::string to_geojson( path_type const& geom)
|
|
|
|
{
|
|
|
|
std::string json;
|
|
|
|
mapnik::json::geometry_generator g;
|
|
|
|
if (!g.generate(json,geom))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to generate GeoJSON");
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
2011-12-12 19:19:52 +01:00
|
|
|
|
2009-01-13 01:56:09 +01:00
|
|
|
void export_geometry()
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
using namespace boost::python;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2011-02-28 13:40:59 +01:00
|
|
|
enum_<mapnik::eGeomType>("GeometryType")
|
|
|
|
.value("Point",mapnik::Point)
|
|
|
|
.value("LineString",mapnik::LineString)
|
|
|
|
.value("Polygon",mapnik::Polygon)
|
|
|
|
;
|
2012-01-05 12:56:38 +01:00
|
|
|
|
2012-01-07 01:22:39 +01:00
|
|
|
#if BOOST_VERSION >= 104700
|
2012-01-05 12:56:38 +01:00
|
|
|
enum_<mapnik::util::wkbByteOrder>("wkbByteOrder")
|
|
|
|
.value("XDR",mapnik::util::wkbXDR)
|
|
|
|
.value("NDR",mapnik::util::wkbNDR)
|
|
|
|
;
|
2012-01-07 01:22:39 +01:00
|
|
|
#endif
|
2012-02-02 02:30:26 +01:00
|
|
|
|
2011-02-28 13:40:59 +01:00
|
|
|
using mapnik::geometry_type;
|
2011-12-03 04:22:04 +01:00
|
|
|
class_<geometry_type, std::auto_ptr<geometry_type>, boost::noncopyable>("Geometry2d",no_init)
|
2010-11-03 14:19:15 +01:00
|
|
|
.def("envelope",&geometry_type::envelope)
|
|
|
|
// .def("__str__",&geometry_type::to_string)
|
|
|
|
.def("type",&geometry_type::type)
|
2011-12-03 04:22:04 +01:00
|
|
|
.def("to_wkb",&to_wkb)
|
2011-12-08 17:08:15 +01:00
|
|
|
.def("to_wkt",&to_wkt)
|
2010-11-03 14:19:15 +01:00
|
|
|
// TODO add other geometry_type methods
|
2010-06-02 13:03:30 +02:00
|
|
|
;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2011-12-03 04:22:04 +01:00
|
|
|
class_<path_type, boost::shared_ptr<path_type>, boost::noncopyable>("Path")
|
2011-09-08 13:52:59 +02:00
|
|
|
.def("__getitem__", getitem_impl,return_value_policy<reference_existing_object>())
|
2011-09-13 15:23:23 +02:00
|
|
|
.def("__len__", &path_type::size)
|
2011-12-03 04:22:04 +01:00
|
|
|
.def("add_wkt",add_wkt_impl)
|
|
|
|
.def("add_wkb",add_wkb_impl)
|
2012-08-17 13:48:41 +02:00
|
|
|
.def("add_geojson",add_geojson_impl)
|
2011-12-12 19:19:52 +01:00
|
|
|
.def("to_wkt",&to_wkt2)
|
2012-01-05 12:56:38 +01:00
|
|
|
.def("to_wkb",&to_wkb2)
|
2011-09-08 13:52:59 +02:00
|
|
|
.def("from_wkt",from_wkt_impl)
|
2011-09-13 13:54:12 +02:00
|
|
|
.def("from_wkb",from_wkb_impl)
|
2012-08-17 13:48:41 +02:00
|
|
|
.def("from_geojson",from_geojson_impl)
|
2012-02-20 13:05:11 +01:00
|
|
|
.def("to_geojson",to_geojson)
|
2011-12-03 04:22:04 +01:00
|
|
|
.staticmethod("from_wkt")
|
|
|
|
.staticmethod("from_wkb")
|
2012-08-17 13:48:41 +02:00
|
|
|
.staticmethod("from_geojson")
|
2011-09-08 13:52:59 +02:00
|
|
|
;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-01-13 01:56:09 +01:00
|
|
|
}
|