pub struct LineString<T = f64>(pub Vec<Coord<T>>)
where
T: CoordNum;
Expand description
An ordered collection of two or more Coord
s, representing a
path between locations.
Semantics
- A
LineString
is closed if it is empty, or if the first and last coordinates are the same. - The boundary of a
LineString
is either:- empty if it is closed (see 1) or
- contains the start and end coordinates.
- The interior is the (infinite) set of all coordinates along the
LineString
, not including the boundary. - 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). - A simple and closed
LineString
is aLinearRing
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 LineString
s.
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!
][crate::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 collect
ing 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 Coord
s 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 Coord
s or Point
s:
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§
§impl<T> LineString<T>where
T: CoordNum,
impl<T> LineString<T>where
T: CoordNum,
pub fn new(value: Vec<Coord<T>>) -> LineString<T>
pub fn new(value: Vec<Coord<T>>) -> LineString<T>
Instantiate Self from the raw content value
pub fn points_iter(&self) -> PointsIter<'_, T>
👎Deprecated: Use points() instead
pub fn points_iter(&self) -> PointsIter<'_, T>
Return an iterator yielding the coordinates of a LineString
as Point
s
pub fn points(&self) -> PointsIter<'_, T>
pub fn points(&self) -> PointsIter<'_, T>
Return an iterator yielding the coordinates of a LineString
as Point
s
pub fn coords(&self) -> impl DoubleEndedIterator
pub fn coords(&self) -> impl DoubleEndedIterator
Return an iterator yielding the members of a LineString
as Coord
s
pub fn coords_mut(&mut self) -> impl DoubleEndedIterator
pub fn coords_mut(&mut self) -> impl DoubleEndedIterator
Return an iterator yielding the coordinates of a LineString
as mutable Coord
s
pub fn into_points(self) -> Vec<Point<T>>
pub fn into_points(self) -> Vec<Point<T>>
Return the coordinates of a LineString
as a Vec
of Point
s
pub fn into_inner(self) -> Vec<Coord<T>>
pub fn into_inner(self) -> Vec<Coord<T>>
Return the coordinates of a LineString
as a Vec
of Coord
s
pub fn lines(&self) -> impl ExactSizeIterator
pub fn lines(&self) -> impl ExactSizeIterator
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());
pub fn triangles(&self) -> impl ExactSizeIterator
pub fn triangles(&self) -> impl ExactSizeIterator
An iterator which yields the coordinates of a LineString
as [Triangle]s
pub fn close(&mut self)
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
.
pub fn num_coords(&self) -> usize
👎Deprecated: Use geo::CoordsIter::coords_count instead
pub fn num_coords(&self) -> usize
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());
pub fn is_closed(&self) -> bool
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 LineString
s used in
non-LinearRing
contexts.
Trait Implementations§
§impl<T> AbsDiffEq for LineString<T>where
T: AbsDiffEq<Epsilon = T> + CoordNum,
impl<T> AbsDiffEq for LineString<T>where
T: AbsDiffEq<Epsilon = T> + CoordNum,
§fn abs_diff_eq(
&self,
other: &LineString<T>,
epsilon: <LineString<T> as AbsDiffEq>::Epsilon
) -> bool
fn abs_diff_eq( &self, other: &LineString<T>, epsilon: <LineString<T> as AbsDiffEq>::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)
§fn default_epsilon() -> <LineString<T> as AbsDiffEq>::Epsilon
fn default_epsilon() -> <LineString<T> as AbsDiffEq>::Epsilon
§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq
].§impl<T> Clone for LineString<T>where
T: Clone + CoordNum,
impl<T> Clone for LineString<T>where
T: Clone + CoordNum,
§fn clone(&self) -> LineString<T>
fn clone(&self) -> LineString<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl<T> Debug for LineString<T>where
T: Debug + CoordNum,
impl<T> Debug for LineString<T>where
T: Debug + CoordNum,
§impl<T> From<&Line<T>> for LineString<T>where
T: CoordNum,
impl<T> From<&Line<T>> for LineString<T>where
T: CoordNum,
§fn from(line: &Line<T>) -> LineString<T>
fn from(line: &Line<T>) -> LineString<T>
§impl From<GeoLineString> for LineString
impl From<GeoLineString> for LineString
§fn from(field: GeoLineString) -> LineString
fn from(field: GeoLineString) -> LineString
§impl<T> From<Line<T>> for LineString<T>where
T: CoordNum,
impl<T> From<Line<T>> for LineString<T>where
T: CoordNum,
§fn from(line: Line<T>) -> LineString<T>
fn from(line: Line<T>) -> LineString<T>
§impl From<LineString> for GeoLineString
impl From<LineString> for GeoLineString
§fn from(field: LineString) -> GeoLineString
fn from(field: LineString) -> GeoLineString
§impl<T, IC> From<Vec<IC>> for LineString<T>
impl<T, IC> From<Vec<IC>> for LineString<T>
Turn a Vec
of Point
-like objects into a LineString
.
§fn from(v: Vec<IC>) -> LineString<T>
fn from(v: Vec<IC>) -> LineString<T>
§impl<T, IC> FromIterator<IC> for LineString<T>
impl<T, IC> FromIterator<IC> for LineString<T>
Turn an iterator of Point
-like objects into a LineString
.
§fn from_iter<I>(iter: I) -> LineString<T>where
I: IntoIterator<Item = IC>,
fn from_iter<I>(iter: I) -> LineString<T>where
I: IntoIterator<Item = IC>,
§impl<T> Hash for LineString<T>where
T: Hash + CoordNum,
impl<T> Hash for LineString<T>where
T: Hash + CoordNum,
§impl<T> Index<usize> for LineString<T>where
T: CoordNum,
impl<T> Index<usize> for LineString<T>where
T: CoordNum,
§impl<T> IndexMut<usize> for LineString<T>where
T: CoordNum,
impl<T> IndexMut<usize> for LineString<T>where
T: CoordNum,
§impl<'a, T> IntoIterator for &'a LineString<T>where
T: CoordNum,
impl<'a, T> IntoIterator for &'a LineString<T>where
T: CoordNum,
§impl<'a, T> IntoIterator for &'a mut LineString<T>where
T: CoordNum,
impl<'a, T> IntoIterator for &'a mut LineString<T>where
T: CoordNum,
Mutably iterate over all the Coord
s in this LineString
§impl<T> IntoIterator for LineString<T>where
T: CoordNum,
impl<T> IntoIterator for LineString<T>where
T: CoordNum,
Iterate over all the Coord
s in this LineString
.
§impl<T> PartialEq for LineString<T>where
T: PartialEq + CoordNum,
impl<T> PartialEq for LineString<T>where
T: PartialEq + CoordNum,
§fn eq(&self, other: &LineString<T>) -> bool
fn eq(&self, other: &LineString<T>) -> bool
self
and other
values to be equal, and is used
by ==
.§impl<T> PointDistance for LineString<T>where
T: Float + RTreeNum,
impl<T> PointDistance for LineString<T>where
T: Float + RTreeNum,
§fn distance_2(&self, point: &Point<T>) -> T
fn distance_2(&self, point: &Point<T>) -> T
§fn contains_point(&self, point: &<Self::Envelope as Envelope>::Point) -> bool
fn contains_point(&self, point: &<Self::Envelope as Envelope>::Point) -> bool
true
if a point is contained within this object. Read more§fn distance_2_if_less_or_equal(
&self,
point: &<Self::Envelope as Envelope>::Point,
max_distance_2: <<Self::Envelope as Envelope>::Point as Point>::Scalar
) -> Option<<<Self::Envelope as Envelope>::Point as Point>::Scalar>
fn distance_2_if_less_or_equal( &self, point: &<Self::Envelope as Envelope>::Point, max_distance_2: <<Self::Envelope as Envelope>::Point as Point>::Scalar ) -> Option<<<Self::Envelope as Envelope>::Point as Point>::Scalar>
None
if the distance
is larger than a given maximum value. Read more§impl<T> RTreeObject for LineString<T>where
T: Float + RTreeNum,
impl<T> RTreeObject for LineString<T>where
T: Float + RTreeNum,
§impl<T> RelativeEq for LineString<T>where
T: AbsDiffEq<Epsilon = T> + CoordNum + RelativeEq,
impl<T> RelativeEq for LineString<T>where
T: AbsDiffEq<Epsilon = T> + CoordNum + RelativeEq,
§fn relative_eq(
&self,
other: &LineString<T>,
epsilon: <LineString<T> as AbsDiffEq>::Epsilon,
max_relative: <LineString<T> as AbsDiffEq>::Epsilon
) -> bool
fn relative_eq( &self, other: &LineString<T>, epsilon: <LineString<T> as AbsDiffEq>::Epsilon, max_relative: <LineString<T> as AbsDiffEq>::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)
§fn default_max_relative() -> <LineString<T> as AbsDiffEq>::Epsilon
fn default_max_relative() -> <LineString<T> as AbsDiffEq>::Epsilon
§fn relative_ne(
&self,
other: &Rhs,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon
) -> bool
fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool
RelativeEq::relative_eq
].§impl<T> TryFrom<Geometry<T>> for LineString<T>where
T: CoordNum,
impl<T> TryFrom<Geometry<T>> for LineString<T>where
T: CoordNum,
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.
§fn try_from(
geom: Geometry<T>
) -> Result<LineString<T>, <LineString<T> as TryFrom<Geometry<T>>>::Error>
fn try_from( geom: Geometry<T> ) -> Result<LineString<T>, <LineString<T> as TryFrom<Geometry<T>>>::Error>
impl<T> Eq for LineString<T>where
T: Eq + CoordNum,
impl<T> StructuralEq for LineString<T>where
T: CoordNum,
impl<T> StructuralPartialEq for LineString<T>where
T: CoordNum,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CallHasher for T
impl<T> CallHasher for T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request