OSMServer/OSMDatastructure/OSMEdge.cs
C9Glax c182818aff enum wayType now typeof byte
prevented waytype.EMPTY in tags
2023-02-08 18:08:42 +01:00

196 lines
6.6 KiB
C#

namespace OSMDatastructure;
public class OsmEdge
{
public Coordinates neighborCoordinates { get; }
public readonly Dictionary<tagType, object> tags = new();
public OsmEdge(float lat, float lon)
{
this.neighborCoordinates = new Coordinates(lat, lon);
this.tags = new();
}
public OsmEdge(Coordinates neighborCoordinates)
{
this.neighborCoordinates = neighborCoordinates;
this.tags = new();
}
public OsmEdge(float lat, float lon, Dictionary<tagType, object> tags)
{
this.neighborCoordinates = new Coordinates(lat, lon);
//To prevent "EMPTY" tags
foreach (KeyValuePair<tagType, object> tag in tags)
{
if(tag.Key != tagType.EMPTY)
this.tags.Add(tag.Key, tag.Value);
}
}
public OsmEdge(Coordinates neighborCoordinates, Dictionary<tagType, object> tags)
{
this.neighborCoordinates = neighborCoordinates;
//To prevent "EMPTY" tags
foreach (KeyValuePair<tagType, object> tag in tags)
{
if(tag.Key != tagType.EMPTY)
this.tags.Add(tag.Key, tag.Value);
}
}
public enum tagType : byte
{
highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id, EMPTY
}
public static Dictionary<wayType, byte> speedcar = new() {
{ wayType.NONE, 0 },
{ wayType.motorway, 110 },
{ wayType.trunk, 100 },
{ wayType.primary, 80 },
{ wayType.secondary, 80 },
{ wayType.tertiary, 70 },
{ wayType.unclassified, 20 },
{ wayType.residential, 10 },
{ wayType.motorway_link, 50 },
{ wayType.trunk_link, 50 },
{ wayType.primary_link, 30 },
{ wayType.secondary_link, 25 },
{ wayType.tertiary_link, 25 },
{ wayType.living_street, 10 },
{ wayType.service, 0 },
{ wayType.pedestrian, 0 },
{ wayType.track, 0 },
{ wayType.bus_guideway, 0 },
{ wayType.escape, 0 },
{ wayType.raceway, 0 },
{ wayType.road, 25 },
{ wayType.busway, 0 },
{ wayType.footway, 0 },
{ wayType.bridleway, 0 },
{ wayType.steps, 0 },
{ wayType.corridor, 0 },
{ wayType.path, 0 },
{ wayType.cycleway, 0 },
{ wayType.construction, 0 }
};
public static Dictionary<wayType, byte> speedped = new() {
{ wayType.NONE, 0 },
{ wayType.motorway, 0 },
{ wayType.trunk, 0 },
{ wayType.primary, 0 },
{ wayType.secondary, 0 },
{ wayType.tertiary, 0 },
{ wayType.unclassified, 1 },
{ wayType.residential, 3 },
{ wayType.motorway_link, 0 },
{ wayType.trunk_link, 0 },
{ wayType.primary_link, 0 },
{ wayType.secondary_link, 0 },
{ wayType.tertiary_link, 0 },
{ wayType.living_street, 5 },
{ wayType.service, 2 },
{ wayType.pedestrian, 5 },
{ wayType.track, 0 },
{ wayType.bus_guideway, 0 },
{ wayType.escape, 0 },
{ wayType.raceway, 0 },
{ wayType.road, 3 },
{ wayType.busway, 0 },
{ wayType.footway, 4 },
{ wayType.bridleway, 1 },
{ wayType.steps, 2 },
{ wayType.corridor, 3 },
{ wayType.path, 4 },
{ wayType.cycleway, 2 },
{ wayType.construction, 0 }
};
public enum wayType : byte { NONE, 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, cycleway, construction }
public enum speedType { pedestrian, car, road }
public void AddTag(string key, string value)
{
KeyValuePair<tagType, object> tag = ConvertToTag(key, value);
if(tag.Key != tagType.EMPTY)
this.tags.Add(tag.Key, tag.Value);
}
public static KeyValuePair<tagType, object> ConvertToTag(string key, string value)
{
switch (key)
{
case "highway":
try
{
return new KeyValuePair<tagType, object>(tagType.highway, (wayType)Enum.Parse(typeof(wayType), value, true));
}
catch (ArgumentException)
{
return new KeyValuePair<tagType, object>(tagType.highway, wayType.unclassified);
}
case "maxspeed":
try
{
return new KeyValuePair<tagType, object>(tagType.maxspeed, Convert.ToByte(value));
}
catch (Exception)
{
//Console.WriteLine(e);
//Console.WriteLine("Continuing...");
return new KeyValuePair<tagType, object>(tagType.maxspeed, byte.MaxValue);
}
case "oneway":
switch (value)
{
case "yes":
return new KeyValuePair<tagType, object>(tagType.oneway, true);
case "-1":
return new KeyValuePair<tagType, object>(tagType.forward, false);
case "no":
return new KeyValuePair<tagType, object>(tagType.oneway, false);
}
break;
case "id":
return new KeyValuePair<tagType, object>(tagType.id, Convert.ToUInt64(value));
}
return new KeyValuePair<tagType, object>(tagType.EMPTY, 0);
}
public ulong GetId()
{
return this.tags.ContainsKey(tagType.id) ? (ulong)this.tags[tagType.id] : 0;
}
public wayType GetHighwayType()
{
return this.tags.ContainsKey(tagType.highway) ? (wayType)this.tags[tagType.highway] : wayType.NONE;
}
public bool IsOneWay()
{
return this.tags.ContainsKey(tagType.oneway) && (bool)this.tags[tagType.oneway];
}
public byte? GetMaxSpeed(speedType type)
{
if(type == speedType.road)
{
return this.tags.ContainsKey(tagType.maxspeed) ? (byte)this.tags[tagType.maxspeed] : null;
}
if(type == speedType.car)
{
return this.tags.ContainsKey(tagType.maxspeed)
? (byte)this.tags[tagType.maxspeed]
: speedcar[this.GetHighwayType()];
}
return speedped[this.GetHighwayType()];
}
public bool IsForward()
{
return this.tags.ContainsKey(tagType.forward) && (bool)this.tags[tagType.forward];
}
}