From 38dc240d6d134a71aa780cab9677ccdb731411f4 Mon Sep 17 00:00:00 2001 From: artemp Date: Thu, 5 Mar 2015 12:11:23 +0100 Subject: [PATCH] geometry - add generic is_valid(), is_simple() and correct() --- bindings/python/mapnik_geometry.cpp | 22 ++++++++ include/mapnik/geometry_correct.hpp | 72 +++++++++++++++++++++++++++ include/mapnik/geometry_envelope.hpp | 2 +- include/mapnik/geometry_is_simple.hpp | 68 +++++++++++++++++++++++++ include/mapnik/geometry_is_valid.hpp | 68 +++++++++++++++++++++++++ 5 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 include/mapnik/geometry_correct.hpp create mode 100644 include/mapnik/geometry_is_simple.hpp create mode 100644 include/mapnik/geometry_is_valid.hpp diff --git a/bindings/python/mapnik_geometry.cpp b/bindings/python/mapnik_geometry.cpp index b58ddbad9..75e0067ca 100644 --- a/bindings/python/mapnik_geometry.cpp +++ b/bindings/python/mapnik_geometry.cpp @@ -42,6 +42,10 @@ #include #include #include +#include +#include +#include + //#include // from_wkt //#include #include // from_geojson @@ -232,6 +236,21 @@ mapnik::box2d geometry_envelope_impl(mapnik::new_geometry::geometry cons return mapnik::new_geometry::envelope(geom); } +bool geometry_is_valid_impl(mapnik::new_geometry::geometry const& geom) +{ + return mapnik::new_geometry::is_valid(geom); +} + +bool geometry_is_simple_impl(mapnik::new_geometry::geometry const& geom) +{ + return mapnik::new_geometry::is_simple(geom); +} + +void geometry_correct_impl(mapnik::new_geometry::geometry & geom) +{ + mapnik::new_geometry::correct(geom); +} + /* // https://github.com/mapnik/mapnik/issues/1437 std::string to_svg2( mapnik::geometry_container const& geom) @@ -273,6 +292,9 @@ void export_geometry() .staticmethod("from_geojson") // .def("__str__",&mapnik::geometry_type::to_string) .def("type",&geometry_type_impl) + .def("is_valid", &geometry_is_valid_impl) + .def("is_simple", &geometry_is_simple_impl) + .def("correct", &geometry_correct_impl) //.def("to_wkb",&to_wkb) //.def("to_wkt",&to_wkt) .def("to_geojson",&to_geojson_impl) diff --git a/include/mapnik/geometry_correct.hpp b/include/mapnik/geometry_correct.hpp new file mode 100644 index 000000000..0dbe3f859 --- /dev/null +++ b/include/mapnik/geometry_correct.hpp @@ -0,0 +1,72 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2015 Artem Pavlenko + * + * 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 + * + *****************************************************************************/ + +#ifndef MAPNIK_GEOMETRY_CORRECT_HPP +#define MAPNIK_GEOMETRY_CORRECT_HPP + +#include +#include +#include + +namespace mapnik { namespace new_geometry { + +namespace detail { + +struct geometry_correct +{ + using result_type = void; + + result_type operator() (geometry_collection & collection) const + { + for (auto & geom : collection) + { + (*this)(geom); + } + } + + result_type operator() (polygon & poly) const + { + return boost::geometry::correct(poly); + } + + result_type operator() (multi_polygon & multi_poly) const + { + return boost::geometry::correct(multi_poly); + } + + template + result_type operator() (T & geom) const + { + //no-op + } +}; + +} + +inline void correct(mapnik::new_geometry::geometry & geom) +{ + return mapnik::util::apply_visitor(detail::geometry_correct(), geom); +} + +}} + +#endif // MAPNIK_GEOMETRY_CORRECT_HPP diff --git a/include/mapnik/geometry_envelope.hpp b/include/mapnik/geometry_envelope.hpp index a58eb0a75..4c65673ec 100644 --- a/include/mapnik/geometry_envelope.hpp +++ b/include/mapnik/geometry_envelope.hpp @@ -2,7 +2,7 @@ * * This file is part of Mapnik (c++ mapping toolkit) * - * Copyright (C) 2014 Artem Pavlenko + * Copyright (C) 2015 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/include/mapnik/geometry_is_simple.hpp b/include/mapnik/geometry_is_simple.hpp new file mode 100644 index 000000000..ab733a35e --- /dev/null +++ b/include/mapnik/geometry_is_simple.hpp @@ -0,0 +1,68 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2015 Artem Pavlenko + * + * 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 + * + *****************************************************************************/ + +#ifndef MAPNIK_GEOMETRY_IS_SIMPLE_HPP +#define MAPNIK_GEOMETRY_IS_SIMPLE_HPP + +#include +#include +#include + +namespace mapnik { namespace new_geometry { + +namespace detail { + +struct geometry_is_simple +{ + using result_type = bool; + + result_type operator() (geometry const& geom) const + { + return mapnik::util::apply_visitor(*this, geom); + } + + result_type operator() (geometry_collection const& collection) const + { + for (auto const& geom : collection) + { + if ( !(*this)(geom)) return false; + } + return true; + } + + template + result_type operator() (T const& geom) const + { + return boost::geometry::is_simple(geom); + } +}; + +} + +inline bool is_simple(mapnik::new_geometry::geometry const& geom) +{ + return detail::geometry_is_simple() (geom); +} + +}} + +#endif // MAPNIK_GEOMETRY_IS_SIMPLE_HPP diff --git a/include/mapnik/geometry_is_valid.hpp b/include/mapnik/geometry_is_valid.hpp new file mode 100644 index 000000000..9047a3ce9 --- /dev/null +++ b/include/mapnik/geometry_is_valid.hpp @@ -0,0 +1,68 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2015 Artem Pavlenko + * + * 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 + * + *****************************************************************************/ + +#ifndef MAPNIK_GEOMETRY_IS_VALID_HPP +#define MAPNIK_GEOMETRY_IS_VALID_HPP + +#include +#include +#include + +namespace mapnik { namespace new_geometry { + +namespace detail { + +struct geometry_is_valid +{ + using result_type = bool; + + result_type operator() (geometry const& geom) const + { + return mapnik::util::apply_visitor(*this, geom); + } + + result_type operator() (geometry_collection const& collection) const + { + for (auto const& geom : collection) + { + if ( !(*this)(geom)) return false; + } + return true; + } + + template + result_type operator() (T const& geom) const + { + return boost::geometry::is_valid(geom); + } +}; + +} + +inline bool is_valid(mapnik::new_geometry::geometry const& geom) +{ + return detail::geometry_is_valid() (geom); +} + +}} + +#endif // MAPNIK_GEOMETRY_IS_VALID_HPP