Compare commits

..

No commits in common. "2c5ab070a2895449b7e0d0e94b58d3c733b8bcb4" and "82d2de1537cae7884783eaeddf3d51e245d27ee4" have entirely different histories.

6 changed files with 50 additions and 67 deletions

View File

@ -6,7 +6,7 @@ namespace OSMDatastructure;
[Serializable] [Serializable]
public class Region public class Region
{ {
[NonSerialized]public const float RegionSize = 0.025f; [NonSerialized]public const float RegionSize = 0.1f;
public readonly HashSet<OsmNode> nodes = new(); public readonly HashSet<OsmNode> nodes = new();
public ulong regionHash { get; } public ulong regionHash { get; }
public TagManager tagManager { get; } public TagManager tagManager { get; }

View File

@ -14,23 +14,27 @@ public class Pathfinder
if (startRegion is null || goalRegion is null) if (startRegion is null || goalRegion is null)
return new List<OsmNode>(); return new List<OsmNode>();
OsmNode? startNode = ClosestNodeToCoordinates(start, startRegion, vehicle); OsmNode? startNode = ClosestNodeToCoordinates(start, startRegion);
OsmNode? goalNode = ClosestNodeToCoordinates(goal, goalRegion, vehicle); OsmNode? goalNode = ClosestNodeToCoordinates(goal, goalRegion);
if (startNode == null || goalNode == null) if (startNode == null || goalNode == null)
return new List<OsmNode>(); return new List<OsmNode>();
PriorityQueue<OsmNode, double> toVisit = new(); List<OsmNode> toVisit = new() { startNode };
toVisit.Enqueue(startNode, 0); OsmNode closestNodeToGoal = toVisit.First();
startNode.currentPathWeight = 0; closestNodeToGoal.currentPathWeight = 0;
startNode.currentPathLength = 0; closestNodeToGoal.currentPathLength = 0;
startNode.directDistanceToGoal = Utils.DistanceBetween(startNode, goalNode);
OsmNode closestNodeToGoal = startNode;
bool stop = false; bool stop = false;
while (toVisit.Count > 0 && !stop) while (toVisit.Count > 0 && !stop)
{ {
closestNodeToGoal = toVisit.Dequeue(); //Console.WriteLine("toVisit-length: {0}", toVisit.Count);
Console.WriteLine($"{toVisit.Count:000} {closestNodeToGoal.directDistanceToGoal:#.00} current:{closestNodeToGoal}"); closestNodeToGoal = toVisit.First();
foreach (OsmNode node in toVisit.Where(node => node.directDistanceToGoal.Equals(Double.MaxValue)))
{
node.directDistanceToGoal = Utils.DistanceBetween(node, goalNode);
}
closestNodeToGoal = toVisit.OrderBy(node => node.directDistanceToGoal).First();
foreach (OsmEdge edge in closestNodeToGoal.edges) foreach (OsmEdge edge in closestNodeToGoal.edges)
{ {
@ -45,20 +49,19 @@ public class Pathfinder
neighbor.currentPathWeight = newPotentialWeight; neighbor.currentPathWeight = newPotentialWeight;
neighbor.currentPathLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor); neighbor.currentPathLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor);
if (neighbor.Equals(goalNode) || closestNodeToGoal.directDistanceToGoal < 250) if (neighbor.Equals(goalNode))
stop = true; stop = true;
else else
{ toVisit.Add(neighbor);
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
}
} }
} }
} }
toVisit.Remove(closestNodeToGoal);
} }
List<OsmNode> path = new(); List<OsmNode> path = new();
OsmNode? currentNode = closestNodeToGoal; OsmNode? currentNode = goalNode;
while (currentNode != null && !currentNode.Equals(startNode)) while (currentNode != null && !currentNode.Equals(startNode))
{ {
path.Add(currentNode); path.Add(currentNode);
@ -70,33 +73,14 @@ public class Pathfinder
return path; return path;
} }
private static OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Region region, Tag.SpeedType vehicle) private static OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Region region)
{ {
OsmNode? closest = null; OsmNode? closest = null;
double distance = double.MaxValue; double distance = double.MaxValue;
foreach (OsmNode node in region.nodes) foreach (OsmNode node in region.nodes)
{ {
bool hasConnection = false;
foreach (OsmEdge edge in node.edges)
{
byte speed = 0;
switch (vehicle)
{
case Tag.SpeedType.road:
case Tag.SpeedType.car:
speed = Tag.defaultSpeedCar[(Tag.WayType)region.tagManager.GetTag(edge.wayId, Tag.TagType.highway)!];
break;
case Tag.SpeedType.pedestrian:
speed = Tag.defaultSpeedPedestrian[
(Tag.WayType)region.tagManager.GetTag(edge.wayId, Tag.TagType.highway)!];
break;
}
if (speed != 0)
hasConnection = true;
}
double nodeDistance = Utils.DistanceBetween(node, coordinates); double nodeDistance = Utils.DistanceBetween(node, coordinates);
if (nodeDistance < distance && hasConnection) if (nodeDistance < distance)
{ {
closest = node; closest = node;
distance = nodeDistance; distance = nodeDistance;

View File

@ -43,7 +43,6 @@ namespace OSMImporter
private Region? LoadRegion(ulong id) private Region? LoadRegion(ulong id)
{ {
Console.WriteLine($"Load Region {id}");
return Region.FromId(workingDirectory, id); return Region.FromId(workingDirectory, id);
} }

View File

@ -28,7 +28,7 @@ public class ConsoleWriter : TextWriter
public override void WriteLine(string? text) public override void WriteLine(string? text)
{ {
DateTime now = DateTime.Now; DateTime now = DateTime.Now;
string dateTimeString = $"[{now.ToUniversalTime():u} ({Math.Floor((now - execStart).TotalMilliseconds):###,###,###})]"; string dateTimeString = $"[{now.ToUniversalTime():u} ({Math.Floor((now - execStart).TotalMilliseconds):####00000})]";
if(text is not null) if(text is not null)
OnWriteLine?.Invoke(this, new ConsoleWriterEventArgs($"{dateTimeString} {text}")); OnWriteLine?.Invoke(this, new ConsoleWriterEventArgs($"{dateTimeString} {text}"));
stdOut.WriteLine($"{dateTimeString} {text}"); stdOut.WriteLine($"{dateTimeString} {text}");

View File

@ -251,16 +251,14 @@ public class RegionConverter
throw new FileNotFoundException("Region does not exist"); throw new FileNotFoundException("Region does not exist");
#pragma warning disable SYSLIB0011 #pragma warning disable SYSLIB0011
string waysPath = Path.Join(folderPath, regionHash.ToString(), RegionConverter.WaysFileName); using (FileStream wayFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.WaysFileName), FileMode.Open))
if(File.Exists(waysPath)) {
using (FileStream wayFileStream = new FileStream(waysPath, FileMode.Open)) while (wayFileStream.Position < wayFileStream.Length)
{ {
while (wayFileStream.Position < wayFileStream.Length) OsmEdge deserializedEdge = (OsmEdge)bFormatter.Deserialize(wayFileStream);
{ ways.Add(deserializedEdge);
OsmEdge deserializedEdge = (OsmEdge)bFormatter.Deserialize(wayFileStream);
ways.Add(deserializedEdge);
}
} }
}
using (FileStream nodeFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.NodesFileName), FileMode.Open)) using (FileStream nodeFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.NodesFileName), FileMode.Open))
{ {
@ -270,19 +268,17 @@ public class RegionConverter
newRegion.nodes.Add(deserializedNode); newRegion.nodes.Add(deserializedNode);
} }
} }
string tagsPath = Path.Join(folderPath, regionHash.ToString(), RegionConverter.TagsFileName); using (FileStream tagsFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.TagsFileName), FileMode.Open))
if(File.Exists(tagsPath)) {
using (FileStream tagsFileStream = new FileStream(tagsPath, FileMode.Open)) while (tagsFileStream.Position < tagsFileStream.Length)
{ {
while (tagsFileStream.Position < tagsFileStream.Length) TagManager tm = (TagManager)bFormatter.Deserialize(tagsFileStream);
{ ulong id = (ulong)tm.wayTagSets.First()!.Value.First(tag => tag.key == Tag.TagType.id)!.value;
TagManager tm = (TagManager)bFormatter.Deserialize(tagsFileStream); foreach(Tag tag in tm.wayTagSets.First()!.Value)
ulong id = (ulong)tm.wayTagSets.First()!.Value.First(tag => tag.key == Tag.TagType.id)!.value; newRegion.tagManager.AddTag(id, tag);
foreach(Tag tag in tm.wayTagSets.First()!.Value)
newRegion.tagManager.AddTag(id, tag);
}
} }
}
#pragma warning restore SYSLIB0011 #pragma warning restore SYSLIB0011
return new ValueTuple<Region, HashSet<OsmEdge>>(newRegion, ways); return new ValueTuple<Region, HashSet<OsmEdge>>(newRegion, ways);

View File

@ -18,15 +18,19 @@ public class Server
Console.WriteLine("Loaded"); Console.WriteLine("Loaded");
Coordinates start = new Coordinates(48.793319f, 9.832102f); Coordinates start = new Coordinates(48.7933989f, 9.8301467f);
Coordinates finish = new Coordinates(48.840728f, 10.067603f); Coordinates finish = new Coordinates(48.7906258f, 9.8355983f);
DateTime startTime = DateTime.Now; OsmNode[] path = Pathfinder.CustomAStar("D:/map", start, finish, Tag.SpeedType.pedestrian).ToArray();
OsmNode[] path = Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", start, finish, Tag.SpeedType.car).ToArray(); Console.WriteLine("{0}\n", path[0].ToString());
TimeSpan duration = DateTime.Now - startTime;
Console.WriteLine($"Took {duration.TotalMilliseconds}ms ({duration:g})");
for (int i = 0; i < path.Length - 1; i++) for (int i = 0; i < path.Length - 1; i++)
{ {
Console.WriteLine(path[i]); OsmNode n1 = path[i];
OsmNode n2 = path[i + 1];
OsmEdge? e = n1.GetEdgeToNode(n2);
if(e != null)
Console.WriteLine("{0}\n{1}", e.ToString(), n2.ToString());
else
Console.WriteLine("NO EDGE\n{0}", n2.ToString());
} }
Console.WriteLine(); Console.WriteLine();
} }