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

PrevUpHomeNext
distance

Calculate the distance of two geometries.

Description

The default strategy is used, corresponding to the coordinate system of the geometries

Synopsis

template<typename Geometry1, typename Geometry2>
default_distance_result<Geometry1, Geometry2>::type distance(Geometry1 const & geometry1, Geometry2 const & geometry2)

Parameters

Type

Concept

Name

Description

Geometry1 const &

Any type fulfilling a Geometry Concept

geometry1

A model of the specified concept

Geometry2 const &

Any type fulfilling a Geometry Concept

geometry2

A model of the specified concept

Returns

The calculated distance

Header

Either

#include <boost/geometry.hpp>

Or

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

Conformance

The function distance implements function Distance from the OGC Simple Feature Specification.

Supported geometries

Point

Segment

Box

Linestring

Ring

Polygon

MultiPoint

MultiLinestring

MultiPolygon

Variant

Point

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

Segment

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

Box

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

Linestring

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

Ring

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

Polygon

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

MultiPoint

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

MultiLinestring

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

MultiPolygon

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

Variant

ok

ok

ok

ok

ok

ok

ok

ok

ok

ok

Complexity

For point to geometry: linear

For multi-point to ring/polygon/multi-polygon: currently quadratic

For all other geometry combinations: linearithmic

Example

Shows calculation of distance of point to some other geometries

#include <iostream>
#include <list>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/multi_point.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>

#include <boost/foreach.hpp>

int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    typedef boost::geometry::model::polygon<point_type> polygon_type;
    typedef boost::geometry::model::linestring<point_type> linestring_type;
    typedef boost::geometry::model::multi_point<point_type> multi_point_type;

    point_type p(1,2);
    polygon_type poly;
    linestring_type line;
    multi_point_type mp;

    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", poly);
    line.push_back(point_type(0,0));
    line.push_back(point_type(0,3));
    mp.push_back(point_type(0,0));
    mp.push_back(point_type(3,3));

    std::cout
        << "Point-Poly: " << boost::geometry::distance(p, poly) << std::endl
        << "Point-Line: " << boost::geometry::distance(p, line) << std::endl
        << "Point-MultiPoint: " << boost::geometry::distance(p, mp) << std::endl;

    return 0;
}

Output:

Point-Poly: 1.22066
Point-Line: 1
Point-MultiPoint: 2.23607

PrevUpHomeNext