Compare commits
17 Commits
fe0ccd0fca
...
6eab23ff16
Author | SHA1 | Date | |
---|---|---|---|
6eab23ff16 | |||
206f9c5811 | |||
e0bb3ce3de | |||
2904be84f0 | |||
13beaeaf73 | |||
ea7ce1f630 | |||
9c7fec1c37 | |||
5efec08bbc | |||
05ae0bff6e | |||
8bd0c5a4d4 | |||
feb9b70e50 | |||
9ef0e421bc | |||
a54b189b08 | |||
585a9213ce | |||
bf08f38a1e | |||
fc5d388ecd | |||
58d1031524 |
@ -1,6 +1,6 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
using OSMDatastructure;
|
||||||
using OSMDatastructure.Graph;
|
using OSMDatastructure.Graph;
|
||||||
using OSMImporter;
|
|
||||||
using Pathfinding;
|
using Pathfinding;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@ -16,11 +16,20 @@ var app = builder.Build();
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.MapGet("/getRoute", (float latStart, float lonStart, float latEnd, float lonEnd) =>
|
app.MapGet("/getRouteDistance", (float latStart, float lonStart, float latEnd, float lonEnd) =>
|
||||||
{
|
{
|
||||||
ValueTuple<TimeSpan, List<PathNode>> result = Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd),
|
DateTime startCalc = DateTime.Now;
|
||||||
Tag.SpeedType.car);
|
List<PathNode> result = Pathfinder.AStarDistance("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd));
|
||||||
PathResult pathResult = new PathResult(result.Item1, result.Item2);
|
PathResult pathResult = new PathResult(DateTime.Now - startCalc, result);
|
||||||
|
return pathResult;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
app.MapGet("/getRouteTime", (float latStart, float lonStart, float latEnd, float lonEnd, Tag.SpeedType vehicle) =>
|
||||||
|
{
|
||||||
|
DateTime startCalc = DateTime.Now;
|
||||||
|
List<PathNode> result = Pathfinder.AStarTime("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd), vehicle);
|
||||||
|
PathResult pathResult = new PathResult(DateTime.Now - startCalc, result);
|
||||||
return pathResult;
|
return pathResult;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -28,7 +37,7 @@ app.MapGet("/getRoute", (float latStart, float lonStart, float latEnd, float lon
|
|||||||
app.MapGet("/getClosestNode", (float lat, float lon) =>
|
app.MapGet("/getClosestNode", (float lat, float lon) =>
|
||||||
{
|
{
|
||||||
RegionManager regionManager = new RegionManager("D:/stuttgart-regbez-latest");
|
RegionManager regionManager = new RegionManager("D:/stuttgart-regbez-latest");
|
||||||
return Pathfinder.ClosestNodeToCoordinates(new Coordinates(lat, lon), Tag.SpeedType.car, ref regionManager);
|
return regionManager.ClosestNodeToCoordinates(new Coordinates(lat, lon), Tag.SpeedType.road);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
@ -49,15 +58,18 @@ app.Run();
|
|||||||
internal class PathResult
|
internal class PathResult
|
||||||
{
|
{
|
||||||
[JsonInclude]public TimeSpan calcTime;
|
[JsonInclude]public TimeSpan calcTime;
|
||||||
[JsonInclude] public double pathWeight;
|
[JsonInclude] public double pathWeight = double.MaxValue;
|
||||||
[JsonInclude] public double pathTravelDistance;
|
[JsonInclude] public double pathTravelDistance = double.MaxValue;
|
||||||
[JsonInclude]public List<PathNode> pathNodes;
|
[JsonInclude]public List<PathNode> pathNodes;
|
||||||
|
|
||||||
public PathResult(TimeSpan calcTime, List<PathNode> pathNodes)
|
public PathResult(TimeSpan calcTime, List<PathNode> pathNodes)
|
||||||
{
|
{
|
||||||
this.calcTime = calcTime;
|
this.calcTime = calcTime;
|
||||||
this.pathNodes = pathNodes;
|
this.pathNodes = pathNodes;
|
||||||
this.pathWeight = pathNodes.Last().currentPathWeight;
|
if (pathNodes.Count > 0)
|
||||||
this.pathTravelDistance = pathNodes.Last().currentPathLength;
|
{
|
||||||
|
this.pathWeight = pathNodes.Last().currentPathWeight;
|
||||||
|
this.pathTravelDistance = pathNodes.Last().currentPathLength;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,3 @@
|
|||||||
using System.ComponentModel;
|
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
@ -59,16 +59,4 @@ public class Region
|
|||||||
else return null;
|
else return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Region? FromFile(string filePath)
|
|
||||||
{
|
|
||||||
if (File.Exists(filePath))
|
|
||||||
return JsonSerializer.Deserialize<Region>(new FileStream(filePath, FileMode.Open), serializerOptions)!;
|
|
||||||
else return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Region? FromId(string path, ulong regionId)
|
|
||||||
{
|
|
||||||
string filePath = Path.Join(path, $"{regionId}.region");
|
|
||||||
return FromFile(filePath);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace OSMDatastructure.Graph;
|
namespace OSMDatastructure;
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class Tag
|
public class Tag
|
||||||
@ -41,25 +41,6 @@ public class Tag
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Tag FromBytes(byte[] bytes)
|
|
||||||
{
|
|
||||||
TagType type = (TagType)bytes[0];
|
|
||||||
dynamic value = false;
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case TagType.highway:
|
|
||||||
case TagType.oneway:
|
|
||||||
case TagType.forward:
|
|
||||||
value = BitConverter.ToBoolean(bytes, 1);
|
|
||||||
break;
|
|
||||||
case TagType.maxspeed:
|
|
||||||
value = bytes[1];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Tag(type, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Tag? ConvertToTag(string key, string value)
|
public static Tag? ConvertToTag(string key, string value)
|
||||||
{
|
{
|
||||||
switch (key)
|
switch (key)
|
||||||
@ -116,7 +97,7 @@ public class Tag
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static readonly Dictionary<WayType, byte> defaultSpeedCar = new() {
|
public static readonly Dictionary<WayType, byte> defaultSpeedCar = new() {
|
||||||
{ WayType.NONE, 1 },
|
{ WayType.NONE, 0 },
|
||||||
{ WayType.motorway, 110 },
|
{ WayType.motorway, 110 },
|
||||||
{ WayType.trunk, 100 },
|
{ WayType.trunk, 100 },
|
||||||
{ WayType.primary, 80 },
|
{ WayType.primary, 80 },
|
||||||
@ -129,7 +110,7 @@ public class Tag
|
|||||||
{ WayType.primary_link, 30 },
|
{ WayType.primary_link, 30 },
|
||||||
{ WayType.secondary_link, 25 },
|
{ WayType.secondary_link, 25 },
|
||||||
{ WayType.tertiary_link, 25 },
|
{ WayType.tertiary_link, 25 },
|
||||||
{ WayType.living_street, 10 },
|
{ WayType.living_street, 5 },
|
||||||
{ WayType.service, 1 },
|
{ WayType.service, 1 },
|
||||||
{ WayType.pedestrian, 0 },
|
{ WayType.pedestrian, 0 },
|
||||||
{ WayType.track, 15 },
|
{ WayType.track, 15 },
|
||||||
@ -168,17 +149,19 @@ public class Tag
|
|||||||
{ WayType.bus_guideway, 0 },
|
{ WayType.bus_guideway, 0 },
|
||||||
{ WayType.escape, 1 },
|
{ WayType.escape, 1 },
|
||||||
{ WayType.raceway, 0 },
|
{ WayType.raceway, 0 },
|
||||||
{ WayType.road, 3 },
|
{ WayType.road, 2 },
|
||||||
{ WayType.busway, 0 },
|
{ WayType.busway, 0 },
|
||||||
{ WayType.footway, 4 },
|
{ WayType.footway, 4 },
|
||||||
{ WayType.bridleway, 1 },
|
{ WayType.bridleway, 1 },
|
||||||
{ WayType.steps, 2 },
|
{ WayType.steps, 2 },
|
||||||
{ WayType.corridor, 3 },
|
{ WayType.corridor, 3 },
|
||||||
{ WayType.path, 4 },
|
{ WayType.path, 4 },
|
||||||
{ WayType.cycleway, 2 },
|
{ WayType.cycleway, 1 },
|
||||||
{ WayType.construction, 0 }
|
{ WayType.construction, 0 }
|
||||||
};
|
};
|
||||||
|
// ReSharper disable InconsistentNaming
|
||||||
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 }
|
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, road }
|
||||||
}
|
}
|
@ -1,6 +1,7 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
using OSMDatastructure.Graph;
|
||||||
|
|
||||||
namespace OSMDatastructure.Graph;
|
namespace OSMDatastructure;
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class TagManager
|
public class TagManager
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
using OSMDatastructure;
|
||||||
using OSMDatastructure.Graph;
|
using OSMDatastructure.Graph;
|
||||||
|
|
||||||
namespace Pathfinding;
|
namespace Pathfinding;
|
||||||
|
|
||||||
public class PathNode : OsmNode
|
public class PathNode : OsmNode
|
||||||
{
|
{
|
||||||
[JsonInclude]public new double currentPathWeight = double.MaxValue;
|
|
||||||
[JsonInclude]public new double currentPathLength = double.MaxValue;
|
|
||||||
[JsonInclude]public new double directDistanceToGoal = double.MaxValue;
|
[JsonInclude]public new double directDistanceToGoal = double.MaxValue;
|
||||||
|
[JsonInclude]public new double currentPathLength = double.MaxValue;
|
||||||
|
[JsonInclude]public double distanceBetweenNodes = double.MaxValue;
|
||||||
|
[JsonInclude]public new double currentPathWeight = double.MaxValue;
|
||||||
|
[JsonInclude]public double weightBetweenNodes = double.MaxValue;
|
||||||
|
[JsonInclude]public Dictionary<string, string> tags = new();
|
||||||
|
|
||||||
public PathNode(ulong nodeId, float lat, float lon) : base(nodeId, lat, lon)
|
public PathNode(ulong nodeId, float lat, float lon) : base(nodeId, lat, lon)
|
||||||
{
|
{
|
||||||
@ -17,16 +21,24 @@ public class PathNode : OsmNode
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PathNode? FromOsmNode(OsmNode? node)
|
public static PathNode? FromOsmNode(OsmNode? node, HashSet<Tag>? tags, double distance, double weight)
|
||||||
{
|
{
|
||||||
if (node is null)
|
if (node is null)
|
||||||
return null;
|
return null;
|
||||||
PathNode retNode = new PathNode(node.nodeId, node.coordinates)
|
PathNode retNode = new(node.nodeId, node.coordinates)
|
||||||
{
|
{
|
||||||
currentPathLength = node.currentPathLength,
|
currentPathLength = node.currentPathLength,
|
||||||
currentPathWeight = node.currentPathWeight,
|
currentPathWeight = double.IsPositiveInfinity(node.currentPathWeight) ? double.MaxValue : node.currentPathWeight,
|
||||||
directDistanceToGoal = node.directDistanceToGoal
|
directDistanceToGoal = node.directDistanceToGoal,
|
||||||
|
distanceBetweenNodes = distance,
|
||||||
|
weightBetweenNodes = weight
|
||||||
};
|
};
|
||||||
|
if (tags != null)
|
||||||
|
foreach (Tag tag in tags)
|
||||||
|
{
|
||||||
|
retNode.tags.Add(tag.key.ToString(), tag.value.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
return retNode;
|
return retNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,135 +1,60 @@
|
|||||||
using OSMDatastructure;
|
using OSMDatastructure;
|
||||||
using OSMDatastructure.Graph;
|
using OSMDatastructure.Graph;
|
||||||
using OSMImporter;
|
|
||||||
|
|
||||||
namespace Pathfinding;
|
namespace Pathfinding;
|
||||||
|
|
||||||
public static class Pathfinder
|
public static partial class Pathfinder
|
||||||
{
|
{
|
||||||
public static ValueTuple<TimeSpan, List<PathNode>> CustomAStar(string workingDir, Coordinates start, Coordinates goal, Tag.SpeedType vehicle)
|
|
||||||
|
private static ValueTuple<OsmNode?, OsmNode?> SetupNodes(Coordinates startCoordinates, Coordinates goalCoordinates, RegionManager regionManager )
|
||||||
{
|
{
|
||||||
DateTime startTime = DateTime.Now;
|
ValueTuple<OsmNode?, OsmNode?> retTuple = new();
|
||||||
TimeSpan calcTime;
|
retTuple.Item1 = regionManager.ClosestNodeToCoordinates(startCoordinates, Tag.SpeedType.road);
|
||||||
RegionManager regionManager = new (workingDir);
|
retTuple.Item2 = regionManager.ClosestNodeToCoordinates(goalCoordinates, Tag.SpeedType.road);
|
||||||
OsmNode? startNode = ClosestNodeToCoordinates(start, vehicle, ref regionManager);
|
if (retTuple.Item1 is null || retTuple.Item2 is null)
|
||||||
OsmNode? goalNode = ClosestNodeToCoordinates(goal, vehicle, ref regionManager);
|
return retTuple;
|
||||||
if (startNode == null || goalNode == null)
|
retTuple.Item1.currentPathWeight = 0;
|
||||||
{
|
retTuple.Item1.currentPathLength = 0;
|
||||||
calcTime = DateTime.Now - startTime;
|
retTuple.Item1.directDistanceToGoal = Utils.DistanceBetween(retTuple.Item1, retTuple.Item2);
|
||||||
return new ValueTuple<TimeSpan, List<PathNode>>(calcTime, new List<PathNode>());
|
return retTuple;
|
||||||
}
|
}
|
||||||
|
|
||||||
PriorityQueue<OsmNode, double> toVisit = new();
|
private static double EdgeWeight(OsmNode node1, OsmEdge edge, Tag.SpeedType vehicle, RegionManager regionManager)
|
||||||
toVisit.Enqueue(startNode, 0);
|
{
|
||||||
startNode.currentPathWeight = 0;
|
OsmNode? node2 = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
||||||
startNode.currentPathLength = 0;
|
if (node2 is null)
|
||||||
startNode.directDistanceToGoal = Utils.DistanceBetween(startNode, goalNode);
|
return double.MaxValue;
|
||||||
bool stop = false;
|
double distance = Utils.DistanceBetween(node1, node2);
|
||||||
|
double speed = regionManager.GetSpeedForEdge(node1, edge.wayId, vehicle);
|
||||||
while (toVisit.Count > 0)
|
if (speed is 0)
|
||||||
{
|
return double.MaxValue;
|
||||||
OsmNode closestNodeToGoal = toVisit.Dequeue();
|
return distance / speed;
|
||||||
//Console.WriteLine($"{toVisit.Count:000} {closestNodeToGoal.directDistanceToGoal:#.00} current:{closestNodeToGoal}");
|
}
|
||||||
|
|
||||||
foreach (OsmEdge edge in closestNodeToGoal.edges)
|
|
||||||
{
|
|
||||||
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
|
||||||
if (neighbor is not null)
|
|
||||||
{
|
|
||||||
double newPotentialWeight =
|
|
||||||
closestNodeToGoal.currentPathWeight + EdgeWeight(closestNodeToGoal, neighbor, edge.wayId, vehicle, ref regionManager);
|
|
||||||
if (newPotentialWeight < neighbor.currentPathWeight)
|
|
||||||
{
|
|
||||||
neighbor.previousPathNode = closestNodeToGoal;
|
|
||||||
neighbor.currentPathWeight = newPotentialWeight;
|
|
||||||
neighbor.currentPathLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor);
|
|
||||||
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
|
||||||
|
|
||||||
if (neighbor.Equals(goalNode) || closestNodeToGoal.directDistanceToGoal < 10)
|
|
||||||
{
|
|
||||||
stop = true;
|
|
||||||
goalNode = neighbor;
|
|
||||||
}
|
|
||||||
else if(!stop)
|
|
||||||
{
|
|
||||||
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private static List<PathNode> GetRouteFromCalc(OsmNode goalNode, RegionManager regionManager)
|
||||||
|
{
|
||||||
List<PathNode> path = new();
|
List<PathNode> path = new();
|
||||||
OsmNode? currentNode = goalNode;
|
OsmNode? currentNode = goalNode;
|
||||||
while (currentNode is not null)
|
while (currentNode is not null)
|
||||||
{
|
{
|
||||||
path.Add(PathNode.FromOsmNode(currentNode)!);
|
HashSet<Tag>? tags = null;
|
||||||
|
double distance = 0;
|
||||||
|
double weight = 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
PathNode? pn = PathNode.FromOsmNode(currentNode, tags, distance, weight);
|
||||||
|
if(pn is not null)
|
||||||
|
path.Add(pn!);
|
||||||
currentNode = currentNode.previousPathNode;
|
currentNode = currentNode.previousPathNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
path.Reverse();
|
path.Reverse();
|
||||||
|
return path;
|
||||||
calcTime = DateTime.Now - startTime;
|
|
||||||
return new ValueTuple<TimeSpan, List<PathNode>>(calcTime, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Tag.SpeedType vehicle, ref RegionManager regionManager)
|
|
||||||
{
|
|
||||||
OsmNode? closest = null;
|
|
||||||
double distance = double.MaxValue;
|
|
||||||
Region? region = regionManager.GetRegion(coordinates);
|
|
||||||
if (region is null)
|
|
||||||
return null;
|
|
||||||
foreach (OsmNode node in region.nodes)
|
|
||||||
{
|
|
||||||
bool hasConnectionUsingVehicle = false;
|
|
||||||
foreach (OsmEdge edge in node.edges)
|
|
||||||
{
|
|
||||||
double speed = GetSpeed(node, edge.wayId, vehicle, ref regionManager);
|
|
||||||
if (speed != 0)
|
|
||||||
hasConnectionUsingVehicle = true;
|
|
||||||
}
|
|
||||||
double nodeDistance = Utils.DistanceBetween(node, coordinates);
|
|
||||||
if (nodeDistance < distance && hasConnectionUsingVehicle)
|
|
||||||
{
|
|
||||||
closest = node;
|
|
||||||
distance = nodeDistance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return closest;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static double GetSpeed(OsmNode node1, ulong wayId, Tag.SpeedType vehicle, ref RegionManager regionManager)
|
|
||||||
{
|
|
||||||
TagManager tags = regionManager.GetRegion(node1.coordinates)!.tagManager;
|
|
||||||
Tag.WayType wayType = (Tag.WayType)tags.GetTag(wayId, Tag.TagType.highway)!;
|
|
||||||
switch (vehicle)
|
|
||||||
{
|
|
||||||
case Tag.SpeedType.pedestrian:
|
|
||||||
byte speed = Tag.defaultSpeedPedestrian[wayType];
|
|
||||||
if (speed is not 0)
|
|
||||||
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;
|
|
||||||
maxSpeed = Tag.defaultSpeedCar[wayType];
|
|
||||||
if(maxSpeed is not 0)
|
|
||||||
return (byte)maxSpeed;
|
|
||||||
return 0;
|
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static double EdgeWeight(OsmNode node1, OsmNode node2, ulong wayId, Tag.SpeedType vehicle, ref RegionManager regionManager)
|
|
||||||
{
|
|
||||||
double distance = Utils.DistanceBetween(node1, node2);
|
|
||||||
double speed = GetSpeed(node1, wayId, vehicle, ref regionManager);
|
|
||||||
if (speed is not 0)
|
|
||||||
return distance / speed;
|
|
||||||
return double.PositiveInfinity;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
51
Pathfinding/Pathfinder_Distance.cs
Normal file
51
Pathfinding/Pathfinder_Distance.cs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
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 closestNodeToGoal = toVisit.Dequeue();
|
||||||
|
|
||||||
|
foreach (OsmEdge edge in closestNodeToGoal.edges)
|
||||||
|
{
|
||||||
|
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
||||||
|
if (neighbor is not null)
|
||||||
|
{
|
||||||
|
double newPotentialLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor);
|
||||||
|
if (newPotentialLength < neighbor.currentPathLength)
|
||||||
|
{
|
||||||
|
neighbor.previousPathNode = closestNodeToGoal;
|
||||||
|
neighbor.currentPathLength = newPotentialLength;
|
||||||
|
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
||||||
|
|
||||||
|
if (neighbor.Equals(goalNode))
|
||||||
|
{
|
||||||
|
stop = true;
|
||||||
|
}
|
||||||
|
else if(!stop)
|
||||||
|
{
|
||||||
|
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return GetRouteFromCalc(goalNode, regionManager);
|
||||||
|
}
|
||||||
|
}
|
55
Pathfinding/Pathfinder_Time.cs
Normal file
55
Pathfinding/Pathfinder_Time.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
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 closestNodeToGoal = toVisit.Dequeue();
|
||||||
|
|
||||||
|
foreach (OsmEdge edge in closestNodeToGoal.edges.Where(
|
||||||
|
edge => regionManager.TestValidConnectionForType(closestNodeToGoal, edge, vehicle)))
|
||||||
|
{
|
||||||
|
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
||||||
|
if (neighbor is not null)
|
||||||
|
{
|
||||||
|
double newPotentialWeight = closestNodeToGoal.currentPathWeight +
|
||||||
|
EdgeWeight(closestNodeToGoal, edge, vehicle, regionManager);
|
||||||
|
if (newPotentialWeight < neighbor.currentPathWeight)
|
||||||
|
{
|
||||||
|
neighbor.previousPathNode = closestNodeToGoal;
|
||||||
|
neighbor.currentPathLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor);
|
||||||
|
neighbor.currentPathWeight = newPotentialWeight;
|
||||||
|
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
||||||
|
|
||||||
|
if (neighbor.Equals(goalNode))
|
||||||
|
{
|
||||||
|
stop = true;
|
||||||
|
}
|
||||||
|
else if(!stop)
|
||||||
|
{
|
||||||
|
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return GetRouteFromCalc(goalNode, regionManager);
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,8 @@
|
|||||||
|
using System.Text.Json;
|
||||||
using OSMDatastructure;
|
using OSMDatastructure;
|
||||||
using OSMDatastructure.Graph;
|
using OSMDatastructure.Graph;
|
||||||
|
|
||||||
namespace OSMImporter
|
namespace Pathfinding
|
||||||
{
|
{
|
||||||
public class RegionManager
|
public class RegionManager
|
||||||
{
|
{
|
||||||
@ -24,7 +25,7 @@ namespace OSMImporter
|
|||||||
return value;
|
return value;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Region? loadedRegion = LoadRegion(id);
|
Region? loadedRegion = RegionFromId(id);
|
||||||
if(loadedRegion is not null)
|
if(loadedRegion is not null)
|
||||||
_regions.Add(loadedRegion!.regionHash, value: loadedRegion);
|
_regions.Add(loadedRegion!.regionHash, value: loadedRegion);
|
||||||
return loadedRegion;
|
return loadedRegion;
|
||||||
@ -36,23 +37,22 @@ namespace OSMImporter
|
|||||||
return this._regions.Values.ToArray();
|
return this._regions.Values.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Region? LoadRegion(Coordinates coordinates)
|
private Region? RegionFromFile(string filePath)
|
||||||
{
|
{
|
||||||
return LoadRegion(Coordinates.GetRegionHashCode(coordinates));
|
Region? retRegion = null;
|
||||||
|
if (File.Exists(filePath))
|
||||||
|
{
|
||||||
|
FileStream regionFile = new FileStream(filePath, FileMode.Open);
|
||||||
|
retRegion = JsonSerializer.Deserialize<Region>(regionFile, Region.serializerOptions)!;
|
||||||
|
regionFile.Dispose();
|
||||||
|
}
|
||||||
|
return retRegion;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Region? LoadRegion(ulong id)
|
private Region? RegionFromId(ulong regionId)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Load Region {id}");
|
string filePath = Path.Join(workingDirectory, $"{regionId}.region");
|
||||||
return Region.FromId(workingDirectory, id);
|
return RegionFromFile(filePath);
|
||||||
}
|
|
||||||
|
|
||||||
public OsmNode? GetNode(Coordinates coordinates)
|
|
||||||
{
|
|
||||||
Region? regionWithNode = GetRegion(coordinates);
|
|
||||||
if (regionWithNode is not null)
|
|
||||||
return regionWithNode.GetNode(coordinates);
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public OsmNode? GetNode(ulong nodeId, ulong regionId)
|
public OsmNode? GetNode(ulong nodeId, ulong regionId)
|
||||||
@ -60,5 +60,76 @@ namespace OSMImporter
|
|||||||
Region? r = GetRegion(regionId);
|
Region? r = GetRegion(regionId);
|
||||||
return r?.GetNode(nodeId);
|
return r?.GetNode(nodeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool TestValidConnectionForType(OsmNode node1, OsmNode node2, Tag.SpeedType type)
|
||||||
|
{
|
||||||
|
foreach (OsmEdge edge in node1.edges)
|
||||||
|
{
|
||||||
|
if (edge.neighborId.Equals(node2.nodeId))
|
||||||
|
return TestValidConnectionForType(node1, edge, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TestValidConnectionForType(OsmNode node1, OsmEdge edge, Tag.SpeedType type)
|
||||||
|
{
|
||||||
|
double speed = GetSpeedForEdge(node1, edge.wayId, type);
|
||||||
|
if (speed != 0)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Tag.SpeedType vehicle)
|
||||||
|
{
|
||||||
|
OsmNode? closest = null;
|
||||||
|
double distance = double.MaxValue;
|
||||||
|
Region? region = GetRegion(coordinates);
|
||||||
|
if (region is null)
|
||||||
|
return null;
|
||||||
|
foreach (OsmNode node in region.nodes)
|
||||||
|
{
|
||||||
|
bool 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)
|
||||||
|
{
|
||||||
|
closest = node;
|
||||||
|
distance = nodeDistance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return closest;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double GetSpeedForEdge(OsmNode node1, ulong wayId, Tag.SpeedType vehicle)
|
||||||
|
{
|
||||||
|
TagManager tags = GetRegion(node1.coordinates)!.tagManager;
|
||||||
|
Tag.WayType wayType = (Tag.WayType)tags.GetTag(wayId, Tag.TagType.highway)!;
|
||||||
|
switch (vehicle)
|
||||||
|
{
|
||||||
|
case Tag.SpeedType.pedestrian:
|
||||||
|
byte speed = Tag.defaultSpeedPedestrian[wayType];
|
||||||
|
if (speed is not 0)
|
||||||
|
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;
|
||||||
|
maxSpeed = Tag.defaultSpeedCar[wayType];
|
||||||
|
if(maxSpeed is not 0)
|
||||||
|
return (byte)maxSpeed;
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user