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};
pub fn merge_short_roads(map: &mut RawMap, consolidate_all: bool) -> BTreeSet<NodeID> {
#![allow(clippy::logic_bug)]
let mut merged = BTreeSet::new();
let mut queue: VecDeque<OriginalRoad> = VecDeque::new();
for r in map.roads.keys() {
queue.push_back(*r);
if false && connects_dual_carriageway(map, r) {
debug!("{} connects dual carriageways", r);
}
}
while !queue.is_empty() {
let id = queue.pop_front().unwrap();
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);
}
}
}
}
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 {
if map.roads[id].osm_tags.is("junction", "intersection") {
return true;
}
if !consolidate_all {
return false;
}
let road_length = if let Some(pl) = map.trimmed_road_geometry(*id) {
pl.length()
} else {
return false;
};
if road_length < Distance::meters(5.0) {
return true;
}
if connects_dual_carriageway(map, id) && road_length < Distance::meters(10.0) {
return true;
}
false
}
fn connects_dual_carriageway(map: &RawMap, id: &OriginalRoad) -> bool {
let connectors_angle = angle(&map.roads[id]);
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())
}