Compare commits

..

7 Commits

Author SHA1 Message Date
f0cd97fbc7 working 2023-04-01 01:00:22 +02:00
3fce0f990a Initial commit 2023-04-01 01:00:17 +02:00
102499891c Moved id to tag storage 2023-04-01 01:00:10 +02:00
9ef1d4a978 Readbility through Methods 2023-04-01 00:52:13 +02:00
b12f959f48 Made Fields FileNames public 2023-04-01 00:40:06 +02:00
e7119b22ae Added ways to Region 2023-04-01 00:39:49 +02:00
cd1584eee7 Adjusted ToString() to include startId 2023-04-01 00:39:37 +02:00
5 changed files with 134 additions and 81 deletions

View File

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

View File

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

View File

@ -17,9 +17,9 @@ public class RegionConverter
NumberDecimalSeparator = "."
};
private const string NodesFileName = "region.nodes";
private const string WaysFileName = "region.ways";
private const string tagsFileName = "waytags";
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)
{
@ -43,7 +43,7 @@ public class RegionConverter
Dictionary<ulong, ulong> nodeRegions = new();
Dictionary<ulong, FileStream> regionFileStreams = new();
Dictionary<ulong, OsmNode> tmpAllNodes = new();
bool isHighway = false;
bool isHighway;
HashSet<ulong> currentIds = new();
while (xmlReader.Read())
{
@ -67,12 +67,8 @@ public class RegionConverter
}
else if (xmlReader.Name == "nd")
{
try
{
ulong id = Convert.ToUInt64(xmlReader.GetAttribute("ref")!);
currentIds.Add(id);
}
catch (FormatException) { };
ulong id = Convert.ToUInt64(xmlReader.GetAttribute("ref")!);
currentIds.Add(id);
}
}
wayReader.Close();
@ -107,46 +103,34 @@ public class RegionConverter
xmlReader.Close();
return nodeRegions;
}
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();
Dictionary<Tag.TagType, dynamic> currentTags = new();
Dictionary<ulong, FileStream> regionWaysFileStreams = new();
string wayFileStreamPath = "";
FileStream wayFileStream = new FileStream(Path.GetTempFileName(), FileMode.Create);
Dictionary<ulong, FileStream> regionTagsFileStreams = new();
while (xmlReader.ReadToFollowing("way"))
{
ulong wayId = Convert.ToUInt64(xmlReader.GetAttribute("id")!);
currentWayIsHighway = false;
currentNodeIds.Clear();
currentTags.Clear();
XmlReader wayReader = xmlReader.ReadSubtree();
while (wayReader.Read())
{
currentWayId = Convert.ToUInt64(wayReader.GetAttribute("id")!);
currentTags.TryAdd(Tag.TagType.id, Convert.ToUInt64(wayReader.GetAttribute("id")!));
if (wayReader.Name == "tag")
{
Tag wayTag = Tag.ConvertToTag(wayReader.GetAttribute("k")!, wayReader.GetAttribute("v")!);
currentTags.TryAdd(wayTag.key, wayTag.value);
if(wayTag.key == Tag.TagType.highway)
currentWayIsHighway = true;
}
else if (wayReader.Name == "nd")
{
try
{
ulong nodeId = Convert.ToUInt64(wayReader.GetAttribute("ref"));
currentNodeIds.Add(nodeId);
}
catch (FormatException) { };
ulong nodeId = Convert.ToUInt64(wayReader.GetAttribute("ref"));
currentNodeIds.Add(nodeId);
}
}
wayReader.Close();
if (currentWayIsHighway)
if (currentTags.ContainsKey(Tag.TagType.highway))
{
for (int i = 0; i < currentNodeIds.Count - 1; i++)
{
@ -156,68 +140,61 @@ public class RegionConverter
{
if (currentTags.ContainsKey(Tag.TagType.forward) && !(bool)currentTags[Tag.TagType.forward])
{
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));
}
#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();
OsmWay n21e = new OsmWay(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
WriteTag(ref regionTagsFileStreams, nodeRegions[node2Id], currentTags, outputPath);
}
else
{
OsmWay n12e = new OsmWay(currentWayId, node1Id, node2Id, nodeRegions[node2Id]);
if (!regionWaysFileStreams.ContainsKey(nodeRegions[node1Id]))
{
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();
OsmWay n12e = new OsmWay(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath);
WriteTag(ref regionTagsFileStreams, nodeRegions[node1Id], currentTags, outputPath);
}
}
else if(nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id))
{
OsmWay n12e = new OsmWay(currentWayId, node1Id, node2Id, nodeRegions[node2Id]);
if (!regionWaysFileStreams.ContainsKey(nodeRegions[node1Id]))
{
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();
OsmWay n12e = new OsmWay(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath);
WriteTag(ref regionTagsFileStreams, nodeRegions[node1Id], currentTags, outputPath);
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
OsmWay n21e = new OsmWay(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
WriteTag(ref regionTagsFileStreams, 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, 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
}
}

68
Server/RegionLoader.cs Normal file
View File

@ -0,0 +1,68 @@
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,3 +1,6 @@
using OSMDatastructure;
using OSMDatastructure.Graph;
namespace Server;
public class Server
@ -9,9 +12,13 @@ public class Server
Console.SetOut(newConsole);
Console.SetError(newConsole);
RegionConverter.ConvertXMLToRegions("D:/stuttgart-regbez-latest.osm", "D:/stuttgart-regbez-latest");
//RegionConverter.ConvertXMLToRegions("D:/map.osm", "D:/map");
//RegionConverter.ConvertXMLToRegions("D:/stuttgart-regbez-latest.osm", "D:/stuttgart-regbez-latest");
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 finish = new Coordinates(48.25239f, 11.53272f);