Compare commits

..

No commits in common. "f0cd97fbc79b98be07984e7db59aa88bdad6aaaa" and "e2a50250b8d87e708b6f9b25d0404c68b5704dfa" have entirely different histories.

5 changed files with 81 additions and 134 deletions

View File

@ -19,6 +19,6 @@ public class OsmWay
public override string ToString() public override string ToString()
{ {
return $"EDGE WayID: {wayId} StartID: {startId} NeighborID: {neighborId} in {neighborRegion}"; return $"EDGE WayID: {wayId} NeighborID: {neighborId} {neighborRegion}";
} }
} }

View File

@ -7,7 +7,6 @@ public class Region
{ {
[NonSerialized]public const float RegionSize = 0.1f; [NonSerialized]public const float RegionSize = 0.1f;
public readonly HashSet<OsmNode> nodes = new(); public readonly HashSet<OsmNode> nodes = new();
public readonly HashSet<OsmWay> ways = new();
public ulong regionHash { get; } public ulong regionHash { get; }
public TagManager tagManager { get; } public TagManager tagManager { get; }

View File

@ -17,9 +17,9 @@ public class RegionConverter
NumberDecimalSeparator = "." NumberDecimalSeparator = "."
}; };
public const string NodesFileName = "region.nodes"; private const string NodesFileName = "region.nodes";
public const string WaysFileName = "region.ways"; private const string WaysFileName = "region.ways";
public const string tagsFileName = "region.tags"; private const string tagsFileName = "waytags";
public static void ConvertXMLToRegions(string filePath, string outputPath) public static void ConvertXMLToRegions(string filePath, string outputPath)
{ {
@ -43,7 +43,7 @@ public class RegionConverter
Dictionary<ulong, ulong> nodeRegions = new(); Dictionary<ulong, ulong> nodeRegions = new();
Dictionary<ulong, FileStream> regionFileStreams = new(); Dictionary<ulong, FileStream> regionFileStreams = new();
Dictionary<ulong, OsmNode> tmpAllNodes = new(); Dictionary<ulong, OsmNode> tmpAllNodes = new();
bool isHighway; bool isHighway = false;
HashSet<ulong> currentIds = new(); HashSet<ulong> currentIds = new();
while (xmlReader.Read()) while (xmlReader.Read())
{ {
@ -66,10 +66,14 @@ public class RegionConverter
isHighway = true; isHighway = true;
} }
else if (xmlReader.Name == "nd") else if (xmlReader.Name == "nd")
{
try
{ {
ulong id = Convert.ToUInt64(xmlReader.GetAttribute("ref")!); ulong id = Convert.ToUInt64(xmlReader.GetAttribute("ref")!);
currentIds.Add(id); currentIds.Add(id);
} }
catch (FormatException) { };
}
} }
wayReader.Close(); wayReader.Close();
@ -106,31 +110,43 @@ public class RegionConverter
private static void ImportWays(XmlReader xmlReader, Dictionary<ulong, ulong> nodeRegions, string outputPath) private static void ImportWays(XmlReader xmlReader, Dictionary<ulong, ulong> nodeRegions, string outputPath)
{ {
BinaryFormatter bFormatter = new BinaryFormatter();
bool currentWayIsHighway;
ulong currentWayId = 0;
List<ulong> currentNodeIds = new(); List<ulong> currentNodeIds = new();
Dictionary<Tag.TagType, dynamic> currentTags = new(); Dictionary<Tag.TagType, dynamic> currentTags = new();
Dictionary<ulong, FileStream> regionWaysFileStreams = new(); Dictionary<ulong, FileStream> regionWaysFileStreams = new();
Dictionary<ulong, FileStream> regionTagsFileStreams = new(); string wayFileStreamPath = "";
FileStream wayFileStream = new FileStream(Path.GetTempFileName(), FileMode.Create);
while (xmlReader.ReadToFollowing("way")) while (xmlReader.ReadToFollowing("way"))
{ {
ulong wayId = Convert.ToUInt64(xmlReader.GetAttribute("id")!);
currentWayIsHighway = false;
currentNodeIds.Clear(); currentNodeIds.Clear();
currentTags.Clear(); currentTags.Clear();
XmlReader wayReader = xmlReader.ReadSubtree(); XmlReader wayReader = xmlReader.ReadSubtree();
while (wayReader.Read()) while (wayReader.Read())
{ {
currentTags.TryAdd(Tag.TagType.id, Convert.ToUInt64(wayReader.GetAttribute("id")!)); currentWayId = Convert.ToUInt64(wayReader.GetAttribute("id")!);
if (wayReader.Name == "tag") if (wayReader.Name == "tag")
{ {
Tag wayTag = Tag.ConvertToTag(wayReader.GetAttribute("k")!, wayReader.GetAttribute("v")!); Tag wayTag = Tag.ConvertToTag(wayReader.GetAttribute("k")!, wayReader.GetAttribute("v")!);
currentTags.TryAdd(wayTag.key, wayTag.value); currentTags.TryAdd(wayTag.key, wayTag.value);
if(wayTag.key == Tag.TagType.highway)
currentWayIsHighway = true;
} }
else if (wayReader.Name == "nd") else if (wayReader.Name == "nd")
{
try
{ {
ulong nodeId = Convert.ToUInt64(wayReader.GetAttribute("ref")); ulong nodeId = Convert.ToUInt64(wayReader.GetAttribute("ref"));
currentNodeIds.Add(nodeId); currentNodeIds.Add(nodeId);
} }
catch (FormatException) { };
}
} }
wayReader.Close(); wayReader.Close();
if (currentTags.ContainsKey(Tag.TagType.highway)) if (currentWayIsHighway)
{ {
for (int i = 0; i < currentNodeIds.Count - 1; i++) for (int i = 0; i < currentNodeIds.Count - 1; i++)
{ {
@ -140,61 +156,68 @@ public class RegionConverter
{ {
if (currentTags.ContainsKey(Tag.TagType.forward) && !(bool)currentTags[Tag.TagType.forward]) if (currentTags.ContainsKey(Tag.TagType.forward) && !(bool)currentTags[Tag.TagType.forward])
{ {
OsmWay n21e = new OsmWay(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]); OsmWay n21e = new OsmWay(currentWayId, node2Id, node1Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); if (!regionWaysFileStreams.ContainsKey(nodeRegions[node2Id]))
WriteTag(ref regionTagsFileStreams, nodeRegions[node2Id], currentTags, outputPath); {
string waysRegionPath = Path.Combine(outputPath, nodeRegions[node2Id].ToString(), WaysFileName);
regionWaysFileStreams.Add(nodeRegions[node2Id], new FileStream(waysRegionPath, FileMode.OpenOrCreate));
}
#pragma warning disable SYSLIB0011
bFormatter.Serialize(regionWaysFileStreams[nodeRegions[node2Id]], n21e);
wayFileStreamPath = Path.Combine(outputPath, nodeRegions[node2Id].ToString(), $"{wayId}.{tagsFileName}");
wayFileStream = new FileStream(wayFileStreamPath, FileMode.OpenOrCreate);
bFormatter.Serialize(wayFileStream, currentTags);
wayFileStream.Dispose();
} }
else else
{ {
OsmWay n12e = new OsmWay(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]); OsmWay n12e = new OsmWay(currentWayId, node1Id, node2Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath); if (!regionWaysFileStreams.ContainsKey(nodeRegions[node1Id]))
WriteTag(ref regionTagsFileStreams, nodeRegions[node1Id], currentTags, outputPath); {
string waysRegionPath = Path.Combine(outputPath, nodeRegions[node1Id].ToString(), WaysFileName);
regionWaysFileStreams.Add(nodeRegions[node1Id], new FileStream(waysRegionPath, FileMode.OpenOrCreate));
}
bFormatter.Serialize(regionWaysFileStreams[nodeRegions[node1Id]], n12e);
wayFileStreamPath = Path.Combine(outputPath, nodeRegions[node1Id].ToString(), $"{wayId}.{tagsFileName}");
wayFileStream = new FileStream(wayFileStreamPath, FileMode.OpenOrCreate);
bFormatter.Serialize(wayFileStream, currentTags);
wayFileStream.Dispose();
} }
} }
else if(nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id)) else if(nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id))
{ {
OsmWay n12e = new OsmWay(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]); OsmWay n12e = new OsmWay(currentWayId, node1Id, node2Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath); if (!regionWaysFileStreams.ContainsKey(nodeRegions[node1Id]))
WriteTag(ref regionTagsFileStreams, nodeRegions[node1Id], currentTags, outputPath); {
string waysRegionPath = Path.Combine(outputPath, nodeRegions[node1Id].ToString(), WaysFileName);
regionWaysFileStreams.Add(nodeRegions[node1Id], new FileStream(waysRegionPath, FileMode.OpenOrCreate));
}
bFormatter.Serialize(regionWaysFileStreams[nodeRegions[node1Id]], n12e);
OsmWay n21e = new OsmWay(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]); wayFileStreamPath = Path.Combine(outputPath, nodeRegions[node1Id].ToString(), $"{wayId}.{tagsFileName}");
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); wayFileStream = new FileStream(wayFileStreamPath, FileMode.OpenOrCreate);
WriteTag(ref regionTagsFileStreams, nodeRegions[node2Id], currentTags, outputPath); bFormatter.Serialize(wayFileStream, currentTags);
wayFileStream.Dispose();
OsmWay n21e = new OsmWay(currentWayId, node2Id, node1Id, nodeRegions[node2Id]);
if (!regionWaysFileStreams.ContainsKey(nodeRegions[node2Id]))
{
string waysRegionPath = Path.Combine(outputPath, nodeRegions[node2Id].ToString(), WaysFileName);
regionWaysFileStreams.Add(nodeRegions[node2Id], new FileStream(waysRegionPath, FileMode.OpenOrCreate));
}
bFormatter.Serialize(regionWaysFileStreams[nodeRegions[node1Id]], n21e);
wayFileStreamPath = Path.Combine(outputPath, nodeRegions[node2Id].ToString(), $"{wayId}.{tagsFileName}");
wayFileStream = new FileStream(wayFileStreamPath, FileMode.OpenOrCreate);
bFormatter.Serialize(wayFileStream, currentTags);
wayFileStream.Dispose();
#pragma warning restore SYSLIB0011
} }
} }
} }
} }
xmlReader.Close(); 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, OsmWay way, 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], way);
#pragma warning restore SYSLIB0011
}
private static void WriteTag(ref Dictionary<ulong, FileStream> regionTagsFileStreams, ulong regionHash, Dictionary<Tag.TagType, dynamic> currentTags, string outputPath)
{
BinaryFormatter bFormatter = new BinaryFormatter();
if (!regionTagsFileStreams.ContainsKey(regionHash))
{
string tagsRegionPath = Path.Combine(outputPath, regionHash.ToString(), tagsFileName);
regionTagsFileStreams.Add(regionHash, new FileStream(tagsRegionPath, FileMode.OpenOrCreate));
}
#pragma warning disable SYSLIB0011
bFormatter.Serialize(regionTagsFileStreams[regionHash], currentTags);
#pragma warning restore SYSLIB0011
} }
} }

View File

@ -1,68 +0,0 @@
using System.Runtime.Serialization.Formatters.Binary;
using OSMDatastructure;
using OSMDatastructure.Graph;
namespace Server;
public class RegionLoader
{
private string path { get; }
private Dictionary<ulong, Region> regionIdsDict;
public RegionLoader(string path)
{
this.path = path;
this.regionIdsDict = new();
}
public HashSet<OsmNode> GetNodes(ulong regionHash)
{
Region r = GetRegion(regionHash);
return r.nodes;
}
public HashSet<OsmWay> GetWays(ulong regionHash)
{
Region r = GetRegion(regionHash);
return r.ways;
}
public Region GetRegion(ulong regionHash)
{
if (regionIdsDict.ContainsKey(regionHash))
return regionIdsDict[regionHash];
else return LoadRegion(regionHash);
}
private Region LoadRegion(ulong regionHash)
{
Region newRegion = new Region(regionHash);
BinaryFormatter bFormatter = new BinaryFormatter();
if (regionIdsDict.ContainsKey(regionHash))
throw new Exception("Region already loaded");
if (!Directory.Exists(Path.Join(path, regionHash.ToString())))
throw new FileNotFoundException("Region does not exist");
#pragma warning disable SYSLIB0011
using (FileStream wayFileStream = new FileStream(Path.Join(path, regionHash.ToString(), RegionConverter.WaysFileName), FileMode.Open))
{
while (wayFileStream.Position < wayFileStream.Length)
{
OsmWay deserializedWay = (OsmWay)bFormatter.Deserialize(wayFileStream);
newRegion.ways.Add(deserializedWay);
}
}
using (FileStream nodeFileStream = new FileStream(Path.Join(path, regionHash.ToString(), RegionConverter.NodesFileName), FileMode.Open))
{
while (nodeFileStream.Position < nodeFileStream.Length)
{
OsmNode deserializedNode = (OsmNode)bFormatter.Deserialize(nodeFileStream);
newRegion.nodes.Add(deserializedNode);
}
}
#pragma warning restore SYSLIB0011
regionIdsDict.Add(regionHash, newRegion);
return newRegion;
}
}

View File

@ -1,6 +1,3 @@
using OSMDatastructure;
using OSMDatastructure.Graph;
namespace Server; namespace Server;
public class Server public class Server
@ -12,12 +9,8 @@ public class Server
Console.SetOut(newConsole); Console.SetOut(newConsole);
Console.SetError(newConsole); Console.SetError(newConsole);
//RegionConverter.ConvertXMLToRegions("D:/stuttgart-regbez-latest.osm", "D:/stuttgart-regbez-latest"); RegionConverter.ConvertXMLToRegions("D:/stuttgart-regbez-latest.osm", "D:/stuttgart-regbez-latest");
RegionConverter.ConvertXMLToRegions("D:/map.osm", "D:/map"); //RegionConverter.ConvertXMLToRegions("D:/map.osm", "D:/map");
Console.WriteLine("Loaded");
RegionLoader rl = new RegionLoader("D:/map");
Region r = rl.GetRegion(13870001898000);
/* /*
Coordinates start = new Coordinates(48.243351f, 11.640417f); Coordinates start = new Coordinates(48.243351f, 11.640417f);