2009-01-13 00:56:09 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:54:32 +00:00
|
|
|
*
|
2009-01-13 00:56:09 +00: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2014-08-12 16:43:04 +00:00
|
|
|
#include <mapnik/config.hpp>
|
|
|
|
|
2014-10-21 23:37:27 +00:00
|
|
|
// boost
|
2013-10-23 22:42:01 +00:00
|
|
|
#include "boost_std_shared_shim.hpp"
|
2014-10-21 23:37:27 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
|
|
|
|
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
2013-10-23 22:42:01 +00:00
|
|
|
|
2009-01-13 00:56:09 +00:00
|
|
|
#include <boost/python.hpp>
|
|
|
|
#include <boost/python/def.hpp>
|
2011-02-28 12:40:59 +00:00
|
|
|
#include <boost/python/exception_translator.hpp>
|
|
|
|
#include <boost/python/manage_new_object.hpp>
|
2011-09-08 11:52:59 +00:00
|
|
|
#include <boost/python/iterator.hpp>
|
2012-12-16 20:50:36 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2014-07-24 21:31:59 +00:00
|
|
|
#include <boost/version.hpp>
|
2014-10-21 23:37:27 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2011-09-08 11:52:59 +00:00
|
|
|
|
2009-01-13 00:56:09 +00:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/geometry.hpp>
|
2014-09-30 01:40:57 +00:00
|
|
|
#include <mapnik/geometry_container.hpp>
|
2014-07-24 21:31:59 +00:00
|
|
|
#include <mapnik/wkt/wkt_factory.hpp> // from_wkt
|
2011-12-08 16:08:15 +00:00
|
|
|
#include <mapnik/util/geometry_to_wkt.hpp>
|
2014-07-24 21:31:59 +00:00
|
|
|
#include <mapnik/json/geometry_parser.hpp> // from_geojson
|
|
|
|
#include <mapnik/util/geometry_to_geojson.hpp>
|
2013-04-19 11:11:18 +00:00
|
|
|
#include <mapnik/util/geometry_to_svg.hpp>
|
2014-07-24 21:31:59 +00:00
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include <mapnik/util/geometry_to_wkb.hpp>
|
2011-02-28 12:40:59 +00:00
|
|
|
|
2013-06-03 03:19:33 +00:00
|
|
|
// stl
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2011-02-28 12:40:59 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
mapnik::geometry_type const& getitem_impl(mapnik::geometry_container & p, int key)
|
2011-09-08 11:52:59 +00:00
|
|
|
{
|
2012-06-26 18:23:26 +00:00
|
|
|
if (key >=0 && key < static_cast<int>(p.size()))
|
2011-09-08 11:52:59 +00:00
|
|
|
return p[key];
|
|
|
|
PyErr_SetString(PyExc_IndexError, "Index is out of range");
|
|
|
|
throw boost::python::error_already_set();
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
void add_wkt_impl(mapnik::geometry_container& p, std::string const& wkt)
|
2011-02-28 12:40:59 +00:00
|
|
|
{
|
2012-08-17 11:48:41 +00:00
|
|
|
if (!mapnik::from_wkt(wkt , p))
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
2011-02-28 12:40:59 +00:00
|
|
|
}
|
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
void add_wkb_impl(mapnik::geometry_container& p, std::string const& wkb)
|
2011-09-13 11:54:12 +00:00
|
|
|
{
|
2012-08-17 11:48:41 +00:00
|
|
|
if (!mapnik::geometry_utils::from_wkb(p, wkb.c_str(), wkb.size()))
|
|
|
|
throw std::runtime_error("Failed to parse WKB");
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
void add_geojson_impl(mapnik::geometry_container& paths, std::string const& json)
|
2012-08-17 11:48:41 +00:00
|
|
|
{
|
2014-07-24 21:31:59 +00:00
|
|
|
if (!mapnik::json::from_geojson(json, paths))
|
2012-08-17 11:48:41 +00:00
|
|
|
throw std::runtime_error("Failed to parse geojson geometry");
|
2011-09-13 11:54:12 +00:00
|
|
|
}
|
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
std::shared_ptr<mapnik::geometry_container> from_wkt_impl(std::string const& wkt)
|
2011-12-03 03:22:04 +00:00
|
|
|
{
|
2014-07-24 21:31:59 +00:00
|
|
|
std::shared_ptr<mapnik::geometry_container> paths = std::make_shared<mapnik::geometry_container>();
|
2012-08-17 11:48:41 +00:00
|
|
|
if (!mapnik::from_wkt(wkt, *paths))
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
2011-12-03 03:22:04 +00:00
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
std::shared_ptr<mapnik::geometry_container> from_wkb_impl(std::string const& wkb)
|
2011-12-03 03:22:04 +00:00
|
|
|
{
|
2014-07-24 21:31:59 +00:00
|
|
|
std::shared_ptr<mapnik::geometry_container> paths = std::make_shared<mapnik::geometry_container>();
|
2012-08-17 11:48:41 +00:00
|
|
|
if (!mapnik::geometry_utils::from_wkb(*paths, wkb.c_str(), wkb.size()))
|
|
|
|
throw std::runtime_error("Failed to parse WKB");
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
std::shared_ptr<mapnik::geometry_container> from_geojson_impl(std::string const& json)
|
2012-08-17 11:48:41 +00:00
|
|
|
{
|
2014-07-24 21:31:59 +00:00
|
|
|
std::shared_ptr<mapnik::geometry_container> paths = std::make_shared<mapnik::geometry_container>();
|
|
|
|
if (!mapnik::json::from_geojson(json, *paths))
|
2012-08-17 11:48:41 +00:00
|
|
|
throw std::runtime_error("Failed to parse geojson geometry");
|
2011-12-03 03:22:04 +00:00
|
|
|
return paths;
|
2011-02-28 12:40:59 +00:00
|
|
|
}
|
2009-01-13 00:56:09 +00:00
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
mapnik::box2d<double> envelope_impl(mapnik::geometry_container & p)
|
2012-08-17 15:53:43 +00:00
|
|
|
{
|
|
|
|
mapnik::box2d<double> b;
|
|
|
|
bool first = true;
|
2013-04-24 15:40:35 +00:00
|
|
|
for (mapnik::geometry_type const& geom : p)
|
2012-08-17 15:53:43 +00:00
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
b = geom.envelope();
|
|
|
|
first=false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b.expand_to_include(geom.envelope());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:22:04 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 11:56:38 +00:00
|
|
|
inline std::string boost_version()
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << BOOST_VERSION/100000 << "." << BOOST_VERSION/100 % 1000 << "." << BOOST_VERSION % 100;
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
PyObject* to_wkb(mapnik::geometry_type const& geom, mapnik::util::wkbByteOrder byte_order)
|
2011-12-03 03:22:04 +00:00
|
|
|
{
|
2012-01-05 11:56:38 +00:00
|
|
|
mapnik::util::wkb_buffer_ptr wkb = mapnik::util::to_wkb(geom,byte_order);
|
2012-08-07 13:47:28 +00:00
|
|
|
if (wkb)
|
|
|
|
{
|
|
|
|
return
|
2011-12-03 03:22:04 +00:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
2012-08-07 13:47:28 +00:00
|
|
|
::PyBytes_FromStringAndSize
|
2011-12-03 03:22:04 +00:00
|
|
|
#else
|
2012-08-07 13:47:28 +00:00
|
|
|
::PyString_FromStringAndSize
|
2011-12-03 03:22:04 +00:00
|
|
|
#endif
|
2012-08-07 13:47:28 +00:00
|
|
|
((const char*)wkb->buffer(),wkb->size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-07 14:43:40 +00:00
|
|
|
Py_RETURN_NONE;
|
2012-08-07 13:47:28 +00:00
|
|
|
}
|
2012-01-06 00:45:12 +00:00
|
|
|
}
|
2012-01-05 11:56:38 +00:00
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
PyObject* to_wkb2( mapnik::geometry_container const& p, mapnik::util::wkbByteOrder byte_order)
|
2012-01-05 11:56:38 +00:00
|
|
|
{
|
|
|
|
mapnik::util::wkb_buffer_ptr wkb = mapnik::util::to_wkb(p,byte_order);
|
2012-08-07 13:47:28 +00:00
|
|
|
if (wkb)
|
|
|
|
{
|
|
|
|
return
|
2012-01-05 11:56:38 +00:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
2012-08-07 13:47:28 +00:00
|
|
|
::PyBytes_FromStringAndSize
|
2012-01-05 11:56:38 +00:00
|
|
|
#else
|
2012-08-07 13:47:28 +00:00
|
|
|
::PyString_FromStringAndSize
|
2012-01-05 11:56:38 +00:00
|
|
|
#endif
|
2012-08-07 13:47:28 +00:00
|
|
|
((const char*)wkb->buffer(),wkb->size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-07 14:43:40 +00:00
|
|
|
Py_RETURN_NONE;
|
2012-08-07 13:47:28 +00:00
|
|
|
}
|
2012-01-06 00:45:12 +00:00
|
|
|
}
|
2012-01-05 11:56:38 +00:00
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
std::string to_wkt(mapnik::geometry_type const& geom)
|
2011-12-08 16:08:15 +00:00
|
|
|
{
|
2014-07-24 21:31:59 +00:00
|
|
|
std::string wkt;
|
|
|
|
if (!mapnik::util::to_wkt(wkt,geom))
|
2011-12-08 16:08:15 +00:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Generate WKT failed");
|
|
|
|
}
|
|
|
|
return wkt;
|
|
|
|
}
|
2011-12-03 03:22:04 +00:00
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
std::string to_wkt2(mapnik::geometry_container const& geom)
|
2011-12-12 18:19:52 +00:00
|
|
|
{
|
2014-07-24 21:31:59 +00:00
|
|
|
std::string wkt;
|
|
|
|
if (!mapnik::util::to_wkt(wkt,geom))
|
2011-12-12 18:19:52 +00:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Generate WKT failed");
|
|
|
|
}
|
|
|
|
return wkt;
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
std::string to_geojson(mapnik::geometry_type const& geom)
|
2012-02-20 12:05:11 +00:00
|
|
|
{
|
2014-07-24 21:31:59 +00:00
|
|
|
std::string wkt;
|
|
|
|
if (!mapnik::util::to_geojson(wkt,geom))
|
2012-02-20 12:05:11 +00:00
|
|
|
{
|
2014-07-24 21:31:59 +00:00
|
|
|
throw std::runtime_error("Generate JSON failed");
|
2012-02-20 12:05:11 +00:00
|
|
|
}
|
2014-07-24 21:31:59 +00:00
|
|
|
return wkt;
|
2012-02-20 12:05:11 +00:00
|
|
|
}
|
2011-12-12 18:19:52 +00:00
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
std::string to_geojson2(mapnik::geometry_container const& geom)
|
2012-08-23 02:13:37 +00:00
|
|
|
{
|
2014-07-24 21:31:59 +00:00
|
|
|
std::string wkt;
|
|
|
|
if (!mapnik::util::to_geojson(wkt,geom))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Generate JSON failed");
|
|
|
|
}
|
|
|
|
return wkt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string to_svg(mapnik::geometry_type const& geom)
|
|
|
|
{
|
|
|
|
std::string svg;
|
|
|
|
if (!mapnik::util::to_svg(svg,geom))
|
2012-08-23 02:13:37 +00:00
|
|
|
{
|
2013-02-10 14:17:04 +00:00
|
|
|
throw std::runtime_error("Generate SVG failed");
|
2012-08-23 02:13:37 +00:00
|
|
|
}
|
|
|
|
return svg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// https://github.com/mapnik/mapnik/issues/1437
|
2014-07-24 21:31:59 +00:00
|
|
|
std::string to_svg2( mapnik::geometry_container const& geom)
|
2012-08-23 02:13:37 +00:00
|
|
|
{
|
|
|
|
std::string svg; // Use Python String directly ?
|
|
|
|
bool result = mapnik::util::to_svg(svg,geom);
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Generate WKT failed");
|
|
|
|
}
|
|
|
|
return svg;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
2009-01-13 00:56:09 +00:00
|
|
|
void export_geometry()
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
using namespace boost::python;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2013-09-03 11:15:31 +00:00
|
|
|
enum_<mapnik::geometry_type::types>("GeometryType")
|
|
|
|
.value("Point",mapnik::geometry_type::types::Point)
|
|
|
|
.value("LineString",mapnik::geometry_type::types::LineString)
|
|
|
|
.value("Polygon",mapnik::geometry_type::types::Polygon)
|
2011-02-28 12:40:59 +00:00
|
|
|
;
|
2012-01-05 11:56:38 +00:00
|
|
|
|
|
|
|
enum_<mapnik::util::wkbByteOrder>("wkbByteOrder")
|
|
|
|
.value("XDR",mapnik::util::wkbXDR)
|
|
|
|
.value("NDR",mapnik::util::wkbNDR)
|
|
|
|
;
|
2012-02-02 01:30:26 +00:00
|
|
|
|
2011-02-28 12:40:59 +00:00
|
|
|
using mapnik::geometry_type;
|
2014-07-24 21:31:59 +00:00
|
|
|
class_<mapnik::geometry_type, std::shared_ptr<mapnik::geometry_type>, boost::noncopyable>("Geometry2d",no_init)
|
|
|
|
.def("envelope",&mapnik::geometry_type::envelope)
|
|
|
|
// .def("__str__",&mapnik::geometry_type::to_string)
|
|
|
|
.def("type",&mapnik::geometry_type::type)
|
2011-12-03 03:22:04 +00:00
|
|
|
.def("to_wkb",&to_wkb)
|
2011-12-08 16:08:15 +00:00
|
|
|
.def("to_wkt",&to_wkt)
|
2014-07-24 21:31:59 +00:00
|
|
|
.def("to_geojson",&to_geojson)
|
2012-08-23 02:13:37 +00:00
|
|
|
.def("to_svg",&to_svg)
|
2010-11-03 13:19:15 +00:00
|
|
|
// TODO add other geometry_type methods
|
2010-06-02 11:03:30 +00:00
|
|
|
;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2014-07-24 21:31:59 +00:00
|
|
|
class_<mapnik::geometry_container, std::shared_ptr<mapnik::geometry_container>, boost::noncopyable>("Path")
|
2011-09-08 11:52:59 +00:00
|
|
|
.def("__getitem__", getitem_impl,return_value_policy<reference_existing_object>())
|
2014-07-24 21:31:59 +00:00
|
|
|
.def("__len__", &mapnik::geometry_container::size)
|
2012-08-17 15:53:43 +00:00
|
|
|
.def("envelope",envelope_impl)
|
2011-12-03 03:22:04 +00:00
|
|
|
.def("add_wkt",add_wkt_impl)
|
|
|
|
.def("add_wkb",add_wkb_impl)
|
2012-08-17 11:48:41 +00:00
|
|
|
.def("add_geojson",add_geojson_impl)
|
2011-12-12 18:19:52 +00:00
|
|
|
.def("to_wkt",&to_wkt2)
|
2012-08-23 02:13:37 +00:00
|
|
|
//.def("to_svg",&to_svg2)
|
2012-01-05 11:56:38 +00:00
|
|
|
.def("to_wkb",&to_wkb2)
|
2011-09-08 11:52:59 +00:00
|
|
|
.def("from_wkt",from_wkt_impl)
|
2011-09-13 11:54:12 +00:00
|
|
|
.def("from_wkb",from_wkb_impl)
|
2012-08-17 11:48:41 +00:00
|
|
|
.def("from_geojson",from_geojson_impl)
|
2014-07-24 21:31:59 +00:00
|
|
|
.def("to_geojson",&to_geojson2)
|
2011-12-03 03:22:04 +00:00
|
|
|
.staticmethod("from_wkt")
|
|
|
|
.staticmethod("from_wkb")
|
2012-08-17 11:48:41 +00:00
|
|
|
.staticmethod("from_geojson")
|
2011-09-08 11:52:59 +00:00
|
|
|
;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2009-01-13 00:56:09 +00:00
|
|
|
}
|