Changed to JsonSerialization to permanently store regions.

This commit is contained in:
glax 2023-04-06 01:29:30 +02:00
parent e9f1ba2e73
commit 20d4da9e6f
9 changed files with 83 additions and 26 deletions

View File

@ -1,4 +1,5 @@
using System.Globalization; using System.Globalization;
using System.Text.Json.Serialization;
namespace OSMDatastructure.Graph; namespace OSMDatastructure.Graph;
@ -8,6 +9,7 @@ public class Coordinates
public float latitude { get; } public float latitude { get; }
public float longitude { get; } public float longitude { get; }
[JsonConstructor]
public Coordinates(float latitude, float longitude) public Coordinates(float latitude, float longitude)
{ {
this.latitude = latitude; this.latitude = latitude;

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Runtime.Serialization.Json" Version="4.3.0" />
</ItemGroup>
</Project> </Project>

View File

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
namespace OSMDatastructure.Graph; namespace OSMDatastructure.Graph;
[Serializable] [Serializable]
@ -9,6 +11,7 @@ public class OsmEdge
public ulong neighborRegion { get; } public ulong neighborRegion { get; }
[JsonConstructor]
public OsmEdge(ulong wayId, ulong startId, ulong neighborId, ulong neighborRegion) public OsmEdge(ulong wayId, ulong startId, ulong neighborId, ulong neighborRegion)
{ {
this.wayId = wayId; this.wayId = wayId;

View File

@ -1,5 +1,6 @@
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text.Json.Serialization;
namespace OSMDatastructure.Graph; namespace OSMDatastructure.Graph;
@ -7,13 +8,13 @@ namespace OSMDatastructure.Graph;
public class OsmNode public class OsmNode
{ {
public ulong nodeId { get; } public ulong nodeId { get; }
public HashSet<OsmEdge> edges { get; } public HashSet<OsmEdge> edges { get; set; }
public Coordinates coordinates { get; } public Coordinates coordinates { get; }
[NonSerialized]public OsmNode? previousPathNode = null; [JsonIgnore][NonSerialized]public OsmNode? previousPathNode = null;
[NonSerialized]public double currentPathWeight = double.MaxValue; [JsonIgnore][NonSerialized]public double currentPathWeight = double.MaxValue;
[NonSerialized]public double currentPathLength = double.MaxValue; [JsonIgnore][NonSerialized]public double currentPathLength = double.MaxValue;
[NonSerialized]public double directDistanceToGoal = double.MaxValue; [JsonIgnore][NonSerialized]public double directDistanceToGoal = double.MaxValue;
[OnDeserialized] [OnDeserialized]
internal void SetDefaultValues(StreamingContext context) internal void SetDefaultValues(StreamingContext context)
@ -30,6 +31,7 @@ public class OsmNode
this.coordinates = new Coordinates(lat, lon); this.coordinates = new Coordinates(lat, lon);
} }
[JsonConstructor]
public OsmNode(ulong nodeId, Coordinates coordinates) public OsmNode(ulong nodeId, Coordinates coordinates)
{ {
this.nodeId = nodeId; this.nodeId = nodeId;

View File

@ -1,4 +1,5 @@
using System.Runtime.Serialization.Formatters.Binary; using System.Text.Json;
using System.Text.Json.Serialization;
using OSMDatastructure.Graph; using OSMDatastructure.Graph;
namespace OSMDatastructure; namespace OSMDatastructure;
@ -6,21 +7,32 @@ namespace OSMDatastructure;
[Serializable] [Serializable]
public class Region public class Region
{ {
[NonSerialized]public const float RegionSize = 0.025f; [JsonIgnore]public const float RegionSize = 0.025f;
public readonly HashSet<OsmNode> nodes = new(); [JsonIgnore]public static readonly JsonSerializerOptions serializerOptions = new()
public ulong regionHash { get; } {
public TagManager tagManager { get; } IncludeFields = true,
MaxDepth = 32,
IgnoreReadOnlyFields = false//,
//WriteIndented = true
};
[JsonRequired]public HashSet<OsmNode> nodes { get; set; }
public ulong regionHash { get; }
[JsonRequired]public TagManager tagManager { get; set; }
[JsonConstructor]
public Region(ulong regionHash) public Region(ulong regionHash)
{ {
this.regionHash = regionHash; this.regionHash = regionHash;
tagManager = new TagManager(); tagManager = new TagManager();
nodes = new HashSet<OsmNode>();
} }
public Region(Coordinates coordinates) public Region(Coordinates coordinates)
{ {
regionHash = Coordinates.GetRegionHashCode(coordinates); regionHash = Coordinates.GetRegionHashCode(coordinates);
tagManager = new TagManager(); tagManager = new TagManager();
nodes = new HashSet<OsmNode>();
} }
public bool ContainsNode(ulong id) public bool ContainsNode(ulong id)
@ -49,11 +61,8 @@ public class Region
public static Region? FromFile(string filePath) public static Region? FromFile(string filePath)
{ {
BinaryFormatter bFormatter = new BinaryFormatter();
#pragma warning disable SYSLIB0011
if (File.Exists(filePath)) if (File.Exists(filePath))
return (Region)bFormatter.Deserialize(new FileStream(filePath, FileMode.Open)); return JsonSerializer.Deserialize<Region>(new FileStream(filePath, FileMode.Open), serializerOptions)!;
#pragma warning restore SYSLIB0011
else return null; else return null;
} }

View File

@ -1,3 +1,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace OSMDatastructure.Graph; namespace OSMDatastructure.Graph;
[Serializable] [Serializable]
@ -6,11 +9,36 @@ public class Tag
public TagType key { get; } public TagType key { get; }
public dynamic value { get; } public dynamic value { get; }
[JsonConstructor]
public Tag(TagType key, dynamic value) public Tag(TagType key, dynamic value)
{ {
this.key = key; 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) public static Tag FromBytes(byte[] bytes)

View File

@ -1,11 +1,16 @@
using System.Runtime.CompilerServices; using System.Text.Json.Serialization;
namespace OSMDatastructure.Graph; namespace OSMDatastructure.Graph;
[Serializable] [Serializable]
public class TagManager public class TagManager
{ {
public readonly Dictionary<ulong, HashSet<Tag>> wayTagSets = new(); [JsonRequired]public Dictionary<ulong, HashSet<Tag>> wayTagSets { get; set; }
public TagManager()
{
wayTagSets = new();
}
public bool ContainsKey(ulong wayId, Tag.TagType key) public bool ContainsKey(ulong wayId, Tag.TagType key)
{ {

View File

@ -1,5 +1,6 @@
using System.Globalization; using System.Globalization;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using System.Text.Json;
using System.Xml; using System.Xml;
using OSMDatastructure; using OSMDatastructure;
using OSMDatastructure.Graph; using OSMDatastructure.Graph;
@ -46,7 +47,6 @@ public class RegionConverter
private static Dictionary<ulong, ulong> GetNodesAndRegions(XmlReader xmlReader, string outputPath) private static Dictionary<ulong, ulong> GetNodesAndRegions(XmlReader xmlReader, string outputPath)
{ {
BinaryFormatter bFormatter = new BinaryFormatter();
Dictionary<ulong, ulong> nodeRegions = new(); Dictionary<ulong, ulong> nodeRegions = new();
Dictionary<ulong, FileStream> regionFileStreams = new(); Dictionary<ulong, FileStream> regionFileStreams = new();
Dictionary<ulong, OsmNode> tmpAllNodes = new(); Dictionary<ulong, OsmNode> tmpAllNodes = new();
@ -82,6 +82,7 @@ public class RegionConverter
foreach (ulong nodeId in currentIds.Where(key => tmpAllNodes.ContainsKey(key))) foreach (ulong nodeId in currentIds.Where(key => tmpAllNodes.ContainsKey(key)))
{ {
BinaryFormatter bFormatter = new BinaryFormatter();
if (isHighway) if (isHighway)
{ {
ulong regionHash = Coordinates.GetRegionHashCode(tmpAllNodes[nodeId].coordinates); ulong regionHash = Coordinates.GetRegionHashCode(tmpAllNodes[nodeId].coordinates);
@ -97,7 +98,7 @@ public class RegionConverter
regionFileStreams.Add(regionHash, nodesRegionStream); regionFileStreams.Add(regionHash, nodesRegionStream);
} }
nodeRegions.Add(nodeId, regionHash); nodeRegions.Add(nodeId, regionHash);
#pragma warning disable SYSLIB0011 //eheheh #pragma warning disable SYSLIB0011 //eheheh
bFormatter.Serialize(nodesRegionStream, tmpAllNodes[nodeId]); bFormatter.Serialize(nodesRegionStream, tmpAllNodes[nodeId]);
#pragma warning restore SYSLIB0011 #pragma warning restore SYSLIB0011
@ -221,7 +222,6 @@ public class RegionConverter
private static void CombineTmpFiles(string folderPath, HashSet<ulong> regionHashes) private static void CombineTmpFiles(string folderPath, HashSet<ulong> regionHashes)
{ {
BinaryFormatter bFormatter = new BinaryFormatter();
foreach (ulong regionId in regionHashes) foreach (ulong regionId in regionHashes)
{ {
ValueTuple<Region, HashSet<OsmEdge>> tmpRegion = LoadRegion(folderPath, regionId); ValueTuple<Region, HashSet<OsmEdge>> tmpRegion = LoadRegion(folderPath, regionId);
@ -233,11 +233,11 @@ public class RegionConverter
startNode.edges.Add(edge); startNode.edges.Add(edge);
} }
} }
FileStream tmpRegionFileStream = new FileStream(Path.Join(folderPath, $"{regionId}{RegionsFileName}"), FileMode.Create);
#pragma warning disable SYSLIB0011 using (FileStream tmpRegionFileStream = new FileStream(Path.Join(folderPath, $"{regionId}{RegionsFileName}"), FileMode.Create))
bFormatter.Serialize(tmpRegionFileStream, tmpRegion.Item1); {
#pragma warning restore SYSLIB0011 JsonSerializer.Serialize(tmpRegionFileStream, tmpRegion.Item1, typeof(Region), Region.serializerOptions);
tmpRegionFileStream.Dispose(); }
Directory.Delete(Path.Join(folderPath, regionId.ToString()), true); Directory.Delete(Path.Join(folderPath, regionId.ToString()), true);
} }
} }

View File

@ -11,4 +11,8 @@
<ProjectReference Include="..\Pathfinding\Pathfinding.csproj" /> <ProjectReference Include="..\Pathfinding\Pathfinding.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="System.Runtime.Serialization.Json" Version="4.3.0" />
</ItemGroup>
</Project> </Project>