From 9dc282253dd7db6a9e84e416c5c0e1179ba16a2b Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 1 Apr 2023 01:31:58 +0200 Subject: [PATCH] Removed TagType.EMPTY (now returns null instead) AddTag(ulong, KeyValuePair) is now only Wrapper of AddTag(ulong, Tag) AddTag now checks if tags already exist --- OSMDatastructure/Tag.cs | 14 ++++++++++---- OSMDatastructure/TagManager.cs | 30 +++++++++++++++--------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/OSMDatastructure/Tag.cs b/OSMDatastructure/Tag.cs index cd26bde..518bfb7 100644 --- a/OSMDatastructure/Tag.cs +++ b/OSMDatastructure/Tag.cs @@ -32,7 +32,7 @@ public class Tag return new Tag(type, value); } - public static Tag ConvertToTag(string key, string value) + public static Tag? ConvertToTag(string key, string value) { switch (key) { @@ -73,12 +73,18 @@ public class Tag break; } - return new Tag(Tag.TagType.EMPTY, false); + + return null; } - + + public override string ToString() + { + return $"TAG {key.ToString()} {value.ToString()}"; + } + public enum TagType : byte { - highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id, EMPTY + highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id } public static readonly Dictionary defaultSpeedCar = new() { diff --git a/OSMDatastructure/TagManager.cs b/OSMDatastructure/TagManager.cs index 91eb4c4..1253325 100644 --- a/OSMDatastructure/TagManager.cs +++ b/OSMDatastructure/TagManager.cs @@ -5,48 +5,48 @@ namespace OSMDatastructure.Graph; [Serializable] public class TagManager { - public readonly Dictionary> wayTags = new(); + public readonly Dictionary> wayTagSets = new(); public bool ContainsKey(ulong wayId, Tag.TagType key) { - return wayTags.ContainsKey(wayId) && wayTags[wayId].Any(tag => tag.key == key); + return wayTagSets.ContainsKey(wayId) && wayTagSets[wayId].Any(tag => tag.key == key); } public object? GetTag(ulong wayId, Tag.TagType key) { - return ContainsKey(wayId, key) ? wayTags[wayId].First(tag => tag.key == key) : null; + return ContainsKey(wayId, key) ? wayTagSets[wayId].First(tag => tag.key == key) : null; } public void AddTag(ulong wayId, string key, string value) { - Tag tag = Tag.ConvertToTag(key, value); - AddTag(wayId, tag); + Tag? tag = Tag.ConvertToTag(key, value); + if(tag is not null) + AddTag(wayId, tag); } public void AddTag(ulong wayId, Tag tag) { - if (tag.key != Tag.TagType.EMPTY) + if(!wayTagSets.ContainsKey(wayId)) + wayTagSets.Add(wayId, new HashSet()); + HashSet wayTags = wayTagSets[wayId]; + if (!wayTags.Any(wayTag => wayTag.key == tag.key)) { - if(!wayTags.ContainsKey(wayId)) - wayTags.Add(wayId, new HashSet()); - wayTags[wayId].Add(tag); + wayTags.Add(tag); } } - public void AddTag(ulong wayId, KeyValuePair keyValuePair) + public void AddTag(ulong wayId, KeyValuePair tag) { - if(!wayTags.ContainsKey(wayId)) - wayTags.Add(wayId, new HashSet()); - wayTags[wayId].Add(new Tag(keyValuePair.Key, keyValuePair.Value)); + AddTag(wayId, new Tag(tag.Key, tag.Value)); } public HashSet? GetTagsForWayId(ulong wayId) { - return wayTags.TryGetValue(wayId, out HashSet? value) ? value : null; + return wayTagSets.TryGetValue(wayId, out HashSet? value) ? value : null; } public bool ContainsWay(ulong wayId) { - return wayTags.ContainsKey(wayId); + return wayTagSets.ContainsKey(wayId); } } \ No newline at end of file