Skip to content

Commit

Permalink
OSM Tag Schemes (a-b-street#219)
Browse files Browse the repository at this point in the history
Rework Crate Structure
  • Loading branch information
droogmic authored Jun 11, 2022
1 parent 0c8e37b commit b3ab25f
Show file tree
Hide file tree
Showing 34 changed files with 436 additions and 157 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"osm2lanes",
"osm-tags",
"osm-tag-schemes",
"osm2lanes-web",
"osm2lanes-bin",
"osm2lanes-npm",
Expand Down
17 changes: 17 additions & 0 deletions osm-tag-schemes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "osm-tag-schemes"
version = "0.1.0"
edition = "2021"
description = "OSM Tag Scheme Definitions"
repository = "https://github.com/a-b-street/osm2lanes"
license = "Apache 2.0"
keywords = ["openstreetmap", "osm", "tag"]
authors = [
"Dustin Carlino <[email protected]>",
"Michael Droogleever Fortuyn <[email protected]>",
]

[dependencies]
osm-tags = { path = "../osm-tags" }
serde = { version = "1", features = ["derive"] }
strum = { version = "0.24", features = ["derive"] }
3 changes: 3 additions & 0 deletions osm-tags/src/access.rs → osm-tag-schemes/src/access.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use serde::{Deserialize, Serialize};
use strum::{EnumString, IntoStaticStr};

/// Access variants from <https://wiki.openstreetmap.org/wiki/Key:access#List_of_possible_values>
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(IntoStaticStr, EnumString)]
#[strum(serialize_all = "snake_case")]
pub enum Access {
Yes,
No,
Expand Down
118 changes: 38 additions & 80 deletions osm-tags/src/osm.rs → osm-tag-schemes/src/highway.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use osm_tags::Tags;
use serde::{Deserialize, Serialize};
use strum::ParseError;

use crate::{TagKey, Tags};

pub const ONEWAY: TagKey = TagKey::from_static("oneway");
pub const HIGHWAY: TagKey = TagKey::from_static("highway");
const CONSTRUCTION: TagKey = TagKey::from_static("construction");
const PROPOSED: TagKey = TagKey::from_static("proposed");
pub const LIFECYCLE: [TagKey; 3] = [HIGHWAY, CONSTRUCTION, PROPOSED];
use crate::{keys, FromTags, Tagged};

#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum HighwayType {
Classified(HighwayImportance),
Expand All @@ -33,6 +30,7 @@ pub enum HighwayType {
Steps,
}

#[allow(clippy::module_name_repetitions)]
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
pub enum HighwayImportance {
Motorway,
Expand Down Expand Up @@ -70,8 +68,7 @@ impl std::fmt::Display for NonTravel {
}

impl std::str::FromStr for HighwayType {
type Err = String;

type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"motorway" => Self::Classified(HighwayImportance::Motorway),
Expand Down Expand Up @@ -100,7 +97,7 @@ impl std::str::FromStr for HighwayType {
"steps" => Self::Steps,
"track" => Self::Track,
"unclassified" => Self::Unclassified,
_ => return Err(s.to_owned()),
_ => return Err(ParseError::VariantNotFound),
})
}
}
Expand Down Expand Up @@ -187,36 +184,19 @@ impl Highway {
///
/// If highway missing return None
/// If highway unknown return the unknown value
pub fn from_tags(tags: &Tags) -> Result<Self, Option<String>> {
tags.get(&HIGHWAY).ok_or(None).and_then(|s| match s {
"construction" => {
let highway = tags
.get(&CONSTRUCTION)
.map_or(Ok(HighwayType::UnknownRoad), str::parse)
.map_err(Some)?;
Ok(Self {
highway,
lifecycle: Lifecycle::Construction,
})
},
"proposed" => {
let highway = tags
.get(&PROPOSED)
.map_or(Ok(HighwayType::UnknownRoad), str::parse)
.map_err(Some)?;
Ok(Self {
highway,
lifecycle: Lifecycle::Proposed,
})
},
s => {
let highway = s.parse().map_err(Some)?;
Ok(Self {
highway,
lifecycle: Lifecycle::Active,
})
#[must_use]
pub fn from_tags(tags: &Tags) -> Tagged<Self> {
match HighwayType::from_tags(tags, &keys::HIGHWAY) {
Tagged::None => Tagged::None,
Tagged::Some(val) => Tagged::Some(Highway::active(val)),
Tagged::Unknown(key, val) => match val {
"construction" => {
HighwayType::from_tags(tags, &keys::CONSTRUCTION).map(Highway::construction)
},
"proposed" => HighwayType::from_tags(tags, &keys::PROPOSED).map(Highway::proposed),
val => Tagged::Unknown(key, val),
},
})
}
}

/// Active Highway
Expand All @@ -228,7 +208,25 @@ impl Highway {
}
}

/// Is Highway Construction
/// Highway under Construction
#[must_use]
pub fn construction(r#type: HighwayType) -> Self {
Self {
highway: r#type,
lifecycle: Lifecycle::Construction,
}
}

/// Highway Proposed
#[must_use]
pub fn proposed(r#type: HighwayType) -> Self {
Self {
highway: r#type,
lifecycle: Lifecycle::Proposed,
}
}

/// Is Highway under Construction
#[must_use]
pub fn is_construction(&self) -> bool {
matches!(
Expand Down Expand Up @@ -257,44 +255,4 @@ impl Highway {
pub fn r#type(&self) -> HighwayType {
self.highway
}

/// Is Highway Supported
#[must_use]
pub const fn is_supported(&self) -> bool {
self.is_supported_road() || self.is_supported_non_motorized()
}

/// Is Highway Supported and Predominantly Motorized
#[must_use]
pub const fn is_supported_road(&self) -> bool {
matches!(
self,
Highway {
highway: HighwayType::Classified(_)
| HighwayType::Link(_)
| HighwayType::Residential
| HighwayType::Service
| HighwayType::Unclassified
| HighwayType::UnknownRoad,
lifecycle: Lifecycle::Active | Lifecycle::Construction,
}
)
}

/// Is Highway Supported and Predominantly Non-Motorized
#[must_use]
pub const fn is_supported_non_motorized(&self) -> bool {
matches!(
self,
Highway {
highway: HighwayType::Cycleway
| HighwayType::Footway
| HighwayType::Path
| HighwayType::Pedestrian
| HighwayType::Steps
| HighwayType::Track,
lifecycle: Lifecycle::Active,
}
)
}
}
16 changes: 16 additions & 0 deletions osm-tag-schemes/src/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use osm_tags::TagKey;

pub const NAME: TagKey = TagKey::from_static("name");
pub const REF: TagKey = TagKey::from_static("ref");

pub const HIGHWAY: TagKey = TagKey::from_static("highway");
pub const CONSTRUCTION: TagKey = TagKey::from_static("construction");
pub const PROPOSED: TagKey = TagKey::from_static("proposed");
pub const LIFECYCLE: [TagKey; 3] = [HIGHWAY, CONSTRUCTION, PROPOSED];

pub const ONEWAY: TagKey = TagKey::from_static("oneway");

pub const LIT: TagKey = TagKey::from_static("lit");

pub const TRACK_TYPE: TagKey = TagKey::from_static("tracktype");
pub const SMOOTHNESS: TagKey = TagKey::from_static("smoothness");
Loading

0 comments on commit b3ab25f

Please sign in to comment.