AStar/OpenStreetMap Importer/Importer.cs

290 lines
12 KiB
C#
Raw Normal View History

2022-05-05 15:49:28 +02:00
using Logging;
using System.Xml;
using Graph;
2022-05-05 15:49:28 +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
* 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();
List<Way> ways = GetWays(mapData, ref occuranceCount);
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
/*
* 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
{
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-05-05 23:47:15 +02:00
if (way.direction == Way.wayDirection.forward)
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 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]];
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++;
if (!way.oneway)
{
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]);
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 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]];
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++;
if (!way.oneway)
{
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]);
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())
{
logger?.Log(LogLevel.VERBOSE, "WAY {0} nodes {1}", _currentWay.highway.ToString(), _currentWay.nodeIds.Count);
if (_currentWay.highway != Way.highwayType.NONE)
{
_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
switch (key)
{
case "highway":
_currentWay.SetHighwayType(value);
break;
case "oneway":
switch (value)
{
case "yes":
_currentWay.oneway = true;
break;
/*case "no":
currentWay.oneway = false;
break;*/
case "-1":
_currentWay.oneway = true;
_currentWay.direction = Way.wayDirection.backward;
break;
}
break;
/*case "name":
break;*/
}
#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-05-05 15:49:28 +02:00
public bool oneway;
public wayDirection direction;
2022-05-05 23:47:15 +02:00
public highwayType highway;
2022-05-05 15:49:28 +02:00
public enum wayDirection { forward, backward }
2022-10-31 23:10:42 +01:00
public Dictionary<highwayType, uint> speed = new() {
{ highwayType.NONE, 1 },
{ highwayType.motorway, 130 },
{ highwayType.trunk, 125 },
{ highwayType.primary, 110 },
{ highwayType.secondary, 100 },
{ highwayType.tertiary, 90 },
{ highwayType.unclassified, 40 },
{ highwayType.residential, 20 },
{ highwayType.motorway_link, 50 },
{ highwayType.trunk_link, 50 },
{ highwayType.primary_link, 30 },
{ highwayType.secondary_link, 25 },
{ highwayType.tertiary_link, 25 },
{ highwayType.living_street, 20 },
{ highwayType.service, 10 },
{ highwayType.pedestrian, 10 },
{ highwayType.track, 1 },
{ highwayType.bus_guideway, 5 },
{ highwayType.escape, 1 },
{ highwayType.raceway, 1 },
{ highwayType.road, 25 },
{ highwayType.busway, 5 },
{ highwayType.footway, 1 },
{ highwayType.bridleway, 1 },
{ highwayType.steps, 1 },
{ highwayType.corridor, 1 },
{ highwayType.path, 10 },
{ highwayType.cycleway, 5 },
{ highwayType.construction, 1 }
};
public enum highwayType { 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-05 15:49:28 +02:00
public Way()
{
2022-05-05 23:47:15 +02:00
this.nodeIds = new List<ulong>();
2022-05-05 15:49:28 +02:00
this.oneway = false;
this.direction = wayDirection.forward;
2022-05-05 23:47:15 +02:00
this.highway = highwayType.NONE;
2022-05-05 15:49:28 +02:00
}
2022-05-05 16:23:00 +02:00
public void SetHighwayType(string waytype)
2022-05-05 23:47:15 +02:00
{
try
{
this.highway = (highwayType)Enum.Parse(typeof(highwayType), waytype, true);
}catch(Exception)
{
this.highway = highwayType.NONE;
}
}
2022-05-05 16:23:00 +02:00
}
2022-05-05 15:49:28 +02:00
}
}