#[macro_use]
extern crate log;
use abstio::{CityName, MapName};
use abstutil::{basename, Timer};
use geom::Distance;
use kml::ExtraShapes;
use configuration::{load_configuration, ImporterConfiguration};
use dependencies::are_dependencies_callable;
mod berlin;
mod configuration;
mod dependencies;
mod generic;
mod seattle;
#[cfg(feature = "scenarios")]
mod soundcast;
mod uk;
mod utils;
fn main() {
let config: ImporterConfiguration = load_configuration();
if !are_dependencies_callable(&config) {
println!("One or more dependencies aren't callable. Add them to the path and try again.");
std::process::exit(1);
}
let mut args = abstutil::CmdArgs::new();
let skip_ch = args.enabled("--skip_ch");
let keep_bldg_tags = args.enabled("--keep_bldg_tags");
if let Some(path) = args.optional("--oneshot") {
let clip = args.optional("--oneshot_clip");
let drive_on_left = args.enabled("--oneshot_drive_on_left");
args.done();
oneshot(path, clip, !drive_on_left, !skip_ch, keep_bldg_tags);
return;
}
if args.enabled("--regen_all") {
assert!(!skip_ch);
assert!(!keep_bldg_tags);
regenerate_everything(config);
return;
}
let job = Job {
city: match args.optional("--city") {
Some(x) => CityName::parse(&x).unwrap(),
None => CityName::seattle(),
},
osm_to_raw: args.enabled("--raw"),
raw_to_map: args.enabled("--map"),
scenario: args.enabled("--scenario"),
city_overview: args.enabled("--city_overview"),
only_map: args.optional_free(),
};
args.done();
if !job.osm_to_raw && !job.raw_to_map && !job.scenario && !job.city_overview {
println!(
"Nothing to do! Pass some combination of --raw, --map, --scenario, --city_overview, \
or --oneshot"
);
std::process::exit(1);
}
let mut timer = Timer::new("import map data");
job.run(&config, skip_ch, keep_bldg_tags, &mut timer);
}
fn regenerate_everything(config: ImporterConfiguration) {
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 city in all_cities {
let mut job = Job {
city: city.clone(),
osm_to_raw: true,
raw_to_map: true,
scenario: false,
city_overview: false,
only_map: None,
};
if city == CityName::seattle() {
job.scenario = true;
}
if 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")
{
job.city_overview = true;
}
let skip_ch = false;
let keep_bldg_tags = false;
job.run(&config, skip_ch, keep_bldg_tags, &mut timer);
}
}
struct Job {
city: CityName,
osm_to_raw: bool,
raw_to_map: bool,
scenario: bool,
city_overview: bool,
only_map: Option<String>,
}
impl Job {
fn run(
self,
config: &ImporterConfiguration,
skip_ch: bool,
keep_bldg_tags: bool,
timer: &mut Timer,
) {
timer.start(format!("import {}", self.city.describe()));
let names = if let Some(n) = self.only_map {
println!("- Just working on {}", n);
vec![n]
} else {
println!("- Working on all {} maps", self.city.describe());
abstio::list_dir(format!(
"importer/config/{}/{}",
self.city.country, self.city.city
))
.into_iter()
.filter(|path| path.ends_with(".poly"))
.map(basename)
.collect()
};
let (maybe_popdat, maybe_huge_map, maybe_zoning_parcels) = if self.scenario {
assert_eq!(self.city, CityName::seattle());
#[cfg(feature = "scenarios")]
{
let (popdat, huge_map) = seattle::ensure_popdat_exists(timer, config);
let shapes: ExtraShapes = abstio::read_binary(
CityName::seattle().input_path("zoning_parcels.bin"),
timer,
);
(Some(popdat), Some(huge_map), Some(shapes))
}
#[cfg(not(feature = "scenarios"))]
{
panic!("Can't do --scenario without the scenarios feature compiled in");
(None, None, None)
}
} else {
(None, None, None)
};
for name in names {
if self.osm_to_raw {
if self.city == CityName::seattle() {
seattle::osm_to_raw(&name, timer, config);
} else {
let raw = match abstio::maybe_read_json::<generic::GenericCityImporter>(
format!(
"importer/config/{}/{}/cfg.json",
self.city.country, self.city.city
),
timer,
) {
Ok(city_cfg) => city_cfg.osm_to_raw(
MapName::from_city(&self.city, &name),
timer,
config,
),
Err(err) => {
panic!("Can't import city {}: {}", self.city.describe(), err);
}
};
if self.city == CityName::new("de", "berlin") {
berlin::import_extra_data(&raw, config, timer);
} else if self.city == CityName::new("gb", "leeds") && name == "huge" {
uk::import_extra_data(&raw, config, timer);
} else if self.city == CityName::new("gb", "london") {
uk::import_extra_data(&raw, config, timer);
}
}
}
let name = MapName::from_city(&self.city, &name);
let mut maybe_map = if self.raw_to_map {
let mut map = utils::raw_to_map(&name, !skip_ch, keep_bldg_tags, timer);
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()
));
} else if name.city == CityName::seattle() {
timer.start(format!("add GTFS schedules for {}", name.describe()));
seattle::add_gtfs_schedules(&mut map);
timer.stop(format!("add GTFS schedules for {}", name.describe()));
}
Some(map)
} else if self.scenario {
Some(map_model::Map::new(name.path(), timer))
} else {
None
};
if self.scenario {
#[cfg(feature = "scenarios")]
if self.city == CityName::seattle() {
timer.start(format!("scenario for {}", name.describe()));
let scenario = soundcast::make_weekday_scenario(
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()));
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");
}
}
}
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()));
}
}
fn oneshot(
osm_path: String,
clip: Option<String>,
drive_on_right: bool,
build_ch: bool,
keep_bldg_tags: bool,
) {
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(
convert_osm::Options {
osm_input: osm_path,
name: MapName::new("zz", "oneshot", &name),
clip,
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),
},
onstreet_parking: convert_osm::OnstreetParking::JustOSM,
public_offstreet_parking: convert_osm::PublicOffstreetParking::None,
private_offstreet_parking: convert_osm::PrivateOffstreetParking::FixedPerBldg(1),
elevation: None,
include_railroads: true,
extra_buildings: None,
},
&mut timer,
);
raw.save();
let map = map_model::Map::create_from_raw(raw, build_ch, keep_bldg_tags, &mut timer);
timer.start("save map");
map.save();
timer.stop("save map");
println!("{} has been created", map.get_name().path());
}