Skip to content

Commit

Permalink
Introduce internal consistency checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 26, 2022
1 parent 5b40c7a commit 95f39bf
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
14 changes: 14 additions & 0 deletions osm2streets/src/intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ impl Intersection {
pub fn is_map_edge(&self) -> bool {
self.kind == IntersectionKind::MapEdge
}

pub fn describe(&self) -> String {
let osm_ids = self
.osm_ids
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(", ");
if osm_ids.is_empty() {
self.id.to_string()
} else {
format!("{} ({})", self.id, osm_ids)
}
}
}

impl StreetNetwork {
Expand Down
1 change: 1 addition & 0 deletions osm2streets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod render;
mod road;
mod transform;
mod types;
mod validate;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StreetNetwork {
Expand Down
14 changes: 14 additions & 0 deletions osm2streets/src/road.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,20 @@ impl Road {
pub fn from_osm_way(&self, way: osm::WayID) -> bool {
self.osm_ids.iter().any(|id| *id == way)
}

pub fn describe(&self) -> String {
let osm_ids = self
.osm_ids
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(", ");
if osm_ids.is_empty() {
self.id.to_string()
} else {
format!("{} ({})", self.id, osm_ids)
}
}
}

impl StreetNetwork {
Expand Down
52 changes: 52 additions & 0 deletions osm2streets/src/validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::StreetNetwork;

impl StreetNetwork {
/// Validates various things are true about the StreetNetwork, panicking if not.
pub fn check_invariants(&self) {
for r in self.roads.values() {
for i in r.endpoints() {
let i = &self.intersections[&i];
assert!(
i.roads.contains(&r.id),
"{} doesn't list {}",
i.describe(),
r.describe()
);
}
assert!(
!r.lane_specs_ltr.is_empty(),
"{} has no lanes",
r.describe()
);
}

for i in self.intersections.values() {
assert!(!i.roads.is_empty(), "{} has no roads", i.describe());

for r in &i.roads {
let r = &self.roads[r];
assert!(
r.src_i == i.id || r.dst_i == i.id,
"{} contains {}, which doesn't point to it",
i.describe(),
r.describe()
);
}

for (r1, r2) in &i.movements {
assert!(
i.roads.contains(r1),
"{} has a movement for the wrong road {}",
i.describe(),
r1
);
assert!(
i.roads.contains(r2),
"{} has a movement for the wrong road {}",
i.describe(),
r2
);
}
}
}
}
2 changes: 2 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ mod tests {
MapConfig::default(),
&mut timer,
)?;
street_network.check_invariants();
street_network
.apply_transformations(Transformation::standard_for_clipped_areas(), &mut timer);
street_network.check_invariants();
street_network.save_to_geojson(format!("{path}/geometry.json"))?;

let road_network: RoadNetwork = street_network.into();
Expand Down

0 comments on commit 95f39bf

Please sign in to comment.