2023-02-06 17:32:55 +01:00
|
|
|
using System.Text;
|
|
|
|
|
2023-02-03 23:33:43 +01:00
|
|
|
namespace OSMDatastructure;
|
2023-02-03 21:13:51 +01:00
|
|
|
|
|
|
|
public static class ByteConverter
|
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
#region Region
|
2023-02-03 21:13:51 +01:00
|
|
|
/*
|
2023-02-06 17:32:55 +01:00
|
|
|
* | regionHash | Nodes |
|
|
|
|
* |---------------+---------------+
|
|
|
|
* | 8 bytes |
|
|
|
|
* | ulong |
|
2023-02-03 21:13:51 +01:00
|
|
|
*/
|
2023-02-06 17:32:55 +01:00
|
|
|
public static byte[] GetBytes(Region region)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
int totalNodeBytes = 0;
|
|
|
|
HashSet<byte[]> nodes = new();
|
|
|
|
foreach (OsmNode node in region.nodes)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
byte[] nodeBytes = GetBytes(node);
|
|
|
|
totalNodeBytes += nodeBytes.Length;
|
|
|
|
nodes.Add(nodeBytes);
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
byte[] retBytes = new byte[sizeof(ulong) + totalNodeBytes];
|
|
|
|
Array.Copy(BitConverter.GetBytes(region.regionHash), 0, retBytes, 0, sizeof(ulong));
|
|
|
|
int offset = sizeof(ulong);
|
|
|
|
foreach (byte[] node in nodes)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
Array.Copy(node, 0, retBytes, offset, node.Length);
|
|
|
|
offset += node.Length;
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
2023-02-06 17:32:55 +01:00
|
|
|
|
|
|
|
return retBytes;
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
public static Region ToRegion(byte[] bytes)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
Region retRegion = new Region(BitConverter.ToUInt64(bytes, 0));
|
|
|
|
int offset = sizeof(ulong);
|
2023-02-03 21:13:51 +01:00
|
|
|
while (offset < bytes.Length)
|
|
|
|
{
|
|
|
|
int size = BitConverter.ToInt32(bytes, offset);
|
2023-02-06 17:32:55 +01:00
|
|
|
byte[] nodeBytes = new byte[size];
|
|
|
|
Array.Copy(bytes, offset, nodeBytes, 0, size);
|
|
|
|
offset += size;
|
|
|
|
retRegion.nodes.Add(ToNode(nodeBytes));
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
return retRegion;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Coordinates
|
|
|
|
|
|
|
|
/*
|
|
|
|
* | Latitude | Longitude |
|
|
|
|
* |---------------+---------------+
|
|
|
|
* | 4 bytes | 4 bytes |
|
|
|
|
* | float | float |
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static byte[] GetBytes(Coordinates coordinates)
|
|
|
|
{
|
|
|
|
byte[] retBytes = new byte[8];
|
|
|
|
Array.Copy(BitConverter.GetBytes(coordinates.latitude), 0, retBytes, 0, sizeof(float));
|
|
|
|
Array.Copy(BitConverter.GetBytes(coordinates.longitude), 0, retBytes, sizeof(float), sizeof(float));
|
|
|
|
return retBytes;
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
2023-02-06 17:32:55 +01:00
|
|
|
|
|
|
|
public static Coordinates ToCoordinates(byte[] bytes)
|
|
|
|
{
|
|
|
|
if (bytes.Length != 8)
|
|
|
|
throw new ArgumentException("Needs exactly 8 bytes", nameof(bytes));
|
|
|
|
float latitude = BitConverter.ToSingle(bytes, 0);
|
|
|
|
float longitude = BitConverter.ToSingle(bytes, sizeof(float));
|
|
|
|
return new Coordinates(latitude, longitude);
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:13:51 +01:00
|
|
|
#endregion
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
#region OsmNode
|
|
|
|
|
2023-02-03 21:13:51 +01:00
|
|
|
/*
|
2023-02-06 17:32:55 +01:00
|
|
|
* | Size | Coordinates | Edges
|
|
|
|
* |---------------+---------------+---------------+
|
|
|
|
* | 4 bytes | 8 bytes |
|
|
|
|
* | int | byte[] |
|
2023-02-03 21:13:51 +01:00
|
|
|
*/
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
public static byte[] GetBytes(OsmNode node)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-08 18:00:19 +01:00
|
|
|
int totalBytes = sizeof(int) + 8; //size + Coordinates
|
2023-02-06 17:32:55 +01:00
|
|
|
HashSet<byte[]> edges = new();
|
|
|
|
foreach (OsmEdge edge in node.edges)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-08 18:00:19 +01:00
|
|
|
byte[] edgeBytes = GetBytes(edge);
|
|
|
|
edges.Add(edgeBytes);
|
|
|
|
totalBytes += edgeBytes.Length;
|
2023-02-06 17:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
byte[] retBytes = new byte[totalBytes];
|
|
|
|
Array.Copy(BitConverter.GetBytes(totalBytes), retBytes, 4);
|
|
|
|
Array.Copy(GetBytes(node.coordinates), 0, retBytes, 4, 8);
|
|
|
|
int offset = 4 + 8;
|
|
|
|
foreach (byte[] edgeBytes in edges)
|
|
|
|
{
|
|
|
|
Array.Copy(edgeBytes, 0, retBytes, offset, edgeBytes.Length);
|
|
|
|
offset += edgeBytes.Length;
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return retBytes;
|
|
|
|
}
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
public static OsmNode ToNode(byte[] bytes)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
byte[] coordinateBytes = new byte[8];
|
|
|
|
Array.Copy(bytes, 4, coordinateBytes, 0, 8);
|
|
|
|
Coordinates coordinates = ToCoordinates(coordinateBytes);
|
|
|
|
|
|
|
|
OsmNode retNode = new OsmNode(coordinates);
|
|
|
|
|
|
|
|
int offset = sizeof(int) + 8;
|
2023-02-03 21:13:51 +01:00
|
|
|
while (offset < bytes.Length)
|
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
int tagsSize = BitConverter.ToInt32(bytes, offset);
|
|
|
|
byte[] edgeBytes = new byte[sizeof(int) + 8 + tagsSize];
|
|
|
|
Array.Copy(bytes, offset, edgeBytes, 0, edgeBytes.Length);
|
|
|
|
offset += edgeBytes.Length;
|
|
|
|
retNode.edges.Add(ToEdge(edgeBytes));
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
return retNode;
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
2023-02-06 17:32:55 +01:00
|
|
|
|
2023-02-03 21:13:51 +01:00
|
|
|
#endregion
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
#region OsmEdge
|
|
|
|
|
|
|
|
/* Edge:
|
|
|
|
* | tagsSize | neighborCoord | tags
|
|
|
|
* |---------------+---------------+--------------
|
|
|
|
* | 4 bytes | 8 bytes |
|
|
|
|
* | int | byte[] |
|
|
|
|
*
|
2023-02-03 21:13:51 +01:00
|
|
|
*/
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
public static byte[] GetBytes(OsmEdge edge)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
int totalBytes = sizeof(int) + 8; //size + Coordinates
|
|
|
|
int offset = totalBytes;
|
|
|
|
|
|
|
|
int tagsSize = 0;
|
|
|
|
HashSet<byte[]> tags = new();
|
|
|
|
foreach (KeyValuePair<OsmEdge.tagType, object> kv in edge.tags)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
byte[] tagBytes = GetBytes(kv);
|
|
|
|
tags.Add(tagBytes);
|
|
|
|
tagsSize += tagBytes.Length;
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
2023-02-06 17:32:55 +01:00
|
|
|
|
2023-02-08 18:00:19 +01:00
|
|
|
totalBytes += tagsSize;
|
2023-02-06 17:32:55 +01:00
|
|
|
byte[] retBytes = new byte[totalBytes];
|
|
|
|
Array.Copy(BitConverter.GetBytes(tagsSize), retBytes, 4);
|
|
|
|
Array.Copy(GetBytes(edge.neighborCoordinates), 0, retBytes, sizeof(int), 8);
|
|
|
|
|
|
|
|
foreach (byte[] tag in tags)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
Array.Copy(tag, 0, retBytes, offset, tag.Length);
|
|
|
|
offset += tag.Length;
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return retBytes;
|
|
|
|
}
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
public static OsmEdge ToEdge(byte[] bytes)
|
2023-02-03 21:13:51 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
byte[] coordinateBytes = new byte[8];
|
|
|
|
Array.Copy(bytes, 4, coordinateBytes, 0, 8);
|
|
|
|
Coordinates coordinates = ToCoordinates(coordinateBytes);
|
2023-02-05 20:45:33 +01:00
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
OsmEdge retEdge = new OsmEdge(coordinates);
|
2023-02-03 21:13:51 +01:00
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
int offset = sizeof(int) + 8;
|
2023-02-03 21:13:51 +01:00
|
|
|
while (offset < bytes.Length)
|
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
int tagContentSize = BitConverter.ToInt32(bytes, offset);
|
|
|
|
byte[] tagBytes = new byte[sizeof(int) + 1 + tagContentSize];
|
|
|
|
Array.Copy(bytes, offset, tagBytes, 0, tagBytes.Length);
|
|
|
|
offset += tagBytes.Length;
|
|
|
|
KeyValuePair<OsmEdge.tagType, object> tag = ToTag(tagBytes);
|
|
|
|
retEdge.tags.Add(tag.Key, tag.Value);
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
return retEdge;
|
2023-02-03 21:13:51 +01:00
|
|
|
}
|
2023-02-06 17:32:55 +01:00
|
|
|
|
2023-02-03 21:13:51 +01:00
|
|
|
#endregion
|
2023-02-06 17:32:55 +01:00
|
|
|
|
|
|
|
#region OsmEdge.Tag
|
|
|
|
/*
|
|
|
|
* Tag:
|
|
|
|
* | contentSize | tagType | tagContent
|
|
|
|
* |---------------+---------------+---------------
|
|
|
|
* | 4 bytes | 1 byte |
|
|
|
|
* | int | byte |
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static byte[] GetBytes(KeyValuePair<OsmEdge.tagType, object> tag)
|
|
|
|
{
|
2023-02-08 18:00:19 +01:00
|
|
|
byte[] objectBytes;
|
|
|
|
Type objectType = tag.Value.GetType();
|
|
|
|
if (objectType == Type.GetType("System.String"))
|
2023-02-06 17:32:55 +01:00
|
|
|
{
|
|
|
|
objectBytes = Encoding.ASCII.GetBytes((string)tag.Value);
|
|
|
|
}
|
2023-02-08 18:00:19 +01:00
|
|
|
else if (objectType == Type.GetType("System.Boolean"))
|
2023-02-06 17:32:55 +01:00
|
|
|
{
|
|
|
|
objectBytes = BitConverter.GetBytes((bool)tag.Value);
|
|
|
|
}
|
2023-02-08 18:00:19 +01:00
|
|
|
else if (objectType == Type.GetType("System.Int32"))
|
2023-02-06 17:32:55 +01:00
|
|
|
{
|
|
|
|
objectBytes = BitConverter.GetBytes((int)tag.Value);
|
|
|
|
}
|
2023-02-08 18:00:19 +01:00
|
|
|
else if (objectType == Type.GetType("System.UInt64"))
|
2023-02-06 17:32:55 +01:00
|
|
|
{
|
|
|
|
objectBytes = BitConverter.GetBytes((ulong)tag.Value);
|
|
|
|
}
|
2023-02-08 18:00:19 +01:00
|
|
|
else if (objectType == Type.GetType("System.Byte") || objectType.Name == "wayType")
|
2023-02-06 17:32:55 +01:00
|
|
|
{
|
|
|
|
objectBytes = new[] { (byte)tag.Value };
|
|
|
|
}
|
2023-02-08 18:00:19 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception(string.Format("Tag-Bytes object-Type: {0}", tag.Value.GetType()));
|
|
|
|
}
|
2023-02-06 17:32:55 +01:00
|
|
|
|
|
|
|
byte[] retBytes = new byte[sizeof(int) + 1 + objectBytes.Length];
|
|
|
|
Array.Copy(BitConverter.GetBytes(objectBytes.Length), 0, retBytes, 0, sizeof(int));
|
|
|
|
retBytes[sizeof(int)] = (byte)tag.Key;
|
2023-02-08 18:00:19 +01:00
|
|
|
Array.Copy(objectBytes, 0, retBytes, sizeof(int) + 1, objectBytes.Length);
|
2023-02-06 17:32:55 +01:00
|
|
|
return retBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static KeyValuePair<OsmEdge.tagType, object> ToTag(byte[] bytes)
|
|
|
|
{
|
|
|
|
OsmEdge.tagType tagType = (OsmEdge.tagType)bytes[sizeof(int)];
|
|
|
|
int contentSize = BitConverter.ToInt32(bytes, 0);
|
|
|
|
byte[] content = new byte[contentSize];
|
|
|
|
Array.Copy(bytes, sizeof(int) + 1, content, 0, contentSize);
|
|
|
|
|
|
|
|
switch (tagType)
|
|
|
|
{
|
|
|
|
case OsmEdge.tagType.highway:
|
2023-02-07 23:52:23 +01:00
|
|
|
return new KeyValuePair<OsmEdge.tagType, object>(tagType, (OsmEdge.wayType)content[0]);
|
2023-02-06 17:32:55 +01:00
|
|
|
case OsmEdge.tagType.maxspeed:
|
|
|
|
return new KeyValuePair<OsmEdge.tagType, object>(tagType, content[0]);
|
|
|
|
case OsmEdge.tagType.oneway:
|
|
|
|
return new KeyValuePair<OsmEdge.tagType, object>(tagType, BitConverter.ToBoolean(content));
|
|
|
|
case OsmEdge.tagType.forward:
|
|
|
|
return new KeyValuePair<OsmEdge.tagType, object>(tagType, BitConverter.ToBoolean(content));
|
|
|
|
case OsmEdge.tagType.id:
|
|
|
|
return new KeyValuePair<OsmEdge.tagType, object>(tagType, BitConverter.ToUInt64(content));
|
|
|
|
default:
|
|
|
|
return new KeyValuePair<OsmEdge.tagType, object>(tagType, content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
|