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
// TODO Time to rename this crate

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

use std::collections::BTreeMap;

use anyhow::Result;
use serde::{Deserialize, Serialize};

use abstutil::{prettyprint_usize, Timer};
use geom::{GPSBounds, LonLat};

/// Some dataset imported from KML, CSV, or something else. If the dataset is large, converting to
/// this format and serializing is faster than parsing the original again.
#[derive(Serialize, Deserialize)]
pub struct ExtraShapes {
    pub shapes: Vec<ExtraShape>,
}

/// A single object in the dataset.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExtraShape {
    /// The object has a different inferred shape depending on the points:
    /// - a single point just represents a position
    /// - a ring of points (with the first and last matching) is interpreted as a polygon
    /// - multiple points are interpreted as a PolyLine
    pub points: Vec<LonLat>,
    /// Arbitrary key/value pairs associated with this object; no known schema.
    pub attributes: BTreeMap<String, String>,
}

/// Parses a .kml file and returns ExtraShapes. Objects will be clipped to the given gps_bounds. If
/// require_all_pts_in_bounds is true, objects that're partly out-of-bounds will be skipped.
pub fn load(
    path: String,
    gps_bounds: &GPSBounds,
    require_all_pts_in_bounds: bool,
    timer: &mut Timer,
) -> Result<ExtraShapes> {
    timer.start(format!("read {}", path));
    let bytes = abstio::slurp_file(&path)?;
    let raw_string = std::str::from_utf8(&bytes)?;
    let tree = roxmltree::Document::parse(raw_string)?;
    timer.stop(format!("read {}", path));

    let mut shapes = Vec::new();
    let mut skipped_count = 0;
    let mut kv = BTreeMap::new();

    timer.start("scrape objects");
    recurse(
        tree.root(),
        &mut shapes,
        &mut skipped_count,
        &mut kv,
        gps_bounds,
        require_all_pts_in_bounds,
    )?;
    timer.stop("scrape objects");

    info!(
        "Got {} shapes from {} and skipped {} shapes",
        prettyprint_usize(shapes.len()),
        path,
        prettyprint_usize(skipped_count)
    );

    Ok(ExtraShapes { shapes })
}

fn recurse(
    node: roxmltree::Node,
    shapes: &mut Vec<ExtraShape>,
    skipped_count: &mut usize,
    kv: &mut BTreeMap<String, String>,
    gps_bounds: &GPSBounds,
    require_all_pts_in_bounds: bool,
) -> Result<()> {
    for child in node.children() {
        recurse(
            child,
            shapes,
            skipped_count,
            kv,
            gps_bounds,
            require_all_pts_in_bounds,
        )?;
    }
    if node.tag_name().name() == "SimpleData" {
        let key = node.attribute("name").unwrap().to_string();
        let value = node
            .text()
            .map(|x| x.to_string())
            .unwrap_or_else(String::new);
        kv.insert(key, value);
    } else if node.tag_name().name() == "coordinates" {
        let mut any_oob = false;
        let mut any_ok = false;
        let mut pts: Vec<LonLat> = Vec::new();
        if let Some(txt) = node.text() {
            for pair in txt.split(' ') {
                if let Some(pt) = parse_pt(pair) {
                    pts.push(pt);
                    if gps_bounds.contains(pt) {
                        any_ok = true;
                    } else {
                        any_oob = true;
                    }
                } else {
                    bail!("Malformed coordinates: {}", pair);
                }
            }
        }
        if any_ok && (!any_oob || !require_all_pts_in_bounds) {
            let attributes = std::mem::take(kv);
            shapes.push(ExtraShape {
                points: pts,
                attributes,
            });
        } else {
            *skipped_count += 1;
        }
    }
    Ok(())
}

fn parse_pt(input: &str) -> Option<LonLat> {
    let coords: Vec<&str> = input.split(',').collect();
    // Normally each coordinate is just (X, Y), but for census tract files, there's a third Z
    // component that's always 0. Just ignore it.
    if coords.len() < 2 {
        return None;
    }
    match (coords[0].parse::<f64>(), coords[1].parse::<f64>()) {
        (Ok(lon), Ok(lat)) => Some(LonLat::new(lon, lat)),
        _ => None,
    }
}

impl ExtraShapes {
    /// Parses a .csv file and returns ExtraShapes. Each record must EITHER have a column called
    /// 'Longitude' and 'Latitude', representing a single point, OR a column called 'geometry' with
    /// a WKT-style linestring. All other columns will just be attributes. Objects that're partly
    /// out-of-bounds will be excluded.
    pub fn load_csv(
        path: String,
        gps_bounds: &GPSBounds,
        timer: &mut Timer,
    ) -> Result<ExtraShapes> {
        timer.start(format!("read {}", path));
        let mut shapes = Vec::new();
        for rec in csv::Reader::from_path(&path)?.deserialize() {
            let mut rec: BTreeMap<String, String> = rec?;
            match (
                rec.remove("Longitude"),
                rec.remove("Latitude"),
                rec.remove("geometry"),
            ) {
                (Some(lon), Some(lat), _) => {
                    if let (Ok(lon), Ok(lat)) = (lon.parse::<f64>(), lat.parse::<f64>()) {
                        let pt = LonLat::new(lon, lat);
                        if gps_bounds.contains(pt) {
                            shapes.push(ExtraShape {
                                points: vec![pt],
                                attributes: rec,
                            });
                        }
                    }
                }
                (None, None, Some(raw)) => {
                    if let Some(points) = LonLat::parse_wkt_linestring(&raw) {
                        if gps_bounds.try_convert(&points).is_some() {
                            shapes.push(ExtraShape {
                                points,
                                attributes: rec,
                            });
                        }
                    }
                }
                _ => {
                    timer.stop(format!("read {}", path));
                    bail!(
                        "{} doesn't have a column called Longitude, Latitude, or geometry",
                        path
                    )
                }
            }
        }
        timer.stop(format!("read {}", path));
        Ok(ExtraShapes { shapes })
    }
}