Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow LineString creation from vec of two-element CoordinateType array #223

Merged
merged 2 commits into from
May 15, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement LineString::into() for 2-element CoordinateType arrays
Also add some docs to Polygon to show the most direct creation method
  • Loading branch information
urschrei committed May 15, 2018
commit 5ac5cf94cf384ab55fee837659982b58d31aa515
29 changes: 29 additions & 0 deletions geo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ impl<T: CoordinateType> From<(T, T)> for Coordinate<T> {
}
}

impl<T: CoordinateType> From<[T; 2]> for Coordinate<T> {
fn from(coords: [T; 2]) -> Self {
Coordinate {
x: coords[0],
y: coords[1],
}
}
}

/// A single Point in 2D space.
///
/// Points can be created using the `new(x, y)` constructor, or from a `Coordinate` or pair of points.
Expand Down Expand Up @@ -78,6 +87,12 @@ impl<T: CoordinateType> From<(T, T)> for Point<T> {
}
}

impl<T: CoordinateType> From<[T; 2]> for Point<T> {
fn from(coords: [T; 2]) -> Point<T> {
Point::new(coords[0], coords[1])
}
}

impl<T> Point<T>
where
T: CoordinateType + ToPrimitive,
Expand Down Expand Up @@ -471,6 +486,11 @@ where
/// let line: LineString<f32> = vec![(0., 0.), (10., 0.)].into();
/// ```
///
/// ```
/// # use geo_types::{LineString, Point};
/// let line: LineString<f64> = vec![[0., 0.], [10., 0.]].into();
/// ```
///
/// Or `collect`ing from a Point iterator
///
/// ```
Expand Down Expand Up @@ -594,6 +614,15 @@ impl<T: CoordinateType> IntoIterator for MultiLineString<T> {
/// A representation of an area. Its outer boundary is represented by a [`LineString`](struct.LineString.html) that is both closed and simple
///
/// It has one exterior *ring* or *shell*, and zero or more interior rings, representing holes.
///
/// Polygons can be created from collections of `Point`-like objects, such as arrays or tuples:
///
/// ```
/// use geo_types::{Point, LineString, Polygon};
/// let poly1 = Polygon::new(vec![[0., 0.], [10., 0.]].into(), vec![]);
/// let poly2 = Polygon::new(vec![(0., 0.), (10., 0.)].into(), vec![]);
/// ```
///
#[derive(PartialEq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Polygon<T>
Expand Down