use super::{along, attr_f64, material, BakedRoad, Warning};
use crate::coords::Point;
use crate::road::RoadSection;
use crate::{
Access, Along, DefaultRegulations, Lane, LaneId, Material, RoadType, Semantic, SpeedLimit,
Unit, Visibility,
};
#[derive(Clone, PartialEq)]
pub(super) enum SpeedDef {
Limit(SpeedLimit),
Undefined,
Unreadable { max: String, unit: String },
}
impl SpeedDef {
fn parse(node: roxmltree::Node, words: bool) -> Self {
let max = node.attribute("max").unwrap_or_default();
match max {
"no limit" if words => return Self::Limit(SpeedLimit::Unlimited),
"undefined" if words => return Self::Undefined,
_ => {}
}
let scale = match node.attribute("unit") {
None | Some("m/s") => Some(1.0),
Some("km/h") => Some(1.0 / 3.6),
Some("mph") => Some(0.44704),
Some(_) => None,
};
let limit = attr_f64(node, "max")
.filter(|v| *v >= 0.0)
.zip(scale)
.map(|(v, k)| (v * k) as f32)
.filter(|v| v.is_finite());
match limit {
Some(mps) => Self::Limit(SpeedLimit::Max(mps)),
None => Self::Unreadable {
max: max.to_string(),
unit: node.attribute("unit").unwrap_or_default().to_string(),
},
}
}
fn limit(&self) -> Option<SpeedLimit> {
match self {
Self::Limit(limit) => Some(*limit),
Self::Undefined | Self::Unreadable { .. } => None,
}
}
}
pub(super) struct TypeDef {
s: f64,
kind: RoadType,
country: String,
speed: Option<SpeedDef>,
}
#[derive(Clone)]
pub(super) struct LaneSpeedDef {
pub(super) s_offset: f64,
speed: SpeedDef,
}
pub(super) fn road_types(road: roxmltree::Node) -> Vec<TypeDef> {
let mut types: Vec<TypeDef> = road
.children()
.filter(|n| n.has_tag_name("type"))
.filter_map(|n| {
Some(TypeDef {
s: attr_f64(n, "s")?,
kind: road_type(n.attribute("type")),
country: n.attribute("country").unwrap_or_default().to_string(),
speed: n
.children()
.find(|c| c.has_tag_name("speed"))
.map(|c| SpeedDef::parse(c, true)),
})
})
.collect();
types.sort_by(|a, b| a.s.total_cmp(&b.s));
types
}
pub(super) fn lane_speeds(lane: roxmltree::Node) -> Vec<LaneSpeedDef> {
let mut speeds: Vec<LaneSpeedDef> = lane
.children()
.filter(|n| n.has_tag_name("speed"))
.map(|n| LaneSpeedDef {
s_offset: attr_f64(n, "sOffset").unwrap_or(0.0).max(0.0),
speed: SpeedDef::parse(n, false),
})
.collect();
speeds.sort_by(|a, b| a.s_offset.total_cmp(&b.s_offset));
speeds
}
#[derive(Clone, PartialEq)]
pub(super) enum AccessDef {
Restricted(Access),
Unrestricted,
Unreadable { rule: String },
}
impl AccessDef {
fn parse(node: roxmltree::Node) -> Self {
let users: Vec<String> = node
.attribute("restriction")
.into_iter()
.chain(
node.children()
.filter(|n| n.has_tag_name("restriction"))
.filter_map(|n| n.attribute("type")),
)
.filter(|u| !u.is_empty())
.map(String::from)
.collect();
match node.attribute("rule") {
Some("deny") if users.iter().all(|u| u == "none") && !users.is_empty() => {
Self::Unrestricted
}
Some("allow") => Self::Restricted(Access::Allow(users)),
Some("deny") => Self::Restricted(Access::Deny(
users.into_iter().filter(|u| u != "none").collect(),
)),
rule => Self::Unreadable {
rule: rule.unwrap_or_default().to_string(),
},
}
}
fn access(&self) -> Option<Access> {
match self {
Self::Restricted(access) => Some(access.clone()),
Self::Unrestricted | Self::Unreadable { .. } => None,
}
}
}
fn by_offset<T>(
lane: roxmltree::Node,
tag: &str,
read: impl Fn(roxmltree::Node) -> Option<T>,
) -> Vec<(f64, T)> {
let mut out: Vec<(f64, T)> = lane
.children()
.filter(|n| n.has_tag_name(tag))
.filter_map(|n| Some((attr_f64(n, "sOffset").unwrap_or(0.0).max(0.0), read(n)?)))
.collect();
out.sort_by(|a, b| a.0.total_cmp(&b.0));
out
}
pub(super) fn lane_rules(lane: roxmltree::Node) -> Vec<(f64, String)> {
let mut out: Vec<(f64, String)> = Vec::new();
for (s, rule) in by_offset(lane, "rule", |n| n.attribute("value").map(String::from)) {
match out.last_mut() {
Some((at, last)) if *at == s => {
last.push_str("; ");
last.push_str(&rule);
}
_ => out.push((s, rule)),
}
}
out
}
pub(super) fn lane_materials(lane: roxmltree::Node) -> Vec<(f64, Material)> {
by_offset(lane, "material", |n| Some(material(n)))
}
pub(super) fn lane_access(lane: roxmltree::Node) -> Vec<(f64, AccessDef)> {
let defs = by_offset(lane, "access", |n| Some(AccessDef::parse(n)));
let mut out = Vec::new();
for group in defs.chunk_by(|a, b| a.0 == b.0) {
let s = group[0].0;
let (mut allow, mut deny, mut lifted) = (None::<Vec<String>>, Vec::new(), false);
for (_, def) in group {
match def {
AccessDef::Restricted(Access::Allow(users)) => {
allow
.get_or_insert_with(Vec::new)
.extend(users.iter().cloned());
}
AccessDef::Restricted(Access::Deny(users)) => deny.extend(users.iter().cloned()),
AccessDef::Unrestricted => lifted = true,
AccessDef::Unreadable { .. } => out.push((s, def.clone())),
}
}
let resolved = match allow {
Some(allow) => Some(Access::Allow(
allow.into_iter().filter(|u| !deny.contains(u)).collect(),
)),
None if !deny.is_empty() => Some(Access::Deny(deny)),
None => None,
};
match resolved {
Some(access) => out.push((s, AccessDef::Restricted(access))),
None if lifted => out.push((s, AccessDef::Unrestricted)),
None => {}
}
}
out
}
#[derive(Clone, PartialEq)]
pub(super) enum VisibilityDef {
Seen(Visibility),
Unreadable {
distance: &'static str,
value: String,
},
}
impl VisibilityDef {
fn parse(node: roxmltree::Node) -> Self {
let distance = |name: &'static str| {
attr_f64(node, name)
.map(|v| v as f32)
.filter(|v| v.is_finite() && *v >= 0.0)
.ok_or(name)
};
let read = || -> Result<Visibility, &'static str> {
Ok(Visibility {
forward: distance("forward")?,
back: distance("back")?,
left: distance("left")?,
right: distance("right")?,
})
};
match read() {
Ok(seen) => Self::Seen(seen),
Err(distance) => Self::Unreadable {
distance,
value: node.attribute(distance).unwrap_or_default().to_string(),
},
}
}
fn visibility(&self) -> Option<Visibility> {
match self {
Self::Seen(seen) => Some(*seen),
Self::Unreadable { .. } => None,
}
}
}
pub(super) fn lane_visibility(lane: roxmltree::Node) -> Vec<(f64, VisibilityDef)> {
by_offset(lane, "visibility", |n| Some(VisibilityDef::parse(n)))
}
pub(super) fn speed_limit(semantics: &[Semantic]) -> Option<SpeedLimit> {
semantics.iter().find_map(|s| match s {
Semantic::Speed {
kind,
value: Some(value),
unit,
} if kind == "maximum" && *value >= 0.0 => {
let scale = match unit {
Some(Unit::MetrePerSecond) => 1.0,
Some(Unit::KilometrePerHour) => 1.0 / 3.6,
Some(Unit::MilePerHour) => 0.44704,
_ => return None,
};
Some(SpeedLimit::Max((value * scale) as f32))
.filter(|l| matches!(l, SpeedLimit::Max(v) if v.is_finite()))
}
_ => None,
})
}
pub(super) fn road_type(od_type: Option<&str>) -> RoadType {
match od_type.unwrap_or_default() {
"rural" => RoadType::Rural,
"motorway" => RoadType::Motorway,
"town" => RoadType::Town,
"lowSpeed" => RoadType::LowSpeed,
"pedestrian" => RoadType::Pedestrian,
"bicycle" => RoadType::Bicycle,
"townExpressway" => RoadType::TownExpressway,
"townCollector" => RoadType::TownCollector,
"townArterial" => RoadType::TownArterial,
"townPrivate" => RoadType::TownPrivate,
"townLocal" => RoadType::TownLocal,
"townPlayStreet" => RoadType::TownPlayStreet,
_ => RoadType::Unknown,
}
}
#[derive(Default)]
pub(super) struct Placed {
pub speed_limits: Vec<Along<SpeedLimit>>,
pub road_types: Vec<Along<RoadType>>,
pub road_countries: Vec<Along<String>>,
pub lane_rules: Vec<Along<String>>,
pub lane_access: Vec<Along<Access>>,
pub lane_materials: Vec<Along<Material>>,
pub lane_visibility: Vec<Along<Visibility>>,
}
pub(super) fn place<'a>(
roads: impl Iterator<Item = &'a BakedRoad>,
lanes: &[Lane],
defaults: &DefaultRegulations,
) -> Placed {
let mut out = Placed::default();
let default_for = |kind: RoadType| {
let rule = defaults.roads.iter().find(|r| r.road_type == kind)?;
speed_limit(&rule.semantics)
};
for road in roads {
let type_at = |s: f64| road.types.iter().rev().find(|t| t.s <= s + 1e-9);
for (sec, sec_def) in road.sections() {
for &(od_id, id) in &sec.lanes {
let Some(def) = sec_def.lane(od_id) else {
continue;
};
let speeds: Vec<(f64, Option<SpeedLimit>)> = def
.speeds
.iter()
.map(|l| (sec.start + l.s_offset, l.speed.limit()))
.collect();
let own = |s: f64| speeds.iter().rev().find(|(at, _)| *at <= s + 1e-9);
let type_starts = road.types.iter().map(|t| t.s);
let points = lanes[id.0].center.points();
let kinds = stretches(sec, type_starts.clone(), |s| type_at(s).map(|t| t.kind));
let countries = stretches(sec, type_starts.clone(), |s| {
type_at(s)
.map(|t| t.country.clone())
.filter(|c| !c.is_empty())
});
let limits = stretches(sec, type_starts.chain(speeds.iter().map(|l| l.0)), |s| {
own(s).map_or_else(
|| {
let kind = type_at(s)?;
match &kind.speed {
Some(speed) => speed.limit(),
None => default_for(kind.kind),
}
},
|l| l.1,
)
});
out.road_types
.extend(on_lane(id, points, &sec.stations, kinds));
out.road_countries
.extend(on_lane(id, points, &sec.stations, countries));
out.speed_limits
.extend(on_lane(id, points, &sec.stations, limits));
let rules = own_stretches(sec, &def.rules, |r| Some(r.clone()));
let access = own_stretches(sec, &def.access, AccessDef::access);
let materials = own_stretches(sec, &def.materials, |m| Some(m.clone()));
let visibility = own_stretches(sec, &def.visibility, VisibilityDef::visibility);
out.lane_rules
.extend(on_lane(id, points, &sec.stations, rules));
out.lane_access
.extend(on_lane(id, points, &sec.stations, access));
out.lane_materials
.extend(on_lane(id, points, &sec.stations, materials));
out.lane_visibility
.extend(on_lane(id, points, &sec.stations, visibility));
}
}
}
out
}
fn on_lane<'a, T: 'a>(
lane: LaneId,
points: &'a [Point],
stations: &'a [f64],
stretches: Vec<(f64, f64, T)>,
) -> impl Iterator<Item = Along<T>> + 'a {
stretches
.into_iter()
.map(move |(from, to, value)| Along {
lane,
from: along(points, stations, from),
to: along(points, stations, to),
value,
})
.filter(|a| a.to > a.from)
}
fn own_stretches<E, T: PartialEq>(
sec: &RoadSection,
entries: &[(f64, E)],
value: impl Fn(&E) -> Option<T>,
) -> Vec<(f64, f64, T)> {
let at = |s: f64| {
entries
.iter()
.rev()
.find(|(offset, _)| sec.start + offset <= s + 1e-9)
.and_then(|(_, e)| value(e))
};
stretches(
sec,
entries.iter().map(|(offset, _)| sec.start + offset),
at,
)
}
fn stretches<T: PartialEq>(
sec: &RoadSection,
cuts: impl Iterator<Item = f64>,
value_at: impl Fn(f64) -> Option<T>,
) -> Vec<(f64, f64, T)> {
let mut cuts: Vec<f64> = cuts
.filter(|&s| s > sec.start + 1e-6 && s < sec.end - 1e-6)
.chain([sec.start, sec.end])
.collect();
cuts.sort_by(f64::total_cmp);
cuts.dedup_by(|a, b| (*a - *b).abs() < 1e-6);
let mut out: Vec<(f64, f64, T)> = Vec::new();
for w in cuts.windows(2) {
let Some(value) = value_at(w[0]) else {
continue;
};
match out.last_mut() {
Some(last) if last.1 == w[0] && last.2 == value => last.1 = w[1],
_ => out.push((w[0], w[1], value)),
}
}
out
}
impl BakedRoad {
pub(super) fn access_warnings(&self) -> Vec<Warning> {
self.sections()
.flat_map(|(sec, def)| {
def.left.iter().chain(&def.right).flat_map(move |l| {
l.access.iter().filter_map(move |(offset, def)| match def {
AccessDef::Unreadable { rule } => Some(Warning::AccessDropped {
road_id: self.road.od_id.clone(),
s: sec.start + offset,
lane: l.id,
rule: rule.clone(),
}),
AccessDef::Restricted(_) | AccessDef::Unrestricted => None,
})
})
})
.collect()
}
pub(super) fn visibility_warnings(&self) -> Vec<Warning> {
self.sections()
.flat_map(|(sec, def)| {
def.left.iter().chain(&def.right).flat_map(move |l| {
l.visibility
.iter()
.filter_map(move |(offset, def)| match def {
VisibilityDef::Unreadable { distance, value } => {
Some(Warning::VisibilityDropped {
road_id: self.road.od_id.clone(),
s: sec.start + offset,
lane: l.id,
distance: distance.to_string(),
value: value.clone(),
})
}
VisibilityDef::Seen(_) => None,
})
})
})
.collect()
}
pub(super) fn speed_warnings(&self) -> Vec<Warning> {
let dropped = |s, lane, speed: &SpeedDef| match speed {
SpeedDef::Unreadable { max, unit } => Some(Warning::SpeedLimitDropped {
road_id: self.road.od_id.clone(),
s,
lane,
max: max.clone(),
unit: unit.clone(),
}),
SpeedDef::Limit(_) | SpeedDef::Undefined => None,
};
let road = self
.types
.iter()
.filter_map(|t| dropped(t.s, None, t.speed.as_ref()?));
let lanes = self.sections().flat_map(|(sec, def)| {
def.left.iter().chain(&def.right).flat_map(move |l| {
l.speeds
.iter()
.map(move |sp| (sec.start + sp.s_offset, l.id, &sp.speed))
})
});
road.chain(lanes.filter_map(|(s, id, speed)| dropped(s, Some(id), speed)))
.collect()
}
}