Compare commits
2 Commits
eabceed59d
...
0758ae1d86
Author | SHA1 | Date | |
---|---|---|---|
0758ae1d86 | |||
f8acf3c9eb |
@ -2,6 +2,7 @@
|
|||||||
using Graph;
|
using Graph;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Graph.Utils;
|
using Graph.Utils;
|
||||||
|
using OSM_Graph.Enums;
|
||||||
using OSM_Regions;
|
using OSM_Regions;
|
||||||
|
|
||||||
namespace astar
|
namespace astar
|
||||||
@ -13,13 +14,13 @@ namespace astar
|
|||||||
{
|
{
|
||||||
RegionLoader rl = new(regionSize, importFolderPath, logger: logger);
|
RegionLoader rl = new(regionSize, importFolderPath, logger: logger);
|
||||||
Graph graph = Spiral(rl, startLat, startLon, regionSize);
|
Graph graph = Spiral(rl, startLat, startLon, regionSize);
|
||||||
|
Graph endRegion = Spiral(rl, endLat, endLon, regionSize);
|
||||||
|
graph.ConcatGraph(endRegion);
|
||||||
KeyValuePair<ulong, Node> startNode = graph.ClosestNodeToCoordinates(startLat, startLon, car);
|
KeyValuePair<ulong, Node> startNode = graph.ClosestNodeToCoordinates(startLat, startLon, car);
|
||||||
startNode.Value.PreviousIsFromStart = true;
|
startNode.Value.PreviousIsFromStart = true;
|
||||||
startNode.Value.PreviousNodeId = startNode.Key;
|
startNode.Value.PreviousNodeId = startNode.Key;
|
||||||
startNode.Value.Distance = 0f;
|
startNode.Value.Distance = 0f;
|
||||||
|
|
||||||
Graph endRegion = Spiral(rl, endLat, endLon, regionSize);
|
|
||||||
graph.ConcatGraph(endRegion);
|
|
||||||
KeyValuePair<ulong, Node> endNode = graph.ClosestNodeToCoordinates(endLat, endLon, car);
|
KeyValuePair<ulong, Node> endNode = graph.ClosestNodeToCoordinates(endLat, endLon, car);
|
||||||
endNode.Value.PreviousIsFromStart = false;
|
endNode.Value.PreviousIsFromStart = false;
|
||||||
endNode.Value.PreviousNodeId = endNode.Key;
|
endNode.Value.PreviousNodeId = endNode.Key;
|
||||||
@ -41,20 +42,25 @@ namespace astar
|
|||||||
{
|
{
|
||||||
ulong currentNodeStartId = toVisitStart.Dequeue();
|
ulong currentNodeStartId = toVisitStart.Dequeue();
|
||||||
Node currentNodeStart = graph.Nodes[currentNodeStartId];
|
Node currentNodeStart = graph.Nodes[currentNodeStartId];
|
||||||
foreach ((ulong neighborId, ulong wayId) in currentNodeStart.Neighbors)
|
foreach ((ulong neighborId, KeyValuePair<ulong, bool> wayId) in currentNodeStart.Neighbors)
|
||||||
{
|
{
|
||||||
if (!graph.ContainsNode(neighborId))
|
if (!graph.ContainsNode(neighborId))
|
||||||
graph.ConcatGraph(Graph.FromGraph(rl.LoadRegionFromNodeId(neighborId)));
|
graph.ConcatGraph(Graph.FromGraph(rl.LoadRegionFromNodeId(neighborId)));
|
||||||
if (!graph.ContainsWay(wayId))
|
if (!graph.ContainsWay(wayId.Key))
|
||||||
{
|
{
|
||||||
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId))
|
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId.Key))
|
||||||
graph.ConcatGraph(Graph.FromGraph(g));
|
graph.ConcatGraph(Graph.FromGraph(g));
|
||||||
}
|
}
|
||||||
|
|
||||||
Way way = graph.Ways[wayId];
|
OSM_Graph.Way way = graph.Ways[wayId.Key];
|
||||||
byte speed = SpeedHelper.GetSpeed(way, car);
|
byte speed = SpeedHelper.GetSpeed(way, car);
|
||||||
if(speed < 1)
|
if(speed < 1)
|
||||||
continue;
|
continue;
|
||||||
|
if(wayId.Value && way.GetDirection() == WayDirection.Forwards && car)
|
||||||
|
continue;
|
||||||
|
if(!wayId.Value && way.GetDirection() == WayDirection.Backwards && car)
|
||||||
|
continue;
|
||||||
|
|
||||||
Node neighborNode = graph.Nodes[neighborId];
|
Node neighborNode = graph.Nodes[neighborId];
|
||||||
|
|
||||||
if (neighborNode.PreviousIsFromStart is false)//Check if we found the opposite End
|
if (neighborNode.PreviousIsFromStart is false)//Check if we found the opposite End
|
||||||
@ -73,20 +79,26 @@ namespace astar
|
|||||||
|
|
||||||
ulong currentNodeEndId = toVisitEnd.Dequeue();
|
ulong currentNodeEndId = toVisitEnd.Dequeue();
|
||||||
Node currentNodeEnd = graph.Nodes[currentNodeEndId];
|
Node currentNodeEnd = graph.Nodes[currentNodeEndId];
|
||||||
foreach ((ulong neighborId, ulong wayId) in currentNodeEnd.Neighbors)
|
foreach ((ulong neighborId, KeyValuePair<ulong, bool> wayId) in currentNodeEnd.Neighbors)
|
||||||
{
|
{
|
||||||
if (!graph.ContainsNode(neighborId))
|
if (!graph.ContainsNode(neighborId))
|
||||||
graph.ConcatGraph(Graph.FromGraph(rl.LoadRegionFromNodeId(neighborId)));
|
graph.ConcatGraph(Graph.FromGraph(rl.LoadRegionFromNodeId(neighborId)));
|
||||||
if (!graph.ContainsWay(wayId))
|
if (!graph.ContainsWay(wayId.Key))
|
||||||
{
|
{
|
||||||
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId))
|
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId.Key))
|
||||||
graph.ConcatGraph(Graph.FromGraph(g));
|
graph.ConcatGraph(Graph.FromGraph(g));
|
||||||
}
|
}
|
||||||
|
|
||||||
Way way = graph.Ways[wayId];
|
OSM_Graph.Way way = graph.Ways[wayId.Key];
|
||||||
byte speed = SpeedHelper.GetSpeed(way, car);
|
byte speed = SpeedHelper.GetSpeed(way, car);
|
||||||
if(speed < 1)
|
if(speed < 1)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if(wayId.Value && way.GetDirection() == WayDirection.Backwards && car)
|
||||||
|
continue;
|
||||||
|
if(!wayId.Value && way.GetDirection() == WayDirection.Forwards && car)
|
||||||
|
continue;
|
||||||
|
|
||||||
Node neighborNode = graph.Nodes[neighborId];
|
Node neighborNode = graph.Nodes[neighborId];
|
||||||
|
|
||||||
if (neighborNode.PreviousIsFromStart is true)//Check if we found the opposite End
|
if (neighborNode.PreviousIsFromStart is true)//Check if we found the opposite End
|
||||||
|
@ -6,7 +6,7 @@ namespace astar;
|
|||||||
public class Graph
|
public class Graph
|
||||||
{
|
{
|
||||||
public readonly Dictionary<ulong, Node> Nodes = new();
|
public readonly Dictionary<ulong, Node> Nodes = new();
|
||||||
public readonly Dictionary<ulong, Way> Ways = new ();
|
public readonly Dictionary<ulong, OSM_Graph.Way> Ways = new ();
|
||||||
|
|
||||||
public static Graph? FromGraph(global::Graph.Graph? graph)
|
public static Graph? FromGraph(global::Graph.Graph? graph)
|
||||||
{
|
{
|
||||||
@ -16,7 +16,7 @@ public class Graph
|
|||||||
foreach ((ulong id, global::Graph.Node? node) in graph.Nodes)
|
foreach ((ulong id, global::Graph.Node? node) in graph.Nodes)
|
||||||
ret.Nodes.Add(id, Node.FromGraphNode(node));
|
ret.Nodes.Add(id, Node.FromGraphNode(node));
|
||||||
foreach ((ulong id, Way? way) in graph.Ways)
|
foreach ((ulong id, Way? way) in graph.Ways)
|
||||||
ret.Ways.Add(id, way);
|
ret.Ways.Add(id, new OSM_Graph.Way(id, way.Tags, new()));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ public class Graph
|
|||||||
return;
|
return;
|
||||||
foreach ((ulong id, Node n) in graph.Nodes)
|
foreach ((ulong id, Node n) in graph.Nodes)
|
||||||
this.Nodes.TryAdd(id, n);
|
this.Nodes.TryAdd(id, n);
|
||||||
foreach ((ulong id, Way w) in graph.Ways)
|
foreach ((ulong id, OSM_Graph.Way w) in graph.Ways)
|
||||||
this.Ways.TryAdd(id, w);
|
this.Ways.TryAdd(id, w);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ public class Graph
|
|||||||
return Nodes.ContainsKey(nodeId);
|
return Nodes.ContainsKey(nodeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ContainsWay(Way way)
|
public bool ContainsWay(OSM_Graph.Way way)
|
||||||
{
|
{
|
||||||
return Ways.ContainsValue(way);
|
return Ways.ContainsValue(way);
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ public class Graph
|
|||||||
|
|
||||||
public KeyValuePair<ulong, Node> ClosestNodeToCoordinates(float lat, float lon, bool car = true)
|
public KeyValuePair<ulong, Node> ClosestNodeToCoordinates(float lat, float lon, bool car = true)
|
||||||
{
|
{
|
||||||
return Nodes.Where(n => n.Value.Neighbors.Values.Any(wayId => SpeedHelper.GetSpeed(Ways[wayId], car) > 0)).MinBy(n => n.Value.DistanceTo(lat, lon));
|
return Nodes.Where(n => n.Value.Neighbors.Values.Any(way => SpeedHelper.GetSpeed(Ways[way.Key], car) > 0)).MinBy(n => n.Value.DistanceTo(lat, lon));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
namespace astar;
|
namespace astar;
|
||||||
|
|
||||||
public class Node(float lat, float lon, Dictionary<ulong, ulong>? neighbors = null) : global::Graph.Node(lat, lon, neighbors)
|
public class Node(float lat, float lon, Dictionary<ulong, KeyValuePair<ulong, bool>>? neighbors = null) : global::Graph.Node(lat, lon, neighbors)
|
||||||
{
|
{
|
||||||
public ulong? PreviousNodeId = null;
|
public ulong? PreviousNodeId = null;
|
||||||
public float? Distance = null;
|
public float? Distance = null;
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
// ReSharper disable InconsistentNaming
|
|
||||||
// ReSharper disable UnusedMember.Global
|
|
||||||
namespace astar.PathingHelper;
|
|
||||||
|
|
||||||
public enum HighwayType
|
|
||||||
{
|
|
||||||
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
|
|
||||||
}
|
|
@ -1,23 +1,17 @@
|
|||||||
using Graph;
|
using Graph;
|
||||||
|
using OSM_Graph.Enums;
|
||||||
|
|
||||||
namespace astar.PathingHelper;
|
namespace astar.PathingHelper;
|
||||||
|
|
||||||
internal static class SpeedHelper
|
internal static class SpeedHelper
|
||||||
{
|
{
|
||||||
public static byte GetSpeed(Way way, bool car = true)
|
public static byte GetSpeed(OSM_Graph.Way way, bool car = true)
|
||||||
{
|
{
|
||||||
if (!way.Tags.TryGetValue("highway", out string? highwayTypeStr))
|
byte maxspeed = way.GetMaxSpeed();
|
||||||
return 0;
|
if (maxspeed != 0)
|
||||||
if (!Enum.TryParse(highwayTypeStr, out HighwayType highwayType))
|
return maxspeed;
|
||||||
return 0;
|
HighwayType highwayType = way.GetHighwayType();
|
||||||
byte speed = car ? SpeedCar[highwayType] : SpeedPedestrian[highwayType];
|
return car ? SpeedCar[highwayType] : SpeedPedestrian[highwayType];
|
||||||
if (speed < 1)
|
|
||||||
return speed;
|
|
||||||
if(!way.Tags.TryGetValue("maxspeed", out string? maxSpeedStr))
|
|
||||||
return speed;
|
|
||||||
if (!byte.TryParse(maxSpeedStr, out speed))
|
|
||||||
return speed;
|
|
||||||
return speed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte GetMaxSpeed(bool car = true)
|
public static byte GetMaxSpeed(bool car = true)
|
||||||
|
Loading…
Reference in New Issue
Block a user