OSMServer/OsmXmlToRegionConverter/Region.cs

47 lines
1.3 KiB
C#
Raw Normal View History

2023-03-30 16:29:42 +02:00
namespace OsmXmlToRegionConverter;
public class Region
{
private HashSet<Node> nodes;
public ulong regionHash { get; }
private TagManager tagManager { get; }
public Region(ulong regionHash)
{
this.regionHash = regionHash;
nodes = new HashSet<Node>();
tagManager = new TagManager();
}
public void AddNode(Node nodeToAdd)
{
if (nodes.Contains(nodeToAdd))
throw new Exception("Node already in region");
else
nodes.Add(nodeToAdd);
}
public Node[] GetNodes()
{
return this.nodes.ToArray();
}
public const int ByteSizeEmpty = sizeof(int) + sizeof(ulong);
public static Region FromBytes(byte[] bytes)
{
using (MemoryStream m = new MemoryStream(bytes)) {
using (BinaryReader r = new BinaryReader(m))
{
int nodesInRegionCount = r.ReadInt32();
ulong regionHash = r.ReadUInt64();
Region retRegion = new Region(regionHash);
HashSet<object> nodesSet =
ByteConverter.GetObjectsFromBytes(bytes.SubArray(ByteSizeEmpty, bytes.Length - ByteSizeEmpty));
foreach(object node in nodesSet)
retRegion.AddNode((Node)node);
return retRegion;
}
}
}
}