Compare commits

..

2 Commits

Author SHA1 Message Date
0f53ae579c Made speedtype any generic.
Will use any connection (highway), and return the same speed for all highways.
2023-04-09 18:27:53 +02:00
d8f8a41dcc Renamed PathNode distance and weight to include "delta".
Added directDistanceDelta
2023-04-09 17:52:37 +02:00
5 changed files with 33 additions and 20 deletions

View File

@ -37,7 +37,7 @@ app.MapGet("/getRouteTime", (float latStart, float lonStart, float latEnd, float
app.MapGet("/getClosestNode", (float lat, float lon) =>
{
RegionManager regionManager = new RegionManager("D:/stuttgart-regbez-latest");
return regionManager.ClosestNodeToCoordinates(new Coordinates(lat, lon), Tag.SpeedType.road);
return regionManager.ClosestNodeToCoordinates(new Coordinates(lat, lon), Tag.SpeedType.any);
});
// Configure the HTTP request pipeline.

View File

@ -163,5 +163,5 @@ public class Tag
public enum WayType : byte { NONE, motorway, trunk, primary, secondary, tertiary, unclassified, residential, motorway_link, trunk_link, primary_link, secondary_link, tertiary_link, living_street, service, pedestrian, track, bus_guideway, escape, raceway, road, busway, footway, bridleway, steps, corridor, path, cycleway, construction }
// ReSharper restore InconsistentNaming
public enum SpeedType { pedestrian, car, road }
public enum SpeedType { pedestrian, car, any }
}

View File

@ -7,10 +7,11 @@ namespace Pathfinding;
public class PathNode : OsmNode
{
[JsonInclude]public new double directDistanceToGoal = double.MaxValue;
[JsonInclude]public double directDistanceDelta = double.MaxValue;
[JsonInclude]public new double currentPathLength = double.MaxValue;
[JsonInclude]public double distanceBetweenNodes = double.MaxValue;
[JsonInclude]public double pathDistanceDelta = double.MaxValue;
[JsonInclude]public new double currentPathWeight = double.MaxValue;
[JsonInclude]public double weightBetweenNodes = double.MaxValue;
[JsonInclude]public double pathWeightDelta = double.MaxValue;
[JsonInclude]public Dictionary<string, string> tags = new();
public PathNode(ulong nodeId, float lat, float lon) : base(nodeId, lat, lon)
@ -21,7 +22,7 @@ public class PathNode : OsmNode
{
}
public static PathNode? FromOsmNode(OsmNode? node, HashSet<Tag>? tags, double distance, double weight)
public static PathNode? FromOsmNode(OsmNode? node, HashSet<Tag>? tags, double pathDistanceDelta, double pathWeightDelta, double directDistanceDelta)
{
if (node is null)
return null;
@ -30,8 +31,9 @@ public class PathNode : OsmNode
currentPathLength = node.currentPathLength,
currentPathWeight = double.IsPositiveInfinity(node.currentPathWeight) ? double.MaxValue : node.currentPathWeight,
directDistanceToGoal = node.directDistanceToGoal,
distanceBetweenNodes = distance,
weightBetweenNodes = weight
directDistanceDelta = directDistanceDelta,
pathDistanceDelta = pathDistanceDelta,
pathWeightDelta = pathWeightDelta
};
if (tags != null)
foreach (Tag tag in tags)

View File

@ -9,8 +9,8 @@ public static partial class Pathfinder
private static ValueTuple<OsmNode?, OsmNode?> SetupNodes(Coordinates startCoordinates, Coordinates goalCoordinates, RegionManager regionManager )
{
ValueTuple<OsmNode?, OsmNode?> retTuple = new();
retTuple.Item1 = regionManager.ClosestNodeToCoordinates(startCoordinates, Tag.SpeedType.road);
retTuple.Item2 = regionManager.ClosestNodeToCoordinates(goalCoordinates, Tag.SpeedType.road);
retTuple.Item1 = regionManager.ClosestNodeToCoordinates(startCoordinates, Tag.SpeedType.any);
retTuple.Item2 = regionManager.ClosestNodeToCoordinates(goalCoordinates, Tag.SpeedType.any);
if (retTuple.Item1 is null || retTuple.Item2 is null)
return retTuple;
retTuple.Item1.currentPathWeight = 0;
@ -38,17 +38,20 @@ public static partial class Pathfinder
while (currentNode is not null)
{
HashSet<Tag>? tags = null;
double distance = 0;
double weight = 0;
double pathDistanceDelta = 0;
double pathWeightDelta = 0;
double directDistanceDelta = 0;
if (currentNode.previousPathNode is not null)
{
OsmEdge edge = currentNode.previousPathNode!.edges.First(e => e.neighborId.Equals(currentNode.nodeId));
tags = regionManager.GetRegion(currentNode.coordinates)!.tagManager.GetTagsForWayId(edge.wayId);
distance = currentNode.currentPathLength - currentNode.previousPathNode.currentPathLength;
weight = currentNode.currentPathWeight - currentNode.previousPathNode.currentPathWeight;
pathDistanceDelta = currentNode.currentPathLength - currentNode.previousPathNode.currentPathLength;
pathWeightDelta = currentNode.currentPathWeight - currentNode.previousPathNode.currentPathWeight;
directDistanceDelta =
currentNode.directDistanceToGoal - currentNode.previousPathNode.directDistanceToGoal;
}
PathNode? pn = PathNode.FromOsmNode(currentNode, tags, distance, weight);
PathNode? pn = PathNode.FromOsmNode(currentNode, tags, pathDistanceDelta, pathWeightDelta, directDistanceDelta);
if(pn is not null)
path.Add(pn!);
currentNode = currentNode.previousPathNode;

View File

@ -74,6 +74,8 @@ namespace Pathfinding
public bool TestValidConnectionForType(OsmNode node1, OsmEdge edge, Tag.SpeedType type)
{
if (type == Tag.SpeedType.any)
return true;
double speed = GetSpeedForEdge(node1, edge.wayId, type);
if (speed != 0)
return true;
@ -89,13 +91,18 @@ namespace Pathfinding
return null;
foreach (OsmNode node in region.nodes)
{
bool hasConnectionUsingVehicle = false;
foreach (OsmEdge edge in node.edges)
bool hasConnectionUsingVehicle = true;
if (vehicle is not Tag.SpeedType.any)
{
double speed = GetSpeedForEdge(node, edge.wayId, vehicle);
if (speed != 0)
hasConnectionUsingVehicle = true;
hasConnectionUsingVehicle = false;
foreach (OsmEdge edge in node.edges)
{
double speed = GetSpeedForEdge(node, edge.wayId, vehicle);
if (speed != 0)
hasConnectionUsingVehicle = true;
}
}
double nodeDistance = Utils.DistanceBetween(node, coordinates);
if (nodeDistance < distance && hasConnectionUsingVehicle)
{
@ -119,7 +126,6 @@ namespace Pathfinding
return speed;
return 0;
case Tag.SpeedType.car:
case Tag.SpeedType.road:
byte? maxSpeed = (byte?)tags.GetTag(wayId, Tag.TagType.maxspeed);
if (maxSpeed is not null)
return (double)maxSpeed;
@ -127,6 +133,8 @@ namespace Pathfinding
if(maxSpeed is not 0)
return (byte)maxSpeed;
return 0;
case Tag.SpeedType.any:
return 1;
default:
return 0;
}