Struct geo::geometry::LineString

source ·
pub struct LineString<T = f64>(pub Vec<Coord<T>, Global>)
where
    T: CoordNum
;
Expand description

An ordered collection of two or more Coords, 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::{coord, LineString};

let line_string = LineString::new(vec![
    coord! { x: 0., y: 0. },
    coord! { 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 = vec![[0., 0.], [10., 0.]].into();

Or by collecting from a Coord iterator

use geo_types::{coord, LineString};

let mut coords_iter =
    vec![coord! { x: 0., y: 0. }, coord! { 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::{coord, LineString};

let line_string = LineString::new(vec![
    coord! { x: 0., y: 0. },
    coord! { 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 Coords when looping:

use geo_types::{coord, LineString};

let line_string = LineString::new(vec![
    coord! { x: 0., y: 0. },
    coord! { 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 Coords or Points:

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

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

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

Tuple Fields§

§0: Vec<Coord<T>, Global>

Implementations§

Instantiate Self from the raw content value

👎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 Coords

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

Return the coordinates of a LineString as a Vec of Points

Return the coordinates of a LineString as a Vec of Coords

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

Examples
use geo_types::{coord, 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(
        coord! { x: 0., y: 0. },
        coord! { x: 5., y: 0. }
    )),
    lines.next()
);
assert_eq!(
    Some(Line::new(
        coord! { x: 5., y: 0. },
        coord! { 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 Coord, and the value of the first Coord does not equal the value of the last Coord, then a new Coord is added to the end with the value of the first Coord.

👎Deprecated: Use geo::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
Deserialize this value from the given Serde deserializer. Read more

LineString to Line

Returns the distance between two geometries Read more

Line to LineString

Returns the distance between two geometries Read more

LineString-LineString distance

Returns the distance between two geometries Read more

Minimum distance from a Point to a LineString

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
Converts to this type from the input type.
Converts to this type from the input type.

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

Converts to this type from the input type.

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::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
Calculates a representative point inside the Geometry 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 [Coordinate]s 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 Coords 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
Iterate over all exterior and (if any) interior lines of a geometry. Read more
Apply a function to all the coordinates in a geometric object, returning a new object. Read more
Map a fallible function over all the coordinates in a geometry, returning a Result Read more
Apply a function to all the coordinates in a geometric object, in place Read more
Map a fallible function over all the coordinates in a geometry, in place, returning a Result. Read more
👎Deprecated since 0.21.0: use MapCoordsInPlace::map_coords_in_place instead which takes a Coord instead of an (x,y) tuple

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

Examples
#[allow(deprecated)]
use geo::MapCoordsInplace;
use geo::Point;
use approx::assert_relative_eq;

let mut p = Point::new(10., 20.);
#[allow(deprecated)]
p.map_coords_inplace(|(x, y)| (x + 1000., y * 2.));

assert_relative_eq!(p, Point::new(1010., 40.), epsilon = 1e-6);
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Returns the squared euclidean distance between 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 object’s 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.

Create a LineString with consecutive repeated points removed.

Remove consecutive repeated points from a LineString inplace.

Serialize this value into the given Serde serializer. Read more
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.
👎Deprecated since 0.21.0: use MapCoords::try_map_coords which takes a Coord instead of an (x,y) tuple
👎Deprecated since 0.21.0: use MapCoords::try_map_coords which takes a Coord instead of an (x,y) tuple
Map a fallible function over all the coordinates in a geometry, returning a Result Read more
👎Deprecated since 0.21.0: use MapCoordsInPlace::try_map_coords_in_place which takes a Coord instead of an (x,y) tuple
Map a fallible function over all the coordinates in a geometry, in place, 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§

Apply transform immutably, outputting a new geometry.
Apply transform to mutate self.
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 its centroid by an angle, in degrees Read more
Mutable version of Self::rotate_around_centroid
Rotate a geometry around the center of its bounding box by an angle, in degrees. Read more
Mutable version of Self::rotate_around_center
Rotate a Geometry around an arbitrary point by an angle, given in degrees Read more
Mutable version of Self::rotate_around_point
Scale a geometry from it’s bounding box center. Read more
Mutable version of scale
Scale a geometry from it’s bounding box center, using different values for x_factor and y_factor to distort the geometry’s aspect ratio. Read more
Mutable version of scale_xy.
Scale a geometry around a point of origin. Read more
Mutable version of scale_around_point.
An affine transformation which skews a geometry, sheared by a uniform angle along the x and y dimensions. Read more
Mutable version of skew.
An affine transformation which skews a geometry, sheared by an angle along the x and y dimensions. Read more
Mutable version of skew_xy.
An affine transformation which skews a geometry around a point of origin, sheared by an angle along the x and y dimensions. Read more
Mutable version of skew_around_point.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
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.