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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! It's assumed that the importer is run with the current directory as the project repository; aka
//! `./data/` and `./importer/config` must exist.

// Disable some noisy clippy lints
#![allow(clippy::type_complexity)]

#[macro_use]
extern crate anyhow;
#[macro_use]
extern crate log;

use structopt::StructOpt;

use abstio::{CityName, MapName};
use abstutil::Timer;
use geom::Distance;
use map_model::RawToMapOptions;

use self::configuration::{load_configuration, ImporterConfiguration};
pub use self::pick_geofabrik::pick_geofabrik;

mod berlin;
mod configuration;
mod map_config;
mod pick_geofabrik;
mod seattle;
mod soundcast;
mod uk;
mod utils;

/// Regenerate all maps and scenarios from scratch.
pub async fn regenerate_everything(shard_num: usize, num_shards: usize) {
    // Discover all cities by looking at config. But always operate on Seattle first. Special
    // treatment ;)
    let mut all_cities = CityName::list_all_cities_from_importer_config();
    all_cities.retain(|x| x != &CityName::seattle());
    all_cities.insert(0, CityName::seattle());

    let mut timer = Timer::new("regenerate all maps");
    for (cnt, city) in all_cities.into_iter().enumerate() {
        let mut job = Job {
            city: city.clone(),
            osm_to_raw: true,
            raw_to_map: true,
            scenario: false,
            city_overview: false,
            only_map: None,
            opts: RawToMapOptions::default(),
        };
        // Only some maps run extra tasks
        if city == CityName::seattle() || city.country == "gb" {
            job.scenario = true;
        }
        // TODO Autodetect this based on number of maps per city?
        if city == CityName::new("ch", "zurich")
            || city == CityName::new("gb", "leeds")
            || city == CityName::new("us", "nyc")
            || city == CityName::new("fr", "charleville_mezieres")
            || city == CityName::new("fr", "paris")
            || city == CityName::new("at", "salzburg")
            || city == CityName::new("ir", "tehran")
        {
            job.city_overview = true;
        }

        if cnt % num_shards == shard_num {
            job.run(&mut timer).await;
        }
    }
}

/// Regenerate all maps from RawMaps in parallel.
pub fn regenerate_all_maps() {
    // Omit Seattle and Berlin, because they have special follow-up actions (minifying some maps
    // and distributing residents)
    let all_maps: Vec<MapName> = CityName::list_all_cities_from_importer_config()
        .into_iter()
        .flat_map(|city| city.list_all_maps_in_city_from_importer_config())
        .filter(|name| {
            name != &MapName::new("de", "berlin", "center") && name.city != CityName::seattle()
        })
        .collect();
    Timer::new("regenerate all maps").parallelize("import each city", all_maps, |name| {
        // Don't pass in a timer; the logs are way too spammy.
        // It's also recommended to run with RUST_LOG=none
        utils::raw_to_map(&name, RawToMapOptions::default(), &mut Timer::throwaway())
    });
}

/// Transforms a .osm file to a map in one step.
pub fn oneshot(
    osm_path: String,
    clip: Option<String>,
    drive_on_right: bool,
    filter_crosswalks: bool,
    opts: RawToMapOptions,
) {
    let mut timer = abstutil::Timer::new("oneshot");
    println!("- Running convert_osm on {}", osm_path);
    let name = abstutil::basename(&osm_path);
    let raw = convert_osm::convert(
        osm_path,
        MapName::new("zz", "oneshot", &name),
        clip,
        convert_osm::Options {
            map_config: map_model::MapConfig {
                driving_side: if drive_on_right {
                    map_model::DrivingSide::Right
                } else {
                    map_model::DrivingSide::Left
                },
                bikes_can_use_bus_lanes: true,
                inferred_sidewalks: true,
                street_parking_spot_length: Distance::meters(8.0),
                turn_on_red: true,
            },

            onstreet_parking: convert_osm::OnstreetParking::JustOSM,
            public_offstreet_parking: convert_osm::PublicOffstreetParking::None,
            private_offstreet_parking: convert_osm::PrivateOffstreetParking::FixedPerBldg(1),
            include_railroads: true,
            extra_buildings: None,
            skip_local_roads: false,
            filter_crosswalks,
            gtfs_url: None,
        },
        &mut timer,
    );
    // Often helpful to save intermediate representation in case user wants to load into map_editor
    raw.save();
    let map = map_model::Map::create_from_raw(raw, opts, &mut timer);
    timer.start("save map");
    map.save();
    timer.stop("save map");
    println!("{} has been created", map.get_name().path());
}

/// A specification for importing all maps in a single city.
#[derive(StructOpt)]
pub struct Job {
    #[structopt(long, parse(try_from_str = CityName::parse), default_value = "us/seattle")]
    pub city: CityName,
    /// Download all raw input files, then convert OSM to the intermediate RawMap.
    #[structopt(long = "--raw")]
    pub osm_to_raw: bool,
    /// Convert the RawMap to the final Map format.
    #[structopt(long = "--map")]
    pub raw_to_map: bool,
    /// Download trip demand data, then produce the typical weekday scenario.
    #[structopt(long)]
    pub scenario: bool,
    /// Produce a city overview from all of the individual maps in a city.
    #[structopt(long)]
    pub city_overview: bool,

    /// Only process one map. If not specified, process all maps defined by clipping polygons in
    /// importer/config/$city/.
    #[structopt()]
    pub only_map: Option<String>,

    #[structopt(flatten)]
    pub opts: RawToMapOptions,
}

impl Job {
    pub async fn run(self, timer: &mut Timer<'_>) {
        if !self.osm_to_raw && !self.raw_to_map && !self.scenario && !self.city_overview {
            println!(
                "Nothing to do! Pass some combination of --raw, --map, --scenario, or --city_overview"
            );
            std::process::exit(1);
        }

        let config: ImporterConfiguration = load_configuration();

        timer.start(format!("import {}", self.city.describe()));
        let names = if let Some(n) = self.only_map {
            println!("- Just working on {}", n);
            vec![MapName::from_city(&self.city, &n)]
        } else {
            println!("- Working on all {} maps", self.city.describe());
            self.city.list_all_maps_in_city_from_importer_config()
        };

        // When regenerating everything, huge_seattle gets created twice! This is expensive enough
        // to hack in a way to avoid the work.
        let mut built_raw_huge_seattle = false;
        let mut built_map_huge_seattle = false;
        let (maybe_popdat, maybe_huge_map, maybe_zoning_parcels) = if self.scenario
            && self.city == CityName::seattle()
        {
            timer.start("ensure_popdat_exists");
            let (popdat, huge_map) = seattle::ensure_popdat_exists(
                timer,
                &config,
                &mut built_raw_huge_seattle,
                &mut built_map_huge_seattle,
            )
            .await;
            // Just assume --raw has been called...
            let shapes: kml::ExtraShapes =
                abstio::read_binary(CityName::seattle().input_path("zoning_parcels.bin"), timer);
            timer.stop("ensure_popdat_exists");
            (Some(popdat), Some(huge_map), Some(shapes))
        } else {
            (None, None, None)
        };

        for name in names {
            timer.start(name.describe());
            if self.osm_to_raw {
                if !built_raw_huge_seattle || name != MapName::seattle("huge_seattle") {
                    let raw = utils::osm_to_raw(name.clone(), timer, &config).await;

                    // The collision data will only cover one part of London, since we don't have a
                    // region-wide map there yet
                    if name.city == CityName::new("de", "berlin") {
                        berlin::import_extra_data(&raw, &config, timer).await;
                    } else if name == MapName::new("gb", "leeds", "huge") {
                        uk::import_collision_data(&raw, &config, timer).await;
                    } else if name == MapName::new("gb", "london", "camden") {
                        uk::import_collision_data(&raw, &config, timer).await;
                    }
                }
            }

            let mut maybe_map = if self.raw_to_map {
                let mut map = if built_map_huge_seattle && name == MapName::seattle("huge_seattle")
                {
                    map_model::Map::load_synchronously(name.path(), timer)
                } else {
                    utils::raw_to_map(&name, self.opts.clone(), timer)
                };

                // Another strange step in the pipeline.
                if name == MapName::new("de", "berlin", "center") {
                    timer.start(format!(
                        "distribute residents from planning areas for {}",
                        name.describe()
                    ));
                    berlin::distribute_residents(&mut map, timer);
                    timer.stop(format!(
                        "distribute residents from planning areas for {}",
                        name.describe()
                    ));
                }

                Some(map)
            } else if self.scenario {
                Some(map_model::Map::load_synchronously(name.path(), timer))
            } else {
                None
            };

            if self.scenario {
                if self.city == CityName::seattle() {
                    timer.start(format!("scenario for {}", name.describe()));
                    let scenario = soundcast::make_scenario(
                        "weekday",
                        maybe_map.as_ref().unwrap(),
                        maybe_popdat.as_ref().unwrap(),
                        maybe_huge_map.as_ref().unwrap(),
                        timer,
                    );
                    scenario.save();
                    timer.stop(format!("scenario for {}", name.describe()));

                    // This is a strange ordering.
                    if name.map == "downtown"
                        || name.map == "qa"
                        || name.map == "south_seattle"
                        || name.map == "wallingford"
                    {
                        timer.start(format!("adjust parking for {}", name.describe()));
                        seattle::adjust_private_parking(maybe_map.as_mut().unwrap(), &scenario);
                        timer.stop(format!("adjust parking for {}", name.describe()));
                    }

                    timer.start("match parcels to buildings");
                    seattle::match_parcels_to_buildings(
                        maybe_map.as_mut().unwrap(),
                        maybe_zoning_parcels.as_ref().unwrap(),
                        timer,
                    );
                    timer.stop("match parcels to buildings");

                    // Even stranger hacks! AFTER generating the scenarios, which requires full
                    // pathfinding, for a few maps, "minify" them to cut down file size for the
                    // bike network tool.
                    if name.map == "central_seattle"
                        || name.map == "north_seattle"
                        || name.map == "south_seattle"
                    {
                        let map = maybe_map.as_mut().unwrap();
                        map.minify(timer);
                        map.save();
                    }
                }

                if self.city.country == "gb" {
                    uk::generate_scenario(maybe_map.as_ref().unwrap(), &config, timer)
                        .await
                        .unwrap();
                }
            }
            timer.stop(name.describe());
        }

        if self.city_overview {
            timer.start(format!(
                "generate city overview for {}",
                self.city.describe()
            ));
            abstio::write_binary(
                abstio::path(format!(
                    "system/{}/{}/city.bin",
                    self.city.country, self.city.city
                )),
                &map_model::City::from_individual_maps(&self.city, timer),
            );
            timer.stop(format!(
                "generate city overview for {}",
                self.city.describe()
            ));
        }

        timer.stop(format!("import {}", self.city.describe()));
    }
}