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
This commit is contained in:
glax 2023-04-01 01:31:58 +02:00
parent f0cd97fbc7
commit 9dc282253d
2 changed files with 25 additions and 19 deletions

View File

@ -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<WayType, byte> defaultSpeedCar = new() {

View File

@ -5,48 +5,48 @@ namespace OSMDatastructure.Graph;
[Serializable]
public class TagManager
{
public readonly Dictionary<ulong, HashSet<Tag>> wayTags = new();
public readonly Dictionary<ulong, HashSet<Tag>> 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<Tag>());
HashSet<Tag> wayTags = wayTagSets[wayId];
if (!wayTags.Any(wayTag => wayTag.key == tag.key))
{
if(!wayTags.ContainsKey(wayId))
wayTags.Add(wayId, new HashSet<Tag>());
wayTags[wayId].Add(tag);
wayTags.Add(tag);
}
}
public void AddTag(ulong wayId, KeyValuePair<Tag.TagType, dynamic> keyValuePair)
public void AddTag(ulong wayId, KeyValuePair<Tag.TagType, dynamic> tag)
{
if(!wayTags.ContainsKey(wayId))
wayTags.Add(wayId, new HashSet<Tag>());
wayTags[wayId].Add(new Tag(keyValuePair.Key, keyValuePair.Value));
AddTag(wayId, new Tag(tag.Key, tag.Value));
}
public HashSet<Tag>? GetTagsForWayId(ulong wayId)
{
return wayTags.TryGetValue(wayId, out HashSet<Tag>? value) ? value : null;
return wayTagSets.TryGetValue(wayId, out HashSet<Tag>? value) ? value : null;
}
public bool ContainsWay(ulong wayId)
{
return wayTags.ContainsKey(wayId);
return wayTagSets.ContainsKey(wayId);
}
}