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

Determine the position of a Coordinate relative to a geometry. #538

Merged
merged 2 commits into from
Nov 23, 2020

Conversation

michaelkirk
Copy link
Member

Examples

use geo::{polygon, Coordinate};
use geo::coordinate_position::{CoordinatePosition, CoordPos};
let square_poly = polygon![(x: 0.0, y: 0.0), (x: 2.0, y: 0.0), (x: 2.0, y: 2.0), (x: 0.0, y: 2.0), (x: 0.0, y: 0.0)];
let inside_coord = Coordinate { x: 1.0, y: 1.0 };
assert_eq!(square_poly.coordinate_position(&inside_coord), CoordPos::Inside);
let boundary_coord = Coordinate { x: 0.0, y: 1.0 };
assert_eq!(square_poly.coordinate_position(&boundary_coord), CoordPos::OnBoundary);
let outside_coord = Coordinate { x: 5.0, y: 5.0 };
assert_eq!(square_poly.coordinate_position(&outside_coord), CoordPos::Outside);

Some work extracted from the geomgraph effort.

impl<T: CoordinateType> Zero for Coordinate<T> {
fn zero() -> Self {
impl<T: CoordinateType> Coordinate<T> {
pub fn zero() -> Self {
Copy link
Member Author

Choose a reason for hiding this comment

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

...tangential quality of life improvement.


/// Calculate the position of a `Coordinate` relative to a
/// closed `LineString`.
pub fn coord_pos_relative_to_ring<T>(coord: Coordinate<T>, linestring: &LineString<T>) -> CoordPos
Copy link
Member Author

Choose a reason for hiding this comment

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

This method was moved from utils unmodified.

In an effort to keep utils from becoming a big junk drawer, this seemed like a more specific home for it.


/// The position of a `Coordinate` relative to a `Geometry`
#[derive(PartialEq, Clone, Debug)]
pub enum CoordPos {
Copy link
Member Author

@michaelkirk michaelkirk Nov 12, 2020

Choose a reason for hiding this comment

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

moved this from utils where it was previously only used as a return from "coord_pos_relative_to_ring".

@rmanoka (et al) - do you see any issue with re-using this enum?

At first I introduced a new "Interior/Exterior/Boundary" enum, but it seemed redundant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds fine to me

/// let outside_coord = Coordinate { x: 5.0, y: 5.0 };
/// assert_eq!(square_poly.coordinate_position(&outside_coord), CoordPos::Outside);
/// ```
pub trait CoordinatePosition<T>
Copy link
Contributor

Choose a reason for hiding this comment

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

@michaelkirk Do you prefer the CoordinatePosition<T> in favor of CoordinatePosition with a associate type Scalar: CoordinateType ? My personal pref. is the latter; optional comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe I've done this here: a60dc27 - can you take a look @rmanoka?

Despite reading a couple articles on this, I only kind of theoretically understand why this distinction is good, and still don't have a very intuitive grasp for when to apply it.

Is your rule of thumb to prefer associated types and only use full-blown generic type params when associated types are insufficient?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll plan to merge this in a couple days if I don't hear otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

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

@michaelkirk Sorry for the late response; the changes look great! My pref. is mostly similar to Iterator vs AddAsign traits in std. I go for an associated type unless the same type could implement the trait with two possible param. Here, we're implementing the trait CoordinatePosition with only T = Ty for Line<Ty>, so prefer it to be associated than make the trait CoordinatePosition<T>.

May be in the future, our scalar types could have additional sophistication that tells us when T1 can be converted to T2 without loss in precision, and we can make some of our traits like CoordinatePosition, ForEach into generics.

Copy link
Member

@frewsxcv frewsxcv left a comment

Choose a reason for hiding this comment

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

looks great, thanks!

geo/src/algorithm/coordinate_position.rs Outdated Show resolved Hide resolved
geo/src/algorithm/coordinate_position.rs Outdated Show resolved Hide resolved
geo/src/algorithm/coordinate_position.rs Outdated Show resolved Hide resolved
@michaelkirk
Copy link
Member Author

bors r=frewsxcv

@bors
Copy link
Contributor

bors bot commented Nov 23, 2020

Merge conflict.

More often than not, I'm using the `zero` method as a convenient way to
get "some" coord, rather than needing a strictly as a mathematical
'zero' value. Having to import num_traits::zero everywhere defeats that
purpose.
coord_pos_relative_to_ring was moved unmodified into the new
coorindate_position module
@michaelkirk michaelkirk force-pushed the mkirk/coordinate-position branch from a60dc27 to 89f62f0 Compare November 23, 2020 16:16
@michaelkirk
Copy link
Member Author

bors retrt

@michaelkirk
Copy link
Member Author

bors retry

@bors
Copy link
Contributor

bors bot commented Nov 23, 2020

Build succeeded:

@bors bors bot merged commit d759812 into master Nov 23, 2020
@michaelkirk michaelkirk deleted the mkirk/coordinate-position branch December 2, 2020 19:41
bors bot added a commit that referenced this pull request Apr 13, 2021
639: Introduce the geomgraph module for DE-9IM Relate trait r=michaelkirk a=michaelkirk

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

Fixes #513, #515

(I'm sorry it's so large)

~~I'm going to leave it as a draft (edit: 🤦 I failed to actually open the PR as a draft) while I wait to merge #636 and #638 and then do some rebasing, but I don't anticipate doing other large changes before review.~~ *update: ready for review!*

Here's some of the earlier work in pursuit of this:

#514
#516
#523
#524 
#538
#552
#561
#611
#628
#629
#636 

Primarily, this introduces the geomgraph module for a DE-9IM `Relate` trait.

geomgraph implements a topology graph largely inspired by JTS's module of the same name:
https://github.com/locationtech/jts/tree/jts-1.18.1/modules/core/src/main/java/org/locationtech/jts/geomgraph

You can see some of the reference code if you omit the "REMOVE JTS COMMENTS" commit. In some places the implementation is quite close to the JTS source. 

The overall "flow" is pretty similar to that of JTS, but in the small, there were some divergences. It's not easy (or desirable) to literally translate a Java codebase making heavy use of inheritance and pointers to rust. Additionally, I chose to take advantage of `Option` and rust's enums with associated data to make some case analysis more explicit.

There is a corresponding PR in our [jts-test-runner](georust/jts-test-runner#6) crate which includes the bulk of the tests for the new Relate trait.

## Algorithm Overview 

This functionality is accessed on geometries, via the `Relate` trait, e.g. `line.relate(point)` which returns a DE-9IM [`IntersectionMatrix`](https://en.wikipedia.org/wiki/DE-9IM#Matrix_model).

The `Relate` trait is driven by the `RelateOperation`. The `RelateOperation` builds a `GeometryGraph` for each of the two geometries being related. 

A `GeometryGraph` is a systematic way to organize the "interesting" parts of a geometry's structure - e.g. where its vertices, lines, and areas lie relative to one another.

Once the `RelateOperation` has built the two `GeometryGraph`s, it uses them to efficiently compare the two Geometries's structures, outputting the `IntesectionMatrix`.





Co-authored-by: Michael Kirk <michael.code@endoftheworl.de>
Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
bors bot added a commit that referenced this pull request Apr 13, 2021
639: Introduce the geomgraph module for DE-9IM Relate trait r=frewsxcv,rmanoka a=michaelkirk

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

Fixes #513, #515

(I'm sorry it's so large)

~~I'm going to leave it as a draft (edit: 🤦 I failed to actually open the PR as a draft) while I wait to merge #636 and #638 and then do some rebasing, but I don't anticipate doing other large changes before review.~~ *update: ready for review!*

Here's some of the earlier work in pursuit of this:

#514
#516
#523
#524 
#538
#552
#561
#611
#628
#629
#636 

Primarily, this introduces the geomgraph module for a DE-9IM `Relate` trait.

geomgraph implements a topology graph largely inspired by JTS's module of the same name:
https://github.com/locationtech/jts/tree/jts-1.18.1/modules/core/src/main/java/org/locationtech/jts/geomgraph

You can see some of the reference code if you omit the "REMOVE JTS COMMENTS" commit. In some places the implementation is quite close to the JTS source. 

The overall "flow" is pretty similar to that of JTS, but in the small, there were some divergences. It's not easy (or desirable) to literally translate a Java codebase making heavy use of inheritance and pointers to rust. Additionally, I chose to take advantage of `Option` and rust's enums with associated data to make some case analysis more explicit.

There is a corresponding PR in our [jts-test-runner](georust/jts-test-runner#6) crate which includes the bulk of the tests for the new Relate trait.

## Algorithm Overview 

This functionality is accessed on geometries, via the `Relate` trait, e.g. `line.relate(point)` which returns a DE-9IM [`IntersectionMatrix`](https://en.wikipedia.org/wiki/DE-9IM#Matrix_model).

The `Relate` trait is driven by the `RelateOperation`. The `RelateOperation` builds a `GeometryGraph` for each of the two geometries being related. 

A `GeometryGraph` is a systematic way to organize the "interesting" parts of a geometry's structure - e.g. where its vertices, lines, and areas lie relative to one another.

Once the `RelateOperation` has built the two `GeometryGraph`s, it uses them to efficiently compare the two Geometries's structures, outputting the `IntesectionMatrix`.





Co-authored-by: Michael Kirk <michael.code@endoftheworl.de>
Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
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.

3 participants