Skip to content

Commit

Permalink
Merge #634
Browse files Browse the repository at this point in the history
634: Add From<Line> for LineString #625 r=michaelkirk a=NolwennD

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

Hello,

Here a small patch for #625, I hope it’s good enough.

Co-authored-by: NolwennD <donolwenn@gmail.com>
  • Loading branch information
bors[bot] and NolwennD committed Mar 4, 2021
2 parents b1e656d + 554dbfa commit 168726a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions geo-types/CHANGES.md
Expand Up @@ -4,6 +4,8 @@

* Implement `RelativeEq` and `AbsDiffEq` for fuzzy comparison of remaining Geometry Types
* <https://github.com/georust/geo/pull/628>
* Implement `From<Line>` for `LineString`
* <https://github.com/georust/geo/pull/634>

## 0.7.1

Expand Down
23 changes: 23 additions & 0 deletions geo-types/src/line_string.rs
Expand Up @@ -254,6 +254,12 @@ impl<T: CoordNum, IC: Into<Coordinate<T>>> From<Vec<IC>> for LineString<T> {
}
}

impl<T: CoordNum> From<Line<T>> for LineString<T> {
fn from(line: Line<T>) -> Self {
LineString(vec![line.start, line.end])
}
}

/// Turn an iterator of `Point`-like objects into a `LineString`.
impl<T: CoordNum, IC: Into<Coordinate<T>>> FromIterator<IC> for LineString<T> {
fn from_iter<I: IntoIterator<Item = IC>>(iter: I) -> Self {
Expand Down Expand Up @@ -474,4 +480,21 @@ mod test {
let ls_oversized: LineString<f32> = coords_x.into_iter().collect();
assert!(ls.relative_ne(&ls_oversized, 1., 1.));
}

#[test]
fn should_be_built_from_line() {
let start = Coordinate { x: 0, y: 0 };
let end = Coordinate { x: 10, y: 10 };
let line = Line::new(start, end);
let expected = LineString(vec![start, end]);

assert_eq!(expected, LineString::from(line));

let start = Coordinate { x: 10., y: 0.5 };
let end = Coordinate { x: 10000., y: 10.4 };
let line = Line::new(start, end);
let expected = LineString(vec![start, end]);

assert_eq!(expected, LineString::from(line));
}
}

0 comments on commit 168726a

Please sign in to comment.