145 lines
5.0 KiB
C#
145 lines
5.0 KiB
C#
namespace OSMImporter
|
|
{
|
|
public class Connection
|
|
{
|
|
public ulong endNodeId { 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)
|
|
{
|
|
this.endNodeId = endNodeId;
|
|
}
|
|
|
|
public Connection(ulong endNodeId, Dictionary<string, string> tags)
|
|
{
|
|
this.endNodeId = endNodeId;
|
|
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;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Value 1: endId (4bytes)
|
|
* Value 2: tagsCount (1byte)
|
|
* Value x: tag + value (2bytes)
|
|
*/
|
|
public byte[] ToByte()
|
|
{
|
|
byte[] ret = new byte[sizeof(ulong) + 1 + this._tags.Count * 2];
|
|
|
|
int offset = 0;
|
|
Buffer.BlockCopy(new []{ this.endNodeId }, 0, ret, offset, sizeof(ulong) );
|
|
offset += sizeof(ulong);
|
|
|
|
byte tagsAmount = Convert.ToByte(this._tags.Count);
|
|
Buffer.SetByte(ret, offset++, tagsAmount);
|
|
|
|
foreach (KeyValuePair<tagType, object> tag in this._tags)
|
|
{
|
|
Buffer.BlockCopy(new byte[]{(byte)tag.Key, (byte)tag.Value}, 0, ret,offset, 2);
|
|
offset += 2;
|
|
}
|
|
return ret;
|
|
}
|
|
}
|
|
} |