refactor osmosis polygon reader

This commit is contained in:
Dustin Carlino 2020-05-17 09:49:03 -07:00
parent e082082c32
commit 1a24fc7fab
4 changed files with 41 additions and 61 deletions

View File

@ -4,8 +4,6 @@ use map_model::raw::{OriginalBuilding, RawArea, RawBuilding, RawMap, RawRoad, Re
use map_model::{osm, AreaType}; use map_model::{osm, AreaType};
use osm_xml; use osm_xml;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fs::File;
use std::io::{BufRead, BufReader};
pub fn extract_osm( pub fn extract_osm(
osm_path: &str, osm_path: &str,
@ -38,8 +36,17 @@ pub fn extract_osm(
); );
done(timer); done(timer);
let mut map = if let Some(ref path) = maybe_clip_path { let mut map = if let Some(path) = maybe_clip_path {
read_osmosis_polygon(path, city_name, map_name) 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 { } else {
let mut m = RawMap::blank(city_name, map_name); let mut m = RawMap::blank(city_name, map_name);
for node in doc.nodes.values() { for node in doc.nodes.values() {
@ -558,33 +565,3 @@ fn glue_to_boundary(result_pl: PolyLine, boundary: &Ring) -> Option<Polygon> {
assert_eq!(trimmed_pts[0], *trimmed_pts.last().unwrap()); assert_eq!(trimmed_pts[0], *trimmed_pts.last().unwrap());
Some(Polygon::new(&trimmed_pts)) Some(Polygon::new(&trimmed_pts))
} }
fn read_osmosis_polygon(path: &str, city_name: &str, map_name: &str) -> RawMap {
let mut pts: Vec<LonLat> = 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::<f64>().unwrap(),
parts[1].parse::<f64>().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
}

View File

@ -13,6 +13,7 @@ use ezgui::{
hotkey, Btn, Choice, Composite, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, hotkey, Btn, Choice, Composite, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome,
TextExt, VerticalAlignment, Widget, Wizard, TextExt, VerticalAlignment, Widget, Wizard,
}; };
use geom::LonLat;
pub struct DevToolsMode { pub struct DevToolsMode {
composite: Composite, 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?", || { let name = wiz.wrap(ctx).choose_string("Edit which polygon?", || {
abstutil::list_all_objects("../data/input/seattle/polygons/".to_string()) 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( Ok(pts) => Some(Transition::Replace(polygon::PolygonEditor::new(
ctx, app, name, pts, ctx, app, name, pts,
))), ))),

View File

@ -5,7 +5,7 @@ use crate::managed::WrappedComposite;
use ezgui::{hotkey, Color, Composite, EventCtx, GfxCtx, Key, Line, Outcome, Text}; use ezgui::{hotkey, Color, Composite, EventCtx, GfxCtx, Key, Line, Outcome, Text};
use geom::{Circle, Distance, LonLat, Polygon, Pt2D}; use geom::{Circle, Distance, LonLat, Polygon, Pt2D};
use std::fs::File; 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); const POINT_RADIUS: Distance = Distance::const_meters(10.0);
// Localized and internal, so don't put in ColorScheme. // Localized and internal, so don't put in ColorScheme.
@ -165,28 +165,3 @@ fn save_as_osmosis(name: &str, pts: &Vec<LonLat>) -> Result<(), Error> {
println!("Exported {}", path); println!("Exported {}", path);
Ok(()) Ok(())
} }
pub fn read_from_osmosis(path: String) -> Result<Vec<LonLat>, 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::<Vec<_>>();
pts.push(LonLat::new(
parts[0]
.parse::<f64>()
.map_err(|err| Error::new(ErrorKind::Other, err))?,
parts[1]
.parse::<f64>()
.map_err(|err| Error::new(ErrorKind::Other, err))?,
));
}
pts.pop();
Ok(pts)
}

View File

@ -2,6 +2,8 @@ use crate::Distance;
use ordered_float::NotNan; use ordered_float::NotNan;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::fs::File;
use std::io::{BufRead, BufReader, Error, ErrorKind};
// longitude is x, latitude is y // longitude is x, latitude is y
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize)]
@ -52,6 +54,31 @@ impl LonLat {
let epsilon = 1e-8; let epsilon = 1e-8;
(self.x() - other.x()).abs() < epsilon && (self.y() - other.y()).abs() < epsilon (self.x() - other.x()).abs() < epsilon && (self.y() - other.y()).abs() < epsilon
} }
pub fn read_osmosis_polygon(path: String) -> Result<Vec<LonLat>, 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::<Vec<_>>();
pts.push(LonLat::new(
parts[0]
.parse::<f64>()
.map_err(|err| Error::new(ErrorKind::Other, err))?,
parts[1]
.parse::<f64>()
.map_err(|err| Error::new(ErrorKind::Other, err))?,
));
}
pts.pop();
Ok(pts)
}
} }
impl fmt::Display for LonLat { impl fmt::Display for LonLat {