2023-02-02 22:30:43 +01:00
|
|
|
using System.Globalization;
|
|
|
|
using System.Text;
|
|
|
|
using System.Xml;
|
2023-02-03 21:13:51 +01:00
|
|
|
using OSMDatastructure;
|
2023-02-02 22:30:43 +01:00
|
|
|
|
|
|
|
namespace OSMImporter
|
|
|
|
{
|
2023-02-05 19:09:17 +01:00
|
|
|
public static class XmlImporter
|
2023-02-02 22:30:43 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
private static readonly XmlReaderSettings readerSettings = new()
|
|
|
|
{
|
|
|
|
IgnoreWhitespace = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
|
|
|
|
|
|
|
private static readonly NumberFormatInfo coordinateFormat = new NumberFormatInfo()
|
|
|
|
{
|
|
|
|
NumberDecimalSeparator = "."
|
|
|
|
};
|
|
|
|
|
|
|
|
public static void Split(string xmlFilePath, string outputFolderPath)
|
|
|
|
{
|
|
|
|
if (!File.Exists(xmlFilePath))
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
FileStream xmlFileStream = File.OpenRead(xmlFilePath);
|
|
|
|
|
|
|
|
Console.WriteLine("Reading ways once...");
|
|
|
|
Dictionary<ulong, Node?> nodes = ReturnNodeIdDictionary(XmlReader.Create(xmlFileStream, readerSettings));
|
|
|
|
|
|
|
|
xmlFileStream.Position = 0;
|
2023-02-05 19:09:17 +01:00
|
|
|
RegionCollection regionCollection = new RegionCollection();
|
2023-02-02 22:30:43 +01:00
|
|
|
|
|
|
|
Console.WriteLine("Reading nodes...");
|
2023-02-05 19:09:17 +01:00
|
|
|
LoadNodesIntoDictionary(ref nodes, XmlReader.Create(xmlFileStream, readerSettings), ref regionCollection);
|
2023-02-02 22:30:43 +01:00
|
|
|
|
|
|
|
xmlFileStream.Position = 0;
|
|
|
|
|
|
|
|
Console.WriteLine("Reading ways twice...");
|
|
|
|
CreateConnections(XmlReader.Create(xmlFileStream, readerSettings), ref nodes);
|
|
|
|
|
|
|
|
Console.WriteLine("Writing...");
|
|
|
|
|
2023-02-05 19:09:17 +01:00
|
|
|
foreach(Region region in regionCollection.GetAllRegions())
|
2023-02-02 22:30:43 +01:00
|
|
|
WriteRegion(region, outputFolderPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Dictionary<ulong, Node?> ReturnNodeIdDictionary(XmlReader xmlReader)
|
|
|
|
{
|
|
|
|
Dictionary<ulong, Node?> retSet = new Dictionary<ulong, Node?>();
|
|
|
|
while (xmlReader.ReadToFollowing("way"))
|
|
|
|
{
|
|
|
|
byte[] byteArray = Encoding.ASCII.GetBytes(xmlReader.ReadOuterXml());
|
|
|
|
MemoryStream wayStream = new MemoryStream(byteArray);
|
|
|
|
XmlReader wayReader = XmlReader.Create(wayStream);
|
|
|
|
bool addNodes = false;
|
|
|
|
while (wayReader.ReadToFollowing("tag") && !addNodes)
|
|
|
|
{
|
|
|
|
if (wayReader.GetAttribute("v")!.Equals("highway"))
|
|
|
|
{
|
|
|
|
addNodes = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addNodes)
|
|
|
|
{
|
|
|
|
wayStream.Position = 0;
|
|
|
|
wayReader = XmlReader.Create(wayStream);
|
|
|
|
while (wayReader.ReadToFollowing("nd"))
|
|
|
|
{
|
|
|
|
retSet.TryAdd(Convert.ToUInt64(wayReader.GetAttribute("ref")!), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlReader.Close();
|
|
|
|
return retSet;
|
|
|
|
}
|
|
|
|
|
2023-02-05 19:09:17 +01:00
|
|
|
private static void LoadNodesIntoDictionary(ref Dictionary<ulong, Node?> nodes, XmlReader xmlReader, ref RegionCollection regionCollection)
|
2023-02-02 22:30:43 +01:00
|
|
|
{
|
|
|
|
while (xmlReader.ReadToFollowing("node"))
|
|
|
|
{
|
|
|
|
ulong id = Convert.ToUInt64(xmlReader.GetAttribute("id"));
|
|
|
|
if (nodes.ContainsKey(id))
|
|
|
|
{
|
|
|
|
float lat = Convert.ToSingle(xmlReader.GetAttribute("lat")!, coordinateFormat);
|
|
|
|
float lon = Convert.ToSingle(xmlReader.GetAttribute("lon")!, coordinateFormat);
|
|
|
|
Node newNode = new Node(lat, lon);
|
|
|
|
nodes[id] = newNode;
|
2023-02-05 19:09:17 +01:00
|
|
|
regionCollection.GetRegion(newNode).AddNode(id, newNode);
|
2023-02-02 22:30:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xmlReader.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void CreateConnections(XmlReader xmlReader, ref Dictionary<ulong, Node?> nodes)
|
|
|
|
{
|
|
|
|
while (xmlReader.ReadToFollowing("way"))
|
|
|
|
{
|
|
|
|
byte[] byteArray = Encoding.ASCII.GetBytes(xmlReader.ReadOuterXml());
|
|
|
|
MemoryStream wayStream = new MemoryStream(byteArray);
|
|
|
|
XmlReader wayReader = XmlReader.Create(wayStream);
|
|
|
|
Dictionary<string, string> tags = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
bool addNodes = false;
|
|
|
|
while (wayReader.ReadToFollowing("tag"))
|
|
|
|
{
|
|
|
|
if (wayReader.GetAttribute("v")!.Equals("highway"))
|
|
|
|
{
|
|
|
|
addNodes = true;
|
|
|
|
}
|
|
|
|
tags.Add(wayReader.GetAttribute("k")!, value: wayReader.GetAttribute("v")!);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (addNodes)
|
|
|
|
{
|
|
|
|
HashSet<ulong> nodesInWay = new HashSet<ulong>();
|
|
|
|
wayStream.Position = 0;
|
|
|
|
wayReader = XmlReader.Create(wayStream);
|
|
|
|
while (wayReader.ReadToFollowing("nd"))
|
|
|
|
{
|
|
|
|
nodesInWay.Add(Convert.ToUInt64(wayReader.GetAttribute("ref")));
|
|
|
|
}
|
|
|
|
ConnectNodesOfWay(nodes, nodesInWay.ToArray(), tags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xmlReader.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ConnectNodesOfWay(Dictionary<ulong, Node?> nodes, ulong[] nodeIds,
|
|
|
|
Dictionary<string, string> tags)
|
|
|
|
{
|
|
|
|
string oneWayString = tags.ContainsKey("oneway") ? tags["oneway"] : "no";
|
|
|
|
bool oneWay = false;
|
|
|
|
bool forward = true;
|
|
|
|
switch (oneWayString)
|
|
|
|
{
|
|
|
|
case "yes":
|
|
|
|
oneWay = true;
|
|
|
|
break;
|
|
|
|
case "-1":
|
|
|
|
forward = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < nodeIds.Length - 1; i++)
|
|
|
|
{
|
|
|
|
if (oneWay)
|
|
|
|
{
|
|
|
|
if(nodes.ContainsKey(nodeIds[i]))
|
2023-02-03 21:13:51 +01:00
|
|
|
nodes[nodeIds[i]]!.AddConnection(new Connection(nodeIds[i + 1], nodes[nodeIds[i + 1]]!, tags));
|
2023-02-02 22:30:43 +01:00
|
|
|
if(nodes.ContainsKey(nodeIds[i + 1]))
|
2023-02-03 21:13:51 +01:00
|
|
|
nodes[nodeIds[i + 1]]!.AddConnection(new Connection(nodeIds[i], nodes[nodeIds[i]]!, tags));
|
2023-02-02 22:30:43 +01:00
|
|
|
}
|
|
|
|
else if (forward)
|
|
|
|
{
|
|
|
|
if(nodes.ContainsKey(nodeIds[i]))
|
2023-02-03 21:13:51 +01:00
|
|
|
nodes[nodeIds[i]]!.AddConnection(new Connection(nodeIds[i + 1], nodes[nodeIds[i + 1]]!, tags));
|
2023-02-02 22:30:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(nodes.ContainsKey(nodeIds[i + 1]))
|
2023-02-03 21:13:51 +01:00
|
|
|
nodes[nodeIds[i + 1]]!.AddConnection(new Connection(nodeIds[i], nodes[nodeIds[i]]!, tags));
|
2023-02-02 22:30:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void WriteRegion(Region region, string outputFolderPath)
|
|
|
|
{
|
|
|
|
if (!Directory.Exists(outputFolderPath))
|
|
|
|
Directory.CreateDirectory(outputFolderPath);
|
|
|
|
string fileName = region.regionHash.ToString();
|
|
|
|
string fullPath = Path.Combine(outputFolderPath, fileName);
|
|
|
|
if (!File.Exists(fullPath))
|
|
|
|
File.Create(fullPath).Close();
|
|
|
|
FileStream fileStream = new FileStream(fullPath, FileMode.Append);
|
2023-02-03 21:13:51 +01:00
|
|
|
fileStream.Write(ByteConverter.GetBytes(region));
|
2023-02-02 22:30:43 +01:00
|
|
|
fileStream.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|