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-05-06 00:43:52 +02:00
|
|
|
|
public static Dictionary<ulong, Node> Import(Logger ?logger = null)
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
List<Way> ways = new List<Way>();
|
|
|
|
|
Dictionary<ulong, Node> nodes = new Dictionary<ulong, Node>();
|
2022-05-05 15:49:28 +02:00
|
|
|
|
|
2022-05-05 23:47:15 +02:00
|
|
|
|
bool wayTag = false;
|
2022-05-05 15:49:28 +02:00
|
|
|
|
Way currentWay = new Way();
|
|
|
|
|
|
2022-05-05 23:47:15 +02:00
|
|
|
|
/*
|
|
|
|
|
* First iteration
|
|
|
|
|
* Import "ways" with a tag "highway" and add the node-ids to the list of nodes
|
|
|
|
|
*/
|
|
|
|
|
XmlReaderSettings readerSettings = new XmlReaderSettings()
|
|
|
|
|
{
|
|
|
|
|
IgnoreWhitespace = true,
|
|
|
|
|
IgnoreComments = true
|
|
|
|
|
};
|
|
|
|
|
XmlReader reader = XmlReader.Create(new MemoryStream(OSM_Data.map), readerSettings);
|
|
|
|
|
reader.MoveToContent();
|
2022-05-05 15:49:28 +02:00
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
if (reader.Name == "way" && reader.IsStartElement())
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-11 19:45:35 +02:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "WAY {0} nodes {1}", currentWay.highway.ToString(), currentWay.nodeIds.Count);
|
2022-05-05 23:47:15 +02:00
|
|
|
|
if (currentWay.highway != Way.highwayType.NONE)
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
ways.Add(currentWay);
|
|
|
|
|
foreach (ulong id in currentWay.nodeIds)
|
2022-05-06 00:02:10 +02:00
|
|
|
|
nodes.TryAdd(id, Node.nullnode);
|
2022-05-05 15:49:28 +02:00
|
|
|
|
}
|
2022-05-05 23:47:15 +02:00
|
|
|
|
wayTag = true;
|
2022-05-05 15:49:28 +02:00
|
|
|
|
currentWay = new Way();
|
2022-05-05 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
else if (reader.Name == "tag" && wayTag)
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-06 00:40:45 +02:00
|
|
|
|
#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");
|
2022-05-11 19:45:35 +02:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "TAG {0} {1}", key, value);
|
2022-05-06 00:40:45 +02:00
|
|
|
|
#pragma warning restore CS8600
|
2022-05-05 23:47:15 +02:00
|
|
|
|
switch (key)
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
case "highway":
|
2022-05-06 00:02:10 +02:00
|
|
|
|
currentWay.SetHighwayType(value);
|
2022-05-05 23:47:15 +02:00
|
|
|
|
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;
|
2022-05-05 15:49:28 +02:00
|
|
|
|
/*case "name":
|
2022-05-05 23:47:15 +02:00
|
|
|
|
|
|
|
|
|
break;*/
|
2022-05-05 15:49:28 +02:00
|
|
|
|
}
|
2022-05-06 00:43:52 +02:00
|
|
|
|
#pragma warning restore CS8604
|
2022-05-06 00:02:10 +02:00
|
|
|
|
}
|
|
|
|
|
else if(reader.Name == "nd" && wayTag)
|
|
|
|
|
{
|
|
|
|
|
ulong id = Convert.ToUInt64(reader.GetAttribute("ref"));
|
|
|
|
|
currentWay.nodeIds.Add(id);
|
|
|
|
|
}
|
2022-05-05 23:47:15 +02:00
|
|
|
|
else if(reader.Name == "node")
|
|
|
|
|
{
|
|
|
|
|
wayTag = false;
|
2022-05-05 15:49:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 19:45:35 +02:00
|
|
|
|
logger?.Log(LogLevel.DEBUG, "Ways: {0} Nodes: {1}", ways.Count, nodes.Count);
|
2022-05-05 15:49:28 +02:00
|
|
|
|
|
2022-05-05 23:47:15 +02:00
|
|
|
|
reader.Close();
|
|
|
|
|
reader = XmlReader.Create(new MemoryStream(OSM_Data.map), readerSettings);
|
|
|
|
|
reader.MoveToContent();
|
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"
|
|
|
|
|
*/
|
|
|
|
|
while (reader.Read())
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
if (reader.Name == "node")
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
ulong id = Convert.ToUInt64(reader.GetAttribute("id"));
|
|
|
|
|
if (nodes.ContainsKey(id))
|
|
|
|
|
{
|
2022-05-06 00:40:45 +02:00
|
|
|
|
#pragma warning disable CS8602 //node will always have a lat and lon
|
2022-05-05 23:47:15 +02:00
|
|
|
|
float lat = Convert.ToSingle(reader.GetAttribute("lat").Replace('.', ','));
|
|
|
|
|
float lon = Convert.ToSingle(reader.GetAttribute("lon").Replace('.', ','));
|
2022-05-06 00:40:45 +02:00
|
|
|
|
#pragma warning restore CS8602
|
2022-05-05 23:47:15 +02:00
|
|
|
|
nodes[id] = new Node(lat, lon);
|
2022-05-11 19:45:35 +02:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "NODE {0} {1} {2}", id, lat, lon);
|
2022-05-05 23:47:15 +02:00
|
|
|
|
}
|
2022-05-05 15:49:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-05 23:47:15 +02:00
|
|
|
|
|
2022-05-11 19:45:35 +02:00
|
|
|
|
logger?.Log(LogLevel.INFO, "Import finished. Calculating distances.");
|
2022-05-05 23:47:15 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Add connections between nodes based on ways and calculate distance
|
|
|
|
|
*/
|
|
|
|
|
ulong edges = 0;
|
|
|
|
|
foreach(Way way in ways)
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
if (way.direction == Way.wayDirection.forward)
|
2022-05-05 15:49:28 +02:00
|
|
|
|
{
|
2022-05-05 23:47:15 +02:00
|
|
|
|
for (int index = 0; index < way.nodeIds.Count - 1; index++)
|
|
|
|
|
{
|
|
|
|
|
Node currentNode = nodes[way.nodeIds[index]];
|
|
|
|
|
Node neighborNode = nodes[way.nodeIds[index + 1]];
|
2022-05-06 00:02:10 +02:00
|
|
|
|
double weight = Utils.DistanceBetweenNodes(currentNode, neighborNode);
|
2022-05-05 23:47:15 +02:00
|
|
|
|
currentNode.edges.Add(new Edge(neighborNode, weight));
|
2022-05-11 19:45:35 +02:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index + 1]);
|
2022-05-05 23:47:15 +02:00
|
|
|
|
edges++;
|
|
|
|
|
if (!way.oneway)
|
|
|
|
|
{
|
|
|
|
|
neighborNode.edges.Add(new Edge(currentNode, weight));
|
|
|
|
|
edges++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int index = way.nodeIds.Count - 1; index > 1; index--)
|
|
|
|
|
{
|
|
|
|
|
Node currentNode = nodes[way.nodeIds[index]];
|
|
|
|
|
Node neighborNode = nodes[way.nodeIds[index - 1]];
|
2022-05-06 00:02:10 +02:00
|
|
|
|
double weight = Utils.DistanceBetweenNodes(currentNode, neighborNode);
|
2022-05-05 23:47:15 +02:00
|
|
|
|
currentNode.edges.Add(new Edge(neighborNode, weight));
|
2022-05-11 19:45:35 +02:00
|
|
|
|
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index - 1]);
|
2022-05-05 23:47:15 +02:00
|
|
|
|
edges++;
|
|
|
|
|
if (!way.oneway)
|
|
|
|
|
{
|
|
|
|
|
neighborNode.edges.Add(new Edge(currentNode, weight));
|
|
|
|
|
edges++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-05 15:49:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-06 00:31:35 +02:00
|
|
|
|
reader.Close();
|
2022-05-05 15:49:28 +02:00
|
|
|
|
|
2022-05-11 19:45:35 +02:00
|
|
|
|
logger?.Log(LogLevel.DEBUG, "Edges: {0}", edges);
|
2022-05-05 23:47:15 +02:00
|
|
|
|
return nodes;
|
|
|
|
|
}
|
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-05-05 23:47:15 +02:00
|
|
|
|
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
|
|
|
|
|
2022-05-06 00:02:10 +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
|
|
|
|
}
|
|
|
|
|
}
|