diff --git a/convert_osm/src/osm_reader.rs b/convert_osm/src/osm_reader.rs index 587d3c99d3..e404dbab87 100644 --- a/convert_osm/src/osm_reader.rs +++ b/convert_osm/src/osm_reader.rs @@ -4,8 +4,6 @@ use map_model::raw::{OriginalBuilding, RawArea, RawBuilding, RawMap, RawRoad, Re use map_model::{osm, AreaType}; use osm_xml; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; -use std::fs::File; -use std::io::{BufRead, BufReader}; pub fn extract_osm( osm_path: &str, @@ -38,8 +36,17 @@ pub fn extract_osm( ); done(timer); - let mut map = if let Some(ref path) = maybe_clip_path { - read_osmosis_polygon(path, city_name, map_name) + let mut map = if let Some(path) = maybe_clip_path { + let pts = LonLat::read_osmosis_polygon(path.to_string()).unwrap(); + let mut gps_bounds = GPSBounds::new(); + for pt in &pts { + gps_bounds.update(*pt); + } + + let mut map = RawMap::blank(city_name, map_name); + map.boundary_polygon = Polygon::new(&gps_bounds.must_convert(&pts)); + map.gps_bounds = gps_bounds; + map } else { let mut m = RawMap::blank(city_name, map_name); for node in doc.nodes.values() { @@ -558,33 +565,3 @@ fn glue_to_boundary(result_pl: PolyLine, boundary: &Ring) -> Option { assert_eq!(trimmed_pts[0], *trimmed_pts.last().unwrap()); Some(Polygon::new(&trimmed_pts)) } - -fn read_osmosis_polygon(path: &str, city_name: &str, map_name: &str) -> RawMap { - let mut pts: Vec = Vec::new(); - let mut gps_bounds = GPSBounds::new(); - for (idx, maybe_line) in BufReader::new(File::open(path).unwrap()) - .lines() - .enumerate() - { - if idx == 0 || idx == 1 { - continue; - } - let line = maybe_line.unwrap(); - if line == "END" { - break; - } - let parts: Vec<&str> = line.trim_start().split(" ").collect(); - assert!(parts.len() == 2); - let pt = LonLat::new( - parts[0].parse::().unwrap(), - parts[1].parse::().unwrap(), - ); - pts.push(pt); - gps_bounds.update(pt); - } - - let mut map = RawMap::blank(city_name, map_name); - map.boundary_polygon = Polygon::new(&gps_bounds.must_convert(&pts)); - map.gps_bounds = gps_bounds; - map -} diff --git a/game/src/devtools/mod.rs b/game/src/devtools/mod.rs index 4ec353c3a5..2518b717b9 100644 --- a/game/src/devtools/mod.rs +++ b/game/src/devtools/mod.rs @@ -13,6 +13,7 @@ use ezgui::{ hotkey, Btn, Choice, Composite, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, TextExt, VerticalAlignment, Widget, Wizard, }; +use geom::LonLat; pub struct DevToolsMode { composite: Composite, @@ -119,7 +120,7 @@ fn choose_polygon(wiz: &mut Wizard, ctx: &mut EventCtx, app: &mut App) -> Option let name = wiz.wrap(ctx).choose_string("Edit which polygon?", || { abstutil::list_all_objects("../data/input/seattle/polygons/".to_string()) })?; - match polygon::read_from_osmosis(format!("../data/input/seattle/polygons/{}.poly", name)) { + match LonLat::read_osmosis_polygon(format!("../data/input/seattle/polygons/{}.poly", name)) { Ok(pts) => Some(Transition::Replace(polygon::PolygonEditor::new( ctx, app, name, pts, ))), diff --git a/game/src/devtools/polygon.rs b/game/src/devtools/polygon.rs index 6cf0beb927..89291a5e38 100644 --- a/game/src/devtools/polygon.rs +++ b/game/src/devtools/polygon.rs @@ -5,7 +5,7 @@ use crate::managed::WrappedComposite; use ezgui::{hotkey, Color, Composite, EventCtx, GfxCtx, Key, Line, Outcome, Text}; use geom::{Circle, Distance, LonLat, Polygon, Pt2D}; use std::fs::File; -use std::io::{BufRead, BufReader, Error, ErrorKind, Write}; +use std::io::{Error, Write}; const POINT_RADIUS: Distance = Distance::const_meters(10.0); // Localized and internal, so don't put in ColorScheme. @@ -165,28 +165,3 @@ fn save_as_osmosis(name: &str, pts: &Vec) -> Result<(), Error> { println!("Exported {}", path); Ok(()) } - -pub fn read_from_osmosis(path: String) -> Result, Error> { - let f = File::open(&path)?; - let mut pts = Vec::new(); - for (idx, line) in BufReader::new(f).lines().enumerate() { - if idx < 2 { - continue; - } - let line = line?; - if line == "END" { - break; - } - let parts = line.trim().split(" ").collect::>(); - pts.push(LonLat::new( - parts[0] - .parse::() - .map_err(|err| Error::new(ErrorKind::Other, err))?, - parts[1] - .parse::() - .map_err(|err| Error::new(ErrorKind::Other, err))?, - )); - } - pts.pop(); - Ok(pts) -} diff --git a/geom/src/gps.rs b/geom/src/gps.rs index 6f2798ce7c..40717b6ede 100644 --- a/geom/src/gps.rs +++ b/geom/src/gps.rs @@ -2,6 +2,8 @@ use crate::Distance; use ordered_float::NotNan; use serde_derive::{Deserialize, Serialize}; use std::fmt; +use std::fs::File; +use std::io::{BufRead, BufReader, Error, ErrorKind}; // longitude is x, latitude is y #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize)] @@ -52,6 +54,31 @@ impl LonLat { let epsilon = 1e-8; (self.x() - other.x()).abs() < epsilon && (self.y() - other.y()).abs() < epsilon } + + pub fn read_osmosis_polygon(path: String) -> Result, Error> { + let f = File::open(&path)?; + let mut pts = Vec::new(); + for (idx, line) in BufReader::new(f).lines().enumerate() { + if idx < 2 { + continue; + } + let line = line?; + if line == "END" { + break; + } + let parts = line.trim().split(" ").collect::>(); + pts.push(LonLat::new( + parts[0] + .parse::() + .map_err(|err| Error::new(ErrorKind::Other, err))?, + parts[1] + .parse::() + .map_err(|err| Error::new(ErrorKind::Other, err))?, + )); + } + pts.pop(); + Ok(pts) + } } impl fmt::Display for LonLat {