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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::collections::{BTreeSet, VecDeque};

use abstutil::Timer;
use geom::{Angle, Distance};

use crate::osm;
use crate::osm::NodeID;
use crate::raw::{OriginalRoad, RawMap, RawRoad};

/// Merge tiny "roads" that're actually just part of a complicated intersection. Returns all
/// surviving intersections adjacent to one of these merged roads.
pub fn merge_short_roads(map: &mut RawMap, consolidate_all: bool) -> BTreeSet<NodeID> {
    #![allow(clippy::logic_bug)] // remove once the TODO below is taken care of
    let mut merged = BTreeSet::new();

    let mut queue: VecDeque<OriginalRoad> = VecDeque::new();
    for r in map.roads.keys() {
        queue.push_back(*r);

        // TODO Enable after improving this heuristic.
        if false && connects_dual_carriageway(map, r) {
            debug!("{} connects dual carriageways", r);
        }
    }

    while !queue.is_empty() {
        let id = queue.pop_front().unwrap();

        // The road might've been deleted
        if !map.roads.contains_key(&id) {
            continue;
        }

        if should_merge(map, &id, consolidate_all) {
            match map.merge_short_road(id) {
                Ok((i, _, _, new_roads)) => {
                    merged.insert(i);
                    queue.extend(new_roads);
                }
                Err(err) => {
                    warn!("Not merging short road / junction=intersection: {}", err);
                }
            }
        }
    }

    // Use this to quickly test overrides to some ways before upstreaming in OSM.
    // Since these IDs might be based on already merged roads, do these last.
    if let Ok(ways) = abstio::maybe_read_json::<Vec<OriginalRoad>>(
        "merge_osm_ways.json".to_string(),
        &mut Timer::throwaway(),
    ) {
        for id in ways {
            match map.merge_short_road(id) {
                Ok((i, _, _, _)) => {
                    merged.insert(i);
                }
                Err(err) => {
                    warn!("Not merging short road / junction=intersection: {}", err);
                }
            }
        }
    }

    merged
}

fn should_merge(map: &RawMap, id: &OriginalRoad, consolidate_all: bool) -> bool {
    // See https://wiki.openstreetmap.org/wiki/Proposed_features/junction%3Dintersection
    if map.roads[id].osm_tags.is("junction", "intersection") {
        return true;
    }

    // TODO Keep everything below disabled until merging works better.
    if !consolidate_all {
        return false;
    }

    let road_length = if let Some(pl) = map.trimmed_road_geometry(*id) {
        pl.length()
    } else {
        // The road or something near it collapsed down into a single point or something. This can
        // happen while merging several short roads around a single junction.
        return false;
    };

    // Any road anywhere shorter than this should get merged.
    if road_length < Distance::meters(5.0) {
        return true;
    }

    // Roads connecting dual carriageways can use a longer threshold for merging.
    if connects_dual_carriageway(map, id) && road_length < Distance::meters(10.0) {
        return true;
    }

    false
}

// 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.
fn connects_dual_carriageway(map: &RawMap, id: &OriginalRoad) -> bool {
    let connectors_angle = angle(&map.roads[id]);
    // 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(angle(road), 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(angle(road), 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()
}

fn angle(r: &RawRoad) -> Angle {
    r.center_points[0].angle_to(*r.center_points.last().unwrap())
}