Split into Datastructure, Added ByteConverter, Full Split & Load

This commit is contained in:
2023-02-03 21:13:51 +01:00
parent f31b1b577b
commit 311afcc02f
14 changed files with 332 additions and 187 deletions

View File

@ -0,0 +1,130 @@
namespace OSMDatastructure
{
public class Connection
{
public ulong endNodeId { get; }
public Coordinates endNodeCoordinates { get; }
private readonly Dictionary<tagType, object> _tags = new Dictionary<tagType, object>();
public enum tagType : byte
{
highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, unknown
}
public Connection(ulong endNodeId, Coordinates endNodeCoordinates)
{
this.endNodeId = endNodeId;
this.endNodeCoordinates = endNodeCoordinates;
}
public Connection(ulong endNodeId, Coordinates endNodeCoordinates, Dictionary<string, string> tags)
{
this.endNodeId = endNodeId;
this.endNodeCoordinates = endNodeCoordinates;
ImportTags(tags);
}
public void AddTag(string key, string value)
{
switch (key)
{
case "highway":
if(Enum.TryParse(value, out highwayType hwType))
this._tags.Add(key: tagType.highway, hwType);
break;
case "footway":
if(Enum.TryParse(value, out footwayType fwType))
this._tags.Add(tagType.footway, fwType);
break;
case "oneway":
if (value.Equals("yes", StringComparison.CurrentCultureIgnoreCase))
{
this._tags.Add(tagType.oneway, (byte)1);
}
else if (value.Equals("no", StringComparison.CurrentCultureIgnoreCase))
{
this._tags.Add(tagType.oneway, (byte)0);
}
else if (value.Equals("reversible", StringComparison.CurrentCultureIgnoreCase))
{
this._tags.Add(tagType.oneway, (byte)1);
this._tags.Add(tagType.forward, (byte)0);
}
break;
case "sidewalk":
if(Enum.TryParse(value, out sidewalkSide swType))
this._tags.Add(tagType.footway, swType);
break;
case "cycleway":
if(Enum.TryParse(value, out cyclewayType cwType))
this._tags.Add(tagType.footway, cwType);
break;
case "busway":
if(Enum.TryParse(value, out buswayType bwType))
this._tags.Add(tagType.footway, bwType);
break;
case "maxspeed":
this._tags.Add(tagType.maxspeed, Convert.ToByte(value));
break;
default:
Console.WriteLine("Unknown Tag: {0} Value: {1}", key, value);
break;
}
}
public void AddTag(byte tag, byte value)
{
switch ((tagType)tag)
{
case tagType.highway:
this._tags.Add(tagType.highway, (highwayType)value);
break;
case tagType.footway:
this._tags.Add(tagType.footway, (footwayType)value);
break;
case tagType.oneway:
this._tags.Add(tagType.oneway, value);
break;
case tagType.sidewalk:
this._tags.Add(tagType.sidewalk, (sidewalkSide)value);
break;
case tagType.cycleway:
this._tags.Add(tagType.cycleway, (cyclewayType)value);
break;
case tagType.busway:
this._tags.Add(tagType.busway, (buswayType)value);
break;
case tagType.maxspeed:
this._tags.Add(tagType.maxspeed, value);
break;
}
}
private void ImportTags(Dictionary<string, string> tags)
{
foreach (KeyValuePair<string, string> tag in tags)
{
this.AddTag(tag.Key, tag.Value);
}
}
public object? GetTag(string key)
{
if (Enum.TryParse(key, out tagType tag))
{
return this._tags.ContainsKey(tag) ? this._tags[tag] : null;
}
else
{
return null;
}
}
public Dictionary<tagType, object> GetTags()
{
return this._tags;
}
}
}

View File

@ -0,0 +1,50 @@
using System.Globalization;
namespace OSMDatastructure
{
public class Coordinates
{
public float lat { get; }
public float lon { get; }
public Coordinates(float lat, float lon)
{
this.lat = lat;
this.lon = lon;
}
private const float decimalCoordsSave = 10000;
private const ulong offset = 10000000;
//Latitude maxChars = 7
//Longitude maxChars = 8
public ulong GetHash()
{
ulong latHash = Convert.ToUInt64((this.lat + 90) * decimalCoordsSave);
ulong lonHash = Convert.ToUInt64((this.lon + 180) * decimalCoordsSave);
return latHash * offset + lonHash;
}
public ulong GetRegionHash(float regionSize)
{
float latRegion = this.lat - this.lat % regionSize;
float lonRegion = this.lon - this.lon % regionSize;
ulong latHash = Convert.ToUInt64((latRegion + 90) * decimalCoordsSave);
ulong lonHash = Convert.ToUInt64((lonRegion + 180) * decimalCoordsSave);
return latHash * offset + lonHash;
}
public static Coordinates GetFromHash(ulong hash)
{
ulong latHash = (hash / offset) - 90;
ulong lonHash = (hash % offset) - 180;
float lat = Convert.ToSingle(latHash) / decimalCoordsSave;
float lon = Convert.ToSingle(lonHash) / decimalCoordsSave;
return new Coordinates(lat, lon);
}
public override string ToString()
{
return string.Format("{0} {1}", this.lat.ToString(CultureInfo.InvariantCulture), this.lon.ToString(CultureInfo.InvariantCulture));
}
}
}

22
OSMDatastructure/Node.cs Normal file
View File

@ -0,0 +1,22 @@
namespace OSMDatastructure
{
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();
}
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,35 @@
namespace OSMDatastructure
{
public class Region
{
private readonly Dictionary<ulong, Node> _nodesInRegion = new();
public ulong regionHash { get; }
public Region(Coordinates regionCoordinates, float regionSize)
{
this.regionHash = regionCoordinates.GetRegionHash(regionSize);
}
public Region(ulong nodeId, Node firstNode, float regionSize)
{
this.regionHash = firstNode.GetRegionHash(regionSize);
this._nodesInRegion.Add(nodeId, value: firstNode);
}
public Region(ulong regionHash)
{
this.regionHash = regionHash;
}
public void AddNode(ulong nodeId, Node node)
{
this._nodesInRegion.Add(nodeId, value: node);
}
public Dictionary<ulong, Node> GetNodes()
{
return this._nodesInRegion;
}
}
}

View File

@ -0,0 +1,67 @@
namespace OSMDatastructure
{
public enum highwayType : byte
{
motorway,
trunk,
primary,
secondary,
tertiary,
unclassified,
residential,
motorway_link,
trunk_link,
primary_link,
secondary_link,
tertiary_link,
living_street,
service,
pedestrian,
track,
bus_guideway,
escape,
raceway,
road,
busway,
footway,
bridleway,
steps,
corridor,
path,
via_ferratsa,
cycleway,
proposed,
construction
}
public enum footwayType : byte
{
sidewalk,
crossing
}
public enum cyclewayType : byte
{
lane,
opposite,
opposite_lane,
track,
opposite_track,
share_busway,
opposite_share_busway,
shared_lane
}
public enum sidewalkSide : byte
{
both,
left,
right,
no
}
public enum buswayType : byte
{
lane
}
}