OSMServer/OSMDatastructure/TagManager.cs

59 lines
1.5 KiB
C#
Raw Normal View History

using System.Text.Json.Serialization;
2023-04-09 17:06:45 +02:00
using OSMDatastructure.Graph;
2023-04-09 17:06:45 +02:00
namespace OSMDatastructure;
[Serializable]
public class TagManager
{
[JsonRequired]public Dictionary<ulong, HashSet<Tag>> wayTagSets { get; set; }
public TagManager()
{
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).value : null;
}
public void AddTag(ulong wayId, string key, string value)
{
2023-04-09 22:10:23 +02:00
HashSet<Tag> pTags = Tag.ConvertToTags(key, value);
2023-04-09 23:55:24 +02:00
if(pTags.Count > 0)
foreach (Tag pTag in pTags)
AddTag(wayId, pTag);
}
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);
}
}