| Home | Libraries | People | FAQ | More

##### for_each_point

Applies function f to each point.

###### Description

Applies a function f (functor, having operator() defined) to each point making up the geometry

###### Synopsis

    template<typename Geometry, typename Functor>
    Functor for_each_point(Geometry & geometry, Functor f)

###### Parameters

Type | Concept | Name | Description
--- | --- | --- | ---
Geometry & | Any type fulfilling a Geometry Concept | geometry | A model of the specified concept
Functor | Function or class with operator() | f | Unary function, taking a point as argument

###### Header

Either

`#include <boost/geometry/geometry.hpp>`

Or

`#include <boost/geometry/algorithms/for_each.hpp>`

###### Conformance

The function for_each_point is not defined by OGC.

The function for_each_point conforms to the std::for_each function of the C++ std-library.

###### Example

Convenient usage of for_each_point, rounding all points of a geometry

    #include <iostream>

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    #include <boost/geometry/geometries/polygon.hpp>
    #include <boost/geometry/io/wkt/wkt.hpp>

    template <typename Point>
    class round_coordinates
    {
    private :
        typedef typename boost::geometry::coordinate_type<Point>::type coordinate_type;
        coordinate_type factor;

        inline coordinate_type round(coordinate_type value)
        {
            return floor(0.5 + (value / factor)) * factor;
        }

    public :
        round_coordinates(coordinate_type f)
            : factor(f)
        {}

        inline void operator()(Point& p)
        {
            using boost::geometry::get;
            using boost::geometry::set;
            set<0>(p, round(get<0>(p)));
            set<1>(p, round(get<1>(p)));
        }
    };

    int main()
    {
        typedef boost::geometry::model::d2::point_xy<double> point;
        boost::geometry::model::polygon<point> poly;
        boost::geometry::read_wkt("POLYGON((0 0,1.123 9.987,8.876 2.234,0 0),(3.345 4.456,7.654 8.765,9.123 5.432,3.345 4.456))", poly);
        boost::geometry::for_each_point(poly, round_coordinates<point>(0.1));
        std::cout << "Rounded: " << boost::geometry::wkt(poly) << std::endl;
        return 0;
    }

Output:

    Rounded: POLYGON((0 0,1.1 10,8.9 2.2,0 0),(3.3 4.5,7.7 8.8,9.1 5.4,3.3 4.5))

Sample using for_each_point, using a function to list coordinates

    #include <iostream>

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    #include <boost/geometry/geometries/polygon.hpp>
    #include <boost/geometry/io/wkt/wkt.hpp>

    template <typename Point>
    void list_coordinates(Point const& p)
    {
        using boost::geometry::get;
        std::cout << "x = " << get<0>(p) << " y = " << get<1>(p) << std::endl;
    }

    int main()
    {
        typedef boost::geometry::model::d2::point_xy<double> point;
        boost::geometry::model::polygon<point> poly;
        boost::geometry::read_wkt("POLYGON((0 0,0 4,4 0,0 0))", poly);
        boost::geometry::for_each_point(poly, list_coordinates<point>);
        return 0;
    }

Output:

    x = 0 y = 0
    x = 0 y = 4
    x = 4 y = 0
    x = 0 y = 0