Skip to main content

multipolygon/
multipolygon.rs

1// SPDX-FileCopyrightText: 2025 rstared contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use geo_types::{MultiPolygon, Point, Polygon, line_string};
6use rstared::AABB;
7use rstared::RTreed;
8
9fn main() {
10    let multipolygon: MultiPolygon<f64> = MultiPolygon::new(vec![]);
11    let mut rtreed = RTreed::new(multipolygon);
12
13    // Push two polygons, recording them in the R-tree.
14    rtreed.push(Polygon::new(
15        line_string![
16            (x: 0.0, y: 0.0),
17            (x: 1.0, y: 0.0),
18            (x: 1.0, y: 1.0),
19            (x: 0.0, y: 0.0),
20        ],
21        vec![],
22    ));
23    rtreed.push(Polygon::new(
24        line_string![
25            (x: 1.0, y: 1.0),
26            (x: 2.0, y: 1.0),
27            (x: 2.0, y: 2.0),
28            (x: 1.0, y: 1.0),
29        ],
30        vec![],
31    ));
32
33    // Locate the two polygons in the R-tree.
34    assert_eq!(
35        rtreed
36            .rtree()
37            .locate_in_envelope(AABB::from_corners(
38                Point::new(0.0, 0.0),
39                Point::new(2.0, 2.0),
40            ))
41            .count(),
42        2
43    );
44
45    // Access a polygon by its index in the MultiPolygon.
46    assert_eq!(
47        rtreed.get(&0),
48        Some(&Polygon::new(
49            line_string![
50                (x: 0.0, y: 0.0),
51                (x: 1.0, y: 0.0),
52                (x: 1.0, y: 1.0),
53                (x: 0.0, y: 0.0),
54            ],
55            vec![],
56        ))
57    );
58}
59
60#[test]
61fn test() {
62    main();
63}