2022-05-05 15:49:28 +02:00
|
|
|
|
using Logging;
|
|
|
|
|
using System.Xml;
|
2022-05-05 16:12:40 +02:00
|
|
|
|
using Graph;
|
2022-05-05 15:49:28 +02:00
|
|
|
|
|
2022-05-05 16:12:40 +02:00
|
|
|
|
namespace OpenStreetMap_Importer
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
|
|
|
|
public class Importer
|
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
|
2022-10-31 23:10:42 +01:00
|
|
|
|
private static XmlReaderSettings readerSettings = new()
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-10-31 23:10:42 +01:00
|
|
|
|
IgnoreWhitespace = true,
|
|
|
|
|
IgnoreComments = true
|
|
|
|
|
};
|
2022-05-05 15:49:28 +02:00
|
|
|
|
|
2022-10-31 23:10:42 +01:00
|
|
|
|
public static Dictionary<ulong, Node> Import(string filePath = "", Logger ?logger = null)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<ulong, Node> nodes;
|
2022-05-05 15:49:28 +02:00
|
|
|
|
|
2022-05-05 23:47:15 +02:00
|
|
|
|
/*
|
|
|
|
|
* First iteration
|
2022-05-11 20:41:47 +02:00
|
|
|
|
* Import "ways" with a tag "highway"
|
|
|
|
|
* Count occurances of "nodes" to find junctions
|
2022-05-05 23:47:15 +02:00
|
|
|
|
*/
|
2022-05-11 22:18:13 +02:00
|
|
|
|
|
2022-10-31 23:10:42 +01:00
|
|
|
|
Stream mapData = File.Exists(filePath) ? new FileStream(filePath, FileMode.Open, FileAccess.Read) : new MemoryStream(OSM_Data.map);
|
|
|
|
|
Dictionary<ulong, ushort> occuranceCount = new();
|
2022-11-01 02:02:38 +01:00
|
|
|
|
List<Way> ways = GetWays(mapData, ref occuranceCount, logger);
|
2022-10-31 23:10:42 +01:00
|
|
|
|
mapData.Close();
|
|
|
|
|
logger?.Log(LogLevel.DEBUG, "Loaded Ways: {0} Required Nodes: {1}", ways.Count, occuranceCount.Count);
|
2022-05-05 15:49:28 +02:00
|
|
|
|
|
2022-05-05 23:47:15 +02:00
|
|
|
|
/*
|
|
|
|
|
* Second iteration
|
|
|
|
|
* Import nodes that are needed by the "ways"
|
|
|
|
|
*/
|
2022-10-31 23:10:42 +01:00
|
|
|
|
mapData = File.Exists(filePath) ? new FileStream(filePath, FileMode.Open, FileAccess.Read) : new MemoryStream(OSM_Data.map);
|
|
|
|
|
nodes = ImportNodes(mapData, occuranceCount, logger);
|
|
|
|
|
mapData.Close();
|
|
|
|
|
|
|
|
|
|
|
2022-05-05 23:47:15 +02:00
|
|
|
|
/*
|
2022-05-11 20:41:47 +02:00
|
|
|
|
* Add connections between nodes (only junctions, e.g. nodes are referenced more than once)
|
|
|
|
|
* Remove non-junction nodes
|
2022-05-05 23:47:15 +02:00
|
|
|
|
*/
|
2022-05-11 22:18:13 +02:00
|
|
|
|
logger?.Log(LogLevel.INFO, "Calculating Edges and distances...");
|
2022-05-05 23:47:15 +02:00
|
|
|
|
ulong edges = 0;
|
|
|
|
|
foreach(Way way in ways)
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-11 20:41:47 +02:00
|
|
|
|
Node junction1 = nodes[way.nodeIds[0]];
|
|
|
|
|
Node junction2;
|
2022-10-31 23:10:42 +01:00
|
|
|
|
float weight = 0;
|
2022-05-11 21:30:54 +02:00
|
|
|
|
//Iterate Node-ids in current way forwards or backwards (depending on way.direction)
|
2022-11-01 02:02:38 +01:00
|
|
|
|
if (way.IsForward())
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-11 21:30:54 +02:00
|
|
|
|
for (int index = 0; index < way.nodeIds.Count - 1; index++)
|
2022-05-05 23:47:15 +02:00
|
|
|
|
{
|
|
|
|
|
Node currentNode = nodes[way.nodeIds[index]];
|
2022-05-11 21:30:54 +02:00
|
|
|
|
Node nextNode = nodes[way.nodeIds[index + 1]];
|
|
|
|
|
weight += Utils.DistanceBetweenNodes(currentNode, nextNode);
|
2022-10-31 23:10:42 +01:00
|
|
|
|
if (occuranceCount[way.nodeIds[index + 1]] > 1 || index == way.nodeIds.Count - 2)
|
2022-05-11 20:41:47 +02:00
|
|
|
|
{
|
2022-05-11 21:30:54 +02:00
|
|
|
|
/*
|
|
|
|
|
* If Node is referenced more than once => Junction
|
|
|
|
|
* If Node is last node of way => Junction
|
|
|
|
|
* Add an edge between two junctions
|
|
|
|
|
*/
|
|
|
|
|
junction2 = nodes[way.nodeIds[index + 1]];
|
2022-05-11 20:41:47 +02:00
|
|
|
|
junction1.edges.Add(new Edge(junction2, weight));
|
2022-05-11 21:30:54 +02:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index + 1]);
|
|
|
|
|
edges++;
|
2022-05-11 20:41:47 +02:00
|
|
|
|
|
2022-11-01 02:02:38 +01:00
|
|
|
|
if (!way.IsOneWay())
|
2022-05-11 20:41:47 +02:00
|
|
|
|
{
|
|
|
|
|
junction2.edges.Add(new Edge(junction1, weight));
|
2022-05-11 21:30:54 +02:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index + 1], weight, way.nodeIds[index]);
|
2022-05-11 20:41:47 +02:00
|
|
|
|
edges++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
junction1 = junction2;
|
|
|
|
|
weight = 0;
|
|
|
|
|
}
|
2022-05-05 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-11 21:30:54 +02:00
|
|
|
|
for (int index = way.nodeIds.Count - 2; index > 0; index--)
|
2022-05-05 23:47:15 +02:00
|
|
|
|
{
|
|
|
|
|
Node currentNode = nodes[way.nodeIds[index]];
|
2022-05-11 21:30:54 +02:00
|
|
|
|
Node nextNode = nodes[way.nodeIds[index - 1]];
|
|
|
|
|
weight += Utils.DistanceBetweenNodes(currentNode, nextNode);
|
2022-10-31 23:10:42 +01:00
|
|
|
|
if (occuranceCount[way.nodeIds[index - 1]] > 1 || index == 1)
|
2022-05-11 20:41:47 +02:00
|
|
|
|
{
|
2022-05-11 21:30:54 +02:00
|
|
|
|
/*
|
|
|
|
|
* If Node is referenced more than once => Junction
|
|
|
|
|
* If Node is last node of way => Junction
|
|
|
|
|
* Add an edge between two junctions
|
|
|
|
|
*/
|
|
|
|
|
junction2 = nodes[way.nodeIds[index - 1]];
|
2022-05-11 20:41:47 +02:00
|
|
|
|
junction1.edges.Add(new Edge(junction2, weight));
|
2022-05-11 21:30:54 +02:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index - 1]);
|
|
|
|
|
edges++;
|
2022-05-11 20:41:47 +02:00
|
|
|
|
|
2022-11-01 02:02:38 +01:00
|
|
|
|
if (!way.IsOneWay())
|
2022-05-11 20:41:47 +02:00
|
|
|
|
{
|
|
|
|
|
junction2.edges.Add(new Edge(junction1, weight));
|
2022-05-11 21:30:54 +02:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index - 1], weight, way.nodeIds[index]);
|
2022-05-11 20:41:47 +02:00
|
|
|
|
edges++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
junction1 = junction2;
|
|
|
|
|
weight = 0;
|
|
|
|
|
}
|
2022-05-05 23:47:15 +02:00
|
|
|
|
}
|
2022-05-05 15:49:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 22:18:13 +02:00
|
|
|
|
logger?.Log(LogLevel.DEBUG, "Loaded Edges: {0}", edges);
|
2022-10-31 23:10:42 +01:00
|
|
|
|
return nodes.Where(node => occuranceCount[node.Key] > 1).ToDictionary(node => node.Key, node => node.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<Way> GetWays(Stream mapData, ref Dictionary<ulong, ushort> occuranceCount, Logger? logger = null)
|
|
|
|
|
{
|
|
|
|
|
List<Way> _ways = new();
|
|
|
|
|
bool _isWay = false;
|
|
|
|
|
Way _currentWay = new();
|
|
|
|
|
|
|
|
|
|
XmlReader _reader = XmlReader.Create(mapData, readerSettings);
|
|
|
|
|
_reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
logger?.Log(LogLevel.INFO, "Importing ways and counting nodes...");
|
|
|
|
|
while (_reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (_reader.Name == "way" && _reader.IsStartElement())
|
|
|
|
|
{
|
2022-11-01 02:02:38 +01:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "WAY {0} nodes {1}", _currentWay.GetHighwayType().ToString(), _currentWay.nodeIds.Count);
|
|
|
|
|
if (_currentWay.GetHighwayType() != Way.type.NONE)
|
2022-10-31 23:10:42 +01:00
|
|
|
|
{
|
|
|
|
|
_ways.Add(_currentWay);
|
|
|
|
|
foreach (ulong id in _currentWay.nodeIds)
|
|
|
|
|
if (occuranceCount.ContainsKey(id))
|
|
|
|
|
occuranceCount[id]++;
|
|
|
|
|
else
|
|
|
|
|
occuranceCount.TryAdd(id, 1);
|
|
|
|
|
}
|
|
|
|
|
_isWay = true;
|
|
|
|
|
_currentWay = new Way();
|
|
|
|
|
}
|
|
|
|
|
else if (_reader.Name == "tag" && _isWay)
|
|
|
|
|
{
|
|
|
|
|
#pragma warning disable CS8600 //tags will always have a value and key
|
|
|
|
|
#pragma warning disable CS8604
|
|
|
|
|
string value = _reader.GetAttribute("v");
|
|
|
|
|
string key = _reader.GetAttribute("k");
|
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "TAG {0} {1}", key, value);
|
|
|
|
|
#pragma warning restore CS8600
|
2022-11-01 02:02:38 +01:00
|
|
|
|
_currentWay.AddTag(key, value);
|
2022-10-31 23:10:42 +01:00
|
|
|
|
#pragma warning restore CS8604
|
|
|
|
|
}
|
|
|
|
|
else if (_reader.Name == "nd" && _isWay)
|
|
|
|
|
{
|
|
|
|
|
ulong id = Convert.ToUInt64(_reader.GetAttribute("ref"));
|
|
|
|
|
_currentWay.nodeIds.Add(id);
|
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "nd: {0}", id);
|
|
|
|
|
}
|
|
|
|
|
else if (_reader.Name == "node")
|
|
|
|
|
{
|
|
|
|
|
_isWay = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_reader.Close();
|
|
|
|
|
GC.Collect();
|
|
|
|
|
return _ways;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Dictionary<ulong, Node> ImportNodes(Stream mapData, Dictionary<ulong, ushort> occuranceCount, Logger? logger = null)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<ulong, Node> nodes = new();
|
|
|
|
|
XmlReader reader = XmlReader.Create(mapData, readerSettings);
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
logger?.Log(LogLevel.INFO, "Importing nodes...");
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (reader.Name == "node")
|
|
|
|
|
{
|
|
|
|
|
ulong id = Convert.ToUInt64(reader.GetAttribute("id"));
|
|
|
|
|
if (occuranceCount.ContainsKey(id))
|
|
|
|
|
{
|
|
|
|
|
#pragma warning disable CS8602 //node will always have a lat and lon
|
|
|
|
|
float lat = Convert.ToSingle(reader.GetAttribute("lat").Replace('.', ','));
|
|
|
|
|
float lon = Convert.ToSingle(reader.GetAttribute("lon").Replace('.', ','));
|
|
|
|
|
#pragma warning restore CS8602
|
|
|
|
|
nodes.TryAdd(id, new Node(lat, lon));
|
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "NODE {0} {1} {2} {3}", id, lat, lon, occuranceCount[id]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
reader.Close();
|
|
|
|
|
GC.Collect();
|
|
|
|
|
return nodes;
|
2022-05-05 23:47:15 +02:00
|
|
|
|
}
|
2022-05-05 15:49:28 +02:00
|
|
|
|
|
|
|
|
|
internal struct Way
|
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
public List<ulong> nodeIds;
|
2022-11-01 02:02:38 +01:00
|
|
|
|
private Dictionary<string, object> tags;
|
|
|
|
|
|
2022-10-31 23:10:42 +01:00
|
|
|
|
|
2022-11-01 02:02:38 +01:00
|
|
|
|
public Dictionary<type, int> speed = new() {
|
|
|
|
|
{ type.NONE, 1 },
|
|
|
|
|
{ type.motorway, 130 },
|
|
|
|
|
{ type.trunk, 125 },
|
|
|
|
|
{ type.primary, 110 },
|
|
|
|
|
{ type.secondary, 100 },
|
|
|
|
|
{ type.tertiary, 90 },
|
|
|
|
|
{ type.unclassified, 40 },
|
|
|
|
|
{ type.residential, 20 },
|
|
|
|
|
{ type.motorway_link, 50 },
|
|
|
|
|
{ type.trunk_link, 50 },
|
|
|
|
|
{ type.primary_link, 30 },
|
|
|
|
|
{ type.secondary_link, 25 },
|
|
|
|
|
{ type.tertiary_link, 25 },
|
|
|
|
|
{ type.living_street, 20 },
|
|
|
|
|
{ type.service, 10 },
|
|
|
|
|
{ type.pedestrian, 10 },
|
|
|
|
|
{ type.track, 1 },
|
|
|
|
|
{ type.bus_guideway, 5 },
|
|
|
|
|
{ type.escape, 1 },
|
|
|
|
|
{ type.raceway, 1 },
|
|
|
|
|
{ type.road, 25 },
|
|
|
|
|
{ type.busway, 5 },
|
|
|
|
|
{ type.footway, 1 },
|
|
|
|
|
{ type.bridleway, 1 },
|
|
|
|
|
{ type.steps, 1 },
|
|
|
|
|
{ type.corridor, 1 },
|
|
|
|
|
{ type.path, 10 },
|
|
|
|
|
{ type.cycleway, 5 },
|
|
|
|
|
{ type.construction, 1 }
|
2022-10-31 23:10:42 +01:00
|
|
|
|
};
|
2022-11-01 02:02:38 +01:00
|
|
|
|
public enum type { 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 }
|
2022-05-11 20:13:19 +02:00
|
|
|
|
|
|
|
|
|
|
2022-05-05 15:49:28 +02:00
|
|
|
|
public Way()
|
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
this.nodeIds = new List<ulong>();
|
2022-11-01 02:02:38 +01:00
|
|
|
|
this.tags = new();
|
2022-05-05 15:49:28 +02:00
|
|
|
|
}
|
2022-11-01 02:02:38 +01:00
|
|
|
|
public void AddTag(string key, string value, Logger? logger = null)
|
2022-05-05 23:47:15 +02:00
|
|
|
|
{
|
2022-11-01 02:02:38 +01:00
|
|
|
|
switch (key)
|
2022-05-05 23:47:15 +02:00
|
|
|
|
{
|
2022-11-01 02:02:38 +01:00
|
|
|
|
case "highway":
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this.tags.Add(key, (type)Enum.Parse(typeof(type), value, true));
|
|
|
|
|
if (this.GetMaxSpeed().Equals((int)type.NONE))
|
|
|
|
|
{
|
|
|
|
|
this.tags["maxspeed"] = (int)this.GetHighwayType();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
this.tags.Add(key, type.NONE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "maxspeed":
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (this.tags.ContainsKey("maxspeed"))
|
|
|
|
|
this.tags["maxspeed"] = Convert.ToInt32(value);
|
|
|
|
|
else
|
|
|
|
|
this.tags.Add(key, Convert.ToInt32(value));
|
|
|
|
|
}
|
|
|
|
|
catch (FormatException)
|
|
|
|
|
{
|
|
|
|
|
this.tags.Add(key, (int)this.GetHighwayType());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "oneway":
|
|
|
|
|
switch (value)
|
|
|
|
|
{
|
|
|
|
|
case "yes":
|
|
|
|
|
this.tags.Add(key, true);
|
|
|
|
|
break;
|
|
|
|
|
case "-1":
|
|
|
|
|
this.tags.Add("forward", false);
|
|
|
|
|
break;
|
|
|
|
|
case "no":
|
|
|
|
|
this.tags.Add(key, false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "Tag {0} - {1} was not added.", key, value);
|
|
|
|
|
break;
|
2022-05-05 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-01 02:02:38 +01:00
|
|
|
|
|
|
|
|
|
public type GetHighwayType()
|
|
|
|
|
{
|
|
|
|
|
return this.tags.ContainsKey("highway") ? (type)this.tags["highway"] : type.NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsOneWay()
|
|
|
|
|
{
|
|
|
|
|
return this.tags.ContainsKey("oneway") ? (bool)this.tags["oneway"] : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetMaxSpeed()
|
|
|
|
|
{
|
|
|
|
|
return this.tags.ContainsKey("maxspeed") ? (int)this.tags["maxspeed"] : (int)this.GetHighwayType();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsForward()
|
|
|
|
|
{
|
|
|
|
|
return this.tags.ContainsKey("forward") ? (bool)this.tags["forward"] : true;
|
|
|
|
|
}
|
2022-05-05 16:23:00 +02:00
|
|
|
|
}
|
2022-05-05 15:49:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|