OSMServer/OSMDatastructure/Tag.cs

156 lines
4.9 KiB
C#

namespace OSMDatastructure.Graph;
[Serializable]
public class Tag
{
public TagType key { get; }
public dynamic value { get; }
public Tag(TagType key, dynamic value)
{
this.key = key;
this.value = value;
}
public static Tag FromBytes(byte[] bytes)
{
TagType type = (TagType)bytes[0];
dynamic value = false;
switch (type)
{
case TagType.highway:
case TagType.oneway:
case TagType.forward:
value = BitConverter.ToBoolean(bytes, 1);
break;
case TagType.maxspeed:
value = bytes[1];
break;
}
return new Tag(type, value);
}
public static Tag? ConvertToTag(string key, string value)
{
switch (key)
{
case "highway":
try
{
return new Tag(TagType.highway, (WayType)Enum.Parse(typeof(WayType), value, true));
}
catch (ArgumentException)
{
return new Tag(TagType.highway, WayType.unclassified);
}
case "maxspeed":
try
{
byte speed = Convert.ToByte(value);
if (speed == 255)
return new Tag(TagType.highway, false);
else
return new Tag(TagType.maxspeed, speed);
}
catch (Exception)
{
//Console.WriteLine(e);
//Console.WriteLine("Continuing...");
return new Tag(TagType.maxspeed, byte.MaxValue);
}
case "oneway":
switch (value)
{
case "yes":
return new Tag(TagType.oneway, true);
case "-1":
return new Tag(TagType.forward, false);
case "no":
return new Tag(TagType.oneway, false);
}
break;
}
return null;
}
public override string ToString()
{
return $"TAG {key.ToString()} {value.ToString()}";
}
public enum TagType : byte
{
highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id
}
public static readonly Dictionary<WayType, byte> defaultSpeedCar = new() {
{ WayType.NONE, 1 },
{ 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, 1 },
{ WayType.pedestrian, 0 },
{ WayType.track, 15 },
{ WayType.bus_guideway, 0 },
{ WayType.escape, 0 },
{ WayType.raceway, 0 },
{ WayType.road, 20 },
{ 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 readonly Dictionary<WayType, byte> defaultSpeedPedestrian = new() {
{ WayType.NONE, 0 },
{ WayType.motorway, 0 },
{ WayType.trunk, 0 },
{ WayType.primary, 0 },
{ WayType.secondary, 0 },
{ WayType.tertiary, 2 },
{ WayType.unclassified, 1 },
{ WayType.residential, 4 },
{ 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, 1 },
{ WayType.bus_guideway, 0 },
{ WayType.escape, 1 },
{ 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 }
}