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
length (with strategy)

Calculates the length of a geometry using the specified strategy.

Description

The free function length calculates the length (the sum of distances between consecutive points) of a geometry using the specified strategy. Reasons to specify a strategy include: use another coordinate system for calculations; construct the strategy beforehand (e.g. with the radius of the Earth); select a strategy when there are more than one available for a calculation.

Synopsis

template<typename Geometry, typename Strategy>
default_length_result<Geometry>::type length(Geometry const & geometry, Strategy const & strategy)

Parameters

Type

Concept

Name

Description

Geometry const &

Any type fulfilling a Geometry Concept

geometry

A model of the specified concept

Strategy const &

Any type fulfilling a distance Strategy Concept

strategy

The strategy which will be used for distance calculations

Returns

The calculated length

Header

Either

#include <boost/geometry.hpp>

Or

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

Conformance

The function length implements function Length from the OGC Simple Feature Specification.

Behavior

Case

Behavior

pointlike (e.g. point)

Returns 0

linear (e.g. linestring)

Returns the length

areal (e.g. polygon)

Returns 0

Complexity

Linear

Examples

The following example shows the length measured over a sphere, expressed in kilometers. To do that the radius of the sphere must be specified in the constructor of the strategy.

#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/linestring.hpp>

int main()
{
    using namespace boost::geometry;
    typedef model::point<float, 2, cs::spherical_equatorial<degree> > P;
    model::linestring<P> line;
    line.push_back(P(2, 41));
    line.push_back(P(2, 48));
    line.push_back(P(5, 52));
    double const mean_radius = 6371.0; 1
    std::cout << "length is "
        << length(line, strategy::distance::haversine<float>(mean_radius) )
        << " kilometers " << std::endl;

    return 0;
}

1

Wiki

Output:

length is 1272.03 kilometers

PrevUpHomeNext