OSMServer/OsmXmlToRegionConverter/Node.cs

48 lines
1.4 KiB
C#
Raw Normal View History

2023-03-30 16:29:42 +02:00
namespace OsmXmlToRegionConverter;
public class Node
{
public ulong id { get; }
public Coordinates coordinates { get; }
public HashSet<Edge> edges { get; }
public Node(ulong id, float latitude, float longitude)
{
this.id = id;
coordinates = new Coordinates(latitude, longitude);
edges = new HashSet<Edge>();
}
public Node(ulong id, Coordinates coordinates)
{
this.id = id;
this.coordinates = coordinates;
edges = new HashSet<Edge>();
}
public void AddEdge(Edge edge)
{
edges.Add(edge);
}
public const int ByteSizeEmpty = sizeof(int) + sizeof(ulong) + Coordinates.ByteSize;
public static Node FromBytes(byte[] bytes)
{
using (MemoryStream m = new MemoryStream(bytes)) {
using (BinaryReader r = new BinaryReader(m))
{
int edgeCount = r.ReadInt32();
ulong id = r.ReadUInt64();
byte[] coordinateBytes = r.ReadBytes(sizeof(float) * 2);
Coordinates coordinates = Coordinates.FromBytes(coordinateBytes);
Node ret = new Node(id, coordinates);
for (int i = 0; i < edgeCount; i++)
ret.AddEdge(Edge.FromBytes(r.ReadBytes(Edge.ByteSize)));
return ret;
}
}
}
}