OSMServer/Server/RegionConverter.cs
glax 806dcf98c9 Renamed OsmWay -> OsmEdge again
Prevented duplicate writes of Tags for way
2023-04-01 01:47:56 +02:00

214 lines
10 KiB
C#

using System.Globalization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using OSMDatastructure.Graph;
namespace Server;
public class RegionConverter
{
private static readonly XmlReaderSettings ReaderSettings = new()
{
IgnoreWhitespace = true,
IgnoreComments = true
};
private static readonly NumberFormatInfo decimalInfo = new()
{
NumberDecimalSeparator = "."
};
public const string NodesFileName = "region.nodes";
public const string WaysFileName = "region.ways";
public const string tagsFileName = "region.tags";
public static void ConvertXMLToRegions(string filePath, string outputPath)
{
if (!File.Exists(filePath))
throw new FileNotFoundException();
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);
Console.WriteLine("Converting Nodes...");
FileStream xmlFileStream = new FileStream(filePath, FileMode.Open);
Dictionary<ulong, ulong> nodeIdRegionDict = GetNodesAndRegions(XmlReader.Create(xmlFileStream, ReaderSettings), outputPath);
xmlFileStream.Position = 0;
Console.WriteLine("Converting Ways...");
ImportWays(XmlReader.Create(xmlFileStream, ReaderSettings), nodeIdRegionDict, outputPath);
}
private static Dictionary<ulong, ulong> GetNodesAndRegions(XmlReader xmlReader, string outputPath)
{
BinaryFormatter bFormatter = new BinaryFormatter();
Dictionary<ulong, ulong> nodeRegions = new();
Dictionary<ulong, FileStream> regionFileStreams = new();
Dictionary<ulong, OsmNode> tmpAllNodes = new();
bool isHighway;
HashSet<ulong> currentIds = new();
while (xmlReader.Read())
{
if (xmlReader.Name == "node")
{
ulong id = Convert.ToUInt64(xmlReader.GetAttribute("id")!);
float lat = Convert.ToSingle(xmlReader.GetAttribute("lat")!, decimalInfo);
float lon = Convert.ToSingle(xmlReader.GetAttribute("lon")!, decimalInfo);
tmpAllNodes.TryAdd(id, new OsmNode(id, lat, lon));
}
else if (xmlReader.Name == "way")
{
isHighway = false;
currentIds.Clear();
XmlReader wayReader = xmlReader.ReadSubtree();
while (wayReader.Read())
{
if (xmlReader.Name == "tag" && xmlReader.GetAttribute("k")!.Equals("highway"))
{
isHighway = true;
}
else if (xmlReader.Name == "nd")
{
ulong id = Convert.ToUInt64(xmlReader.GetAttribute("ref")!);
currentIds.Add(id);
}
}
wayReader.Close();
foreach (ulong nodeId in currentIds.Where(key => tmpAllNodes.ContainsKey(key)))
{
if (isHighway)
{
ulong regionHash = Coordinates.GetRegionHashCode(tmpAllNodes[nodeId].coordinates);
FileStream nodesRegionStream;
if(regionFileStreams.ContainsKey(regionHash))
nodesRegionStream = regionFileStreams[regionHash];
else
{
string regionPath = Path.Combine(outputPath, regionHash.ToString());
Directory.CreateDirectory(regionPath);
string nodesRegionPath = Path.Combine(regionPath, NodesFileName);
nodesRegionStream = new FileStream(nodesRegionPath, FileMode.Create);
regionFileStreams.Add(regionHash, nodesRegionStream);
}
nodeRegions.Add(nodeId, regionHash);
#pragma warning disable SYSLIB0011 //eheheh
bFormatter.Serialize(nodesRegionStream, tmpAllNodes[nodeId]);
#pragma warning restore SYSLIB0011
}
tmpAllNodes.Remove(nodeId);
}
}
}
xmlReader.Close();
return nodeRegions;
}
private static void ImportWays(XmlReader xmlReader, Dictionary<ulong, ulong> nodeRegions, string outputPath)
{
List<ulong> currentNodeIds = new();
Dictionary<Tag.TagType, dynamic> currentTags = new();
Dictionary<ulong, FileStream> regionWaysFileStreams = new();
Dictionary<ulong, FileStream> regionTagsFileStreams = new();
Dictionary<ulong, HashSet<ulong>> writtenWayTagsInRegion = new();
while (xmlReader.ReadToFollowing("way"))
{
currentNodeIds.Clear();
currentTags.Clear();
XmlReader wayReader = xmlReader.ReadSubtree();
while (wayReader.Read())
{
currentTags.TryAdd(Tag.TagType.id, Convert.ToUInt64(wayReader.GetAttribute("id")!));
if (wayReader.Name == "tag")
{
Tag? wayTag = Tag.ConvertToTag(wayReader.GetAttribute("k")!, wayReader.GetAttribute("v")!);
if(wayTag is not null)
currentTags.TryAdd(wayTag.key, wayTag.value);
}
else if (wayReader.Name == "nd")
{
ulong nodeId = Convert.ToUInt64(wayReader.GetAttribute("ref"));
currentNodeIds.Add(nodeId);
}
}
wayReader.Close();
if (currentTags.ContainsKey(Tag.TagType.highway))
{
for (int i = 0; i < currentNodeIds.Count - 1; i++)
{
ulong node1Id = currentNodeIds[i];
ulong node2Id = currentNodeIds[i+1];
if (currentTags.ContainsKey(Tag.TagType.oneway) && (bool)currentTags[Tag.TagType.oneway] && nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id))
{
if (currentTags.ContainsKey(Tag.TagType.forward) && !(bool)currentTags[Tag.TagType.forward])
{
OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath);
}
else
{
OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath);
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath);
}
}
else if(nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id))
{
OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath);
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath);
OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath);
}
}
}
}
xmlReader.Close();
foreach (FileStream f in regionWaysFileStreams.Values)
f.Dispose();
foreach(FileStream f in regionTagsFileStreams.Values)
f.Dispose();
}
private static void WriteWay(ref Dictionary<ulong, FileStream> regionWaysFileStreams, ulong regionHash, OsmEdge edge, string outputPath)
{
BinaryFormatter bFormatter = new BinaryFormatter();
if (!regionWaysFileStreams.ContainsKey(regionHash))
{
string waysRegionPath = Path.Combine(outputPath, regionHash.ToString(), WaysFileName);
regionWaysFileStreams.Add(regionHash, new FileStream(waysRegionPath, FileMode.OpenOrCreate));
}
#pragma warning disable SYSLIB0011
bFormatter.Serialize(regionWaysFileStreams[regionHash], edge);
#pragma warning restore SYSLIB0011
}
private static void WriteTags(ref Dictionary<ulong, FileStream> regionTagsFileStreams, ref Dictionary<ulong, HashSet<ulong>> writtenWayTagsInRegion, ulong regionHash, Dictionary<Tag.TagType, dynamic> currentTags, string outputPath)
{
if (writtenWayTagsInRegion.ContainsKey(regionHash) &&
writtenWayTagsInRegion[regionHash].Contains(currentTags[Tag.TagType.id]))
return;
else if(!writtenWayTagsInRegion.ContainsKey(regionHash))
writtenWayTagsInRegion.Add(regionHash, new HashSet<ulong>());
BinaryFormatter bFormatter = new BinaryFormatter();
if (!regionTagsFileStreams.ContainsKey(regionHash))
{
string tagsRegionPath = Path.Combine(outputPath, regionHash.ToString(), tagsFileName);
regionTagsFileStreams.Add(regionHash, new FileStream(tagsRegionPath, FileMode.OpenOrCreate));
}
ulong wayId = currentTags[Tag.TagType.id];
writtenWayTagsInRegion[regionHash].Add(wayId);
TagManager tm = new TagManager();
foreach(KeyValuePair<Tag.TagType, dynamic> kv in currentTags)
tm.AddTag(wayId, kv);
#pragma warning disable SYSLIB0011
bFormatter.Serialize(regionTagsFileStreams[regionHash], tm);
#pragma warning restore SYSLIB0011
}
}