pub struct LineString<T: CoordNum = f64>(pub Vec<Coord<T>>);
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>>

Implementations§

source§

impl<T: CoordNum> LineString<T>

source

pub fn new(value: Vec<Coord<T>>) -> Self

Instantiate Self from the raw content value

source

pub fn points_iter(&self) -> PointsIter<'_, T>

👎Deprecated: Use points() instead

Return an iterator yielding the coordinates of a LineString as Points

source

pub fn points(&self) -> PointsIter<'_, T>

Return an iterator yielding the coordinates of a LineString as Points

source

pub fn coords(&self) -> impl DoubleEndedIterator<Item = &Coord<T>>

Return an iterator yielding the members of a LineString as Coords

source

pub fn coords_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut Coord<T>>

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

source

pub fn into_points(self) -> Vec<Point<T>>

Return the coordinates of a LineString as a Vec of Points

source

pub fn into_inner(self) -> Vec<Coord<T>>

Return the coordinates of a LineString as a Vec of Coords

source

pub fn lines(&self) -> impl ExactSizeIterator + Iterator<Item = Line<T>> + '_

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());
source

pub fn triangles( &self ) -> impl ExactSizeIterator + Iterator<Item = Triangle<T>> + '_

An iterator which yields the coordinates of a LineString as Triangles

source

pub fn close(&mut self)

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.

source

pub fn num_coords(&self) -> usize

👎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());
source

pub fn is_closed(&self) -> bool

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§

source§

impl<T: AbsDiffEq<Epsilon = T> + CoordNum> AbsDiffEq<LineString<T>> for LineString<T>

source§

fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool

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)
§

type Epsilon = T

Used for specifying relative comparisons.
source§

fn default_epsilon() -> Self::Epsilon

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

fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool

The inverse of AbsDiffEq::abs_diff_eq.
source§

impl<T: Clone + CoordNum> Clone for LineString<T>

source§

fn clone(&self) -> LineString<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + CoordNum> Debug for LineString<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de, T> Deserialize<'de> for LineString<T>where T: Deserialize<'de> + CoordNum,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T: CoordNum> From<Line<T>> for LineString<T>

source§

fn from(line: Line<T>) -> Self

Converts to this type from the input type.
source§

impl<T: CoordNum> From<LineString<T>> for Geometry<T>

source§

fn from(x: LineString<T>) -> Self

Converts to this type from the input type.
source§

impl<T: CoordNum, IC: Into<Coord<T>>> From<Vec<IC, Global>> for LineString<T>

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

source§

fn from(v: Vec<IC>) -> Self

Converts to this type from the input type.
source§

impl<T: CoordNum, IC: Into<Coord<T>>> FromIterator<IC> for LineString<T>

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

source§

fn from_iter<I: IntoIterator<Item = IC>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<T: Hash + CoordNum> Hash for LineString<T>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

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

impl<T: CoordNum> Index<usize> for LineString<T>

§

type Output = Coord<T>

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Coord<T>

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

impl<T: CoordNum> IndexMut<usize> for LineString<T>

source§

fn index_mut(&mut self, index: usize) -> &mut Coord<T>

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

impl<'a, T: CoordNum> IntoIterator for &'a LineString<T>

§

type Item = &'a Coord<T>

The type of the elements being iterated over.
§

type IntoIter = CoordinatesIter<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T: CoordNum> IntoIterator for &'a mut LineString<T>

Mutably iterate over all the Coords in this LineString

§

type Item = &'a mut Coord<T>

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, Coord<T>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IterMut<'a, Coord<T>>

Creates an iterator from a value. Read more
source§

impl<T: CoordNum> IntoIterator for LineString<T>

Iterate over all the Coords in this LineString.

§

type Item = Coord<T>

The type of the elements being iterated over.
§

type IntoIter = IntoIter<Coord<T>, Global>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PartialEq + CoordNum> PartialEq<LineString<T>> for LineString<T>

source§

fn eq(&self, other: &LineString<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> RelativeEq<LineString<T>> for LineString<T>where T: AbsDiffEq<Epsilon = T> + CoordNum + RelativeEq,

source§

fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool

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)
source§

fn default_max_relative() -> Self::Epsilon

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

fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool

The inverse of RelativeEq::relative_eq.
source§

impl<T> Serialize for LineString<T>where T: Serialize + CoordNum,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T: CoordNum> TryFrom<Geometry<T>> for LineString<T>

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.

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(geom: Geometry<T>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: Eq + CoordNum> Eq for LineString<T>

source§

impl<T: CoordNum> StructuralEq for LineString<T>

source§

impl<T: CoordNum> StructuralPartialEq for LineString<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for LineString<T>where T: RefUnwindSafe,

§

impl<T> Send for LineString<T>where T: Send,

§

impl<T> Sync for LineString<T>where T: Sync,

§

impl<T> Unpin for LineString<T>where T: Unpin,

§

impl<T> UnwindSafe for LineString<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,