AStar/OpenStreetMap Importer/Importer.cs

132 lines
4.7 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 16:02:49 +02:00
public static Dictionary<UInt64, Node> Import(Logger logger)
2022-05-05 15:49:28 +02:00
{
XmlReader reader = XmlReader.Create(new MemoryStream(OSM_Data.map));
2022-05-05 15:49:28 +02:00
reader.MoveToContent();
Dictionary<UInt64, Node> nodes = new Dictionary<UInt64, Node>();
nodeType currentNodeType = nodeType.NULL;
Node nullNode = new Node();
Node currentNode = nullNode;
Way currentWay = new Way();
while (reader.Read())
{
if (reader.Name == "node" && reader.IsStartElement())
{
currentNodeType = nodeType.NODE;
nodes.Add(
Convert.ToUInt64(reader.GetAttribute("id")),
new Node(
Convert.ToSingle(reader.GetAttribute("lat")),
Convert.ToSingle(reader.GetAttribute("lon"))
)
);
}
else if (reader.Name == "way")
{
if(currentNodeType == nodeType.WAY)
{
ImportWay(currentWay);
2022-05-05 16:00:56 +02:00
logger.log(loglevel.INFO, "Way nodes: {0}", currentWay.nodes.Count);
2022-05-05 15:49:28 +02:00
}
currentNodeType = nodeType.WAY;
currentNode = nullNode;
currentWay = new Way();
reader.GetAttribute("id");
}else if (reader.Name == "nd" && currentNodeType == nodeType.WAY){
UInt64 id = Convert.ToUInt64(reader.GetAttribute("ref"));
if (!nodes.TryGetValue(id, out currentNode))
{
2022-05-05 16:00:56 +02:00
logger.log(loglevel.DEBUG, "Node with id {0} not imported.", id);
2022-05-05 15:49:28 +02:00
}
else
{
currentWay.nodes.Add(currentNode);
}
}else if (reader.Name == "tag")
{
if(currentNodeType == nodeType.WAY)
{
string value = reader.GetAttribute("v");
switch (reader.GetAttribute("k"))
{
/*case "highway":
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;*/
}
}
}
}
2022-05-05 16:00:56 +02:00
logger.log(loglevel.INFO, "Loaded. Nodes: {0}", nodes.Count);
2022-05-05 16:02:49 +02:00
return nodes;
2022-05-05 15:49:28 +02:00
}
internal static void ImportWay(Way way)
{
if (way.direction == Way.wayDirection.forward)
{
for(int index = 0; index < way.nodes.Count - 1; index++)
{
way.nodes[index].edges.Add(new Edge(way.nodes[index + 1]));
if (!way.oneway)
way.nodes[index+1].edges.Add(new Edge(way.nodes[index]));
}
}
else
{
for (int index = way.nodes.Count-1; index > 1; index--)
{
way.nodes[index].edges.Add(new Edge(way.nodes[index - 1]));
if (!way.oneway)
way.nodes[index - 1].edges.Add(new Edge(way.nodes[index]));
}
}
}
internal enum nodeType { NODE, WAY, NULL }
internal struct Way
{
public List<Node> nodes;
public bool oneway;
public wayDirection direction;
public enum wayDirection { forward, backward }
public Way()
{
this.nodes = new List<Node>();
this.oneway = false;
this.direction = wayDirection.forward;
}
}
}
}