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
36 changes: 36 additions & 0 deletions crates/data/src/cityjson/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ fn instances_and_tin_reliefs_are_said() {
assert!(read("m.city.json", &only, None).unwrap_err().to_string().contains("TINRelief"));
}

/// An instance whose reference point can't be drawn isn't said to be drawn as a point.
#[test]
fn an_instance_kept_without_geometry_isnt_said_to_be_drawn() {
let instance = |at: usize| {
json!({"tree": {"type": "SolitaryVegetationObject", "geometry": [
{"type": "GeometryInstance", "template": 0, "boundaries": [at], "transformationMatrix": []}]}})
};
let vertices = json!([[0, 0, 0], [1e200, 0, 0]]);
let out = read("m.city.json", &doc(vertices.clone(), instance(1)), Crs::from_epsg(3857)).unwrap();
assert!(geometry(&out[0].0, 0).is_none(), "{:?}", geometry(&out[0].0, 0));
assert!(!says(&out, "geometry instances are drawn"), "{:?}", messages_of(&out));
let out = read("m.city.json", &doc(vertices, instance(0)), Crs::from_epsg(3857)).unwrap();
assert!(says(&out, "1 geometry instances are drawn as a point"), "{:?}", messages_of(&out));
}

/// An attribute named as one of the reader's fields keeps its value, under a name that says so.
#[test]
fn an_attribute_named_as_a_field_is_renamed_and_said() {
Expand Down Expand Up @@ -520,6 +535,26 @@ fn a_lone_face_seen_edge_on_has_no_footprint() {
assert!(says(&out, "1 city objects have a shape that can't be drawn"), "{:?}", messages_of(&out));
}

/// A level 0 face leaning a little off vertical shows a sliver from above, which isn't kept as the
/// footprint as it stands: it is measured as [`footprint`] measures any face, and is a wall.
#[test]
fn a_lone_face_nearly_on_edge_is_a_wall() {
let bytes = doc(
json!([[0.0, 0.0, 0.0], [10.0, 0.0, 0.0], [10.0, 0.3, 10.0], [0.0, 0.3, 10.0]]),
json!({"w": {"type": "Building", "geometry": [{"type": "MultiSurface", "lod": "0", "boundaries": [[[0, 1, 2, 3]]]}]}}),
);
let out = read("m.city.json", &bytes, None).unwrap();
assert!(says(&out, "1 city objects with no faces seen from above"), "{:?}", messages_of(&out));
// Lying flat, the same face is the footprint and says nothing.
let bytes = doc(
json!([[0.0, 0.0, 0.0], [10.0, 0.0, 0.0], [10.0, 0.3, 0.0], [0.0, 0.3, 0.0]]),
json!({"w": {"type": "Building", "geometry": [{"type": "MultiSurface", "lod": "0", "boundaries": [[[0, 1, 2, 3]]]}]}}),
);
let out = read("m.city.json", &bytes, None).unwrap();
assert!((area(geometry(&out[0].0, 0)) - 3.0).abs() < 1e-9, "{:?}", geometry(&out[0].0, 0));
assert!(messages_of(&out).is_empty(), "{:?}", messages_of(&out));
}

/// Lines and points show the level of detail they came from, not the footprint's. Their own heights
/// differing is more than `z_min` and `z_max` keep, which is said; all at one height, it isn't.
#[test]
Expand Down Expand Up @@ -981,6 +1016,7 @@ fn a_system_given_over_the_one_named_is_said() {
// ...and without one given, it is an error, not a note.
let e = read("m.city.json", root.to_string().as_bytes(), None).unwrap_err().to_string();
assert!(e.contains("it names a coordinate system RustGIS doesn't know"), "{e}");
assert!(e.contains("give one with the crs parameter of layer.addData"), "{e}");
}

/// One building whose LoD 2 geometry passes the face cap: a face with a courtyard first (marked
Expand Down
11 changes: 7 additions & 4 deletions crates/data/src/citymodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use serde_json::Value as Json;

use crate::footprint::{Hull, P3, footprint, usable_xy};
use crate::footprint::{Hull, P3, footprint, shows, usable_xy};
use crate::raster::Crs;
use crate::{Coord, DataError, Dataset, Field, Geometry, GeometryType, ReadReport, Result, Value};

Expand Down Expand Up @@ -211,7 +211,7 @@ fn lone_face(face: &[Vec<P3>]) -> Option<Geometry> {
hi - lo
};
let size = span(|c| c.x).max(span(|c| c.y));
(crate::geometry::ring_area(outer).abs() > size * size * 5e-5).then(|| Geometry::Polygon(vec![rings]))
shows(face.first()?, size).then(|| Geometry::Polygon(vec![rings]))
}

/// An object's footprint and the level of detail it came from: the first of its LoD 0, the
Expand Down Expand Up @@ -355,7 +355,7 @@ pub(crate) fn coordinate_system(
}
(None, Some(system)) => known_system(system).ok_or_else(|| {
let system: String = system.chars().take(80).collect();
DataError::Format(format!("{file_name}: it names a coordinate system RustGIS doesn't know ({system})"))
DataError::Format(format!("{file_name}: it names a coordinate system RustGIS doesn't know ({system}); give one with the crs parameter of layer.addData (from the command line, Python or the AI Assistant)"))
})?,
(None, None) if within(180.0, 90.0) => {
notes.push("the file names no coordinate system: its coordinates are read as longitude/latitude".into());
Expand Down Expand Up @@ -642,7 +642,10 @@ pub(crate) fn object_rows(parts: ObjectParts, object: usize, counts: &mut Counts
push(Family::Line, line_lod, geometry, heights_differ(lines.iter().flatten()), lines_draw);
}
let Highest { lod: point_lod, parts: points, usable: points_usable, .. } = points;
counts.instances += points.iter().filter(|(_, instance)| *instance).count();
// Only those drawn: a point row kept without geometry draws no instance.
if points_usable {
counts.instances += points.iter().filter(|(_, instance)| *instance).count();
}
let points: Vec<P3> = points.into_iter().map(|(p, _)| p).collect();
if !points.is_empty() {
let geometry = points_usable.then(|| match points.as_slice() {
Expand Down
44 changes: 23 additions & 21 deletions crates/data/src/footprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ pub(crate) enum Hull {
Unmerged,
}

/// Whether a face's outer `ring` shows from above, in a shape `size` across (its larger X or Y
/// span), by the cosine of its tilt (its projected area over its area): walls, even leaning ones,
/// show as slivers, which cover nothing and which the union can't always take. Faces narrower than
/// a ten-thousandth of the shape are left out too (a flat shape).
pub(crate) fn shows(ring: &[P3], size: f64) -> bool {
let (mut nx, mut ny, mut nz) = (0.0, 0.0, 0.0);
let (mut longest, mut area2) = (0.0f64, 0.0);
// From the ring's first point: projected coordinates run to millions, whose products would
// lose the millimetres a small face is measured in.
let o = ring.first().copied().unwrap_or([0.0; 3]);
let local: Vec<P3> = ring.iter().map(|p| [p[0] - o[0], p[1] - o[1], p[2] - o[2]]).collect();
for (a, b) in local.iter().zip(local.iter().cycle().skip(1)) {
nx += (a[1] - b[1]) * (a[2] + b[2]);
ny += (a[2] - b[2]) * (a[0] + b[0]);
nz += (a[0] - b[0]) * (a[1] + b[1]);
longest = longest.max((b[0] - a[0]).hypot(b[1] - a[1]));
area2 += a[0] * b[1] - b[0] * a[1];
}
let n = nx.hypot(ny).hypot(nz);
n > 0.0 && nz.abs() / n > 0.05 && longest > 0.0 && area2.abs() / longest > size * 1e-4
}

/// A shape's footprint: the union of its `faces` (each its outer ring and then its holes) seen from
/// above. Faces standing on edge, such as walls, show nothing and are left out. The footprint is the
/// hull of `points` when no face shows, or when its faces aren't merged: `truncated` (the reader left
Expand Down Expand Up @@ -72,34 +94,14 @@ pub(crate) fn footprint<F: AsRef<[Vec<P3>]>>(
if !size.is_finite() {
return (None, None);
}
// A face shows from above by the cosine of its tilt (its projected area over its area): walls,
// even leaning ones, show as slivers, which cover nothing and which the union can't always
// take. Faces narrower than a ten-thousandth of the shape are left out too (a flat shape).
let shows = |ring: &[P3]| {
let (mut nx, mut ny, mut nz) = (0.0, 0.0, 0.0);
let (mut longest, mut area2) = (0.0f64, 0.0);
// From the ring's first point: projected coordinates run to millions, whose products would
// lose the millimetres a small face is measured in.
let o = ring.first().copied().unwrap_or([0.0; 3]);
let local: Vec<P3> = ring.iter().map(|p| [p[0] - o[0], p[1] - o[1], p[2] - o[2]]).collect();
for (a, b) in local.iter().zip(local.iter().cycle().skip(1)) {
nx += (a[1] - b[1]) * (a[2] + b[2]);
ny += (a[2] - b[2]) * (a[0] + b[0]);
nz += (a[0] - b[0]) * (a[1] + b[1]);
longest = longest.max((b[0] - a[0]).hypot(b[1] - a[1]));
area2 += a[0] * b[1] - b[0] * a[1];
}
let n = nx.hypot(ny).hypot(nz);
n > 0.0 && nz.abs() / n > 0.05 && longest > 0.0 && area2.abs() / longest > size * 1e-4
};
let ring2 = |r: &[P3]| geo::LineString::from(r.iter().map(|p| (p[0], p[1])).collect::<Vec<_>>());
// Past the cap, a partial union would be wrong: the hull below stands in.
let seen: Vec<geo::Polygon<f64>> = faces
.iter()
.filter(|_| !truncated)
.filter_map(|face| {
let (outer, holes) = face.as_ref().split_first()?;
(outer.len() >= 3 && shows(outer))
(outer.len() >= 3 && shows(outer, size))
.then(|| geo::Polygon::new(ring2(outer), holes.iter().map(|h| ring2(h)).collect()).orient(Direction::Default))
})
.collect();
Expand Down
Loading
Loading