1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::collections::BTreeSet;

use crate::{osm, OriginalRoad, RawMap};

/// Does this road go between two divided one-ways? Ideally they're tagged explicitly
/// (https://wiki.openstreetmap.org/wiki/Tag:dual_carriageway%3Dyes), but we can also apply simple
/// heuristics to guess this.
#[allow(unused)]
pub fn connects_dual_carriageway(map: &RawMap, id: &OriginalRoad) -> bool {
    let connectors_angle = map.roads[id].angle();
    // There are false positives like https://www.openstreetmap.org/way/4636259 when we're looking
    // at a segment along a marked dual carriageway. Filter out by requiring the intersecting dual
    // carriageways to differ by a minimum angle.
    let within_degrees = 10.0;

    let mut i1_dual_carriageway = false;
    let mut oneway_names_i1: BTreeSet<String> = BTreeSet::new();
    for r in map.roads_per_intersection(id.i1) {
        let road = &map.roads[&r];
        if r == *id || connectors_angle.approx_eq(road.angle(), within_degrees) {
            continue;
        }
        if road.osm_tags.is("dual_carriageway", "yes") {
            i1_dual_carriageway = true;
        }
        if road.osm_tags.is("oneway", "yes") {
            if let Some(name) = road.osm_tags.get(osm::NAME) {
                oneway_names_i1.insert(name.to_string());
            }
        }
    }

    let mut i2_dual_carriageway = false;
    let mut oneway_names_i2: BTreeSet<String> = BTreeSet::new();
    for r in map.roads_per_intersection(id.i2) {
        let road = &map.roads[&r];
        if r == *id || connectors_angle.approx_eq(road.angle(), within_degrees) {
            continue;
        }
        if road.osm_tags.is("dual_carriageway", "yes") {
            i2_dual_carriageway = true;
        }
        if road.osm_tags.is("oneway", "yes") {
            if let Some(name) = road.osm_tags.get(osm::NAME) {
                oneway_names_i2.insert(name.to_string());
            }
        }
    }

    (i1_dual_carriageway && i2_dual_carriageway)
        || oneway_names_i1
            .intersection(&oneway_names_i2)
            .next()
            .is_some()
}