AStar/OpenStreetMap Importer/Importer.cs

350 lines
15 KiB
C#
Raw Normal View History

2022-11-01 05:09:17 +01:00
#pragma warning disable CS8600
#pragma warning disable CS8602
#pragma warning disable CS8604
using Logging;
2022-05-05 15:49:28 +02:00
using System.Xml;
using Graph;
2022-11-13 14:02:27 +01:00
using Graph.Utils;
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-11-13 14:02:27 +01:00
public static Graph.Graph Import(string filePath = "", bool onlyJunctions = true, Logger? logger = null)
2022-10-31 23:10:42 +01:00
{
2022-05-05 23:47:15 +02:00
/*
2022-11-01 03:52:23 +01:00
* Count Node occurances when tag is "highway"
2022-05-05 23:47:15 +02:00
*/
2022-11-03 18:16:53 +01:00
logger?.Log(LogLevel.DEBUG, "Opening File...");
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);
2022-11-03 18:16:53 +01:00
logger?.Log(LogLevel.INFO, "Counting Node-Occurances...");
2022-11-01 03:52:23 +01:00
Dictionary<ulong, ushort> occuranceCount = CountNodeOccurances(mapData, logger);
logger?.Log(LogLevel.DEBUG, "Way Nodes: {0}", occuranceCount.Count);
2022-05-05 15:49:28 +02:00
2022-05-05 23:47:15 +02:00
/*
2022-11-01 03:52:23 +01:00
* Import Nodes and Edges
2022-05-05 23:47:15 +02:00
*/
2022-11-03 18:16:53 +01:00
mapData.Position = 0;
2022-11-01 03:52:23 +01:00
logger?.Log(LogLevel.INFO, "Importing Graph...");
2022-11-13 14:02:27 +01:00
Graph.Graph _graph = CreateGraph(mapData, occuranceCount, onlyJunctions, logger);
logger?.Log(LogLevel.DEBUG, "Loaded Nodes: {0}", _graph.GetNodeCount());
2022-11-03 18:16:53 +01:00
2022-10-31 23:10:42 +01:00
mapData.Close();
2022-11-01 03:52:23 +01:00
occuranceCount.Clear();
GC.Collect();
2022-10-31 23:10:42 +01:00
2022-11-13 14:02:27 +01:00
return _graph;
2022-11-01 03:52:23 +01:00
}
2022-10-31 23:10:42 +01:00
2022-11-01 03:52:23 +01:00
private static Dictionary<ulong, ushort> CountNodeOccurances(Stream mapData, Logger? logger = null)
{
Dictionary<ulong, ushort> _occurances = new();
XmlReader _reader = XmlReader.Create(mapData, readerSettings);
XmlReader _wayReader;
_reader.MoveToContent();
bool _isHighway;
List<ulong> _currentIds = new();
while (_reader.ReadToFollowing("way"))
2022-05-05 15:49:28 +02:00
{
2022-11-01 03:52:23 +01:00
_isHighway = false;
_currentIds.Clear();
_wayReader = _reader.ReadSubtree();
2022-11-03 18:16:53 +01:00
logger?.Log(LogLevel.VERBOSE, "WAY: {0}", _reader.GetAttribute("id"));
2022-11-01 03:52:23 +01:00
while (_wayReader.Read())
2022-05-05 15:49:28 +02:00
{
2022-11-01 03:52:23 +01:00
if (_reader.Name == "tag" && _reader.GetAttribute("k").Equals("highway"))
2022-05-05 23:47:15 +02:00
{
2022-11-01 03:52:23 +01:00
try
{
2022-11-01 03:52:23 +01:00
if (!Enum.Parse(typeof(Way.type), _reader.GetAttribute("v"), true).Equals(Way.type.NONE))
_isHighway = true;
2022-11-03 18:16:53 +01:00
logger?.Log(LogLevel.VERBOSE, "Highway: {0}", _reader.GetAttribute("v"));
}
2022-11-01 03:52:23 +01:00
catch (ArgumentException) { };
2022-05-05 23:47:15 +02:00
}
2022-11-01 03:52:23 +01:00
else if(_reader.Name == "nd")
2022-05-05 23:47:15 +02:00
{
2022-11-01 03:52:23 +01:00
try
{
2022-11-01 03:52:23 +01:00
_currentIds.Add(Convert.ToUInt64(_reader.GetAttribute("ref")));
2022-11-03 18:16:53 +01:00
logger?.Log(LogLevel.VERBOSE, "node-ref: {0}", _reader.GetAttribute("ref"));
}
2022-11-01 03:52:23 +01:00
catch (FormatException) { };
2022-05-05 23:47:15 +02:00
}
2022-05-05 15:49:28 +02:00
}
2022-11-01 03:52:23 +01:00
if (_isHighway)
{
foreach(ulong _id in _currentIds)
{
if (!_occurances.TryAdd(_id, 1))
_occurances[_id]++;
}
}
_wayReader.Close();
2022-05-05 15:49:28 +02:00
}
2022-11-01 03:52:23 +01:00
_reader.Close();
GC.Collect();
2022-05-05 15:49:28 +02:00
2022-11-01 03:52:23 +01:00
return _occurances;
2022-10-31 23:10:42 +01:00
}
2022-11-13 14:02:27 +01:00
private static Graph.Graph CreateGraph(Stream mapData, Dictionary<ulong, ushort> occuranceCount, bool onlyJunctions, Logger? logger = null)
2022-10-31 23:10:42 +01:00
{
2022-11-13 14:02:27 +01:00
Graph.Graph _graph = new();
2022-11-01 05:09:17 +01:00
Way _currentWay;
Node _n1, _n2, _currentNode;
2022-11-03 19:14:07 +01:00
float _time, _distance = 0;
2022-10-31 23:10:42 +01:00
XmlReader _reader = XmlReader.Create(mapData, readerSettings);
2022-11-01 03:52:23 +01:00
XmlReader _wayReader;
2022-10-31 23:10:42 +01:00
_reader.MoveToContent();
while (_reader.Read())
{
2022-11-01 03:52:23 +01:00
if(_reader.Name == "node")
2022-10-31 23:10:42 +01:00
{
2022-11-01 03:52:23 +01:00
ulong id = Convert.ToUInt64(_reader.GetAttribute("id"));
if (occuranceCount.ContainsKey(id))
2022-10-31 23:10:42 +01:00
{
2022-11-01 03:52:23 +01:00
float lat = Convert.ToSingle(_reader.GetAttribute("lat").Replace('.', ','));
float lon = Convert.ToSingle(_reader.GetAttribute("lon").Replace('.', ','));
2022-11-13 14:02:27 +01:00
_graph.AddNode(new Node(id, lat, lon));
2022-11-01 03:52:23 +01:00
logger?.Log(LogLevel.VERBOSE, "NODE {0} {1} {2} {3}", id, lat, lon, occuranceCount[id]);
2022-10-31 23:10:42 +01:00
}
}
2022-11-01 03:52:23 +01:00
else if(_reader.Name == "way")
2022-10-31 23:10:42 +01:00
{
2022-11-01 03:52:23 +01:00
_wayReader = _reader.ReadSubtree();
2022-11-01 05:09:17 +01:00
_currentWay = new();
2022-11-03 18:16:53 +01:00
logger?.Log(LogLevel.VERBOSE, "WAY: {0}", _reader.GetAttribute("id"));
2022-11-01 03:52:23 +01:00
while (_wayReader.Read())
{
_wayReader = _reader.ReadSubtree();
2022-11-01 05:09:17 +01:00
_currentWay.AddTag("id", _reader.GetAttribute("id"));
2022-11-01 03:52:23 +01:00
while (_wayReader.Read())
{
if (_reader.Name == "tag")
{
string _value = _reader.GetAttribute("v");
string _key = _reader.GetAttribute("k");
logger?.Log(LogLevel.VERBOSE, "TAG {0} {1}", _key, _value);
_currentWay.AddTag(_key, _value);
}
else if (_reader.Name == "nd")
{
ulong _id = Convert.ToUInt64(_reader.GetAttribute("ref"));
_currentWay.nodeIds.Add(_id);
2022-11-03 18:16:53 +01:00
logger?.Log(LogLevel.VERBOSE, "node-ref: {0}", _id);
2022-11-01 03:52:23 +01:00
}
}
}
_wayReader.Close();
2022-10-31 23:10:42 +01:00
2022-11-01 03:52:23 +01:00
if (!_currentWay.GetHighwayType().Equals(Way.type.NONE))
2022-10-31 23:10:42 +01:00
{
2022-11-03 18:16:53 +01:00
logger?.Log(LogLevel.VERBOSE, "WAY Nodes-count: {0} Type: {1}", _currentWay.nodeIds.Count, _currentWay.GetHighwayType());
2022-11-01 03:52:23 +01:00
if (!onlyJunctions)
{
for (int _nodeIdIndex = 0; _nodeIdIndex < _currentWay.nodeIds.Count - 1; _nodeIdIndex++)
{
2022-11-13 14:02:27 +01:00
_n1 = _graph.GetNode(_currentWay.nodeIds[_nodeIdIndex]);
_n2 = _graph.GetNode(_currentWay.nodeIds[_nodeIdIndex + 1]);
2022-11-03 19:14:07 +01:00
2022-11-13 13:21:24 +01:00
_distance = Convert.ToSingle(Utils.DistanceBetweenNodes(_n1, _n2));
2022-11-03 19:14:07 +01:00
_time = _distance / _currentWay.GetMaxSpeed();
2022-11-01 03:52:23 +01:00
if (!_currentWay.IsOneWay())
{
2022-11-03 19:14:07 +01:00
_n1.edges.Add(new Edge(_n2, _time, _distance, _currentWay.GetId()));
_n2.edges.Add(new Edge(_n1, _time, _distance, _currentWay.GetId()));
2022-11-01 03:52:23 +01:00
}
else if (_currentWay.IsForward())
{
2022-11-03 19:14:07 +01:00
_n1.edges.Add(new Edge(_n2, _time, _distance, _currentWay.GetId()));
2022-11-01 03:52:23 +01:00
}
else
{
2022-11-03 19:14:07 +01:00
_n2.edges.Add(new Edge(_n1, _time, _distance, _currentWay.GetId()));
2022-11-01 03:52:23 +01:00
}
2022-11-03 19:14:07 +01:00
logger?.Log(LogLevel.VERBOSE, "Add Edge: {0} & {1} Weight: {2}", _currentWay.nodeIds[_nodeIdIndex], _currentWay.nodeIds[_nodeIdIndex + 1], _time);
2022-11-01 03:52:23 +01:00
}
}
else
{
2022-11-13 14:02:27 +01:00
_n1 = _graph.GetNode(_currentWay.nodeIds[0]);
2022-11-01 05:09:17 +01:00
_currentNode = _n1;
2022-11-01 03:52:23 +01:00
for(int _nodeIdIndex = 0; _nodeIdIndex < _currentWay.nodeIds.Count - 1; _nodeIdIndex++)
{
2022-11-13 14:02:27 +01:00
_n2 = _graph.GetNode(_currentWay.nodeIds[_nodeIdIndex + 1]);
2022-11-13 13:21:24 +01:00
_distance += Convert.ToSingle(Utils.DistanceBetweenNodes(_currentNode, _n2));
2022-11-01 03:52:23 +01:00
if (occuranceCount[_currentWay.nodeIds[_nodeIdIndex]] > 1 || _nodeIdIndex == _currentWay.nodeIds.Count - 2) //junction found
{
2022-11-03 19:14:07 +01:00
_time = _distance / _currentWay.GetMaxSpeed();
2022-11-01 03:52:23 +01:00
if (!_currentWay.IsOneWay())
{
2022-11-03 19:14:07 +01:00
_n1.edges.Add(new Edge(_n2, _time, _distance, _currentWay.GetId()));
_n2.edges.Add(new Edge(_n1, _time, _distance, _currentWay.GetId()));
2022-11-01 03:52:23 +01:00
}
else if (_currentWay.IsForward())
{
2022-11-03 19:14:07 +01:00
_n1.edges.Add(new Edge(_n2, _time, _distance, _currentWay.GetId()));
2022-11-01 03:52:23 +01:00
}
else
{
2022-11-03 19:14:07 +01:00
_n2.edges.Add(new Edge(_n1, _time, _distance, _currentWay.GetId()));
2022-11-01 03:52:23 +01:00
}
2022-11-03 19:14:07 +01:00
_distance = 0;
logger?.Log(LogLevel.VERBOSE, "Add Edge: {0} & {1} Weight: {2}", _currentWay.nodeIds[_nodeIdIndex], _currentWay.nodeIds[_nodeIdIndex + 1], _time);
2022-11-01 03:52:23 +01:00
}
else
{
2022-11-13 14:02:27 +01:00
_graph.RemoveNode(_currentWay.nodeIds[_nodeIdIndex]); //Not a junction
2022-11-01 03:52:23 +01:00
}
2022-11-01 05:09:17 +01:00
_currentNode = _n2;
2022-11-01 03:52:23 +01:00
}
}
2022-10-31 23:10:42 +01:00
}
}
}
2022-11-01 03:52:23 +01:00
_reader.Close();
2022-10-31 23:10:42 +01:00
GC.Collect();
2022-11-01 03:52:23 +01:00
return _graph;
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-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;
2022-11-01 05:09:17 +01:00
case "id":
this.tags.Add(key, Convert.ToUInt64(value));
break;
2022-11-01 02:02:38 +01:00
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
2022-11-01 05:09:17 +01:00
public ulong GetId()
{
return this.tags.ContainsKey("id") ? (ulong)this.tags["id"] : 0;
}
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
}
}