OSMServer/OSMDatastructure/OsmNode.cs

62 lines
2.0 KiB
C#
Raw Normal View History

using System.ComponentModel;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
namespace OSMDatastructure.Graph;
2023-02-06 17:32:55 +01:00
[Serializable]
2023-02-07 23:52:23 +01:00
public class OsmNode
2023-02-06 17:32:55 +01:00
{
public ulong nodeId { get; }
public HashSet<OsmEdge> edges { get; set; }
2023-02-06 17:32:55 +01:00
public Coordinates coordinates { get; }
[JsonIgnore][NonSerialized]public OsmNode? previousPathNode = null;
2023-04-06 02:22:03 +02:00
[JsonInclude][NonSerialized]public double currentPathWeight = double.MaxValue;
[JsonInclude][NonSerialized]public double currentPathLength = double.MaxValue;
[JsonIgnore][NonSerialized]public double directDistanceToGoal = double.MaxValue;
[OnDeserialized]
internal void SetDefaultValues(StreamingContext context)
{
currentPathWeight = double.MaxValue;
currentPathLength = double.MaxValue;
directDistanceToGoal = double.MaxValue;
}
2023-02-06 17:32:55 +01:00
public OsmNode(ulong nodeId, float lat, float lon)
2023-02-06 17:32:55 +01:00
{
this.nodeId = nodeId;
2023-02-06 17:32:55 +01:00
this.edges = new();
this.coordinates = new Coordinates(lat, lon);
}
[JsonConstructor]
public OsmNode(ulong nodeId, Coordinates coordinates)
2023-02-06 17:32:55 +01:00
{
this.nodeId = nodeId;
2023-02-06 17:32:55 +01:00
this.edges = new();
2023-02-07 23:52:23 +01:00
this.coordinates = coordinates;
2023-02-06 17:32:55 +01:00
}
public OsmEdge? GetEdgeToNode(OsmNode n)
2023-02-06 17:32:55 +01:00
{
HashSet<OsmEdge> e = edges.Where(edge => edge.neighborId == n.nodeId).ToHashSet();
if (e.Count > 0)
return e.First();
else return null;
2023-02-06 17:32:55 +01:00
}
public override bool Equals(object? obj)
{
return obj != null && obj.GetType() == this.GetType() && ((OsmNode)obj).nodeId == this.nodeId;
}
public override string ToString()
{
2023-04-01 14:19:36 +02:00
if(previousPathNode is not null)
return $"{nodeId} {coordinates} ec:{edges.Count} d:{directDistanceToGoal} w:{currentPathWeight} l:{currentPathLength} p:{previousPathNode.nodeId}";
2023-04-01 14:19:36 +02:00
return
$"{nodeId} {coordinates} ec:{edges.Count} d:{directDistanceToGoal} w:{currentPathWeight} l:{currentPathLength} null";
}
2023-02-06 17:32:55 +01:00
}