OSMServer/OSMImporter/Node.cs

62 lines
1.8 KiB
C#
Raw Normal View History

2023-02-02 22:30:43 +01:00
namespace OSMImporter
{
public class Node : Coordinates
{
private readonly HashSet<Connection> _connections = new HashSet<Connection>();
public Node(float lat, float lon) : base(lat, lon)
{
}
public void AddConnection(Connection connection)
{
this._connections.Add(connection);
}
public Connection[] GetConnections()
{
return this._connections.ToArray();
}
/*
* Returns byte array in order:
* Value 1: Latitude (4bytes)
* Value 2: Longitude (4bytes)
* Value 3: Connections-Count (1 byte)
2023-02-03 00:02:04 +01:00
* Value x: Connection
2023-02-02 22:30:43 +01:00
*/
public byte[] ToByte()
{
2023-02-03 00:02:04 +01:00
long countBytes = 0;
2023-02-02 22:30:43 +01:00
float[] coords = { this.lat, this.lon };
2023-02-03 00:02:04 +01:00
countBytes += sizeof(float) * 2;
HashSet<byte[]> byteConnections = new();
foreach (Connection connection in this._connections)
2023-02-02 22:30:43 +01:00
{
2023-02-03 00:02:04 +01:00
byte[] connectionBytes = connection.ToByte();
byteConnections.Add(connectionBytes);
countBytes += connectionBytes.Length;
}
byte connectionsAmount = Convert.ToByte(byteConnections.Count);
countBytes++;
int offset = 0;
byte[] ret = new byte[countBytes];
Buffer.BlockCopy(coords, 0, ret, offset, sizeof(float) * 2);
offset += sizeof(float) * 2;
Buffer.SetByte(ret, offset, connectionsAmount);
offset++;
foreach (byte[] connection in byteConnections)
{
Buffer.BlockCopy(connection, 0, ret, offset, connection.Length);
offset += connection.Length;
2023-02-02 22:30:43 +01:00
}
return ret;
}
}
}