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)
|
|
|
|
*
|
2015-03-05 10:45:27 +01:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
|
|
|
*
|
2009-01-13 01:56: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2014-08-12 18:43:04 +02:00
|
|
|
#include <mapnik/config.hpp>
|
|
|
|
|
2014-10-22 01:37:27 +02:00
|
|
|
// boost
|
2013-10-24 00:42:01 +02:00
|
|
|
#include "boost_std_shared_shim.hpp"
|
2014-10-22 01:37:27 +02: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-24 00:42:01 +02:00
|
|
|
|
2009-01-13 01:56:09 +01:00
|
|
|
#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>
|
2012-12-16 21:50:36 +01:00
|
|
|
#include <boost/noncopyable.hpp>
|
2014-07-24 23:31:59 +02:00
|
|
|
#include <boost/version.hpp>
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2011-09-08 13:52:59 +02:00
|
|
|
|
2009-01-13 01:56:09 +01:00
|
|
|
// mapnik
|
2015-03-24 13:32:05 +01:00
|
|
|
#include <mapnik/geometry.hpp>
|
2015-03-05 10:45:27 +01:00
|
|
|
#include <mapnik/geometry_type.hpp>
|
|
|
|
#include <mapnik/geometry_envelope.hpp>
|
2015-03-05 12:11:23 +01:00
|
|
|
#include <mapnik/geometry_is_valid.hpp>
|
|
|
|
#include <mapnik/geometry_is_simple.hpp>
|
2015-03-24 22:16:58 +01:00
|
|
|
#include <mapnik/geometry_is_empty.hpp>
|
2015-03-05 12:11:23 +01:00
|
|
|
#include <mapnik/geometry_correct.hpp>
|
2015-03-11 12:56:38 +01:00
|
|
|
#include <mapnik/geometry_centroid.hpp>
|
2015-03-05 12:11:23 +01:00
|
|
|
|
2015-03-31 18:15:23 +02:00
|
|
|
#include <mapnik/wkt/wkt_factory.hpp> // from_wkt
|
2014-07-24 23:31:59 +02:00
|
|
|
#include <mapnik/json/geometry_parser.hpp> // from_geojson
|
2015-03-12 14:44:13 +01:00
|
|
|
#include <mapnik/util/geometry_to_geojson.hpp> // to_geojson
|
2015-03-16 16:15:21 +01:00
|
|
|
#include <mapnik/util/geometry_to_wkb.hpp> // to_wkb
|
2015-03-31 18:15:23 +02:00
|
|
|
#include <mapnik/util/geometry_to_wkt.hpp> // to_wkt
|
2015-03-05 10:45:27 +01:00
|
|
|
//#include <mapnik/util/geometry_to_svg.hpp>
|
2014-07-24 23:31:59 +02:00
|
|
|
#include <mapnik/wkb.hpp>
|
2015-03-12 14:44:13 +01:00
|
|
|
|
2011-02-28 13:40:59 +01:00
|
|
|
|
2013-06-03 05:19:33 +02:00
|
|
|
// stl
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2011-02-28 13:40:59 +01:00
|
|
|
namespace {
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
std::shared_ptr<mapnik::geometry::geometry<double> > from_wkb_impl(std::string const& wkb)
|
2015-03-11 12:40:39 +01:00
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
std::shared_ptr<mapnik::geometry::geometry<double> > geom = std::make_shared<mapnik::geometry::geometry<double> >();
|
2015-03-11 12:40:39 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
*geom = std::move(mapnik::geometry_utils::from_wkb(wkb.c_str(), wkb.size()));
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse WKB");
|
|
|
|
}
|
|
|
|
return geom;
|
|
|
|
}
|
2015-03-05 10:45:27 +01:00
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
std::shared_ptr<mapnik::geometry::geometry<double> > from_wkt_impl(std::string const& wkt)
|
2015-03-11 12:28:04 +01:00
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
std::shared_ptr<mapnik::geometry::geometry<double> > geom = std::make_shared<mapnik::geometry::geometry<double> >();
|
2015-03-11 12:28:04 +01:00
|
|
|
if (!mapnik::from_wkt(wkt, *geom))
|
|
|
|
throw std::runtime_error("Failed to parse WKT geometry");
|
|
|
|
return geom;
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
std::shared_ptr<mapnik::geometry::geometry<double> > from_geojson_impl(std::string const& json)
|
2012-08-17 13:48:41 +02:00
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
std::shared_ptr<mapnik::geometry::geometry<double> > geom = std::make_shared<mapnik::geometry::geometry<double> >();
|
2015-03-05 10:45:27 +01:00
|
|
|
if (!mapnik::json::from_geojson(json, *geom))
|
2012-08-17 13:48:41 +02:00
|
|
|
throw std::runtime_error("Failed to parse geojson geometry");
|
2015-03-05 10:45:27 +01:00
|
|
|
return geom;
|
2011-09-13 13:54:12 +02: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();
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
PyObject* to_wkb_impl(mapnik::geometry::geometry<double> const& geom, mapnik::wkbByteOrder byte_order)
|
2015-03-16 16:15:21 +01:00
|
|
|
{
|
|
|
|
mapnik::util::wkb_buffer_ptr wkb = mapnik::util::to_wkb(geom,byte_order);
|
|
|
|
if (wkb)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
|
|
|
::PyBytes_FromStringAndSize
|
|
|
|
#else
|
|
|
|
::PyString_FromStringAndSize
|
|
|
|
#endif
|
|
|
|
((const char*)wkb->buffer(),wkb->size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
}
|
2015-03-05 10:45:27 +01:00
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
std::string to_geojson_impl(mapnik::geometry::geometry<double> const& geom)
|
2012-02-20 13:05:11 +01:00
|
|
|
{
|
2014-07-24 23:31:59 +02:00
|
|
|
std::string wkt;
|
2015-03-05 10:45:27 +01:00
|
|
|
if (!mapnik::util::to_geojson(wkt, geom))
|
2012-02-20 13:05:11 +01:00
|
|
|
{
|
2014-07-24 23:31:59 +02:00
|
|
|
throw std::runtime_error("Generate JSON failed");
|
2012-02-20 13:05:11 +01:00
|
|
|
}
|
2014-07-24 23:31:59 +02:00
|
|
|
return wkt;
|
2012-02-20 13:05:11 +01:00
|
|
|
}
|
2011-12-12 19:19:52 +01:00
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
std::string to_wkt_impl(mapnik::geometry::geometry<double> const& geom)
|
2015-03-12 14:44:13 +01:00
|
|
|
{
|
|
|
|
std::string wkt;
|
2015-03-31 18:15:23 +02:00
|
|
|
if (!mapnik::util::to_wkt(wkt,geom))
|
2015-03-12 14:44:13 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Generate WKT failed");
|
|
|
|
}
|
|
|
|
return wkt;
|
|
|
|
}
|
2015-03-05 10:45:27 +01:00
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry_types geometry_type_impl(mapnik::geometry::geometry<double> const& geom)
|
2012-08-23 04:13:37 +02:00
|
|
|
{
|
2015-03-24 14:21:28 +01:00
|
|
|
return mapnik::geometry::geometry_type(geom);
|
2014-07-24 23:31:59 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::box2d<double> geometry_envelope_impl(mapnik::geometry::geometry<double> const& geom)
|
2014-07-24 23:31:59 +02:00
|
|
|
{
|
2015-03-24 14:21:28 +01:00
|
|
|
return mapnik::geometry::envelope(geom);
|
2012-08-23 04:13:37 +02:00
|
|
|
}
|
|
|
|
|
2015-04-23 04:52:46 +02:00
|
|
|
// only Boost >= 1.56 has is_valid and is_simple
|
|
|
|
#if BOOST_VERSION >= 105600
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
bool geometry_is_valid_impl(mapnik::geometry::geometry<double> const& geom)
|
2015-03-05 12:11:23 +01:00
|
|
|
{
|
2015-03-24 14:21:28 +01:00
|
|
|
return mapnik::geometry::is_valid(geom);
|
2015-03-05 12:11:23 +01:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
bool geometry_is_simple_impl(mapnik::geometry::geometry<double> const& geom)
|
2015-03-05 12:11:23 +01:00
|
|
|
{
|
2015-03-24 14:21:28 +01:00
|
|
|
return mapnik::geometry::is_simple(geom);
|
2015-03-05 12:11:23 +01:00
|
|
|
}
|
|
|
|
|
2015-04-23 04:52:46 +02:00
|
|
|
#endif // BOOST_VERSION >= 1.56
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
bool geometry_is_empty_impl(mapnik::geometry::geometry<double> const& geom)
|
2015-03-24 22:16:58 +01:00
|
|
|
{
|
|
|
|
return mapnik::geometry::is_empty(geom);
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
void geometry_correct_impl(mapnik::geometry::geometry<double> & geom)
|
2015-03-05 12:11:23 +01:00
|
|
|
{
|
2015-03-24 14:21:28 +01:00
|
|
|
mapnik::geometry::correct(geom);
|
2015-03-05 12:11:23 +01:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
void polygon_set_exterior_impl(mapnik::geometry::polygon<double> & poly, mapnik::geometry::linear_ring<double> const& ring)
|
2015-03-18 17:37:18 +01:00
|
|
|
{
|
|
|
|
poly.exterior_ring = ring; // copy
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
void polygon_add_hole_impl(mapnik::geometry::polygon<double> & poly, mapnik::geometry::linear_ring<double> const& ring)
|
2015-03-18 17:37:18 +01:00
|
|
|
{
|
|
|
|
poly.interior_rings.push_back(ring); // copy
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::point<double> geometry_centroid_impl(mapnik::geometry::geometry<double> const& geom)
|
2015-03-11 12:56:38 +01:00
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::point<double> pt;
|
2015-03-24 14:21:28 +01:00
|
|
|
mapnik::geometry::centroid(geom, pt);
|
2015-03-11 12:56:38 +01:00
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2012-08-23 04:13:37 +02: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
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
implicitly_convertible<mapnik::geometry::point<double>, mapnik::geometry::geometry<double> >();
|
|
|
|
implicitly_convertible<mapnik::geometry::line_string<double>, mapnik::geometry::geometry<double> >();
|
|
|
|
implicitly_convertible<mapnik::geometry::polygon<double>, mapnik::geometry::geometry<double> >();
|
2015-03-24 14:21:28 +01:00
|
|
|
enum_<mapnik::geometry::geometry_types>("GeometryType")
|
|
|
|
.value("Unknown",mapnik::geometry::geometry_types::Unknown)
|
|
|
|
.value("Point",mapnik::geometry::geometry_types::Point)
|
|
|
|
.value("LineString",mapnik::geometry::geometry_types::LineString)
|
|
|
|
.value("Polygon",mapnik::geometry::geometry_types::Polygon)
|
|
|
|
.value("MultiPoint",mapnik::geometry::geometry_types::MultiPoint)
|
|
|
|
.value("MultiLineString",mapnik::geometry::geometry_types::MultiLineString)
|
|
|
|
.value("MultiPolygon",mapnik::geometry::geometry_types::MultiPolygon)
|
|
|
|
.value("GeometryCollection",mapnik::geometry::geometry_types::GeometryCollection)
|
2011-02-28 13:40:59 +01:00
|
|
|
;
|
2012-01-05 12:56:38 +01:00
|
|
|
|
2015-03-05 10:45:27 +01:00
|
|
|
enum_<mapnik::wkbByteOrder>("wkbByteOrder")
|
|
|
|
.value("XDR",mapnik::wkbXDR)
|
|
|
|
.value("NDR",mapnik::wkbNDR)
|
2012-01-05 12:56:38 +01:00
|
|
|
;
|
2012-02-02 02:30:26 +01:00
|
|
|
|
2015-03-24 14:21:28 +01:00
|
|
|
using mapnik::geometry::geometry;
|
|
|
|
using mapnik::geometry::point;
|
|
|
|
using mapnik::geometry::line_string;
|
|
|
|
using mapnik::geometry::linear_ring;
|
|
|
|
using mapnik::geometry::polygon;
|
2015-03-11 12:56:38 +01:00
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
class_<point<double> >("Point", init<double, double>((arg("x"), arg("y")),
|
2015-03-11 12:56:38 +01:00
|
|
|
"Constructs a new Point object\n"))
|
2015-04-09 22:22:51 +02:00
|
|
|
.add_property("x", &point<double>::x, "X coordinate")
|
|
|
|
.add_property("y", &point<double>::y, "Y coordinate")
|
2015-04-23 04:52:46 +02:00
|
|
|
// only Boost >= 1.56 has is_valid and is_simple
|
|
|
|
#if BOOST_VERSION >= 105600
|
2015-03-18 17:37:18 +01:00
|
|
|
.def("is_valid", &geometry_is_valid_impl)
|
|
|
|
.def("is_simple", &geometry_is_simple_impl)
|
2015-04-23 04:52:46 +02:00
|
|
|
#endif // BOOST_VERSION >= 1.56
|
2015-03-11 13:05:32 +01:00
|
|
|
.def("to_geojson",&to_geojson_impl)
|
2015-03-18 17:37:18 +01:00
|
|
|
.def("to_wkb",&to_wkb_impl)
|
|
|
|
.def("to_wkt",&to_wkt_impl)
|
2015-03-11 12:56:38 +01:00
|
|
|
;
|
2015-03-05 10:45:27 +01:00
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
class_<line_string<double> >("LineString", init<>(
|
2015-03-11 14:02:30 +01:00
|
|
|
"Constructs a new LineString object\n"))
|
2015-04-09 22:22:51 +02:00
|
|
|
.def("add_coord", &line_string<double>::add_coord, "Adds coord")
|
2015-04-23 04:52:46 +02:00
|
|
|
// only Boost >= 1.56 has is_valid and is_simple
|
|
|
|
#if BOOST_VERSION >= 105600
|
2015-03-18 17:37:18 +01:00
|
|
|
.def("is_valid", &geometry_is_valid_impl)
|
|
|
|
.def("is_simple", &geometry_is_simple_impl)
|
2015-04-23 04:52:46 +02:00
|
|
|
#endif // BOOST_VERSION >= 1.56
|
2015-03-18 17:37:18 +01:00
|
|
|
.def("to_geojson",&to_geojson_impl)
|
|
|
|
.def("to_wkb",&to_wkb_impl)
|
|
|
|
.def("to_wkt",&to_wkt_impl)
|
|
|
|
;
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
class_<linear_ring<double> >("LinearRing", init<>(
|
2015-03-18 17:37:18 +01:00
|
|
|
"Constructs a new LinearRtring object\n"))
|
2015-04-09 22:22:51 +02:00
|
|
|
.def("add_coord", &linear_ring<double>::add_coord, "Adds coord")
|
2015-03-18 17:37:18 +01:00
|
|
|
;
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
class_<polygon<double> >("Polygon", init<>(
|
2015-03-18 17:37:18 +01:00
|
|
|
"Constructs a new Polygon object\n"))
|
2015-04-09 22:22:51 +02:00
|
|
|
.add_property("exterior_ring", &polygon<double>::exterior_ring , "Exterior ring")
|
2015-03-18 17:37:18 +01:00
|
|
|
.def("add_hole", &polygon_add_hole_impl, "Add interior ring")
|
|
|
|
.def("num_rings", polygon_set_exterior_impl, "Number of rings (at least 1)")
|
2015-04-23 04:52:46 +02:00
|
|
|
// only Boost >= 1.56 has is_valid and is_simple
|
|
|
|
#if BOOST_VERSION >= 105600
|
2015-03-18 17:37:18 +01:00
|
|
|
.def("is_valid", &geometry_is_valid_impl)
|
|
|
|
.def("is_simple", &geometry_is_simple_impl)
|
2015-04-23 04:52:46 +02:00
|
|
|
#endif // BOOST_VERSION >= 1.56
|
2015-03-11 14:02:30 +01:00
|
|
|
.def("to_geojson",&to_geojson_impl)
|
2015-03-18 17:37:18 +01:00
|
|
|
.def("to_wkb",&to_wkb_impl)
|
|
|
|
.def("to_wkt",&to_wkt_impl)
|
2015-03-11 14:02:30 +01:00
|
|
|
;
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
class_<geometry<double>, std::shared_ptr<geometry<double> >, boost::noncopyable>("Geometry",no_init)
|
2015-03-05 10:45:27 +01:00
|
|
|
.def("envelope",&geometry_envelope_impl)
|
|
|
|
.def("from_geojson", from_geojson_impl)
|
2015-03-11 12:28:04 +01:00
|
|
|
.def("from_wkt", from_wkt_impl)
|
2015-03-11 12:40:39 +01:00
|
|
|
.def("from_wkb", from_wkb_impl)
|
2015-03-05 10:45:27 +01:00
|
|
|
.staticmethod("from_geojson")
|
2015-03-11 12:28:04 +01:00
|
|
|
.staticmethod("from_wkt")
|
2015-03-11 12:40:39 +01:00
|
|
|
.staticmethod("from_wkb")
|
2015-03-12 14:44:13 +01:00
|
|
|
.def("__str__",&to_wkt_impl)
|
2015-03-05 10:45:27 +01:00
|
|
|
.def("type",&geometry_type_impl)
|
2015-04-23 04:52:46 +02:00
|
|
|
// only Boost >= 1.56 has is_valid and is_simple
|
|
|
|
#if BOOST_VERSION >= 105600
|
2015-03-05 12:11:23 +01:00
|
|
|
.def("is_valid", &geometry_is_valid_impl)
|
|
|
|
.def("is_simple", &geometry_is_simple_impl)
|
2015-04-23 04:52:46 +02:00
|
|
|
#endif // BOOST_VERSION >= 1.56
|
2015-03-24 22:16:58 +01:00
|
|
|
.def("is_empty", &geometry_is_empty_impl)
|
2015-03-05 12:11:23 +01:00
|
|
|
.def("correct", &geometry_correct_impl)
|
2015-03-11 12:56:38 +01:00
|
|
|
.def("centroid",&geometry_centroid_impl)
|
2015-03-18 17:37:18 +01:00
|
|
|
.def("to_wkb",&to_wkb_impl)
|
2015-03-12 14:44:13 +01:00
|
|
|
.def("to_wkt",&to_wkt_impl)
|
2015-03-05 10:45:27 +01:00
|
|
|
.def("to_geojson",&to_geojson_impl)
|
|
|
|
//.def("to_svg",&to_svg)
|
2010-11-03 14:19:15 +01:00
|
|
|
// TODO add other geometry_type methods
|
2010-06-02 13:03:30 +02:00
|
|
|
;
|
2009-01-13 01:56:09 +01:00
|
|
|
}
|