OSMServer/OSMDatastructure/TagManager.cs
glax 9dc282253d 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
2023-04-01 01:31:58 +02:00

52 lines
1.4 KiB
C#

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