70 lines
2.1 KiB
C#
70 lines
2.1 KiB
C#
using System.Runtime.CompilerServices;
|
|
|
|
namespace OsmXmlToRegionConverter;
|
|
|
|
public class TagManager
|
|
{
|
|
public readonly Dictionary<ulong, HashSet<Tag>> wayTags = new();
|
|
|
|
public bool ContainsKey(ulong wayId, Tag.TagType key)
|
|
{
|
|
return wayTags.ContainsKey(wayId) && wayTags[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;
|
|
}
|
|
|
|
public void AddTag(ulong wayId, string key, string value)
|
|
{
|
|
Tag tag = Tag.ConvertToTag(key, value);
|
|
AddTag(wayId, tag);
|
|
}
|
|
|
|
private void AddTag(ulong wayId, Tag tag)
|
|
{
|
|
if (tag.key != Tag.TagType.EMPTY)
|
|
{
|
|
if(!wayTags.ContainsKey(wayId))
|
|
wayTags.Add(wayId, new HashSet<Tag>());
|
|
wayTags[wayId].Add(tag);
|
|
}
|
|
}
|
|
|
|
public static TagManager FromBytes(byte[] bytes)
|
|
{
|
|
TagManager newTagManager = new TagManager();
|
|
|
|
using (MemoryStream m = new MemoryStream(bytes)) {
|
|
using (BinaryReader r = new BinaryReader(m))
|
|
{
|
|
while (r.BaseStream.Position < bytes.Length)
|
|
{
|
|
int wayByteSize = r.ReadInt32();
|
|
byte[] wayBytes = r.ReadBytes(wayByteSize);
|
|
AddWayFromBytes(ref newTagManager, wayBytes);
|
|
}
|
|
}
|
|
}
|
|
|
|
return newTagManager;
|
|
}
|
|
|
|
private static void AddWayFromBytes(ref TagManager tagManager, byte[] bytes)
|
|
{
|
|
using (MemoryStream m = new MemoryStream(bytes)) {
|
|
using (BinaryReader r = new BinaryReader(m))
|
|
{
|
|
ulong wayId = r.ReadUInt64();
|
|
while (r.BaseStream.Position < bytes.Length)
|
|
{
|
|
int tagsByteSize = r.ReadInt32();
|
|
byte[] tagBytes = r.ReadBytes(tagsByteSize);
|
|
Tag tag = Tag.FromBytes(tagBytes);
|
|
tagManager.AddTag(wayId, tag);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |