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.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;

View File

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

View File

@ -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;

View File

@ -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<OsmEdge> edges { get; }
public HashSet<OsmEdge> 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;

View File

@ -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<OsmNode> 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<OsmNode> 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<OsmNode>();
}
public Region(Coordinates coordinates)
{
regionHash = Coordinates.GetRegionHashCode(coordinates);
tagManager = new TagManager();
nodes = new HashSet<OsmNode>();
}
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<Region>(new FileStream(filePath, FileMode.Open), serializerOptions)!;
else return null;
}

View File

@ -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)

View File

@ -1,11 +1,16 @@
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
namespace OSMDatastructure.Graph;
[Serializable]
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)
{

View File

@ -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<ulong, ulong> GetNodesAndRegions(XmlReader xmlReader, string outputPath)
{
BinaryFormatter bFormatter = new BinaryFormatter();
Dictionary<ulong, ulong> nodeRegions = new();
Dictionary<ulong, FileStream> regionFileStreams = new();
Dictionary<ulong, OsmNode> 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<ulong> regionHashes)
{
BinaryFormatter bFormatter = new BinaryFormatter();
foreach (ulong regionId in regionHashes)
{
ValueTuple<Region, HashSet<OsmEdge>> 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);
}
}

View File

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