From 20d4da9e6f129f07d11b4d1d6d935a8694421a60 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 6 Apr 2023 01:29:30 +0200 Subject: [PATCH] Changed to JsonSerialization to permanently store regions. --- OSMDatastructure/Coordinates.cs | 2 ++ OSMDatastructure/OSMDatastructure.csproj | 4 +++ OSMDatastructure/OsmEdge.cs | 3 +++ OSMDatastructure/OsmNode.cs | 12 +++++---- OSMDatastructure/Region.cs | 27 +++++++++++++------- OSMDatastructure/Tag.cs | 32 ++++++++++++++++++++++-- OSMDatastructure/TagManager.cs | 9 +++++-- Server/RegionConverter.cs | 16 ++++++------ Server/Server.csproj | 4 +++ 9 files changed, 83 insertions(+), 26 deletions(-) diff --git a/OSMDatastructure/Coordinates.cs b/OSMDatastructure/Coordinates.cs index f077cfe..91730df 100644 --- a/OSMDatastructure/Coordinates.cs +++ b/OSMDatastructure/Coordinates.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text.Json.Serialization; namespace OSMDatastructure.Graph; @@ -8,6 +9,7 @@ public class Coordinates public float latitude { get; } public float longitude { get; } + [JsonConstructor] public Coordinates(float latitude, float longitude) { this.latitude = latitude; diff --git a/OSMDatastructure/OSMDatastructure.csproj b/OSMDatastructure/OSMDatastructure.csproj index 6836c68..89e087d 100644 --- a/OSMDatastructure/OSMDatastructure.csproj +++ b/OSMDatastructure/OSMDatastructure.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/OSMDatastructure/OsmEdge.cs b/OSMDatastructure/OsmEdge.cs index fe3ecff..73ce69f 100644 --- a/OSMDatastructure/OsmEdge.cs +++ b/OSMDatastructure/OsmEdge.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace OSMDatastructure.Graph; [Serializable] @@ -9,6 +11,7 @@ public class OsmEdge public ulong neighborRegion { get; } + [JsonConstructor] public OsmEdge(ulong wayId, ulong startId, ulong neighborId, ulong neighborRegion) { this.wayId = wayId; diff --git a/OSMDatastructure/OsmNode.cs b/OSMDatastructure/OsmNode.cs index 560865c..ebf0676 100644 --- a/OSMDatastructure/OsmNode.cs +++ b/OSMDatastructure/OsmNode.cs @@ -1,5 +1,6 @@ using System.ComponentModel; using System.Runtime.Serialization; +using System.Text.Json.Serialization; namespace OSMDatastructure.Graph; @@ -7,13 +8,13 @@ namespace OSMDatastructure.Graph; public class OsmNode { public ulong nodeId { get; } - public HashSet edges { get; } + public HashSet edges { get; set; } public Coordinates coordinates { get; } - [NonSerialized]public OsmNode? previousPathNode = null; - [NonSerialized]public double currentPathWeight = double.MaxValue; - [NonSerialized]public double currentPathLength = double.MaxValue; - [NonSerialized]public double directDistanceToGoal = double.MaxValue; + [JsonIgnore][NonSerialized]public OsmNode? previousPathNode = null; + [JsonIgnore][NonSerialized]public double currentPathWeight = double.MaxValue; + [JsonIgnore][NonSerialized]public double currentPathLength = double.MaxValue; + [JsonIgnore][NonSerialized]public double directDistanceToGoal = double.MaxValue; [OnDeserialized] internal void SetDefaultValues(StreamingContext context) @@ -30,6 +31,7 @@ public class OsmNode this.coordinates = new Coordinates(lat, lon); } + [JsonConstructor] public OsmNode(ulong nodeId, Coordinates coordinates) { this.nodeId = nodeId; diff --git a/OSMDatastructure/Region.cs b/OSMDatastructure/Region.cs index fd6f784..20cd59a 100644 --- a/OSMDatastructure/Region.cs +++ b/OSMDatastructure/Region.cs @@ -1,4 +1,5 @@ -using System.Runtime.Serialization.Formatters.Binary; +using System.Text.Json; +using System.Text.Json.Serialization; using OSMDatastructure.Graph; namespace OSMDatastructure; @@ -6,21 +7,32 @@ namespace OSMDatastructure; [Serializable] public class Region { - [NonSerialized]public const float RegionSize = 0.025f; - public readonly HashSet nodes = new(); - public ulong regionHash { get; } - public TagManager tagManager { get; } + [JsonIgnore]public const float RegionSize = 0.025f; + [JsonIgnore]public static readonly JsonSerializerOptions serializerOptions = new() + { + IncludeFields = true, + MaxDepth = 32, + IgnoreReadOnlyFields = false//, + //WriteIndented = true + }; + [JsonRequired]public HashSet nodes { get; set; } + public ulong regionHash { get; } + [JsonRequired]public TagManager tagManager { get; set; } + + [JsonConstructor] public Region(ulong regionHash) { this.regionHash = regionHash; tagManager = new TagManager(); + nodes = new HashSet(); } public Region(Coordinates coordinates) { regionHash = Coordinates.GetRegionHashCode(coordinates); tagManager = new TagManager(); + nodes = new HashSet(); } public bool ContainsNode(ulong id) @@ -49,11 +61,8 @@ public class Region 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 + return JsonSerializer.Deserialize(new FileStream(filePath, FileMode.Open), serializerOptions)!; else return null; } diff --git a/OSMDatastructure/Tag.cs b/OSMDatastructure/Tag.cs index 70ac08e..8bf2626 100644 --- a/OSMDatastructure/Tag.cs +++ b/OSMDatastructure/Tag.cs @@ -1,3 +1,6 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + namespace OSMDatastructure.Graph; [Serializable] @@ -6,11 +9,36 @@ public class Tag public TagType key { get; } public dynamic value { get; } - + [JsonConstructor] public Tag(TagType key, dynamic value) { this.key = key; - this.value = value; + if (value is JsonElement) + { + switch (key) + { + case TagType.highway: + this.value = (WayType)value.GetByte(); + break; + case TagType.maxspeed: + this.value = value.GetByte(); + break; + case TagType.forward: + case TagType.oneway: + this.value = value.GetBoolean(); + break; + case TagType.id: + this.value = value.GetUInt64(); + break; + default: + this.value = value; + break; + } + } + else + { + this.value = value; + } } public static Tag FromBytes(byte[] bytes) diff --git a/OSMDatastructure/TagManager.cs b/OSMDatastructure/TagManager.cs index 50263bd..3de7c5b 100644 --- a/OSMDatastructure/TagManager.cs +++ b/OSMDatastructure/TagManager.cs @@ -1,11 +1,16 @@ -using System.Runtime.CompilerServices; +using System.Text.Json.Serialization; namespace OSMDatastructure.Graph; [Serializable] public class TagManager { - public readonly Dictionary> wayTagSets = new(); + [JsonRequired]public Dictionary> wayTagSets { get; set; } + + public TagManager() + { + wayTagSets = new(); + } public bool ContainsKey(ulong wayId, Tag.TagType key) { diff --git a/Server/RegionConverter.cs b/Server/RegionConverter.cs index b7c31d9..12f76c1 100644 --- a/Server/RegionConverter.cs +++ b/Server/RegionConverter.cs @@ -1,5 +1,6 @@ using System.Globalization; using System.Runtime.Serialization.Formatters.Binary; +using System.Text.Json; using System.Xml; using OSMDatastructure; using OSMDatastructure.Graph; @@ -46,7 +47,6 @@ public class RegionConverter private static Dictionary GetNodesAndRegions(XmlReader xmlReader, string outputPath) { - BinaryFormatter bFormatter = new BinaryFormatter(); Dictionary nodeRegions = new(); Dictionary regionFileStreams = new(); Dictionary tmpAllNodes = new(); @@ -82,6 +82,7 @@ public class RegionConverter foreach (ulong nodeId in currentIds.Where(key => tmpAllNodes.ContainsKey(key))) { + BinaryFormatter bFormatter = new BinaryFormatter(); if (isHighway) { ulong regionHash = Coordinates.GetRegionHashCode(tmpAllNodes[nodeId].coordinates); @@ -97,7 +98,7 @@ public class RegionConverter regionFileStreams.Add(regionHash, nodesRegionStream); } nodeRegions.Add(nodeId, regionHash); - + #pragma warning disable SYSLIB0011 //eheheh bFormatter.Serialize(nodesRegionStream, tmpAllNodes[nodeId]); #pragma warning restore SYSLIB0011 @@ -221,7 +222,6 @@ public class RegionConverter private static void CombineTmpFiles(string folderPath, HashSet regionHashes) { - BinaryFormatter bFormatter = new BinaryFormatter(); foreach (ulong regionId in regionHashes) { ValueTuple> tmpRegion = LoadRegion(folderPath, regionId); @@ -233,11 +233,11 @@ public class RegionConverter 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(); + + using (FileStream tmpRegionFileStream = new FileStream(Path.Join(folderPath, $"{regionId}{RegionsFileName}"), FileMode.Create)) + { + JsonSerializer.Serialize(tmpRegionFileStream, tmpRegion.Item1, typeof(Region), Region.serializerOptions); + } Directory.Delete(Path.Join(folderPath, regionId.ToString()), true); } } diff --git a/Server/Server.csproj b/Server/Server.csproj index 0469d09..9022c04 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -11,4 +11,8 @@ + + + +