Compare commits
10 Commits
c238a9eed3
...
308579279b
Author | SHA1 | Date | |
---|---|---|---|
308579279b | |||
2131ac4afe | |||
428fcb9bf8 | |||
6b496957d7 | |||
874d60992e | |||
c1557b7678 | |||
14533c150f | |||
5289020d44 | |||
bb789e731d | |||
9e72d50448 |
@ -1,4 +1,3 @@
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using OSMDatastructure;
|
||||
using OSMDatastructure.Graph;
|
||||
@ -15,22 +14,37 @@ builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapGet("/getRouteDistance", (float latStart, float lonStart, float latEnd, float lonEnd) =>
|
||||
app.MapGet("/getRouteBeta", (float latStart, float lonStart, float latEnd, float lonEnd, Tag.SpeedType vehicle, double stayOnSameRoadPriority, double useHigherLevelRoadsPriority, double useRoadsWithLessJunctionsPriority) =>
|
||||
{
|
||||
DateTime startCalc = DateTime.Now;
|
||||
List<PathNode> result = Pathfinder.AStarDistance("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd));
|
||||
List<PathNode> result = Pathfinder.AStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart),
|
||||
new Coordinates(latEnd, lonEnd), vehicle, useHigherLevelRoadsPriority, stayOnSameRoadPriority,
|
||||
useRoadsWithLessJunctionsPriority);
|
||||
PathResult pathResult = new PathResult(DateTime.Now - startCalc, result);
|
||||
return RenderPath.Renderer.DrawFromPath(result);
|
||||
}
|
||||
);
|
||||
|
||||
app.MapGet("/getRoute", (float latStart, float lonStart, float latEnd, float lonEnd, Tag.SpeedType vehicle, double stayOnSameRoadPriority, double useHigherLevelRoadsPriority, double useRoadsWithLessJunctionsPriority) =>
|
||||
{
|
||||
DateTime startCalc = DateTime.Now;
|
||||
List<PathNode> result = Pathfinder.AStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart),
|
||||
new Coordinates(latEnd, lonEnd), vehicle, useHigherLevelRoadsPriority, stayOnSameRoadPriority,
|
||||
useRoadsWithLessJunctionsPriority);
|
||||
PathResult pathResult = new PathResult(DateTime.Now - startCalc, result);
|
||||
return pathResult;
|
||||
}
|
||||
);
|
||||
|
||||
app.MapGet("/getRouteTime", (float latStart, float lonStart, float latEnd, float lonEnd, Tag.SpeedType vehicle) =>
|
||||
app.MapGet("/getShortestRoute", (float latStart, float lonStart, float latEnd, float lonEnd) =>
|
||||
{
|
||||
DateTime startCalc = DateTime.Now;
|
||||
List<PathNode> result = Pathfinder.AStarTime("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd), vehicle);
|
||||
List<PathNode> result = Pathfinder.AStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart),
|
||||
new Coordinates(latEnd, lonEnd), Tag.SpeedType.any, 0, 0,
|
||||
0);
|
||||
PathResult pathResult = new PathResult(DateTime.Now - startCalc, result);
|
||||
return pathResult;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
app.MapGet("/getClosestNode", (float lat, float lon) =>
|
||||
@ -57,18 +71,11 @@ app.Run();
|
||||
internal class PathResult
|
||||
{
|
||||
[JsonInclude]public TimeSpan calcTime;
|
||||
[JsonInclude] public double pathWeight = double.MaxValue;
|
||||
[JsonInclude] public double pathTravelDistance = double.MaxValue;
|
||||
[JsonInclude]public List<PathNode> pathNodes;
|
||||
|
||||
public PathResult(TimeSpan calcTime, List<PathNode> pathNodes)
|
||||
{
|
||||
this.calcTime = calcTime;
|
||||
this.pathNodes = pathNodes;
|
||||
if (pathNodes.Count > 0)
|
||||
{
|
||||
this.pathWeight = pathNodes.Last().currentPathWeight;
|
||||
this.pathTravelDistance = pathNodes.Last().currentPathLength;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace OSMDatastructure.Graph;
|
||||
@ -8,21 +7,8 @@ public class OsmNode
|
||||
{
|
||||
public ulong nodeId { get; }
|
||||
public HashSet<OsmEdge> edges { get; set; }
|
||||
public Coordinates coordinates { get; }
|
||||
|
||||
[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;
|
||||
public Coordinates coordinates { get; }
|
||||
|
||||
[OnDeserialized]
|
||||
internal void SetDefaultValues(StreamingContext context)
|
||||
{
|
||||
currentPathWeight = double.MaxValue;
|
||||
currentPathLength = double.MaxValue;
|
||||
directDistanceToGoal = double.MaxValue;
|
||||
}
|
||||
|
||||
public OsmNode(ulong nodeId, float lat, float lon)
|
||||
{
|
||||
this.nodeId = nodeId;
|
||||
@ -53,9 +39,6 @@ public class OsmNode
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if(previousPathNode is not null)
|
||||
return $"{nodeId} {coordinates} ec:{edges.Count} d:{directDistanceToGoal} w:{currentPathWeight} l:{currentPathLength} p:{previousPathNode.nodeId}";
|
||||
return
|
||||
$"{nodeId} {coordinates} ec:{edges.Count} d:{directDistanceToGoal} w:{currentPathWeight} l:{currentPathLength} null";
|
||||
return $"{nodeId} {coordinates} ec:{edges.Count}";
|
||||
}
|
||||
}
|
@ -30,6 +30,10 @@ public class Tag
|
||||
case TagType.id:
|
||||
this.value = value.GetUInt64();
|
||||
break;
|
||||
case TagType.name:
|
||||
case TagType.tagref:
|
||||
this.value = value.GetString();
|
||||
break;
|
||||
default:
|
||||
this.value = value;
|
||||
break;
|
||||
@ -41,49 +45,58 @@ public class Tag
|
||||
}
|
||||
}
|
||||
|
||||
public static Tag? ConvertToTag(string key, string value)
|
||||
public static HashSet<Tag> ConvertToTags(string key, string value)
|
||||
{
|
||||
HashSet<Tag> ret = new HashSet<Tag>();
|
||||
switch (key)
|
||||
{
|
||||
case "highway":
|
||||
try
|
||||
{
|
||||
return new Tag(TagType.highway, (WayType)Enum.Parse(typeof(WayType), value, true));
|
||||
ret.Add(new Tag(TagType.highway, (WayType)Enum.Parse(typeof(WayType), value, true)));
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return new Tag(TagType.highway, WayType.unclassified);
|
||||
ret.Add(new Tag(TagType.highway, WayType.unclassified));
|
||||
}
|
||||
break;
|
||||
case "maxspeed":
|
||||
try
|
||||
{
|
||||
byte speed = Convert.ToByte(value);
|
||||
if (speed == 255)
|
||||
return new Tag(TagType.highway, false);
|
||||
else
|
||||
return new Tag(TagType.maxspeed, speed);
|
||||
if (speed != 255)
|
||||
ret.Add(new Tag(TagType.maxspeed, speed));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//Console.WriteLine(e);
|
||||
//Console.WriteLine("Continuing...");
|
||||
return new Tag(TagType.maxspeed, byte.MaxValue);
|
||||
ret.Add(new Tag(TagType.maxspeed, byte.MinValue));
|
||||
}
|
||||
break;
|
||||
case "oneway":
|
||||
switch (value)
|
||||
{
|
||||
case "yes":
|
||||
return new Tag(TagType.oneway, true);
|
||||
ret.Add(new Tag(TagType.oneway, true));
|
||||
break;
|
||||
case "-1":
|
||||
return new Tag(TagType.forward, false);
|
||||
ret.Add(new Tag(TagType.forward, false));
|
||||
ret.Add(new Tag(TagType.oneway, true));
|
||||
break;
|
||||
case "no":
|
||||
return new Tag(TagType.oneway, false);
|
||||
ret.Add(new Tag(TagType.oneway, false));
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
case "name":
|
||||
ret.Add(new Tag(TagType.name, value));
|
||||
break;
|
||||
case "ref":
|
||||
ret.Add(new Tag(TagType.tagref, value));
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
@ -93,7 +106,7 @@ public class Tag
|
||||
|
||||
public enum TagType : byte
|
||||
{
|
||||
highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id
|
||||
highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id, tagref
|
||||
}
|
||||
|
||||
public static readonly Dictionary<WayType, byte> defaultSpeedCar = new() {
|
||||
@ -104,8 +117,8 @@ public class Tag
|
||||
{ WayType.primary, 65 },
|
||||
{ WayType.secondary, 60 },
|
||||
{ WayType.tertiary, 50 },
|
||||
{ WayType.unclassified, 15 },
|
||||
{ WayType.residential, 10 },
|
||||
{ WayType.unclassified, 30 },
|
||||
{ WayType.residential, 15 },
|
||||
{ WayType.motorway_link, 60 },
|
||||
{ WayType.trunk_link, 50 },
|
||||
{ WayType.primary_link, 50 },
|
||||
@ -114,7 +127,7 @@ public class Tag
|
||||
{ WayType.living_street, 10 },
|
||||
{ WayType.service, 1 },
|
||||
{ WayType.pedestrian, 0 },
|
||||
{ WayType.track, 15 },
|
||||
{ WayType.track, 1 },
|
||||
{ WayType.bus_guideway, 0 },
|
||||
{ WayType.escape, 0 },
|
||||
{ WayType.raceway, 0 },
|
||||
|
@ -25,9 +25,10 @@ public class TagManager
|
||||
|
||||
public void AddTag(ulong wayId, string key, string value)
|
||||
{
|
||||
Tag? tag = Tag.ConvertToTag(key, value);
|
||||
if(tag is not null)
|
||||
AddTag(wayId, tag);
|
||||
HashSet<Tag> pTags = Tag.ConvertToTags(key, value);
|
||||
if(pTags.Count > 0)
|
||||
foreach (Tag pTag in pTags)
|
||||
AddTag(wayId, pTag);
|
||||
}
|
||||
|
||||
public void AddTag(ulong wayId, Tag tag)
|
||||
|
@ -6,12 +6,6 @@ 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 pathDistanceDelta = double.MaxValue;
|
||||
[JsonInclude]public new double currentPathWeight = 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)
|
||||
@ -22,19 +16,11 @@ public class PathNode : OsmNode
|
||||
{
|
||||
}
|
||||
|
||||
public static PathNode? FromOsmNode(OsmNode? node, HashSet<Tag>? tags, double pathDistanceDelta, double pathWeightDelta, double directDistanceDelta)
|
||||
public static PathNode? FromOsmNode(OsmNode? node, HashSet<Tag>? tags)
|
||||
{
|
||||
if (node is null)
|
||||
return null;
|
||||
PathNode retNode = new(node.nodeId, node.coordinates)
|
||||
{
|
||||
currentPathLength = node.currentPathLength,
|
||||
currentPathWeight = double.IsPositiveInfinity(node.currentPathWeight) ? double.MaxValue : node.currentPathWeight,
|
||||
directDistanceToGoal = node.directDistanceToGoal,
|
||||
directDistanceDelta = directDistanceDelta,
|
||||
pathDistanceDelta = pathDistanceDelta,
|
||||
pathWeightDelta = pathWeightDelta
|
||||
};
|
||||
PathNode retNode = new(node.nodeId, node.coordinates);
|
||||
if (tags != null)
|
||||
foreach (Tag tag in tags)
|
||||
{
|
||||
|
@ -1,78 +1,129 @@
|
||||
using OSMDatastructure;
|
||||
using OSMDatastructure.Graph;
|
||||
using static OSMDatastructure.Tag;
|
||||
using WayType = OSMDatastructure.Tag.WayType;
|
||||
|
||||
namespace Pathfinding;
|
||||
|
||||
public static partial class Pathfinder
|
||||
public static class Pathfinder
|
||||
{
|
||||
|
||||
private static ValueTuple<OsmNode?, OsmNode?> SetupNodes(Coordinates startCoordinates, Coordinates goalCoordinates, RegionManager regionManager )
|
||||
|
||||
public static List<PathNode> AStar(string workingDir, Coordinates startCoordinates, Coordinates goalCoordinates,
|
||||
SpeedType vehicle, double heuristicRoadLevelPriority, double heuristicSameRoadPriority,
|
||||
double heuristicFewJunctionsPriority)
|
||||
{
|
||||
ValueTuple<OsmNode?, OsmNode?> retTuple = new();
|
||||
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;
|
||||
retTuple.Item1.currentPathLength = 0;
|
||||
retTuple.Item1.directDistanceToGoal = Utils.DistanceBetween(retTuple.Item1, retTuple.Item2);
|
||||
return retTuple;
|
||||
}
|
||||
RegionManager regionManager = new RegionManager(workingDir);
|
||||
OsmNode? startNode = regionManager.ClosestNodeToCoordinates(startCoordinates, vehicle);
|
||||
OsmNode? goalNode = regionManager.ClosestNodeToCoordinates(goalCoordinates, vehicle);
|
||||
if (startNode is null || goalNode is null)
|
||||
return new List<PathNode>();
|
||||
|
||||
private static double EdgeWeight(OsmNode node1, OsmEdge edge, Tag.SpeedType vehicle, RegionManager regionManager)
|
||||
{
|
||||
OsmNode? node2 = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
||||
if (node2 is null)
|
||||
return double.MaxValue;
|
||||
double distance = Utils.DistanceBetween(node1, node2);
|
||||
byte speed = regionManager.GetSpeedForEdge(node1, edge.wayId, vehicle);
|
||||
return speed is 0 ? double.MaxValue : distance / speed;
|
||||
}
|
||||
PriorityQueue<OsmNode, double> openSetfScore = new();
|
||||
openSetfScore.Enqueue(startNode, 0);
|
||||
Dictionary<OsmNode, OsmNode> cameFromDict = new();
|
||||
Dictionary<OsmNode, double> gScore = new();
|
||||
gScore.Add(startNode, 0);
|
||||
|
||||
private static double GetPriority(OsmNode current, OsmNode? previous, OsmEdge edge, Tag.SpeedType vehicle, RegionManager regionManager)
|
||||
{
|
||||
if (vehicle == Tag.SpeedType.any)
|
||||
return 1;
|
||||
const double roadPriorityFactor = 1;
|
||||
const double junctionFactor = 2;
|
||||
const double wayChangeFactor = 2;
|
||||
Region r = regionManager.GetRegion(current.coordinates)!;
|
||||
|
||||
double roadPriority = GetPriorityVehicleRoad(edge, vehicle, r) * 0.1 * roadPriorityFactor;
|
||||
double roadSpeed = regionManager.GetSpeedForEdge(current, edge.wayId, vehicle);
|
||||
|
||||
if(vehicle == Tag.SpeedType.pedestrian)
|
||||
return (current.directDistanceToGoal / roadSpeed) * roadPriority;
|
||||
|
||||
ulong previousWayId = UInt64.MaxValue;
|
||||
if (previous?.edges is not null)
|
||||
while (openSetfScore.Count > 0)
|
||||
{
|
||||
foreach (OsmEdge e in previous.edges)
|
||||
if (e.neighborId.Equals(current.nodeId))
|
||||
OsmNode currentNode = openSetfScore.Dequeue();
|
||||
if (currentNode.Equals(goalNode))
|
||||
return GetPath(cameFromDict, goalNode, regionManager);
|
||||
|
||||
foreach (OsmEdge edge in currentNode.edges)
|
||||
{
|
||||
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
||||
if (neighbor is not null)
|
||||
{
|
||||
previousWayId = e.wayId;
|
||||
break;
|
||||
double tentativeGScore = gScore[currentNode] + Weight(currentNode, neighbor, edge, vehicle, regionManager);
|
||||
gScore.TryAdd(neighbor, double.MaxValue);
|
||||
if (tentativeGScore < gScore[neighbor])
|
||||
{
|
||||
if (cameFromDict.ContainsKey(neighbor))
|
||||
cameFromDict[neighbor] = currentNode;
|
||||
else
|
||||
cameFromDict.Add(neighbor, currentNode);
|
||||
if (gScore.ContainsKey(neighbor))
|
||||
gScore[neighbor] = tentativeGScore;
|
||||
else
|
||||
gScore.Add(neighbor, tentativeGScore);
|
||||
double h = Heuristic(currentNode, neighbor, goalNode, edge, vehicle, regionManager,
|
||||
heuristicRoadLevelPriority, heuristicFewJunctionsPriority, heuristicSameRoadPriority);
|
||||
//Console.WriteLine($"Queue: {openSetfScore.Count:00000} Current Distance: {Utils.DistanceBetween(currentNode, goalNode):000000.00} Visited: {cameFromDict.Count:00000} Current heuristic: {h:00000.00}");
|
||||
openSetfScore.Enqueue(neighbor, tentativeGScore + h);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double junctionPriority = (current.edges.Count > 2 ? 1 : 0) * junctionFactor;
|
||||
double wayChange = (previousWayId != edge.wayId ? 1 : 0) * wayChangeFactor;
|
||||
|
||||
if(vehicle == Tag.SpeedType.car)
|
||||
return (current.directDistanceToGoal / roadSpeed) * roadPriority * ((junctionPriority + wayChange) / 2);
|
||||
|
||||
return double.MaxValue;
|
||||
return new List<PathNode>();
|
||||
}
|
||||
|
||||
private static double GetPriorityVehicleRoad(OsmEdge edge, Tag.SpeedType vehicle, Region region)
|
||||
private static List<PathNode> GetPath(Dictionary<OsmNode, OsmNode> cameFromDict, OsmNode goalNode, RegionManager regionManager)
|
||||
{
|
||||
if (vehicle == Tag.SpeedType.any)
|
||||
List<PathNode> path = new List<PathNode>();
|
||||
OsmNode currentNode = goalNode;
|
||||
while (cameFromDict.ContainsKey(cameFromDict[currentNode]))
|
||||
{
|
||||
OsmEdge? currentEdge = cameFromDict[currentNode].edges.First(edge => edge.neighborId == currentNode.nodeId);
|
||||
HashSet<Tag>? tags =
|
||||
regionManager.GetRegion(currentNode.coordinates)!.tagManager.GetTagsForWayId(currentEdge.wayId);
|
||||
PathNode? newNode = PathNode.FromOsmNode(currentNode, tags);
|
||||
if(newNode is not null)
|
||||
path.Add(newNode);
|
||||
currentNode = cameFromDict[currentNode];
|
||||
}
|
||||
|
||||
path.Reverse();
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
private static double Weight(OsmNode fromNode, OsmNode neighborNode, OsmEdge edge, SpeedType vehicle, RegionManager regionManager)
|
||||
{
|
||||
double distance = Utils.DistanceBetween(fromNode, neighborNode);
|
||||
double speed = regionManager.GetSpeedForEdge(fromNode, edge.wayId, vehicle);
|
||||
double prio = GetPriorityVehicleRoad(edge, vehicle, regionManager.GetRegion(fromNode.coordinates)!);
|
||||
return distance / (speed + (prio * 10));
|
||||
}
|
||||
|
||||
private static double Heuristic(OsmNode fromNode, OsmNode neighborNode, OsmNode goalNode, OsmEdge edge, SpeedType vehicle, RegionManager regionManager, double roadPriorityFactor, double junctionFactor, double sameRoadFactor)
|
||||
{
|
||||
double roadPriority = GetPriorityVehicleRoad(edge, vehicle, regionManager.GetRegion(fromNode.coordinates)!) * roadPriorityFactor;
|
||||
|
||||
TagManager curTags = regionManager.GetRegion(fromNode.coordinates)!.tagManager;
|
||||
TagManager nextTags = regionManager.GetRegion(neighborNode.coordinates)!.tagManager;
|
||||
|
||||
bool sameName = false;
|
||||
string? curName = (string?)curTags.GetTag(edge.wayId, TagType.name);
|
||||
bool sameRef = false;
|
||||
string? curRef = (string?)curTags.GetTag(edge.wayId, TagType.tagref);
|
||||
if(curName is not null)
|
||||
foreach (OsmEdge pEdge in neighborNode.edges)
|
||||
{
|
||||
if ((string?)nextTags.GetTag(pEdge.wayId, TagType.name) == curName)
|
||||
sameName = true;
|
||||
if ((string?)nextTags.GetTag(pEdge.wayId, TagType.tagref) == curRef)
|
||||
sameRef = true;
|
||||
}
|
||||
double sameRoadName = (sameRef || sameName ? 1 : 0) * sameRoadFactor;
|
||||
|
||||
double junctionCount = (neighborNode.edges.Count > 2 ? 0 : 1) * junctionFactor;
|
||||
|
||||
//Console.WriteLine($"{roadPriority:000.00} {sameRoadName:000.00} {junctionCount:000.00} {distanceImprovement:+000.00;-000.00;0000.00}");
|
||||
|
||||
return Utils.DistanceBetween(neighborNode, goalNode) - (roadPriority + sameRoadName + junctionCount) * 100;
|
||||
}
|
||||
|
||||
private static double GetPriorityVehicleRoad(OsmEdge edge, SpeedType vehicle, Region region)
|
||||
{
|
||||
if (vehicle == SpeedType.any)
|
||||
return 1;
|
||||
WayType? wayType = (WayType?)region.tagManager.GetTag(edge.wayId, Tag.TagType.highway);
|
||||
WayType? wayType = (WayType?)region.tagManager.GetTag(edge.wayId, TagType.highway);
|
||||
if(wayType is null)
|
||||
return double.MaxValue;
|
||||
if (vehicle == Tag.SpeedType.car)
|
||||
return 0;
|
||||
if (vehicle == SpeedType.car)
|
||||
{
|
||||
switch (wayType)
|
||||
{
|
||||
@ -83,26 +134,24 @@ public static partial class Pathfinder
|
||||
case WayType.trunk_link:
|
||||
case WayType.primary:
|
||||
case WayType.primary_link:
|
||||
return 1;
|
||||
return 8;
|
||||
case WayType.secondary:
|
||||
case WayType.secondary_link:
|
||||
return 2;
|
||||
return 6;
|
||||
case WayType.tertiary:
|
||||
case WayType.tertiary_link:
|
||||
return 3;
|
||||
return 5;
|
||||
case WayType.unclassified:
|
||||
case WayType.residential:
|
||||
case WayType.road:
|
||||
return 4;
|
||||
case WayType.living_street:
|
||||
return 2;
|
||||
case WayType.service:
|
||||
case WayType.track:
|
||||
return 5;
|
||||
default:
|
||||
return 100;
|
||||
return 0.01;
|
||||
}
|
||||
}
|
||||
if (vehicle == Tag.SpeedType.pedestrian)
|
||||
if (vehicle == SpeedType.pedestrian)
|
||||
{
|
||||
switch (wayType)
|
||||
{
|
||||
@ -113,54 +162,21 @@ public static partial class Pathfinder
|
||||
case WayType.steps:
|
||||
case WayType.residential:
|
||||
case WayType.living_street:
|
||||
return 1;
|
||||
return 10;
|
||||
case WayType.service:
|
||||
case WayType.cycleway:
|
||||
case WayType.bridleway:
|
||||
case WayType.road:
|
||||
case WayType.track:
|
||||
case WayType.unclassified:
|
||||
return 2;
|
||||
return 5;
|
||||
case WayType.tertiary:
|
||||
case WayType.tertiary_link:
|
||||
case WayType.escape:
|
||||
return 5;
|
||||
default:
|
||||
return 100;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
return 100;
|
||||
|
||||
}
|
||||
|
||||
private static List<PathNode> GetRouteFromCalc(OsmNode goalNode, RegionManager regionManager)
|
||||
{
|
||||
List<PathNode> path = new();
|
||||
OsmNode? currentNode = goalNode;
|
||||
while (currentNode is not null)
|
||||
{
|
||||
HashSet<Tag>? tags = null;
|
||||
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);
|
||||
pathDistanceDelta = currentNode.currentPathLength - currentNode.previousPathNode.currentPathLength;
|
||||
pathWeightDelta = currentNode.currentPathWeight - currentNode.previousPathNode.currentPathWeight;
|
||||
directDistanceDelta =
|
||||
currentNode.directDistanceToGoal - currentNode.previousPathNode.directDistanceToGoal;
|
||||
}
|
||||
|
||||
PathNode? pn = PathNode.FromOsmNode(currentNode, tags, pathDistanceDelta, pathWeightDelta, directDistanceDelta);
|
||||
if(pn is not null)
|
||||
path.Add(pn!);
|
||||
currentNode = currentNode.previousPathNode;
|
||||
}
|
||||
|
||||
path.Reverse();
|
||||
return path;
|
||||
return 0.01;
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
using OSMDatastructure;
|
||||
using OSMDatastructure.Graph;
|
||||
|
||||
namespace Pathfinding;
|
||||
|
||||
public static partial class Pathfinder
|
||||
{
|
||||
public static List<PathNode> AStarDistance(string workingDir, Coordinates start,
|
||||
Coordinates goal)
|
||||
{
|
||||
RegionManager regionManager = new (workingDir);
|
||||
ValueTuple<OsmNode?, OsmNode?> startAndEndNode = SetupNodes(start, goal, regionManager);
|
||||
if (startAndEndNode.Item1 is null || startAndEndNode.Item2 is null)
|
||||
return new List<PathNode>();
|
||||
OsmNode goalNode = startAndEndNode.Item2!;
|
||||
|
||||
PriorityQueue<OsmNode, double> toVisit = new();
|
||||
toVisit.Enqueue(startAndEndNode.Item1, 0);
|
||||
bool stop = false;
|
||||
|
||||
while (toVisit.Count > 0)
|
||||
{
|
||||
OsmNode currentNode = toVisit.Dequeue();
|
||||
|
||||
foreach (OsmEdge edge in currentNode.edges)
|
||||
{
|
||||
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
||||
if (neighbor is not null)
|
||||
{
|
||||
if (Math.Abs(neighbor.directDistanceToGoal - double.MaxValue) < 1)
|
||||
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
||||
double newPotentialLength = currentNode.currentPathLength + Utils.DistanceBetween(currentNode, neighbor);
|
||||
if (newPotentialLength < neighbor.currentPathLength)
|
||||
{
|
||||
neighbor.previousPathNode = currentNode;
|
||||
neighbor.currentPathLength = newPotentialLength;
|
||||
|
||||
if(neighbor.Equals(goalNode))
|
||||
return GetRouteFromCalc(goalNode, regionManager);
|
||||
//stop = true;
|
||||
if (!toVisit.UnorderedItems.Any(item => item.Element.Equals(neighbor)) && !stop)
|
||||
{
|
||||
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return GetRouteFromCalc(goalNode, regionManager);
|
||||
}
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
using OSMDatastructure;
|
||||
using OSMDatastructure.Graph;
|
||||
using Utils = OSMDatastructure.Utils;
|
||||
|
||||
namespace Pathfinding;
|
||||
|
||||
public static partial class Pathfinder
|
||||
{
|
||||
public static List<PathNode> AStarTime(string workingDir, Coordinates start,
|
||||
Coordinates goal, Tag.SpeedType vehicle)
|
||||
{
|
||||
RegionManager regionManager = new (workingDir);
|
||||
ValueTuple<OsmNode?, OsmNode?> startAndEndNode = SetupNodes(start, goal, regionManager);
|
||||
if (startAndEndNode.Item1 is null || startAndEndNode.Item2 is null)
|
||||
return new List<PathNode>();
|
||||
OsmNode goalNode = startAndEndNode.Item2!;
|
||||
|
||||
PriorityQueue<OsmNode, double> toVisit = new();
|
||||
toVisit.Enqueue(startAndEndNode.Item1, 0);
|
||||
bool stop = false;
|
||||
|
||||
while (toVisit.Count > 0)
|
||||
{
|
||||
OsmNode currentNode = toVisit.Dequeue();
|
||||
|
||||
foreach (OsmEdge edge in currentNode.edges.Where(
|
||||
edge => regionManager.TestValidConnectionForType(currentNode, edge, vehicle)))
|
||||
{
|
||||
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
||||
if (neighbor is not null)
|
||||
{
|
||||
if (Math.Abs(neighbor.directDistanceToGoal - double.MaxValue) < 1)
|
||||
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
||||
|
||||
double newPotentialWeight = currentNode.currentPathWeight +
|
||||
EdgeWeight(currentNode, edge, vehicle, regionManager);
|
||||
|
||||
if (newPotentialWeight < neighbor.currentPathWeight)
|
||||
{
|
||||
neighbor.previousPathNode = currentNode;
|
||||
neighbor.currentPathWeight = newPotentialWeight;
|
||||
|
||||
if (neighbor.Equals(goalNode))
|
||||
{
|
||||
currentNode = neighbor;
|
||||
currentNode.currentPathLength = 0;
|
||||
while (currentNode != startAndEndNode.Item1 && currentNode.previousPathNode is not null)
|
||||
{
|
||||
currentNode.previousPathNode.currentPathLength = currentNode.currentPathLength +
|
||||
Utils.DistanceBetween(currentNode, currentNode.previousPathNode);
|
||||
currentNode = currentNode.previousPathNode;
|
||||
}
|
||||
|
||||
return GetRouteFromCalc(goalNode, regionManager);
|
||||
}
|
||||
//stop = true;
|
||||
if (!toVisit.UnorderedItems.Any(item => item.Element.Equals(neighbor)))
|
||||
toVisit.Enqueue(neighbor, GetPriority(currentNode, currentNode.previousPathNode, edge, vehicle, regionManager));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return GetRouteFromCalc(goalNode, regionManager);
|
||||
}
|
||||
}
|
@ -95,14 +95,13 @@ namespace Pathfinding
|
||||
hasConnectionUsingVehicle = false;
|
||||
foreach (OsmEdge edge in node.edges)
|
||||
{
|
||||
byte speed = GetSpeedForEdge(node, edge.wayId, vehicle);
|
||||
if (speed != 0)
|
||||
if (TestValidConnectionForType(node, edge, vehicle))
|
||||
hasConnectionUsingVehicle = true;
|
||||
}
|
||||
}
|
||||
|
||||
double nodeDistance = Utils.DistanceBetween(node, coordinates);
|
||||
if (nodeDistance < distance && hasConnectionUsingVehicle)
|
||||
if (hasConnectionUsingVehicle && nodeDistance < distance)
|
||||
{
|
||||
closest = node;
|
||||
distance = nodeDistance;
|
||||
@ -121,7 +120,7 @@ namespace Pathfinding
|
||||
{
|
||||
case Tag.SpeedType.pedestrian:
|
||||
speed = Tag.defaultSpeedPedestrian[wayType];
|
||||
return speed is not 0 ? speed : (byte)0;
|
||||
return speed;
|
||||
case Tag.SpeedType.car:
|
||||
byte? maxSpeed = (byte?)tags.GetTag(wayId, Tag.TagType.maxspeed);
|
||||
speed = Tag.defaultSpeedCar[wayType];
|
||||
|
@ -129,9 +129,10 @@ public class RegionConverter
|
||||
currentTags.TryAdd(Tag.TagType.id, Convert.ToUInt64(wayReader.GetAttribute("id")!));
|
||||
if (wayReader.Name == "tag")
|
||||
{
|
||||
Tag? wayTag = Tag.ConvertToTag(wayReader.GetAttribute("k")!, wayReader.GetAttribute("v")!);
|
||||
if(wayTag is not null)
|
||||
currentTags.TryAdd(wayTag.key, wayTag.value);
|
||||
HashSet<Tag> pTags = Tag.ConvertToTags(wayReader.GetAttribute("k")!, wayReader.GetAttribute("v")!);
|
||||
if(pTags.Count > 0)
|
||||
foreach (Tag pTag in pTags)
|
||||
currentTags.TryAdd(pTag.key, pTag.value);
|
||||
}
|
||||
else if (wayReader.Name == "nd")
|
||||
{
|
||||
@ -146,31 +147,42 @@ public class RegionConverter
|
||||
{
|
||||
ulong node1Id = currentNodeIds[i];
|
||||
ulong node2Id = currentNodeIds[i+1];
|
||||
if (currentTags.ContainsKey(Tag.TagType.oneway) && (bool)currentTags[Tag.TagType.oneway] && nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id))
|
||||
if (nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id))
|
||||
{
|
||||
if (currentTags.ContainsKey(Tag.TagType.forward) && !(bool)currentTags[Tag.TagType.forward])
|
||||
if (currentTags.ContainsKey(Tag.TagType.oneway) && (bool)currentTags[Tag.TagType.oneway])
|
||||
{
|
||||
OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]);
|
||||
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
|
||||
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath);
|
||||
if (currentTags.ContainsKey(Tag.TagType.forward) && !(bool)currentTags[Tag.TagType.forward])
|
||||
{
|
||||
OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id,
|
||||
nodeRegions[node1Id]);
|
||||
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
|
||||
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id],
|
||||
currentTags, outputPath);
|
||||
}
|
||||
else if (currentTags.ContainsKey(Tag.TagType.forward) && (bool)currentTags[Tag.TagType.forward])
|
||||
{
|
||||
OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id,
|
||||
nodeRegions[node2Id]);
|
||||
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath);
|
||||
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id],
|
||||
currentTags, outputPath);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]);
|
||||
OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id,
|
||||
nodeRegions[node2Id]);
|
||||
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath);
|
||||
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath);
|
||||
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id],
|
||||
currentTags, outputPath);
|
||||
|
||||
OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id,
|
||||
nodeRegions[node1Id]);
|
||||
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
|
||||
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id],
|
||||
currentTags, outputPath);
|
||||
}
|
||||
}
|
||||
else if(nodeRegions.ContainsKey(node1Id) && nodeRegions.ContainsKey(node2Id))
|
||||
{
|
||||
OsmEdge n12e = new OsmEdge(currentTags[Tag.TagType.id], node1Id, node2Id, nodeRegions[node2Id]);
|
||||
WriteWay(ref regionWaysFileStreams, nodeRegions[node1Id], n12e, outputPath);
|
||||
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node1Id], currentTags, outputPath);
|
||||
|
||||
OsmEdge n21e = new OsmEdge(currentTags[Tag.TagType.id], node2Id, node1Id, nodeRegions[node2Id]);
|
||||
WriteWay(ref regionWaysFileStreams, nodeRegions[node2Id], n21e, outputPath);
|
||||
WriteTags(ref regionTagsFileStreams, ref writtenWayTagsInRegion, nodeRegions[node2Id], currentTags, outputPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user