Skip to content

Commit

Permalink
Merge pull request #26 from NoahSchiro/node-tags
Browse files Browse the repository at this point in the history
Add tags field to node structure
  • Loading branch information
NoahSchiro authored Nov 17, 2024
2 parents ee46e2e + 48d201b commit dc39d90
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/api/overpass_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Error;
use std::collections::HashMap;

use serde::{Serialize, Deserialize};
use serde_json::Value;
Expand All @@ -17,6 +18,7 @@ pub enum Element {
id: u64,
lat: f64,
lon: f64,
tags: Option<HashMap<String, String>>
},
Way {
id: u64,
Expand Down
2 changes: 1 addition & 1 deletion src/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn create_graph(elements: &Vec<Element>) -> Result<OSMGraph, Box<dyn Error>>

//Add nodes to mapping and to graph
for node in nodes {
let petgraph_index = result.add_node(node).index() as u32;
let petgraph_index = result.add_node(node.clone()).index() as u32;
node_mapping.insert(
node.id(),
petgraph_index
Expand Down
26 changes: 17 additions & 9 deletions src/graph/node.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use core::fmt;
use std::collections::HashSet;
use std::collections::{HashSet, HashMap};
use std::error::Error;

use crate::graph::way::OSMWay;
use crate::api::Element;

type OSMTag = Option<HashMap<String, String>>;

/// OSMNode contains all information that we might care about in a node. Currently, it contains a
/// node ID (as defined in Overpass API) a latitude and a longitude.
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug, Default)]
#[derive(Clone, PartialEq, Debug, Default)]
pub struct OSMNode {
id: u64,
lat: f64,
lon: f64
lon: f64,
tags: OSMTag
}

impl fmt::Display for OSMNode {
Expand All @@ -24,8 +27,8 @@ impl fmt::Display for OSMNode {
impl OSMNode {

/// Create a new OSMNode from fields.
pub fn new(id: u64, lat: f64, lon: f64) -> Self {
OSMNode { id, lat, lon }
pub fn new(id: u64, lat: f64, lon: f64, tags: OSMTag) -> Self {
OSMNode { id, lat, lon, tags}
}

/// Get the node ID.
Expand All @@ -40,6 +43,11 @@ impl OSMNode {
pub fn lon(&self) -> f64 {
self.lon
}

/// Get a reference tags associated with this node
pub fn tags(&self) -> &OSMTag {
&self.tags
}
}

/// Compute the [haversine distance](https://en.wikipedia.org/wiki/Haversine_formula)
Expand Down Expand Up @@ -69,8 +77,8 @@ pub fn get_osm_nodes(elements: &Vec<Element>) -> Result<Vec<OSMNode>, Box<dyn Er
//Only get OSM elements that are nodes
let node_elements: Vec<OSMNode> = elements.into_iter()
.filter_map(|e| {
if let Element::Node { id, lat, lon } = e {
Some(OSMNode { id: *id, lat: *lat, lon: *lon })
if let Element::Node { id, lat, lon, tags } = e {
Some(OSMNode { id: *id, lat: *lat, lon: *lon, tags: tags.clone() })
} else {
None
}
Expand Down Expand Up @@ -115,10 +123,10 @@ pub fn get_nodes_from_ways(elements: &Vec<Element>, ways: &Vec<OSMWay>)
//Only get OSM elements that are nodes
let node_elements: Vec<OSMNode> = elements.clone().into_iter()
.filter_map(|e| {
if let Element::Node { id, lat, lon } = e {
if let Element::Node { id, lat, lon, tags } = e {

if node_ids.contains(&id) {
Some(OSMNode { id, lat, lon })
Some(OSMNode { id, lat, lon, tags})
} else {
None
}
Expand Down

0 comments on commit dc39d90

Please sign in to comment.