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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#[macro_use]
extern crate anyhow;
#[macro_use]
extern crate log;
use anyhow::Result;
use abstio::MapName;
use abstutil::{Tags, Timer};
use geom::{Distance, FindClosest, GPSBounds, LonLat, PolyLine, Pt2D, Ring};
use map_model::raw::RawMap;
use map_model::{osm, raw, Amenity, MapConfig};
use serde::{Deserialize, Serialize};
mod clip;
mod extract;
pub mod osm_geom;
mod parking;
pub mod reader;
mod snappy;
mod split_ways;
mod srtm;
mod transit;
pub struct Options {
pub osm_input: String,
pub name: MapName,
pub clip: Option<String>,
pub map_config: MapConfig,
pub onstreet_parking: OnstreetParking,
pub public_offstreet_parking: PublicOffstreetParking,
pub private_offstreet_parking: PrivateOffstreetParking,
pub elevation: Option<String>,
pub include_railroads: bool,
pub extra_buildings: Option<String>,
}
#[derive(Clone, Serialize, Deserialize)]
pub enum OnstreetParking {
JustOSM,
Blockface(String),
SomeAdditionalWhereNoData {
pct: usize,
},
}
#[derive(Clone, Serialize, Deserialize)]
pub enum PublicOffstreetParking {
None,
GIS(String),
}
#[derive(Clone, Serialize, Deserialize)]
pub enum PrivateOffstreetParking {
FixedPerBldg(usize),
}
pub fn convert(opts: Options, timer: &mut abstutil::Timer) -> RawMap {
let mut map = RawMap::blank(opts.name.clone());
if let Some(ref path) = opts.clip {
let pts = LonLat::read_osmosis_polygon(path).unwrap();
let gps_bounds = GPSBounds::from(pts.clone());
map.boundary_polygon = Ring::must_new(gps_bounds.convert(&pts)).to_polygon();
map.gps_bounds = gps_bounds;
}
let extract = extract::extract_osm(&mut map, &opts, timer);
let (amenities, pt_to_road) = split_ways::split_up_roads(&mut map, extract, timer);
clip::clip_map(&mut map, timer);
abstutil::retain_btreemap(&mut map.roads, |r, _| r.i1 != r.i2);
let all_routes = map.bus_routes.drain(..).collect::<Vec<_>>();
let mut routes = Vec::new();
for route in all_routes {
let name = format!("{} ({})", route.osm_rel_id, route.full_name);
match transit::snap_bus_stops(route, &mut map, &pt_to_road) {
Ok(r) => {
routes.push(r);
}
Err(err) => {
error!("Skipping {}: {}", name, err);
}
}
}
map.bus_routes = routes;
use_amenities(&mut map, amenities, timer);
parking::apply_parking(&mut map, &opts, timer);
if let Some(ref path) = opts.elevation {
use_elevation(&mut map, path, timer);
}
if false {
generate_elevation_queries(&map).unwrap();
}
if let Some(ref path) = opts.extra_buildings {
add_extra_buildings(&mut map, path).unwrap();
}
snappy::snap_cycleways(&mut map, timer);
map.config = opts.map_config;
map
}
fn use_amenities(map: &mut RawMap, amenities: Vec<(Pt2D, Amenity)>, timer: &mut Timer) {
let mut closest: FindClosest<osm::OsmID> = FindClosest::new(&map.gps_bounds.to_bounds());
for (id, b) in &map.buildings {
closest.add(*id, b.polygon.points());
}
timer.start_iter("match building amenities", amenities.len());
for (pt, amenity) in amenities {
timer.next();
if let Some((id, _)) = closest.closest_pt(pt, Distance::meters(50.0)) {
let b = map.buildings.get_mut(&id).unwrap();
if b.polygon.contains_pt(pt) {
b.amenities.push(amenity);
}
}
}
}
fn use_elevation(map: &mut RawMap, path: &str, timer: &mut Timer) {
timer.start("apply elevation data to intersections");
let elevation = srtm::Elevation::load(path).unwrap();
for i in map.intersections.values_mut() {
if map.boundary_polygon.contains_pt(i.point) {
i.elevation = elevation.get(i.point.to_gps(&map.gps_bounds));
}
}
timer.stop("apply elevation data to intersections");
}
fn generate_elevation_queries(map: &RawMap) -> Result<()> {
use std::fs::File;
use std::io::Write;
let mut f = File::create("elevation_queries")?;
for r in map.roads.values() {
if let Ok(pl) = PolyLine::new(r.center_points.clone()) {
let mut pts = Vec::new();
let mut dist = Distance::ZERO;
while dist <= pl.length() {
let (pt, _) = pl.dist_along(dist).unwrap();
pts.push(pt);
dist += Distance::meters(1.0);
}
if *pts.last().unwrap() != pl.last_pt() {
pts.push(pl.last_pt());
}
for (idx, gps) in map.gps_bounds.convert_back(&pts).into_iter().enumerate() {
write!(f, "{},{}", gps.x(), gps.y())?;
if idx != pts.len() - 1 {
write!(f, " ")?;
}
}
writeln!(f)?;
}
}
Ok(())
}
fn add_extra_buildings(map: &mut RawMap, path: &str) -> Result<()> {
let mut polygons = Vec::new();
let bytes = abstio::slurp_file(path)?;
let raw_string = std::str::from_utf8(&bytes)?;
let geojson = raw_string.parse::<geojson::GeoJson>()?;
if let geojson::GeoJson::FeatureCollection(collection) = geojson {
for feature in collection.features {
if let Some(geom) = feature.geometry {
if let geojson::Value::Polygon(raw_pts) = geom.value {
let gps_pts: Vec<LonLat> = raw_pts[0]
.iter()
.map(|pt| LonLat::new(pt[0], pt[1]))
.collect();
if let Some(pts) = map.gps_bounds.try_convert(&gps_pts) {
if let Ok(ring) = Ring::new(pts) {
polygons.push(ring.to_polygon());
}
}
}
}
}
}
let mut id = -1;
for polygon in polygons {
map.buildings.insert(
osm::OsmID::Way(osm::WayID(id)),
raw::RawBuilding {
polygon,
osm_tags: Tags::empty(),
public_garage_name: None,
num_parking_spots: 1,
amenities: Vec::new(),
},
);
id -= -1;
}
Ok(())
}