Renamed OsmWay -> OsmEdge again

Prevented duplicate writes of Tags for way
This commit is contained in:
glax 2023-04-01 01:47:56 +02:00
parent c2cadd678c
commit 806dcf98c9
5 changed files with 30 additions and 22 deletions

View File

@ -1,7 +1,7 @@
namespace OSMDatastructure.Graph; namespace OSMDatastructure.Graph;
[Serializable] [Serializable]
public class OsmWay public class OsmEdge
{ {
public ulong wayId { get; } public ulong wayId { get; }
public ulong startId { get; } public ulong startId { get; }
@ -9,7 +9,7 @@ public class OsmWay
public ulong neighborRegion { get; } public ulong neighborRegion { get; }
public OsmWay(ulong wayId, ulong startId, ulong neighborId, ulong neighborRegion) public OsmEdge(ulong wayId, ulong startId, ulong neighborId, ulong neighborRegion)
{ {
this.wayId = wayId; this.wayId = wayId;
this.startId = startId; this.startId = startId;

View File

@ -4,7 +4,7 @@ namespace OSMDatastructure.Graph;
public class OsmNode public class OsmNode
{ {
public ulong nodeId { get; } public ulong nodeId { get; }
public HashSet<OsmWay> edges { get; } public HashSet<OsmEdge> edges { get; }
public Coordinates coordinates { get; } public Coordinates coordinates { get; }
[NonSerialized]public OsmNode? previousPathNode = null; [NonSerialized]public OsmNode? previousPathNode = null;
@ -26,9 +26,9 @@ public class OsmNode
this.coordinates = coordinates; this.coordinates = coordinates;
} }
public OsmWay? GetEdgeToNode(OsmNode n) public OsmEdge? GetEdgeToNode(OsmNode n)
{ {
foreach (OsmWay e in this.edges) foreach (OsmEdge e in this.edges)
if (e.neighborId == n.nodeId) if (e.neighborId == n.nodeId)
return e; return e;
return null; return null;

View File

@ -7,7 +7,7 @@ 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 readonly HashSet<OsmEdge> ways = new();
public ulong regionHash { get; } public ulong regionHash { get; }
public TagManager tagManager { get; } public TagManager tagManager { get; }

View File

@ -110,6 +110,7 @@ public class RegionConverter
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(); Dictionary<ulong, FileStream> regionTagsFileStreams = new();
Dictionary<ulong, HashSet<ulong>> writtenWayTagsInRegion = new();
while (xmlReader.ReadToFollowing("way")) while (xmlReader.ReadToFollowing("way"))
{ {
currentNodeIds.Clear(); currentNodeIds.Clear();
@ -141,26 +142,26 @@ 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]); OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
WriteTag(ref regionTagsFileStreams, nodeRegions[node2Id], currentTags, outputPath); WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath);
} }
else else
{ {
OsmWay n12e = new OsmWay(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]); OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath); WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath);
WriteTag(ref regionTagsFileStreams, nodeRegions[node1Id], currentTags, outputPath); WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath);
} }
} }
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]); OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath); WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath);
WriteTag(ref regionTagsFileStreams, nodeRegions[node1Id], currentTags, outputPath); WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath);
OsmWay n21e = new OsmWay(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]); OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]);
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath); WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
WriteTag(ref regionTagsFileStreams, nodeRegions[node2Id], currentTags, outputPath); WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath);
} }
} }
} }
@ -172,7 +173,7 @@ public class RegionConverter
f.Dispose(); f.Dispose();
} }
private static void WriteWay(ref Dictionary<ulong, FileStream> regionWaysFileStreams, ulong regionHash, OsmWay way, string outputPath) private static void WriteWay(ref Dictionary<ulong, FileStream> regionWaysFileStreams, ulong regionHash, OsmEdge edge, string outputPath)
{ {
BinaryFormatter bFormatter = new BinaryFormatter(); BinaryFormatter bFormatter = new BinaryFormatter();
if (!regionWaysFileStreams.ContainsKey(regionHash)) if (!regionWaysFileStreams.ContainsKey(regionHash))
@ -181,13 +182,19 @@ public class RegionConverter
regionWaysFileStreams.Add(regionHash, new FileStream(waysRegionPath, FileMode.OpenOrCreate)); regionWaysFileStreams.Add(regionHash, new FileStream(waysRegionPath, FileMode.OpenOrCreate));
} }
#pragma warning disable SYSLIB0011 #pragma warning disable SYSLIB0011
bFormatter.Serialize(regionWaysFileStreams[regionHash], way); bFormatter.Serialize(regionWaysFileStreams[regionHash], edge);
#pragma warning restore SYSLIB0011 #pragma warning restore SYSLIB0011
} }
private static void WriteTag(ref Dictionary<ulong, FileStream> regionTagsFileStreams, ulong regionHash, Dictionary<Tag.TagType, dynamic> currentTags, string outputPath) 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(); BinaryFormatter bFormatter = new BinaryFormatter();
if (!regionTagsFileStreams.ContainsKey(regionHash)) if (!regionTagsFileStreams.ContainsKey(regionHash))
{ {
@ -195,10 +202,11 @@ public class RegionConverter
regionTagsFileStreams.Add(regionHash, new FileStream(tagsRegionPath, FileMode.OpenOrCreate)); regionTagsFileStreams.Add(regionHash, new FileStream(tagsRegionPath, FileMode.OpenOrCreate));
} }
ulong id = currentTags[Tag.TagType.id]; ulong wayId = currentTags[Tag.TagType.id];
writtenWayTagsInRegion[regionHash].Add(wayId);
TagManager tm = new TagManager(); TagManager tm = new TagManager();
foreach(KeyValuePair<Tag.TagType, dynamic> kv in currentTags) foreach(KeyValuePair<Tag.TagType, dynamic> kv in currentTags)
tm.AddTag(id, kv); tm.AddTag(wayId, kv);
#pragma warning disable SYSLIB0011 #pragma warning disable SYSLIB0011
bFormatter.Serialize(regionTagsFileStreams[regionHash], tm); bFormatter.Serialize(regionTagsFileStreams[regionHash], tm);
#pragma warning restore SYSLIB0011 #pragma warning restore SYSLIB0011

View File

@ -20,7 +20,7 @@ public class RegionLoader
return r.nodes; return r.nodes;
} }
public HashSet<OsmWay> GetWays(ulong regionHash) public HashSet<OsmEdge> GetWays(ulong regionHash)
{ {
Region r = GetRegion(regionHash); Region r = GetRegion(regionHash);
return r.ways; return r.ways;
@ -59,8 +59,8 @@ public class RegionLoader
{ {
while (wayFileStream.Position < wayFileStream.Length) while (wayFileStream.Position < wayFileStream.Length)
{ {
OsmWay deserializedWay = (OsmWay)bFormatter.Deserialize(wayFileStream); OsmEdge deserializedEdge = (OsmEdge)bFormatter.Deserialize(wayFileStream);
newRegion.ways.Add(deserializedWay); newRegion.ways.Add(deserializedEdge);
} }
} }