Compare commits

..

No commits in common. "2d5ffabb5d05fa3352a0fa64f4eb877e98b9a5a7" and "806dcf98c94662a3d3deb976d9350cb85ff8438a" have entirely different histories.

5 changed files with 106 additions and 115 deletions

View File

@ -1,4 +1,3 @@
using System.Runtime.Serialization.Formatters.Binary;
using OSMDatastructure.Graph;
namespace OSMDatastructure;
@ -8,6 +7,7 @@ public class Region
{
[NonSerialized]public const float RegionSize = 0.1f;
public readonly HashSet<OsmNode> nodes = new();
public readonly HashSet<OsmEdge> ways = new();
public ulong regionHash { get; }
public TagManager tagManager { get; }
@ -23,43 +23,11 @@ public class Region
tagManager = new TagManager();
}
public bool ContainsNode(ulong id)
public OsmNode? GetNodeWithCoordinates(Coordinates coordinates)
{
return nodes.Any(node => node.nodeId == id);
}
public bool ContainsNode(Coordinates coordinates)
{
return nodes.Any(node => node.coordinates.Equals(coordinates));
}
public OsmNode? GetNode(ulong id)
{
if (ContainsNode(id))
return nodes.First(node => node.nodeId == id);
else return null;
}
public OsmNode? GetNode(Coordinates coordinates)
{
if (ContainsNode(coordinates))
return nodes.First(node => node.coordinates.Equals(coordinates));
else return null;
}
public static Region? FromFile(string filePath)
{
BinaryFormatter bFormatter = new BinaryFormatter();
#pragma warning disable SYSLIB0011
if (File.Exists(filePath))
return (Region)bFormatter.Deserialize(new FileStream(filePath, FileMode.Open));
#pragma warning restore SYSLIB0011
else return null;
}
public static Region? FromId(string path, ulong regionId)
{
string filePath = Path.Join(path, $"{regionId}.region");
return FromFile(filePath);
foreach(OsmNode node in this.nodes)
if (node.coordinates.Equals(coordinates))
return node;
return null;
}
}

View File

@ -50,7 +50,7 @@ namespace OSMImporter
public OsmNode? GetNode(Coordinates coordinates)
{
Region regionWithNode = GetRegion(coordinates);
return regionWithNode.GetNode(coordinates);
return regionWithNode.GetNodeWithCoordinates(coordinates);
}
}
}

View File

@ -1,7 +1,6 @@
using System.Globalization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using OSMDatastructure;
using OSMDatastructure.Graph;
namespace Server;
@ -20,8 +19,7 @@ public class RegionConverter
public const string NodesFileName = "region.nodes";
public const string WaysFileName = "region.ways";
public const string TagsFileName = "region.tags";
public const string RegionsFileName = ".region";
public const string tagsFileName = "region.tags";
public static void ConvertXMLToRegions(string filePath, string outputPath)
{
@ -37,11 +35,6 @@ public class RegionConverter
Console.WriteLine("Converting Ways...");
ImportWays(XmlReader.Create(xmlFileStream, ReaderSettings), nodeIdRegionDict, outputPath);
xmlFileStream.Dispose();
Console.WriteLine("Combining tmpFiles into region");
CombineTmpFiles(outputPath, nodeIdRegionDict.Values.ToHashSet());
}
private static Dictionary<ulong, ulong> GetNodesAndRegions(XmlReader xmlReader, string outputPath)
@ -205,7 +198,7 @@ public class RegionConverter
BinaryFormatter bFormatter = new BinaryFormatter();
if (!regionTagsFileStreams.ContainsKey(regionHash))
{
string tagsRegionPath = Path.Combine(outputPath, regionHash.ToString(), TagsFileName);
string tagsRegionPath = Path.Combine(outputPath, regionHash.ToString(), tagsFileName);
regionTagsFileStreams.Add(regionHash, new FileStream(tagsRegionPath, FileMode.OpenOrCreate));
}
@ -218,69 +211,4 @@ public class RegionConverter
bFormatter.Serialize(regionTagsFileStreams[regionHash], tm);
#pragma warning restore SYSLIB0011
}
private static void CombineTmpFiles(string folderPath, HashSet<ulong> regionHashes)
{
BinaryFormatter bFormatter = new BinaryFormatter();
foreach (ulong regionId in regionHashes)
{
ValueTuple<Region, HashSet<OsmEdge>> tmpRegion = LoadRegion(folderPath, regionId);
foreach (OsmEdge edge in tmpRegion.Item2)
{
OsmNode? startNode = tmpRegion.Item1.GetNode(edge.startId);
if (startNode is not null)
{
startNode.edges.Add(edge);
}
}
FileStream tmpRegionFileStream = new FileStream(Path.Join(folderPath, $"{regionId}{RegionsFileName}"), FileMode.Create);
#pragma warning disable SYSLIB0011
bFormatter.Serialize(tmpRegionFileStream, tmpRegion.Item1);
#pragma warning restore SYSLIB0011
tmpRegionFileStream.Dispose();
Directory.Delete(Path.Join(folderPath, regionId.ToString()), true);
}
}
private static ValueTuple<Region, HashSet<OsmEdge>> LoadRegion(string folderPath, ulong regionHash)
{
Region newRegion = new Region(regionHash);
HashSet<OsmEdge> ways = new();
BinaryFormatter bFormatter = new BinaryFormatter();
if (!Directory.Exists(Path.Join(folderPath, regionHash.ToString())))
throw new FileNotFoundException("Region does not exist");
#pragma warning disable SYSLIB0011
using (FileStream wayFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.WaysFileName), FileMode.Open))
{
while (wayFileStream.Position < wayFileStream.Length)
{
OsmEdge deserializedEdge = (OsmEdge)bFormatter.Deserialize(wayFileStream);
ways.Add(deserializedEdge);
}
}
using (FileStream nodeFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.NodesFileName), FileMode.Open))
{
while (nodeFileStream.Position < nodeFileStream.Length)
{
OsmNode deserializedNode = (OsmNode)bFormatter.Deserialize(nodeFileStream);
newRegion.nodes.Add(deserializedNode);
}
}
using (FileStream tagsFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.TagsFileName), FileMode.Open))
{
while (tagsFileStream.Position < tagsFileStream.Length)
{
TagManager tm = (TagManager)bFormatter.Deserialize(tagsFileStream);
ulong id = (ulong)tm.wayTagSets.First()!.Value.First(tag => tag.key == Tag.TagType.id)!.value;
foreach(Tag tag in tm.wayTagSets.First()!.Value)
newRegion.tagManager.AddTag(id, tag);
}
}
#pragma warning restore SYSLIB0011
return new ValueTuple<Region, HashSet<OsmEdge>>(newRegion, ways);
}
}

91
Server/RegionLoader.cs Normal file
View File

@ -0,0 +1,91 @@
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<OsmEdge> GetWays(ulong regionHash)
{
Region r = GetRegion(regionHash);
return r.ways;
}
public TagManager GetTags(ulong regionHash)
{
Region r = GetRegion(regionHash);
return r.tagManager;
}
public HashSet<Tag>? GetTagsForWay(ulong regionHash, ulong wayId)
{
Region r = GetRegion(regionHash);
return r.tagManager.GetTagsForWayId(wayId);
}
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)
{
OsmEdge deserializedEdge = (OsmEdge)bFormatter.Deserialize(wayFileStream);
newRegion.ways.Add(deserializedEdge);
}
}
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);
}
}
using (FileStream tagsFileStream = new FileStream(Path.Join(path, regionHash.ToString(), RegionConverter.tagsFileName), FileMode.Open))
{
while (tagsFileStream.Position < tagsFileStream.Length)
{
TagManager tm = (TagManager)bFormatter.Deserialize(tagsFileStream);
ulong id = (ulong)tm.wayTagSets.First()!.Value.First(tag => tag.key == Tag.TagType.id)!.value;
foreach(Tag tag in tm.wayTagSets.First()!.Value)
newRegion.tagManager.AddTag(id, tag);
}
}
#pragma warning restore SYSLIB0011
regionIdsDict.Add(regionHash, newRegion);
return newRegion;
}
}

View File

@ -15,8 +15,12 @@ public class Server
//RegionConverter.ConvertXMLToRegions("D:/stuttgart-regbez-latest.osm", "D:/stuttgart-regbez-latest");
RegionConverter.ConvertXMLToRegions("D:/map.osm", "D:/map");
Console.WriteLine("Loaded");
Region? r = Region.FromFile("D:/map/13870001898000.region");
Console.WriteLine("loaded region");
RegionLoader rl = new RegionLoader("D:/map");
Region r = rl.GetRegion(13870001898000);
Console.WriteLine(r.nodes.First());
Console.WriteLine(r.ways.First());
Console.WriteLine(rl.GetTagsForWay(13870001898000, 5897420)!.Last());
Console.WriteLine("Region loaded");
/*
Coordinates start = new Coordinates(48.243351f, 11.640417f);