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
use serde::{Deserialize, Serialize};

use abstio::MapName;
use map_model::raw::RawMap;

use crate::configuration::ImporterConfiguration;
use crate::utils::{download, osmconvert};

/// Importing a new city can be done just by filling out this config file and specifying some
/// polygon boundaries. Most fields are directly from `convert_osm::Options`.
///
/// If any extra data is imported for a city (like collisions or population), then for now, don't
/// use this.
#[derive(Serialize, Deserialize)]
pub struct GenericCityImporter {
    /// The URL to a .osm or .osm.pbf file containing the entire city.
    /// http://download.geofabrik.de/ is recommended.
    ///
    /// You can also put a path like `input/us/seattle/osm/washington-latest.osm.pbf` in here,
    /// and instead that file will be used. This is kind of a hack, because it'll assume the cities
    /// are imported in the proper order, but it prevents having to download duplicate large files.
    pub osm_url: String,

    pub map_config: map_model::MapConfig,
    pub onstreet_parking: convert_osm::OnstreetParking,
    pub public_offstreet_parking: convert_osm::PublicOffstreetParking,
    pub private_offstreet_parking: convert_osm::PrivateOffstreetParking,
    /// OSM railway=rail will be included as light rail if so. Cosmetic only.
    pub include_railroads: bool,
    /// If provided, read polygons from this GeoJSON file and add them to the RawMap as buildings.
    pub extra_buildings: Option<String>,
    /// Downgrade crosswalks not matching a `highway=crossing` OSM node into unmarked crossings.
    pub filter_crosswalks: bool,
}

impl GenericCityImporter {
    pub async fn osm_to_raw(
        &self,
        name: MapName,
        timer: &mut abstutil::Timer<'_>,
        config: &ImporterConfiguration,
    ) -> RawMap {
        let local_osm_file = if self.osm_url.starts_with("http") {
            let file = name.city.input_path(format!(
                "osm/{}",
                std::path::Path::new(&self.osm_url)
                    .file_name()
                    .unwrap()
                    .to_os_string()
                    .into_string()
                    .unwrap()
            ));
            download(config, file.clone(), &self.osm_url).await;
            file
        } else {
            self.osm_url.clone()
        };

        osmconvert(
            local_osm_file,
            format!(
                "importer/config/{}/{}/{}.poly",
                name.city.country, name.city.city, name.map
            ),
            name.city.input_path(format!("osm/{}.osm", name.map)),
            config,
        );

        let map = convert_osm::convert(
            convert_osm::Options {
                osm_input: name.city.input_path(format!("osm/{}.osm", name.map)),
                name: name.clone(),

                clip: Some(format!(
                    "importer/config/{}/{}/{}.poly",
                    name.city.country, name.city.city, name.map
                )),
                map_config: self.map_config.clone(),
                onstreet_parking: self.onstreet_parking.clone(),
                public_offstreet_parking: self.public_offstreet_parking.clone(),
                private_offstreet_parking: self.private_offstreet_parking.clone(),
                include_railroads: self.include_railroads,
                extra_buildings: self.extra_buildings.clone(),
                // TODO Total hack! Need to figure out how to express per-map config overrides
                skip_local_roads: name == MapName::new("us", "phoenix", "loop101"),
                filter_crosswalks: self.filter_crosswalks,
            },
            timer,
        );
        map.save();
        map
    }
}