logo
pub struct LineString<T>(pub Vec<Coordinate<T>, Global>)
where
    T: CoordNum
;
Expand description

An ordered collection of two or more Coordinates, representing a path between locations.

Semantics

  1. A LineString is closed if it is empty, or if the first and last coordinates are the same.
  2. The boundary of a LineString is either:
    • empty if it is closed (see 1) or
    • contains the start and end coordinates.
  3. The interior is the (infinite) set of all coordinates along the LineString, not including the boundary.
  4. A LineString is simple if it does not intersect except optionally at the first and last coordinates (in which case it is also closed, see 1).
  5. A simple and closed LineString is a LinearRing as defined in the OGC-SFA (but is not defined as a separate type in this crate).

Validity

A LineString is valid if it is either empty or contains 2 or more coordinates.

Further, a closed LineString must not self-intersect. Note that its validity is not enforced, and operations and predicates are undefined on invalid LineStrings.

Examples

Creation

Create a LineString by calling it directly:

use geo_types::{Coordinate, LineString};

let line_string = LineString(vec![
    Coordinate { x: 0., y: 0. },
    Coordinate { x: 10., y: 0. },
]);

Create a LineString with the [line_string!] macro:

use geo_types::line_string;

let line_string = line_string![
    (x: 0., y: 0.),
    (x: 10., y: 0.),
];

By converting from a Vec of coordinate-like things:

use geo_types::LineString;

let line_string: LineString<f32> = vec![(0., 0.), (10., 0.)].into();
use geo_types::LineString;

let line_string: LineString<f64> = vec![[0., 0.], [10., 0.]].into();

Or by collecting from a Coordinate iterator

use geo_types::{Coordinate, LineString};

let mut coords_iter =
    vec![Coordinate { x: 0., y: 0. }, Coordinate { x: 10., y: 0. }].into_iter();

let line_string: LineString<f32> = coords_iter.collect();

Iteration

LineString provides five iterators: coords, coords_mut, points, lines, and triangles:

use geo_types::{Coordinate, LineString};

let line_string = LineString(vec![
    Coordinate { x: 0., y: 0. },
    Coordinate { x: 10., y: 0. },
]);

line_string.coords().for_each(|coord| println!("{:?}", coord));

for point in line_string.points() {
    println!("Point x = {}, y = {}", point.x(), point.y());
}

Note that its IntoIterator impl yields Coordinates when looping:

use geo_types::{Coordinate, LineString};

let line_string = LineString(vec![
    Coordinate { x: 0., y: 0. },
    Coordinate { x: 10., y: 0. },
]);

for coord in &line_string {
    println!("Coordinate x = {}, y = {}", coord.x, coord.y);
}

for coord in line_string {
    println!("Coordinate x = {}, y = {}", coord.x, coord.y);
}

Decomposition

You can decompose a LineString into a Vec of Coordinates or Points:

use geo_types::{Coordinate, LineString, Point};

let line_string = LineString(vec![
    Coordinate { x: 0., y: 0. },
    Coordinate { x: 10., y: 0. },
]);

let coordinate_vec = line_string.clone().into_inner();
let point_vec = line_string.clone().into_points();

Tuple Fields

0: Vec<Coordinate<T>, Global>

Implementations

👎 Deprecated:

Use points() instead

Return an iterator yielding the coordinates of a LineString as Points

Return an iterator yielding the coordinates of a LineString as Points

Return an iterator yielding the members of a LineString as Coordinates

Return an iterator yielding the coordinates of a LineString as mutable Coordinates

Return the coordinates of a LineString as a Vec of Points

Return the coordinates of a LineString as a Vec of Coordinates

Return an iterator yielding one Line for each line segment in the LineString.

Examples
use geo_types::{Coordinate, Line, LineString};

let mut coords = vec![(0., 0.), (5., 0.), (7., 9.)];
let line_string: LineString<f32> = coords.into_iter().collect();

let mut lines = line_string.lines();
assert_eq!(
    Some(Line::new(
        Coordinate { x: 0., y: 0. },
        Coordinate { x: 5., y: 0. }
    )),
    lines.next()
);
assert_eq!(
    Some(Line::new(
        Coordinate { x: 5., y: 0. },
        Coordinate { x: 7., y: 9. }
    )),
    lines.next()
);
assert!(lines.next().is_none());

An iterator which yields the coordinates of a LineString as Triangles

Close the LineString. Specifically, if the LineString has at least one Coordinate, and the value of the first Coordinate does not equal the value of the last Coordinate, then a new Coordinate is added to the end with the value of the first Coordinate.

👎 Deprecated:

Use geo::algorithm::coords_iter::CoordsIter::coords_count instead

Return the number of coordinates in the LineString.

Examples
use geo_types::LineString;

let mut coords = vec![(0., 0.), (5., 0.), (7., 9.)];
let line_string: LineString<f32> = coords.into_iter().collect();

assert_eq!(3, line_string.num_coords());

Checks if the linestring is closed; i.e. it is either empty or, the first and last points are the same.

Examples
use geo_types::LineString;

let mut coords = vec![(0., 0.), (5., 0.), (0., 0.)];
let line_string: LineString<f32> = coords.into_iter().collect();
assert!(line_string.is_closed());

Note that we diverge from some libraries (JTS et al), which have a LinearRing type, separate from LineString. Those libraries treat an empty LinearRing as closed by definition, while treating an empty LineString as open. Since we don’t have a separate LinearRing type, and use a LineString in its place, we adopt the JTS LinearRing is_closed behavior in all places: that is, we consider an empty LineString as closed.

This is expected when used in the context of a Polygon.exterior and elsewhere; And there seems to be no reason to maintain the separate behavior for LineStrings used in non-LinearRing contexts.

Trait Implementations

Equality assertion with an absolute limit.

Examples
use geo_types::LineString;

let mut coords_a = vec![(0., 0.), (5., 0.), (7., 9.)];
let a: LineString<f32> = coords_a.into_iter().collect();

let mut coords_b = vec![(0., 0.), (5., 0.), (7.001, 9.)];
let b: LineString<f32> = coords_b.into_iter().collect();

approx::assert_relative_eq!(a, b, epsilon=0.1)

Used for specifying relative comparisons.

The default tolerance to use when testing values that are close together. Read more

The inverse of [AbsDiffEq::abs_diff_eq].

Return the BoundingRect for a LineString

create a new geometry with the Chaikin smoothing being applied n_iterations times. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Find the closest point between self and p.

Return the number of coordinates in the LineString.

Iterate over all exterior and (if any) interior coordinates of a geometry. Read more

Iterate over all exterior coordinates of a geometry. Read more

Formats the value using the given formatter. Read more

LineString to Line

Returns the distance between two geometries Read more

Minimum distance from a Point to a LineString

Line to LineString

Returns the distance between two geometries Read more

LineString-LineString distance

Returns the distance between two geometries Read more

Polygon to LineString distance

Returns the distance between two geometries Read more

Minimum distance from a LineString to a Point

LineString to Polygon

Returns the distance between two geometries Read more

Calculation of the length of a Line Read more

Determine the similarity between two LineStrings using the Frechet distance. Read more

Performs the conversion.

Performs the conversion.

Turn a Vec of Point-like objects into a LineString.

Performs the conversion.

Turn an iterator of Point-like objects into a LineString.

Creates a value from an iterator. Read more

Determine the length of a geometry on an ellipsoidal model of the earth. Read more

use geo_types::line_string;
use geo::algorithm::dimensions::{HasDimensions, Dimensions};

let ls = line_string![(x: 0.,  y: 0.), (x: 0., y: 1.), (x: 1., y: 1.)];
assert_eq!(Dimensions::ZeroDimensional, ls.boundary_dimensions());

let ls = line_string![(x: 0.,  y: 0.), (x: 0., y: 1.), (x: 1., y: 1.), (x: 0., y: 0.)];
assert_eq!(Dimensions::Empty, ls.boundary_dimensions());

Some geometries, like a MultiPoint, can have zero coordinates - we call these empty. Read more

The dimensions of some geometries are fixed, e.g. a Point always has 0 dimensions. However for others, the dimensionality depends on the specific geometry instance - for example typical Rects are 2-dimensional, but it’s possible to create degenerate Rects which have either 1 or 0 dimensions. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

Determine the length of a geometry using the haversine formula. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Mutably iterate over all the Coordinates in this LineString

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Iterate over all the Coordinates in this LineString.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Test and get the orientation if the shape is convex. Tests for strict convexity if allow_collinear, and only accepts a specific orientation if provided. Read more

Test if the shape lies on a line.

Test if the shape is convex.

Test if the shape is convex, and oriented counter-clockwise. Read more

Test if the shape is convex, and oriented clockwise.

Test if the shape is strictly convex.

Test if the shape is strictly convex, and oriented counter-clockwise. Read more

Test if the shape is strictly convex, and oriented clockwise. Read more

Apply a function to all the coordinates in a geometric object, returning a new object. Read more

Apply a function to all the coordinates in a geometric object, in place Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Returns the squared euclidean distance of an object to a point.

Returns true if a point is contained within this object. Read more

Returns the squared distance to this object or None if the distance is larger than a given maximum value. Read more

The object’s envelope type. Usually, AABB will be the right choice. This type also defines the objects dimensionality. Read more

Returns the object’s envelope. Read more

Equality assertion within a relative limit.

Examples
use geo_types::LineString;

let mut coords_a = vec![(0., 0.), (5., 0.), (7., 9.)];
let a: LineString<f32> = coords_a.into_iter().collect();

let mut coords_b = vec![(0., 0.), (5., 0.), (7.001, 9.)];
let b: LineString<f32> = coords_b.into_iter().collect();

approx::assert_relative_eq!(a, b, max_relative=0.1)

The default relative tolerance for testing values that are far-apart. Read more

The inverse of [RelativeEq::relative_eq].

Rotate the LineString about its centroid by the given number of degrees

Rotate a geometry around the center of its bounding box by an angle, in degrees. Read more

👎 Deprecated:

Equivalent to rotate_around_centroid except for Polygon<T>, where it is equivalent to rotating around the polygon’s outer ring. Call that instead, or rotate_around_center if you’d like to rotate around the geometry’s bounding box center.

Returns the simplified representation of a geometry, using the Ramer–Douglas–Peucker algorithm Read more

Returns the simplified indices of a geometry, using the Ramer–Douglas–Peucker algorithm Read more

Returns the simplified representation of a geometry, using the Visvalingam-Whyatt algorithm Read more

Returns the simplified representation of a geometry, using a topology-preserving variant of the Visvalingam-Whyatt algorithm. Read more

Returns the simplified representation of a geometry, using the Visvalingam-Whyatt algorithm Read more

Convert a Geometry enum into its inner type.

Fails if the enum case does not match the type you are trying to convert it to.

The type returned in the event of a conversion error.

Performs the conversion.

Map a fallible function over all the coordinates in a geometry, returning a Result Read more

Determine the length of a geometry using Vincenty’s formulae. Read more

Iterate over the points in a clockwise order

The Linestring isn’t changed, and the points are returned either in order, or in reverse order, so that the resultant order makes it appear clockwise

Iterate over the points in a counter-clockwise order

The Linestring isn’t changed, and the points are returned either in order, or in reverse order, so that the resultant order makes it appear counter-clockwise

Change this line’s points so they are in clockwise winding order

Change this line’s points so they are in counterclockwise winding order

Return the winding order of this object if it contains at least three distinct coordinates, and None otherwise. Read more

True iff this is wound clockwise

True iff this is wound counterclockwise

Return a clone of this object, but in the specified winding order

Change the winding order so that it is in this winding order

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Rotate a Geometry around an arbitrary point by an angle, given in degrees Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Translate a Geometry along its axes by the given offsets Read more

Translate a Geometry along its axes, but in place.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.