length

| Home | Libraries | People | FAQ | More

##### length

Calculates the length of a geometry.

###### Description

The free function length calculates the length (the sum of distances between consecutive points) of a geometry. It uses the default strategy, based on the coordinate system of the geometry.

###### Synopsis

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

###### Parameters

Type | Concept | Name | Description
--- | --- | --- | ---
Geometry const & | Any type fulfilling a Geometry Concept | geometry | A model of the specified concept

###### 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 simple example shows the calculation of the length of a linestring containing three points

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

    int main()
    {
        using namespace boost::geometry;
        model::linestring<model::d2::point_xy<double> > line;
        read_wkt("linestring(0 0,1 1,4 8,3 2)", line);
        std::cout << "linestring length is "
            << length(line)
            << " units" << std::endl;

        return 0;
    }

Output:

    linestring length is 15.1127 units