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 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<Polygon> {
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<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,
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,
))),

View File

@ -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<LonLat>) -> Result<(), Error> {
println!("Exported {}", path);
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 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<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 {