Compare commits
2 Commits
806dcf98c9
...
2d5ffabb5d
Author | SHA1 | Date | |
---|---|---|---|
2d5ffabb5d | |||
0bfc120ede |
@ -1,3 +1,4 @@
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using OSMDatastructure.Graph;
|
||||
|
||||
namespace OSMDatastructure;
|
||||
@ -7,7 +8,6 @@ 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,11 +23,43 @@ public class Region
|
||||
tagManager = new TagManager();
|
||||
}
|
||||
|
||||
public OsmNode? GetNodeWithCoordinates(Coordinates coordinates)
|
||||
public bool ContainsNode(ulong id)
|
||||
{
|
||||
foreach(OsmNode node in this.nodes)
|
||||
if (node.coordinates.Equals(coordinates))
|
||||
return node;
|
||||
return null;
|
||||
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);
|
||||
}
|
||||
}
|
@ -50,7 +50,7 @@ namespace OSMImporter
|
||||
public OsmNode? GetNode(Coordinates coordinates)
|
||||
{
|
||||
Region regionWithNode = GetRegion(coordinates);
|
||||
return regionWithNode.GetNodeWithCoordinates(coordinates);
|
||||
return regionWithNode.GetNode(coordinates);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Xml;
|
||||
using OSMDatastructure;
|
||||
using OSMDatastructure.Graph;
|
||||
|
||||
namespace Server;
|
||||
@ -19,7 +20,8 @@ public class RegionConverter
|
||||
|
||||
public const string NodesFileName = "region.nodes";
|
||||
public const string WaysFileName = "region.ways";
|
||||
public const string tagsFileName = "region.tags";
|
||||
public const string TagsFileName = "region.tags";
|
||||
public const string RegionsFileName = ".region";
|
||||
|
||||
public static void ConvertXMLToRegions(string filePath, string outputPath)
|
||||
{
|
||||
@ -35,6 +37,11 @@ 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)
|
||||
@ -198,7 +205,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));
|
||||
}
|
||||
|
||||
@ -211,4 +218,69 @@ 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);
|
||||
}
|
||||
}
|
@ -1,91 +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<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;
|
||||
}
|
||||
}
|
@ -15,12 +15,8 @@ public class Server
|
||||
//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);
|
||||
Console.WriteLine(r.nodes.First());
|
||||
Console.WriteLine(r.ways.First());
|
||||
Console.WriteLine(rl.GetTagsForWay(13870001898000, 5897420)!.Last());
|
||||
Console.WriteLine("Region loaded");
|
||||
Region? r = Region.FromFile("D:/map/13870001898000.region");
|
||||
Console.WriteLine("loaded region");
|
||||
|
||||
/*
|
||||
Coordinates start = new Coordinates(48.243351f, 11.640417f);
|
||||
|
Loading…
Reference in New Issue
Block a user