Skip to content

Intersection of closed polygons can be non-closed. #3

@rtavenner

Description

@rtavenner

As far as I can tell, the geo_types::LineString in a geo_types::Polygon is supposed to have its first and last coordinates be the same. (I believe this is what the docs mean by "closed". Is that correct?)
I have found a case where poly1 and poly2 both satisfy that condition, but poly1.intersection(&poly2) does not.

Is this a bug? Or am I just doing something wrong?

Cargo.toml

...
[dependencies]
geo-types = "0.3.0"
geo-booleanop = { git = "https://github.com/21re/rust-geo-booleanop.git" }

main.rs

#![forbid(unsafe_code)]

extern crate geo_types;
extern crate geo_booleanop;

use geo_types::*;
use geo_booleanop::boolean::BooleanOp;

fn is_closed(p: &Polygon<f64>) -> bool {
    &p.exterior.0[0] == p.exterior.0.last().unwrap()
}

fn main() {
    let poly1: Polygon<f64> = 
        Polygon {
            exterior: LineString(
                vec![
                    Coordinate {
                        x: -530.,
                        y: -530.
                    },
                    Coordinate {
                        x: -530.,
                        y: 530.
                    },
                    Coordinate {
                        x: 530.,
                        y: 530.
                    },
                    Coordinate {
                        x: 530.,
                        y: -530.
                    },
                    Coordinate {
                        x: -530.,
                        y: -530.
                    }
                ]
            ),
            interiors: vec![]
        };
    let poly2: Polygon<f64> =
        Polygon {
            exterior: LineString(
                vec![
                    Coordinate {
                        x: 1.2500125250252,
                        y: -531.
                    },
                    Coordinate {
                        x: -98.,
                        y: -531.
                    },
                    Coordinate {
                        x: -98.,
                        y: 531.
                    },
                    Coordinate {
                        x: 1.250012525025,
                        y: 531.
                    },
                    Coordinate {
                        x: 1.2500125250252,
                        y: -531.
                    }
                ]
            ),
            interiors: vec![]
        };

    println!("{}", is_closed(&poly1));
    println!("{}", is_closed(&poly2));
    println!("{}", is_closed(&poly1.intersection(&poly2).0[0]));

}

Output:

true
true
false

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions