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

Add Point to Polygon and Point to LineString distance methods #61

Merged
19 commits merged into from
Aug 25, 2016

Conversation

urschrei
Copy link
Member

@urschrei urschrei commented Aug 18, 2016

This PR adds distance methods from a Point to:

  • a LineString
  • a Polygon

To note:

  • Points inside a Polygon return 0.0
  • Points on a Polygon boundary return 0.0
  • Points on a LineString return 0.0
  • distance to an empty LineString returns 0.0
  • distance to an empty Polygon (shouldn't be possible, but we're handling it for now) returns 0.0
  • I'm using a BinaryHeap implemented as a minimum priority queue with some floating-point-compatible comparator methods (see the Ord and PartialOrd impls on Mindist) – I just adapted the linked example to use partial ordering. Please chime in if this is Bad, or if you can think of a better design.

Also WIP because:

  • no handling of empty geometries yet
  • no docs / doctests yet

This will close #59 if/when it merges.

// final point in an odd-numbered exterior ring
line_segment_distance(&self, chunk.first().unwrap(), chunk.first().unwrap())
}
};
Copy link
Member

@frewsxcv frewsxcv Aug 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively:

let dist = line_segment_distance(self, chunk[0], chunk.last().or(chunk[0]));

The slice returned by the chunks iterator guarantees that element [0] exists.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to use unwrap_or, but still ✨

@urschrei
Copy link
Member Author

Empty geometries are handled gracefully, and everything's documented.

@urschrei
Copy link
Member Author

The Point-to-Polygon method now also accounts for the possibility that the point is inside an interior ring:
screenshot 2016-08-19 12 36 25

@@ -131,6 +131,12 @@ impl<T> Distance<T, Polygon<T>> for Point<T>
}
// minimum priority queue
let mut dist_queue: BinaryHeap<Mindist<T>> = BinaryHeap::new();
// we've got interior rings
if polygon.1.len() > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This outer if isn't necessary. If it's empty, the for loop won't iterate, which is fine.

@frewsxcv
Copy link
Member

Looks great! Awesome work @urschrei. I left a bunch of nits, but feel free to merge whenever 👍

These should probably be tested using a nearlyEquals functions
Given a vec of 1, 2, 3, 4
chunks(2) gives:
1, 2
3, 4
But that leaves out segment 2, 3

windows(2) gives:
1, 2
2, 3
3, 4

Which is what we want
@urschrei
Copy link
Member Author

urschrei commented Aug 20, 2016

Now that I've fixed my embarrassing chunks(2) mistake ( 😳), I'm happy for this to merge, the Ord etc. stuff notwithstanding.

@urschrei
Copy link
Member Author

(I have a working implementation of Polylabel ready to go, when this merges and releases)

@frewsxcv
Copy link
Member

@aelita-mergebot r+

@ghost
Copy link

ghost commented Aug 25, 2016

😱 Internal error: no commit found for PR

@frewsxcv frewsxcv closed this Aug 25, 2016
@frewsxcv frewsxcv reopened this Aug 25, 2016
@frewsxcv
Copy link
Member

@aelita-mergebot r+

ghost pushed a commit that referenced this pull request Aug 25, 2016
Merge #61 a=@urschrei r=@frewsxcv
________________________________________________________________________

This PR adds distance methods from a `Point` to:
- a `LineString`
- a `Polygon`

To note:
- Points **inside a `Polygon`** return `0.0`
- Points on a **`Polygon` boundary** return `0.0`
- Points **on a `LineString`** return `0.0`
- distance to an **empty `LineString`** returns `0.0`
- distance to an **empty `Polygon`** (shouldn't be possible, but we're handling it for now) returns `0.0`
- I'm using a [`BinaryHeap`](https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html) implemented as a minimum priority queue with some floating-point-compatible comparator methods (see the `Ord` and `PartialOrd` `impl`s on `Mindist`) – I just adapted the linked example to use partial ordering. Please chime in if this is Bad, or if you can think of a better design.

Also WIP because:
- <s>no handling of empty geometries yet</s>
- <s>no docs / doctests yet</s>

This will close #59 if/when it merges.
@ghost
Copy link

ghost commented Aug 25, 2016

👍 Build succeeded

@ghost ghost merged commit 73374da into georust:master Aug 25, 2016
@urschrei
Copy link
Member Author

Thanks 🎉!

@urschrei urschrei deleted the new_distance_methods branch March 14, 2017 18:13
nyurik pushed a commit to nyurik/geo that referenced this pull request Mar 19, 2022
62: deserialize point r=frewsxcv a=michaelkirk

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [n/a] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
  -  > Add helper function for deserializing from WKT format into geo_types::Geometry
    
      I think the existing unreleased note pretty much covers it 
---

Addresses one little bit of georust/wkt#61, though leaves unanswered the bigger questions, around how to handle `GEOMETRYCOLLETION(POINT EMPTY)` etc.

This might be controversial as it arguably muddies the water for addressing georust#61 more comprehensively. Please take a look at georust#61 for context.

Co-authored-by: Michael Kirk <michael.code@endoftheworl.de>
nyurik pushed a commit to nyurik/geo that referenced this pull request Mar 19, 2022
64: Support 'POINT EMPTY' conversion to geo_types r=michaelkirk a=rmanoka

- [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.
---

closes  georust#61 

Co-authored-by: Rajsekar Manokaran <rajsekar@gmail.com>
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How best to calculate Point to Polygon distance?
2 participants