Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ of use.
| QGIS test corpus (`crates/map/testdata/qgis/generated/`: styles, projects and their sample data) | Dominik Strebel ([@pick2510](https://github.com/pick2510)) for the RustGIS project: written by [QGIS](https://qgis.org) 4.2.3 through its API, from `generate.py` there; the data is made up | Apache-2.0 |
| CFF test font (`crates/render/testdata/test-cff.otf`), read only by the PDF font tests | The RustGIS project: generated for this test with [fontTools](https://github.com/fonttools/fonttools) by the script in `crates/render/testdata/README.md`; its glyphs are made up | Apache-2.0 |
| JAXA Earth catalog test copy (`crates/tiles/tests/data/jaxa_catalog.v2.md`), read only by tests | JAXA: the [JAXA Earth API](https://data.earth.jaxa.jp/) dataset catalog ([catalog.v2.md](https://data.earth.jaxa.jp/app/mcp/catalog.v2.md)), copied on 2026-11-08 | JAXA's [terms of use](https://earth.jaxa.jp/en/data/policy/): free to use and redistribute, with credit to JAXA |
| CityGML test mesh (`crates/data/tests/data/rocking-chair.gml`), read only by tests | 97 faces of the rocking chair in OGC's CityGML 3.0 sample `Building_LOD3-EPSG25832.gml` ([CityGML-3.0Encodings](https://github.com/opengeospatial/CityGML-3.0Encodings/blob/master/Moved_to_CITYGML-3.0Encoding_CityGML/Examples/Examples-from-CityGML-2.0/building/CityGMLv3/Building_LOD3-EPSG25832.gml)), copyright (c) 2013 3DXScape, copied on 2026-11-12 and rewritten as a CityGML 2.0 object, coordinates unchanged | MIT |
| Analysis reference corpus (`crates/analysis/tests/reference/`: layers and expected results) | Written for the RustGIS project from `generate.py` there: the layers are made up; the expected results were computed by [GEOS](https://libgeos.org) 3.13.1 through [Shapely](https://shapely.readthedocs.io) 2.1.2, and distances, buffers, areas and lengths by GeographicLib in [PROJ](https://proj.org) 9.5.1 through [pyproj](https://pyproj4.github.io/pyproj/) 3.7.2, run only to write them (GEOS is LGPL-2.1, Shapely BSD-3-Clause, PROJ and pyproj MIT; none is included in or linked into RustGIS) | Apache-2.0 |

Raster coordinate systems are looked up by EPSG code in the PROJ definitions of the
Expand Down
15 changes: 15 additions & 0 deletions crates/data/src/citygml/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2318,3 +2318,18 @@ fn a_prototype_solid_with_a_linked_exterior_is_said_as_its_link() {
let placed = of_type(&out, GeometryType::Polygon);
assert_eq!(value(placed, row_of(placed, "t"), "z_max"), Value::Float(1.0), "the cavity isn't placed: {:?}", messages_of(&out));
}

/// A curved mesh seen from above lays its faces almost on each other: 97 faces of the rocking chair
/// in OGC's CityGML 3.0 LoD 3 sample, whose union took 15 seconds, and the whole chair's 956 never
/// finished (#1891). With their corners on the union's grid, the chair is read within five seconds
/// (milliseconds, in fact), its faces merged rather than drawn as the outline of their points.
#[test]
fn a_curved_mesh_is_merged_in_a_moment() {
let bytes = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/rocking-chair.gml"));
let (sent, got) = std::sync::mpsc::channel();
std::thread::spawn(move || sent.send(read("chair.gml", bytes, None)));
let out = got.recv_timeout(std::time::Duration::from_secs(5)).expect("read within 5 seconds").unwrap();
assert_eq!(out[0].0.features.len(), 1);
assert!(out[0].0.features[0].geometry.is_some());
assert!(!says(&out, "outline"), "merged, not drawn as the outline of its points: {:?}", messages_of(&out));
}
44 changes: 41 additions & 3 deletions crates/data/src/footprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ const VERTICES_PER_FACE: usize = 5;
/// they overflow to a NaN, on which the hull panics (in the browser, where it can't be caught).
const MAX_COORDINATE: f64 = 1e150;

/// The grid, as a share of a shape's size, its faces' corners are rounded to before their union
/// (to the power of two below it): a millimetre or less on a kilometre, a hundredth of the
/// narrowest face [`shows`] takes. A curved mesh seen from above lays faces almost on each other,
/// their edges apart by less than a micrometre, and the union's work grows with every near
/// crossing: 97 faces of a chair took 15 seconds, and its 956 never finished (#1891). On the grid,
/// corners that nearly meet do meet, and the chair's union takes milliseconds. The grid narrows the
/// union's worst cases; it doesn't bound its time, and a union is one call that Cancel can't stop.
const UNION_GRID: f64 = 1e-6;

/// Whether a point's X and Y are ones a footprint takes: finite numbers within [`MAX_COORDINATE`].
pub(crate) fn usable_xy(p: &P3) -> bool {
p[0].abs() <= MAX_COORDINATE && p[1].abs() <= MAX_COORDINATE
Expand Down Expand Up @@ -74,7 +83,9 @@ pub(crate) fn shows(ring: &[P3], size: f64) -> bool {
/// is too narrow to show. A shape with a point or a face whose X or Y isn't a finite number, or
/// passes [`MAX_COORDINATE`], has no footprint: the hull, and orienting a face, panic on NaN. A
/// height that isn't a finite number (or passes the limit) can't say which way a face looks: all of
/// them go, not only that one (which would drop it silently), and the hull stands in.
/// them go, not only that one (which would drop it silently), and the hull stands in. A merged
/// footprint's corners are on [`UNION_GRID`]: within a two-millionth of the shape's size of the
/// faces'.
pub(crate) fn footprint<F: AsRef<[Vec<P3>]>>(
faces: &[F],
truncated: bool,
Expand All @@ -94,7 +105,13 @@ pub(crate) fn footprint<F: AsRef<[Vec<P3>]>>(
if !size.is_finite() {
return (None, None);
}
let ring2 = |r: &[P3]| geo::LineString::from(r.iter().map(|p| (p[0], p[1])).collect::<Vec<_>>());
// From the shape's corner, on the grid (see `UNION_GRID`); the union is moved back below. The
// grid is a power of two: a corner a whole number of steps from the shape's corner stays as it
// is (a box from 0 keeps its corners), others move by half a step at most. A shape with no
// size (a point) has no grid, and nothing of it shows anyway.
let grid = if size > 0.0 { (size * UNION_GRID).log2().floor().exp2() } else { 0.0 };
let snap = |v: f64, from: f64| if grid > 0.0 { ((v - from) / grid).round() * grid } else { v - from };
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let ring2 = |r: &[P3]| geo::LineString::from(r.iter().map(|p| (snap(p[0], x0), snap(p[1], y0))).collect::<Vec<_>>());
// Past the cap, a partial union would be wrong: the hull below stands in.
let seen: Vec<geo::Polygon<f64>> = faces
.iter()
Expand All @@ -116,7 +133,10 @@ pub(crate) fn footprint<F: AsRef<[Vec<P3>]>>(
// Spent before the union runs, so faces whose union panics (and become the hull) count too:
// the budget bounds the work tried, not the footprints made.
*budget -= cost;
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| geo::unary_union(seen.iter()))).ok().filter(|u| !u.0.is_empty())
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| geo::unary_union(seen.iter())))
.ok()
.filter(|u| !u.0.is_empty())
.map(|u| geo::MapCoords::map_coords(&u, |c| geo::coord! { x: c.x + x0, y: c.y + y0 }))
};
let (shape, hull) = match union {
Some(u) => (u, None),
Expand Down Expand Up @@ -163,6 +183,24 @@ mod tests {
assert!((area(&shape) - 100.0).abs() < 1e-9, "{shape:?}");
}

/// Far from the origin, as projected coordinates are, a footprint is merged near 0 and moved
/// back: the box keeps its place and its area.
#[test]
fn a_footprint_far_from_the_origin_is_where_its_faces_are() {
let (x, y) = (458_879.0, 5_438_353.0);
let square = |z: f64| vec![[x, y, z], [x + 10.0, y, z], [x + 10.0, y + 10.0, z], [x, y + 10.0, z], [x, y, z]];
let mut budget = 10;
let (shape, hull) = footprint(&[vec![square(0.0)], vec![square(5.0)]], false, &[square(0.0), square(5.0)], &mut budget);
assert_eq!(hull, None);
let Some(Geometry::Polygon(polys)) = &shape else { panic!("{shape:?}") };
let corners: Vec<&Coord> = polys.iter().flatten().flatten().collect();
let (xs, ys): (Vec<f64>, Vec<f64>) = corners.iter().map(|c| (c.x, c.y)).unzip();
let min = |v: &[f64]| v.iter().copied().fold(f64::INFINITY, f64::min);
let max = |v: &[f64]| v.iter().copied().fold(f64::NEG_INFINITY, f64::max);
assert_eq!((min(&xs), max(&xs), min(&ys), max(&ys)), (x, x + 10.0, y, y + 10.0), "{shape:?}");
assert!((area(&shape) - 100.0).abs() < 1e-6, "{shape:?}");
}

/// A courtyard stays open: a face's hole is kept in the footprint, not filled.
#[test]
fn a_hole_in_a_face_stays_in_the_footprint() {
Expand Down
Loading
Loading