diff --git a/bindings/python/mapnik_envelope.cpp b/bindings/python/mapnik_envelope.cpp index f95cb3299..08723be5c 100644 --- a/bindings/python/mapnik_envelope.cpp +++ b/bindings/python/mapnik_envelope.cpp @@ -84,6 +84,9 @@ box2d (box2d::*intersect)(box2d const&) const = &box2d::*re_center_p1)(double,double) = &box2d::re_center; void (box2d::*re_center_p2)(coord const& ) = &box2d::re_center; +// clip +void (box2d::*clip)(box2d const&) = &box2d::clip; + void export_envelope() { using namespace boost::python; @@ -144,6 +147,20 @@ void export_envelope() ">>> e\n" "Box2d(10.0, 10.0, 110.0, 110.0)\n" ) + .def("clip", clip, + (arg("other")), + "Clip the envelope based on the bounds of another envelope.\n" + "\n " + "Example:\n" + ">>> e = Box2d(0, 0, 100, 100)\n" + ">>> e.center(Coord60, 60)\n" + ">>> e.center()\n" + "Coord(60.0,60.0)\n" + ">>> (e.width(), e.height())\n" + "(100.0, 100.0)\n" + ">>> e\n" + "Box2d(10.0, 10.0, 110.0, 110.0)\n" + ) .def("width", width_p1, (arg("new_width")), "Sets the width to new_width of the envelope preserving its center.\n" diff --git a/include/mapnik/box2d.hpp b/include/mapnik/box2d.hpp index 3364eb73c..040989b0b 100644 --- a/include/mapnik/box2d.hpp +++ b/include/mapnik/box2d.hpp @@ -79,6 +79,7 @@ public: void re_center(T cx,T cy); void re_center(const coord& c); void init(T x0,T y0,T x1,T y1); + void clip(const box2d_type &other); bool from_string(const std::string& s); bool valid() const; diff --git a/src/box2d.cpp b/src/box2d.cpp index 5e2bdb6b5..b19892ece 100644 --- a/src/box2d.cpp +++ b/src/box2d.cpp @@ -307,6 +307,19 @@ void box2d::init(T x0,T y0,T x1,T y1) } } +template +#if !defined(__SUNPRO_CC) +inline +#endif +void box2d::clip(const box2d_type& other) +{ + minx_ = std::max(minx_,other.minx()); + miny_ = std::max(miny_,other.miny()); + maxx_ = std::min(maxx_,other.maxx()); + maxy_ = std::min(maxy_,other.maxy()); +} + + template #if !defined(__SUNPRO_CC) inline diff --git a/tests/python_tests/object_test.py b/tests/python_tests/object_test.py index 5a05c474d..000195e0d 100644 --- a/tests/python_tests/object_test.py +++ b/tests/python_tests/object_test.py @@ -674,3 +674,23 @@ def test_envelope_multiplication(): eq_(c.x, 150) eq_(c.y, 150) + +# Box2d clipping +def test_envelope_pickle(): + e1 = mapnik2.Box2d(-180,-90,180,90) + e2 = mapnik2.Box2d(-120,40,-110,48) + e1.clip(e2) + eq_(e1,e2) + + # madagascar in merc + e1 = mapnik2.Box2d(4772116.5490, -2744395.0631, 5765186.4203, -1609458.0673) + e2 = mapnik2.Box2d(5124338.3753, -2240522.1727, 5207501.8621, -2130452.8520) + e1.clip(e2) + eq_(e1,e2) + + # nz in lon/lat + e1 = mapnik2.Box2d(163.8062, -47.1897, 179.3628, -33.9069) + e2 = mapnik2.Box2d(173.7378, -39.6395, 174.4849, -38.9252) + e1.clip(e2) + eq_(e1,e2) +