From 0974842637a25b38b957bfd8d162de1e92e9c898 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Thu, 7 Dec 2023 12:00:04 +0800 Subject: [PATCH] Start reorganizing osm parsing code --- backend/src/lib.rs | 11 ++++---- backend/src/{osm.rs => osm_reader/mod.rs} | 27 +++++++++++++++++++ .../src/{parse_osm.rs => osm_reader/xml.rs} | 23 ++-------------- backend/src/scrape.rs | 5 ++-- 4 files changed, 36 insertions(+), 30 deletions(-) rename backend/src/{osm.rs => osm_reader/mod.rs} (71%) rename backend/src/{parse_osm.rs => osm_reader/xml.rs} (89%) diff --git a/backend/src/lib.rs b/backend/src/lib.rs index 1bcdd91..9e7dc74 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -14,8 +14,7 @@ use wasm_bindgen::prelude::*; mod heatmap; mod node_map; -mod osm; -mod parse_osm; +mod osm_reader; mod route; mod scrape; @@ -53,9 +52,9 @@ pub struct Road { id: RoadID, src_i: IntersectionID, dst_i: IntersectionID, - way: osm::WayID, - node1: osm::NodeID, - node2: osm::NodeID, + way: osm_reader::WayID, + node1: osm_reader::NodeID, + node2: osm_reader::NodeID, linestring: LineString, tags: HashMap, kind: RoadKind, @@ -77,7 +76,7 @@ pub enum RoadKind { pub struct Intersection { id: IntersectionID, #[allow(dead_code)] - node: osm::NodeID, + node: osm_reader::NodeID, point: Point, roads: Vec, } diff --git a/backend/src/osm.rs b/backend/src/osm_reader/mod.rs similarity index 71% rename from backend/src/osm.rs rename to backend/src/osm_reader/mod.rs index a98d4e6..5b027a1 100644 --- a/backend/src/osm.rs +++ b/backend/src/osm_reader/mod.rs @@ -1,5 +1,12 @@ +mod xml; + +use std::collections::HashMap; use std::fmt; +use geo::Coord; + +pub use self::xml::parse; + #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)] pub struct NodeID(pub i64); @@ -43,3 +50,23 @@ impl fmt::Display for OsmID { } // TODO Into for both directions + +pub enum Element { + Node { + id: NodeID, + // TODO Probably dont do this here + pt: Coord, + tags: HashMap, + }, + Way { + id: WayID, + node_ids: Vec, + tags: HashMap, + }, + Relation { + id: RelationID, + tags: HashMap, + // Role, member ID + members: Vec<(String, OsmID)>, + }, +} diff --git a/backend/src/parse_osm.rs b/backend/src/osm_reader/xml.rs similarity index 89% rename from backend/src/parse_osm.rs rename to backend/src/osm_reader/xml.rs index 7ebd9fd..b0c6814 100644 --- a/backend/src/parse_osm.rs +++ b/backend/src/osm_reader/xml.rs @@ -3,36 +3,17 @@ use std::collections::HashMap; use anyhow::Result; use geo::Coord; -use crate::osm::*; +use crate::osm_reader::*; // Per https://wiki.openstreetmap.org/wiki/OSM_XML#Certainties_and_Uncertainties, we assume // elements come in order: nodes, ways, then relations. -pub fn parse_osm(input_bytes: &[u8]) -> Result> { +pub fn parse(input_bytes: &[u8]) -> Result> { info!("Got {} bytes", input_bytes.len()); // TODO Detect file type parse_xml(input_bytes) } -pub enum Element { - Node { - id: NodeID, - pt: Coord, - tags: HashMap, - }, - Way { - id: WayID, - node_ids: Vec, - tags: HashMap, - }, - Relation { - id: RelationID, - tags: HashMap, - // Role, member ID - members: Vec<(String, OsmID)>, - }, -} - // TODO Iterator instead fn parse_xml(input_bytes: &[u8]) -> Result> { let input_string = String::from_utf8(input_bytes.to_vec())?; diff --git a/backend/src/scrape.rs b/backend/src/scrape.rs index e730642..08bb159 100644 --- a/backend/src/scrape.rs +++ b/backend/src/scrape.rs @@ -3,8 +3,7 @@ use std::collections::HashMap; use anyhow::Result; use geo::{Coord, LineString, Point}; -use crate::osm::{NodeID, WayID}; -use crate::parse_osm::Element; +use crate::osm_reader::{Element, NodeID, WayID}; use crate::{Intersection, IntersectionID, MapModel, Road, RoadID, RoadKind}; struct Way { @@ -16,7 +15,7 @@ struct Way { pub fn scrape_osm(input_bytes: &[u8]) -> Result { let mut node_mapping = HashMap::new(); let mut highways = Vec::new(); - for elem in crate::parse_osm::parse_osm(input_bytes)? { + for elem in crate::osm_reader::parse(input_bytes)? { match elem { Element::Node { id, pt, .. } => { node_mapping.insert(id, pt);