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

Question: Can geo compute polygon/line intersection on a sphere (or Earth)? #1289

Closed
jodavaho opened this issue Dec 21, 2024 · 2 comments
Closed

Comments

@jodavaho
Copy link

I looked at the line/polygon intersection code, and it appears to fall back on a coordinate-based bounding box. However, for spherical coordinates, that will not work.

The minimum example is:

use geo::Contains;
use geo::Distance;
use geo::Bearing;
use geo::Destination;
use geo::Intersects;
use geo::Haversine;
use geo::point;
use geo::Polygon;
fn main() {
    let eu = point!(x: 1.0, y: 45.0);
    let in_pacific = point!(x:179.0, y: 45.0);
    let line = geo::Line::new(eu, in_pacific);
    let poly_exterior = geo::LineString::from(vec![
        point!(x: 89.0, y: 44.0),
        point!(x: 89.0, y: 46.0),
        point!(x: 91.0, y: 46.0),
        point!(x: 91.0, y: 44.0),
        point!(x: 89.0, y: 44.0),
    ]);
    let poly = Polygon::new(
        poly_exterior,
        vec![],
    );
    println!("Line intersects polygon: {}", line.intersects(&poly));

    use geo::Length;

    let length = line.length::<geo::Haversine>();

    println!("Length of line: {}", length);

    let bearing = Haversine::bearing(eu, in_pacific);
    let distance = Haversine::distance(eu, in_pacific);

    let midpoint = Haversine::destination(eu, bearing, distance / 2.0);

    println!("Midpoint of line: {:?}", midpoint);
    println!("Midpoint in polygon? {}", poly.contains(&midpoint));
    //let why_not = Haversine::intersects(&line, &poly);

}

Output:

Line intersects polygon: true
Length of line: 10005616.697776927
Midpoint of line: Point(Coord { x: 90.00000000000023, y: 89.00015227392285 })
Midpoint in polygon? false
@urschrei
Copy link
Member

geo doesn't support any intersection calculations for spherical geometries, and does not itself provide any spherical geometry types (see the documentation for the geometry primitives for details of their semantics).

Point-distance and some related measures are provided for metric spaces other than Euclidean: see the metric spaces module docs.

@jodavaho
Copy link
Author

Thank you!

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

No branches or pull requests

2 participants