Skip to content

Commit

Permalink
Start reorganizing osm parsing code
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 7, 2023
1 parent c8f55cc commit 0974842
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
11 changes: 5 additions & 6 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, String>,
kind: RoadKind,
Expand All @@ -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<RoadID>,
}
Expand Down
27 changes: 27 additions & 0 deletions backend/src/osm.rs → backend/src/osm_reader/mod.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -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<String, String>,
},
Way {
id: WayID,
node_ids: Vec<NodeID>,
tags: HashMap<String, String>,
},
Relation {
id: RelationID,
tags: HashMap<String, String>,
// Role, member ID
members: Vec<(String, OsmID)>,
},
}
23 changes: 2 additions & 21 deletions backend/src/parse_osm.rs → backend/src/osm_reader/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Element>> {
pub fn parse(input_bytes: &[u8]) -> Result<Vec<Element>> {
info!("Got {} bytes", input_bytes.len());
// TODO Detect file type

parse_xml(input_bytes)
}

pub enum Element {
Node {
id: NodeID,
pt: Coord,
tags: HashMap<String, String>,
},
Way {
id: WayID,
node_ids: Vec<NodeID>,
tags: HashMap<String, String>,
},
Relation {
id: RelationID,
tags: HashMap<String, String>,
// Role, member ID
members: Vec<(String, OsmID)>,
},
}

// TODO Iterator instead
fn parse_xml(input_bytes: &[u8]) -> Result<Vec<Element>> {
let input_string = String::from_utf8(input_bytes.to_vec())?;
Expand Down
5 changes: 2 additions & 3 deletions backend/src/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,7 +15,7 @@ struct Way {
pub fn scrape_osm(input_bytes: &[u8]) -> Result<MapModel> {
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);
Expand Down

0 comments on commit 0974842

Please sign in to comment.