From 207df8dcd7446203a853a51a1acca0ee6180c629 Mon Sep 17 00:00:00 2001 From: glax Date: Tue, 23 Jul 2024 15:46:53 +0200 Subject: [PATCH] Add Methods to way to GetDirection GetHighwayType, GetMaxSpeed --- OSM_Graph/Enums/WayDirection.cs | 6 ++++++ OSM_Graph/Way.cs | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 OSM_Graph/Enums/WayDirection.cs diff --git a/OSM_Graph/Enums/WayDirection.cs b/OSM_Graph/Enums/WayDirection.cs new file mode 100644 index 0000000..6bf6934 --- /dev/null +++ b/OSM_Graph/Enums/WayDirection.cs @@ -0,0 +1,6 @@ +namespace OSM_Graph.Enums; + +public enum WayDirection +{ + Forwards, Backwards, Both +} \ No newline at end of file diff --git a/OSM_Graph/Way.cs b/OSM_Graph/Way.cs index 6db8325..53a382a 100644 --- a/OSM_Graph/Way.cs +++ b/OSM_Graph/Way.cs @@ -1,5 +1,6 @@ using System.Runtime.Serialization; using System.Text.RegularExpressions; +using OSM_Graph.Enums; namespace OSM_Graph; @@ -7,6 +8,41 @@ public class Way(ulong id, Dictionary tags, List nodeIds) { public readonly ulong ID = id; public readonly List NodeIds = nodeIds; + + public WayDirection GetDirection() + { + if (Tags.TryGetValue("oneway", out string? onewayStr)) + return onewayStr switch + { + "yes" => WayDirection.Forwards, + "1" => WayDirection.Forwards, + "no" => WayDirection.Both, + "-1" => WayDirection.Backwards, + _ => WayDirection.Both + }; + HighwayType[] impliedTypes = [HighwayType.motorway, HighwayType.motorway_link]; + if (impliedTypes.Contains(GetHighwayType())) + return WayDirection.Forwards; + return WayDirection.Both; + } + + public HighwayType GetHighwayType() + { + if (!Tags.TryGetValue("highway", out string? highwayTypeStr)) + return HighwayType.NONE; + if (!Enum.TryParse(highwayTypeStr, out HighwayType highwayType)) + return HighwayType.NONE; + return highwayType; + } + + public byte GetMaxSpeed() + { + if (!Tags.TryGetValue("maxspeed", out string? maxSpeedStr)) + return 0; + if (!byte.TryParse(maxSpeedStr, out byte speed)) + return 0; + return speed; + } public string Serialize() {