1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
use serde::{ser::Error, Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use super::Coordinate;
/// Represents the type of a `RailwayElement`.
///
/// `ElementType` is an enumeration with two possible values: `Way` and `Node`.
/// It is used to represent the type of an element in a railway network.
#[derive(Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ElementType {
/// Represents a `Way` element in the railway network.
///
/// A `Way` element is a linear feature, such as a railway track or a route.
/// It consists of an ordered list of nodes that define the geometry of the way.
Way,
/// Represents a `Node` element in the railway network.
///
/// A `Node` element is a point feature, such as a railway station or a junction.
/// It is defined by its latitude and longitude coordinates.
Node,
}
/// Represents an element of a railway network.
///
/// A `RailwayElement` struct contains information about a railway element, such as its ID, type, tags, nodes,
/// geometry, latitude, and longitude. It can be used to store and manipulate railway data.
///
/// # Example
///
/// ```
/// use openrailwaymap_exporter::importer::overpass_importer::{ElementType, RailwayElement};
/// let element = RailwayElement::new_with_id(1);
/// assert_eq!(element.id, 1);
/// assert_eq!(element.element_type, ElementType::Node);
/// ```
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct RailwayElement {
/// The unique identifier of the railway element.
pub id: i64,
/// Optional key-value pairs associated with the railway element.
pub tags: Option<HashMap<String, String>>,
/// The type of the railway element, either `Way` or `Node`.
#[serde(rename = "type")]
pub element_type: ElementType,
/// An optional ordered list of node IDs that define the geometry of a `Way` element.
pub nodes: Option<Vec<i64>>,
/// An optional list of coordinates that represent the geometry of a `Way` element.
pub geometry: Option<Vec<Coordinate>>,
/// The latitude coordinate of a `Node` element.
pub lat: Option<f64>,
/// The longitude coordinate of a `Node` element.
pub lon: Option<f64>,
}
impl RailwayElement {
/// Deserialize a JSON value into a vector of `RailwayElement` instances.
///
/// # Arguments
///
/// * `json_value` - A reference to a JSON value containing railway elements data.
///
/// # Returns
///
/// A `Result` containing a vector of `RailwayElement` instances on success, or a `serde_json::Error` on failure.
pub fn from_json(json_value: &Value) -> Result<Vec<RailwayElement>, serde_json::Error> {
let railway_elements = json_value["elements"]
.as_array()
.ok_or_else(|| serde_json::Error::custom("Elements parsing error"))?
.iter()
.filter_map(|elem| serde_json::from_value::<RailwayElement>(elem.clone()).ok())
.collect::<Vec<RailwayElement>>();
Ok(railway_elements)
}
}
impl Default for RailwayElement {
fn default() -> Self {
Self {
id: 0,
tags: None,
element_type: ElementType::Node,
nodes: None,
geometry: None,
lat: None,
lon: None,
}
}
}
impl RailwayElement {
/// Create a new `RailwayElement` instance with the specified ID.
///
/// # Arguments
///
/// * `id` - The ID of the new `RailwayElement` instance.
///
/// # Returns
///
/// A new `RailwayElement` instance with the specified ID.
pub fn new_with_id(id: i64) -> Self {
Self {
id,
..Default::default()
}
}
}
/// Counts the number of `Way` elements in a vector of `RailwayElement`s.
///
/// This function takes a slice of `RailwayElement`s as input and returns the count of `Way` elements as a `usize`.
///
/// # Arguments
///
/// * `elements` - A slice of `RailwayElement`s to count the `Way` elements in.
///
/// # Example
///
/// ```
/// use openrailwaymap_exporter::importer::overpass_importer::count_way_elements;
/// use openrailwaymap_exporter::importer::overpass_importer::{ElementType, RailwayElement};
/// use std::collections::HashMap;
///
/// let elements = vec![
/// RailwayElement {
/// id: 1,
/// element_type: ElementType::Node,
/// lat: Some(50.1109),
/// lon: Some(8.6821),
/// tags: Some(HashMap::new()),
/// nodes: None,
/// geometry: None,
/// },
/// RailwayElement {
/// id: 2,
/// element_type: ElementType::Way,
/// lat: None,
/// lon: None,
/// tags: Some(HashMap::new()),
/// nodes: Some(vec![1, 3]),
/// geometry: None,
/// },
/// ];
///
/// let way_count = count_way_elements(&elements);
/// assert_eq!(way_count, 1);
/// ```
pub fn count_way_elements(elements: &[RailwayElement]) -> usize {
elements
.iter()
.filter(|element| element.element_type == ElementType::Way)
.count()
}
/// Counts the number of `Node` elements in a given vector of `RailwayElement`s.
///
/// This function iterates over the input `RailwayElement`s and filters the elements
/// with the type `ElementType::Node`. It returns the count of such elements.
///
/// # Arguments
///
/// * `elements` - A vector of `RailwayElement`s to count the node elements in.
///
/// # Returns
///
/// An `usize` representing the count of node elements found in the input vector.
pub fn count_node_elements(elements: &[RailwayElement]) -> usize {
elements
.iter()
.filter(|element| element.element_type == ElementType::Node)
.count()
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_railway_element_default() {
let default_element = RailwayElement::default();
assert_eq!(default_element.id, 0);
assert!(default_element.tags.is_none());
assert_eq!(default_element.element_type, ElementType::Node);
assert!(default_element.nodes.is_none());
assert!(default_element.geometry.is_none());
assert!(default_element.lat.is_none());
assert!(default_element.lon.is_none());
}
#[test]
fn test_railway_element_new_with_id() {
let id = 42;
let element_with_id = RailwayElement::new_with_id(id);
assert_eq!(element_with_id.id, id);
assert!(element_with_id.tags.is_none());
assert_eq!(element_with_id.element_type, ElementType::Node);
assert!(element_with_id.nodes.is_none());
assert!(element_with_id.geometry.is_none());
assert!(element_with_id.lat.is_none());
assert!(element_with_id.lon.is_none());
}
#[test]
fn test_railway_element_from_json() {
let json_value = json!({
"elements": [
{
"type": "node",
"id": 1,
"lat": 50.1191127,
"lon": 8.6090232,
"tags": {
"railway": "switch",
"railway:switch": "default",
"railway:turnout_side": "right"
}
},
{
"type": "way",
"id": 2,
"nodes": [1, 2, 3],
"tags": {
"railway": "rail"
}
}
]
});
let result = RailwayElement::from_json(&json_value);
assert!(result.is_ok());
let elements = result.unwrap();
assert_eq!(elements.len(), 2);
let node_element = &elements[0];
assert_eq!(node_element.id, 1);
assert_eq!(node_element.element_type, ElementType::Node);
assert_eq!(node_element.lat, Some(50.1191127));
assert_eq!(node_element.lon, Some(8.6090232));
let way_element = &elements[1];
assert_eq!(way_element.id, 2);
assert_eq!(way_element.element_type, ElementType::Way);
assert_eq!(way_element.nodes, Some(vec![1, 2, 3]));
}
#[test]
fn test_count_way_elements() {
let elements = vec![
RailwayElement {
id: 1,
element_type: ElementType::Node,
lat: Some(50.1109),
lon: Some(8.6821),
tags: Some(HashMap::new()),
nodes: None,
geometry: None,
},
RailwayElement {
id: 2,
element_type: ElementType::Way,
lat: None,
lon: None,
tags: Some(HashMap::new()),
nodes: Some(vec![1, 3]),
geometry: None,
},
RailwayElement {
id: 3,
element_type: ElementType::Way,
lat: None,
lon: None,
tags: Some(HashMap::new()),
nodes: Some(vec![3, 5]),
geometry: None,
},
];
let way_count = count_way_elements(&elements);
assert_eq!(way_count, 2);
}
}