Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

convert

Converts one geometry to another geometry.

Description

The convert algorithm converts one geometry, e.g. a BOX, to another geometry, e.g. a RING. This only if it is possible and applicable.

Synopsis

template<typename Geometry1, typename Geometry2>
void convert(Geometry1 const & geometry1, Geometry2 & geometry2)

Parameters

Type

Concept

Name

Description

Geometry1 const &

Any type fulfilling a Geometry Concept

geometry1

A model of the specified concept (source)

Geometry2 &

Any type fulfilling a Geometry Concept

geometry2

A model of the specified concept (target)

Header

#include <boost/geometry/algorithms/convert.hpp>

[Tip] Tip

convert is not defined within OGC or ISO

[Tip] Tip

convert is modelled as source -> target (where assign is modelled as target := source)

Complexity

Linear

Example

Shows how to convert a geometry into another geometry

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point;
    typedef boost::geometry::model::box<point> box;
    typedef boost::geometry::model::polygon<point> polygon;

    point p1(1, 1);
    box bx = boost::geometry::make<box>(1, 1, 2, 2);
    
    // Assign a box to a polygon (conversion box->poly)
    polygon poly;
    boost::geometry::convert(bx, poly);

    // Convert a point to another point type (conversion of point-type)
    boost::tuple<double, double> p2;
    boost::geometry::convert(p1, p2); // source -> target
        
    using boost::geometry::dsv;
    std::cout
        << "box: " << dsv(bx) << std::endl
        << "polygon: " << dsv(poly) << std::endl
        << "point: " << dsv(p1) << std::endl
        << "point tuples: " << dsv(p2) << std::endl
        ;

    return 0;
}

Output:

box: ((1, 1), (2, 2))
polygon: (((1, 1), (1, 2), (2, 2), (2, 1), (1, 1)))
point: (1, 1)
point tuples: (1, 1)
See also

PrevUpHomeNext