Compare commits

..

No commits in common. "0758ae1d8627f8b24b701dc3726fbea6b8327665" and "eabceed59d49dae21c968df4e3ae0c750c11371f" have entirely different histories.

5 changed files with 65 additions and 35 deletions

View File

@ -2,7 +2,6 @@
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
@ -14,13 +13,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;
@ -42,25 +41,20 @@ namespace astar
{ {
ulong currentNodeStartId = toVisitStart.Dequeue(); ulong currentNodeStartId = toVisitStart.Dequeue();
Node currentNodeStart = graph.Nodes[currentNodeStartId]; Node currentNodeStart = graph.Nodes[currentNodeStartId];
foreach ((ulong neighborId, KeyValuePair<ulong, bool> wayId) in currentNodeStart.Neighbors) foreach ((ulong neighborId, ulong 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.Key)) if (!graph.ContainsWay(wayId))
{ {
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId.Key)) foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId))
graph.ConcatGraph(Graph.FromGraph(g)); graph.ConcatGraph(Graph.FromGraph(g));
} }
OSM_Graph.Way way = graph.Ways[wayId.Key]; Way way = graph.Ways[wayId];
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
@ -79,26 +73,20 @@ namespace astar
ulong currentNodeEndId = toVisitEnd.Dequeue(); ulong currentNodeEndId = toVisitEnd.Dequeue();
Node currentNodeEnd = graph.Nodes[currentNodeEndId]; Node currentNodeEnd = graph.Nodes[currentNodeEndId];
foreach ((ulong neighborId, KeyValuePair<ulong, bool> wayId) in currentNodeEnd.Neighbors) foreach ((ulong neighborId, ulong 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.Key)) if (!graph.ContainsWay(wayId))
{ {
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId.Key)) foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId))
graph.ConcatGraph(Graph.FromGraph(g)); graph.ConcatGraph(Graph.FromGraph(g));
} }
OSM_Graph.Way way = graph.Ways[wayId.Key]; Way way = graph.Ways[wayId];
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

View File

@ -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, OSM_Graph.Way> Ways = new (); public readonly Dictionary<ulong, 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, new OSM_Graph.Way(id, way.Tags, new())); ret.Ways.Add(id, way);
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, OSM_Graph.Way w) in graph.Ways) foreach ((ulong id, 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(OSM_Graph.Way way) public bool ContainsWay(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(way => SpeedHelper.GetSpeed(Ways[way.Key], car) > 0)).MinBy(n => n.Value.DistanceTo(lat, lon)); return Nodes.Where(n => n.Value.Neighbors.Values.Any(wayId => SpeedHelper.GetSpeed(Ways[wayId], car) > 0)).MinBy(n => n.Value.DistanceTo(lat, lon));
} }
public override string ToString() public override string ToString()

View File

@ -1,6 +1,6 @@
namespace astar; namespace astar;
public class Node(float lat, float lon, Dictionary<ulong, KeyValuePair<ulong, bool>>? neighbors = null) : global::Graph.Node(lat, lon, neighbors) public class Node(float lat, float lon, Dictionary<ulong, ulong>? neighbors = null) : global::Graph.Node(lat, lon, neighbors)
{ {
public ulong? PreviousNodeId = null; public ulong? PreviousNodeId = null;
public float? Distance = null; public float? Distance = null;

View File

@ -0,0 +1,36 @@
// 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
}

View File

@ -1,17 +1,23 @@
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(OSM_Graph.Way way, bool car = true) public static byte GetSpeed(Way way, bool car = true)
{ {
byte maxspeed = way.GetMaxSpeed(); if (!way.Tags.TryGetValue("highway", out string? highwayTypeStr))
if (maxspeed != 0) return 0;
return maxspeed; if (!Enum.TryParse(highwayTypeStr, out HighwayType highwayType))
HighwayType highwayType = way.GetHighwayType(); return 0;
return car ? SpeedCar[highwayType] : SpeedPedestrian[highwayType]; byte speed = 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)