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
use anyhow::Result;
use geojson::Feature;

use abstutil::{Tags, Timer};
use geom::{Distance, GPSBounds, PolyLine};

use crate::geometry::{InputRoad, Results};
use crate::{osm, OriginalRoad, RawMap};

impl RawMap {
    pub fn save_osm2polygon_input(&self, output_path: String, i: osm::NodeID) -> Result<()> {
        let mut features = Vec::new();
        for id in self.roads_per_intersection(i) {
            let (untrimmed_center_pts, total_width) = self.untrimmed_road_geometry(id)?;

            let mut properties = serde_json::Map::new();
            properties.insert("osm_way_id".to_string(), id.osm_way_id.0.into());
            properties.insert("src_i".to_string(), id.i1.0.into());
            properties.insert("dst_i".to_string(), id.i2.0.into());
            properties.insert(
                "half_width".to_string(),
                (total_width / 2.0).inner_meters().into(),
            );

            let mut osm_tags = serde_json::Map::new();
            for (k, v) in self.roads[&id].osm_tags.inner() {
                osm_tags.insert(k.to_string(), v.to_string().into());
            }
            properties.insert("osm_tags".to_string(), osm_tags.into());

            // TODO Both for ror reading and writing, we should find a way to pair a serde struct
            // with a geo type
            features.push(Feature {
                geometry: Some(untrimmed_center_pts.to_geojson(Some(&self.gps_bounds))),
                properties: Some(properties),
                bbox: None,
                id: None,
                foreign_members: None,
            });
        }

        // Include extra metadata as GeoJSON foreign members. They'll just show up as a top-level
        // key/values on the FeatureCollection
        let mut extra_props = serde_json::Map::new();
        extra_props.insert("intersection_id".to_string(), i.0.into());
        extra_props.insert("min_lon".to_string(), self.gps_bounds.min_lon.into());
        extra_props.insert("min_lat".to_string(), self.gps_bounds.min_lat.into());
        extra_props.insert("max_lon".to_string(), self.gps_bounds.max_lon.into());
        extra_props.insert("max_lat".to_string(), self.gps_bounds.max_lat.into());
        let fc = geojson::FeatureCollection {
            features,
            bbox: None,
            foreign_members: Some(extra_props),
        };
        let gj = geojson::GeoJson::from(fc);
        abstio::write_json(output_path, &gj);
        Ok(())
    }
}

/// Returns the (intersection_id, input roads, and GPS bounds) previously written by
/// `save_osm2polygon_input`.
pub fn read_osm2polygon_input(path: String) -> Result<(osm::NodeID, Vec<InputRoad>, GPSBounds)> {
    let geojson: geojson::GeoJson = abstio::maybe_read_json(path, &mut Timer::throwaway())?;
    if let geojson::GeoJson::FeatureCollection(collection) = geojson {
        let extra_props = collection.foreign_members.as_ref().unwrap();
        let gps_bounds = GPSBounds {
            min_lon: extra_props.get("min_lon").and_then(|x| x.as_f64()).unwrap(),
            min_lat: extra_props.get("min_lat").and_then(|x| x.as_f64()).unwrap(),
            max_lon: extra_props.get("max_lon").and_then(|x| x.as_f64()).unwrap(),
            max_lat: extra_props.get("max_lat").and_then(|x| x.as_f64()).unwrap(),
        };
        let intersection_id = osm::NodeID(
            extra_props
                .get("intersection_id")
                .and_then(|x| x.as_i64())
                .unwrap(),
        );

        let mut roads = Vec::new();
        for feature in collection.features {
            let center_pts = PolyLine::from_geojson(&feature, Some(&gps_bounds))?;
            let osm_way_id = feature
                .property("osm_way_id")
                .and_then(|x| x.as_i64())
                .unwrap();
            let src_i = feature.property("src_i").and_then(|x| x.as_i64()).unwrap();
            let dst_i = feature.property("dst_i").and_then(|x| x.as_i64()).unwrap();
            let id = OriginalRoad::new(osm_way_id, (src_i, dst_i));
            let half_width = Distance::meters(
                feature
                    .property("half_width")
                    .and_then(|x| x.as_f64())
                    .unwrap(),
            );
            let mut osm_tags = Tags::empty();
            for (k, v) in feature
                .property("osm_tags")
                .and_then(|x| x.as_object())
                .unwrap()
            {
                osm_tags.insert(k, v.as_str().unwrap());
            }

            roads.push(InputRoad {
                id,
                center_pts,
                half_width,
                osm_tags,
            });
        }

        return Ok((intersection_id, roads, gps_bounds));
    }
    bail!("No FeatureCollection")
}

impl Results {
    pub fn save_to_geojson(
        &self,
        output_path: String,
        gps_bounds: &GPSBounds,
        debug_output: bool,
    ) -> Result<()> {
        let mut features = Vec::new();

        {
            let mut properties = serde_json::Map::new();
            properties.insert("intersection_id".to_string(), self.intersection_id.0.into());
            features.push(Feature {
                geometry: Some(self.intersection_polygon.to_geojson(Some(gps_bounds))),
                properties: Some(properties),
                bbox: None,
                id: None,
                foreign_members: None,
            });
        }

        for (id, (pl, half_width)) in &self.trimmed_center_pts {
            // Add both a line-string and polygon per road
            let mut properties = serde_json::Map::new();
            properties.insert("osm_way_id".to_string(), id.osm_way_id.0.into());
            properties.insert("src_i".to_string(), id.i1.0.into());
            properties.insert("dst_i".to_string(), id.i2.0.into());
            features.push(Feature {
                geometry: Some(pl.to_geojson(Some(gps_bounds))),
                properties: Some(properties.clone()),
                bbox: None,
                id: None,
                foreign_members: None,
            });

            features.push(Feature {
                geometry: Some(
                    pl.make_polygons(2.0 * *half_width)
                        .to_geojson(Some(gps_bounds)),
                ),
                properties: Some(properties),
                bbox: None,
                id: None,
                foreign_members: None,
            });
        }

        if debug_output {
            for (label, polygon) in &self.debug {
                let mut properties = serde_json::Map::new();
                properties.insert("debug".to_string(), label.clone().into());
                features.push(Feature {
                    geometry: Some(polygon.to_geojson(Some(gps_bounds))),
                    properties: Some(properties),
                    bbox: None,
                    id: None,
                    foreign_members: None,
                });
            }
        }

        let fc = geojson::FeatureCollection {
            features,
            bbox: None,
            foreign_members: None,
        };
        abstio::write_json(output_path, &geojson::GeoJson::from(fc));
        Ok(())
    }
}