Skip to content

Commit

Permalink
Refactor a proper Amenity struct. Leave room for optional raw OSM tags.
Browse files Browse the repository at this point in the history
Don't expose the tags in the UI yet. [rebuild]
  • Loading branch information
dabreegster committed Oct 25, 2020
1 parent 1a630c6 commit 47fa997
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 180 deletions.
10 changes: 10 additions & 0 deletions book/src/project/CHANGELOG.md
Expand Up @@ -623,3 +623,13 @@ changes here.
- fix by Michael for handling window resizing in panels
- fix original routes on edited maps
- internal code organization and documentation

0.2.16

- UI: click unzoomed agents, switch between metric/imperial units, show reason for cancelled trips, new "faded zoom" color scheme based on mapbox, more detailed agent counts in the top-right panel's tooltips
- started a new dedicated OpenStreetMap viewer, will split out from A/B Street later
- fix alpha colors on web
- bugfixes for the new asynchronous map loading
- some substantial simulation performance gains (168s to 90s on one benchmark!)
- lots of progress towards editing the map without resetting the simulation to midnight. please test with --live_map_edits and report any issues
- internal refactoring and code documentation
61 changes: 25 additions & 36 deletions convert_osm/src/extract.rs
@@ -1,12 +1,12 @@
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::collections::{BTreeMap, HashMap};

use osm::{NodeID, OsmID, RelationID, WayID};

use abstutil::{retain_btreemap, Tags, Timer};
use geom::{HashablePt2D, PolyLine, Polygon, Pt2D, Ring};
use kml::{ExtraShape, ExtraShapes};
use map_model::raw::{RawArea, RawBuilding, RawMap, RawParkingLot, RawRoad, RestrictionType};
use map_model::{osm, AreaType, NamePerLanguage};
use map_model::{osm, Amenity, AreaType, NamePerLanguage};

use crate::reader::{Document, Relation};
use crate::transit;
Expand All @@ -22,8 +22,8 @@ pub struct OsmExtract {
pub simple_turn_restrictions: Vec<(RestrictionType, WayID, NodeID, WayID)>,
/// (relation ID, from way ID, via way ID, to way ID)
pub complicated_turn_restrictions: Vec<(RelationID, WayID, WayID, WayID)>,
/// (location, name, amenity type)
pub amenities: Vec<(Pt2D, NamePerLanguage, String)>,
/// (location, amenity)
pub amenities: Vec<(Pt2D, Amenity)>,
}

pub fn extract_osm(map: &mut RawMap, opts: &Options, timer: &mut Timer) -> OsmExtract {
Expand Down Expand Up @@ -53,19 +53,8 @@ pub fn extract_osm(map: &mut RawMap, opts: &Options, timer: &mut Timer) -> OsmEx
out.traffic_signals
.insert(node.pt.to_hashable(), !backwards);
}
if let Some(amenity) = node.tags.get("amenity") {
out.amenities.push((
node.pt,
NamePerLanguage::new(&node.tags).unwrap_or_else(NamePerLanguage::unnamed),
amenity.clone(),
));
}
if let Some(shop) = node.tags.get("shop") {
out.amenities.push((
node.pt,
NamePerLanguage::new(&node.tags).unwrap_or_else(NamePerLanguage::unnamed),
shop.clone(),
));
for amenity in get_bldg_amenities(&node.tags) {
out.amenities.push((node.pt, amenity));
}
}

Expand Down Expand Up @@ -175,7 +164,7 @@ pub fn extract_osm(map: &mut RawMap, opts: &Options, timer: &mut Timer) -> OsmEx

let boundary = map.boundary_polygon.clone().into_ring();

let mut amenity_areas: Vec<(NamePerLanguage, String, Polygon)> = Vec::new();
let mut amenity_areas: Vec<(Polygon, Amenity)> = Vec::new();
// Vehicle position (stop) -> pedestrian position (platform)
let mut stop_areas: Vec<((osm::NodeID, Pt2D), Pt2D)> = Vec::new();

Expand Down Expand Up @@ -288,15 +277,18 @@ pub fn extract_osm(map: &mut RawMap, opts: &Options, timer: &mut Timer) -> OsmEx
timer,
));
} else if rel.tags.is("type", "multipolygon") && rel.tags.contains_key("amenity") {
let names = NamePerLanguage::new(&rel.tags).unwrap_or_else(NamePerLanguage::unnamed);
let amenity = rel.tags.get("amenity").clone().unwrap();
let amenity = Amenity {
names: NamePerLanguage::new(&rel.tags).unwrap_or_else(NamePerLanguage::unnamed),
amenity_type: rel.tags.get("amenity").unwrap().clone(),
osm_tags: rel.tags.clone(),
};
for (role, member) in &rel.members {
if role != "outer" {
continue;
}
if let OsmID::Way(w) = member {
if let Ok(ring) = Ring::new(doc.ways[w].pts.clone()) {
amenity_areas.push((names.clone(), amenity.clone(), ring.to_polygon()));
amenity_areas.push((ring.to_polygon(), amenity.clone()));
}
}
}
Expand Down Expand Up @@ -352,11 +344,11 @@ pub fn extract_osm(map: &mut RawMap, opts: &Options, timer: &mut Timer) -> OsmEx
}

timer.start_iter("match buildings to amenity areas", amenity_areas.len());
for (names, amenity, poly) in amenity_areas {
for (poly, amenity) in amenity_areas {
timer.next();
for b in map.buildings.values_mut() {
if poly.contains_pt(b.polygon.center()) {
b.amenities.insert((names.clone(), amenity.clone()));
b.amenities.push(amenity.clone());
}
}
}
Expand Down Expand Up @@ -495,19 +487,16 @@ fn is_bldg(tags: &Tags) -> bool {
tags.contains_key("building") && !tags.contains_key("abandoned:man_made")
}

fn get_bldg_amenities(tags: &Tags) -> BTreeSet<(NamePerLanguage, String)> {
let mut amenities = BTreeSet::new();
if let Some(amenity) = tags.get("amenity") {
amenities.insert((
NamePerLanguage::new(tags).unwrap_or_else(NamePerLanguage::unnamed),
amenity.clone(),
));
}
if let Some(shop) = tags.get("shop") {
amenities.insert((
NamePerLanguage::new(tags).unwrap_or_else(NamePerLanguage::unnamed),
shop.clone(),
));
fn get_bldg_amenities(tags: &Tags) -> Vec<Amenity> {
let mut amenities = Vec::new();
for key in vec!["amenity", "shop"] {
if let Some(amenity) = tags.get(key) {
amenities.push(Amenity {
names: NamePerLanguage::new(tags).unwrap_or_else(NamePerLanguage::unnamed),
amenity_type: amenity.clone(),
osm_tags: tags.clone(),
});
}
}
amenities
}
Expand Down
12 changes: 4 additions & 8 deletions convert_osm/src/lib.rs
@@ -1,7 +1,7 @@
use abstutil::Timer;
use geom::{Distance, FindClosest, GPSBounds, LonLat, Pt2D, Ring};
use map_model::raw::RawMap;
use map_model::{osm, MapConfig, NamePerLanguage};
use map_model::{osm, Amenity, MapConfig};

mod clip;
mod extract;
Expand Down Expand Up @@ -109,23 +109,19 @@ pub fn convert(opts: Options, timer: &mut abstutil::Timer) -> RawMap {
map
}

fn use_amenities(
map: &mut RawMap,
amenities: Vec<(Pt2D, NamePerLanguage, String)>,
timer: &mut Timer,
) {
fn use_amenities(map: &mut RawMap, amenities: Vec<(Pt2D, Amenity)>, timer: &mut Timer) {
let mut closest: FindClosest<osm::OsmID> = FindClosest::new(&map.gps_bounds.to_bounds());
for (id, b) in &map.buildings {
closest.add(*id, b.polygon.points());
}

timer.start_iter("match building amenities", amenities.len());
for (pt, names, amenity) in amenities {
for (pt, amenity) in amenities {
timer.next();
if let Some((id, _)) = closest.closest_pt(pt, Distance::meters(50.0)) {
let b = map.buildings.get_mut(&id).unwrap();
if b.polygon.contains_pt(pt) {
b.amenities.insert((names, amenity));
b.amenities.push(amenity);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion convert_osm/src/parking.rs
Expand Up @@ -204,7 +204,7 @@ fn apply_private_offstreet_parking(map: &mut RawMap, policy: &PrivateOffstreetPa
// 1 spot per 30m^2.
b.num_parking_spots = ((b.polygon.area() / 30.0) as usize) * levels;
// Not useful to list this
abstutil::retain_btreeset(&mut b.amenities, |(_, a)| a != "parking");
b.amenities.retain(|a| a.amenity_type != "parking");
} else {
b.num_parking_spots = *n;
}
Expand Down
7 changes: 2 additions & 5 deletions convert_osm/src/split_ways.rs
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use abstutil::{Counter, Timer};
use geom::{Distance, HashablePt2D, Pt2D};
use map_model::raw::{OriginalRoad, RawIntersection, RawMap};
use map_model::{osm, IntersectionType, NamePerLanguage};
use map_model::{osm, Amenity, IntersectionType};

use crate::extract::OsmExtract;

Expand All @@ -13,10 +13,7 @@ pub fn split_up_roads(
map: &mut RawMap,
mut input: OsmExtract,
timer: &mut Timer,
) -> (
Vec<(Pt2D, NamePerLanguage, String)>,
HashMap<HashablePt2D, OriginalRoad>,
) {
) -> (Vec<(Pt2D, Amenity)>, HashMap<HashablePt2D, OriginalRoad>) {
timer.start("splitting up roads");

let mut pt_to_intersection: HashMap<HashablePt2D, osm::NodeID> = HashMap::new();
Expand Down

0 comments on commit 47fa997

Please sign in to comment.