Highway type colors,

Routing (actual route, explored paths)
This commit is contained in:
glax 2024-07-23 03:11:32 +02:00
parent 0ef70bd7ea
commit ac0a516bb7
5 changed files with 191 additions and 1 deletions

View File

@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=C_003A_005CUsers_005CGlax_005CRiderProjects_005Castar_005CExecutable_005Cbin_005CDebug_005Cnet8_002E0_005Castar_002Edll/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=C_003A_005CUsers_005CGlax_005CRiderProjects_005COSM_005FGraph_005COSM_005FGraph_005Cbin_005CDebug_005Cnet8_002E0_005CGraph_002Edll/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=C_003A_005CUsers_005CGlax_005CRiderProjects_005COSM_005FGraph_005COSM_005FGraph_005Cbin_005CDebug_005Cnet8_002E0_005COSM_005FGraph_002Edll/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -0,0 +1,49 @@
using System.Drawing;
using astar.PathingHelper;
using Graph;
namespace Graph_Renderer;
public static class Coloring
{
public static Color GetColorForWay(Way way)
{
if (!way.Tags.TryGetValue("highway", out string? highwayTypeStr))
return Color.Gray;
if (!Enum.TryParse(highwayTypeStr, out HighwayType highwayType))
return Color.Gray;
return ColorDictionary[highwayType];
}
private static readonly Dictionary<HighwayType, Color> ColorDictionary = new() {
{ HighwayType.NONE, Color.Gray },
{ HighwayType.motorway, Color.OrangeRed },
{ HighwayType.trunk, Color.Orange },
{ HighwayType.primary, Color.Yellow },
{ HighwayType.secondary, Color.Aqua },
{ HighwayType.tertiary, Color.Black },
{ HighwayType.unclassified, Color.Gray },
{ HighwayType.residential, Color.Gray },
{ HighwayType.motorway_link, Color.OrangeRed },
{ HighwayType.trunk_link, Color.Orange },
{ HighwayType.primary_link, Color.Yellow },
{ HighwayType.secondary_link, Color.Aqua },
{ HighwayType.tertiary_link, Color.Black },
{ HighwayType.living_street, Color.Gray },
{ HighwayType.service, Color.Gray },
{ HighwayType.pedestrian, Color.ForestGreen },
{ HighwayType.track, Color.Gray },
{ HighwayType.bus_guideway, Color.Navy },
{ HighwayType.escape, Color.Gray },
{ HighwayType.raceway, Color.Gray },
{ HighwayType.road, Color.Gray },
{ HighwayType.busway, Color.Navy },
{ HighwayType.footway, Color.GreenYellow },
{ HighwayType.bridleway, Color.Gray },
{ HighwayType.steps, Color.PaleGreen },
{ HighwayType.corridor, Color.Green },
{ HighwayType.path, Color.Green },
{ HighwayType.cycleway, Color.Salmon },
{ HighwayType.construction, Color.Gray }
};
}

View File

@ -7,4 +7,22 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Reference Include="astar">
<HintPath>..\..\astar\Executable\bin\Debug\net8.0\astar.dll</HintPath>
</Reference>
<Reference Include="Graph">
<HintPath>..\..\OSM_Graph\OSM_Graph\bin\Debug\net8.0\Graph.dll</HintPath>
</Reference>
<Reference Include="OSM_Graph">
<HintPath>..\..\OSM_Graph\OSM_Graph\bin\Debug\net8.0\OSM_Graph.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="GlaxArguments" Version="1.1.0" />
<PackageReference Include="GlaxLogger" Version="1.0.7.2" />
<PackageReference Include="System.Drawing.Common" Version="8.0.7" />
</ItemGroup>
</Project>

View File

@ -1,3 +1,43 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using System.Globalization;
using System.Runtime.InteropServices;
using astar;
using GlaxArguments;
using GlaxLogger;
using Graph_Renderer;
using Microsoft.Extensions.Logging;
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Console.WriteLine("Only runs on Windows. Sorry.");
return -1;
}
Argument regionArg = new (["-r", "--regionSize"], 1, "Region-Size");
Argument importPathArg = new (["-i", "--importPath"], 1, "Region-Directory");
Argument routeCoordinateArg = new(["-c", "--route", "--coordinates"], 4, "Start and end coordinates");
ArgumentFetcher af = new ([regionArg, importPathArg, routeCoordinateArg]);
Dictionary<Argument, string[]> arguments = af.Fetch(args);
if (!arguments.TryGetValue(regionArg, out string[]? regionVal) ||
!float.TryParse(regionVal[0], NumberFormatInfo.InvariantInfo, out float regionSize) ||
!arguments.TryGetValue(importPathArg, out string[]? importPathVal) ||
!arguments.TryGetValue(routeCoordinateArg, out string[]? routeCoordinateVal) ||
!float.TryParse(routeCoordinateVal[0], NumberFormatInfo.InvariantInfo, out float startLat) ||
!float.TryParse(routeCoordinateVal[1], NumberFormatInfo.InvariantInfo, out float startLon) ||
!float.TryParse(routeCoordinateVal[2], NumberFormatInfo.InvariantInfo, out float endLat) ||
!float.TryParse(routeCoordinateVal[3], NumberFormatInfo.InvariantInfo, out float endLon))
{
return -1;
}
Logger logger = new(LogLevel.Information, consoleOut: Console.Out);
Route r = Astar.FindPath(startLat, startLon, endLat, endLon, regionSize, true, importPathVal[0], logger);
Renderer.Render(r, 30000, "render.png");
return 0;

View File

@ -0,0 +1,79 @@
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.Versioning;
using astar;
namespace Graph_Renderer;
public static class Renderer
{
[SupportedOSPlatform("Windows")]
public static void Render(Route r, int longestEdge, string outputPath)
{
ValueTuple<float, float> minCoordinates = r.MinCoordinates();
ValueTuple<float, float> maxCoordinates = r.MaxCoordinates();
float deltaLat = maxCoordinates.Item1 - minCoordinates.Item1;
float deltaLon = maxCoordinates.Item2 - minCoordinates.Item2;
float multiplier = deltaLat > deltaLon ? longestEdge / deltaLat : longestEdge / deltaLon;
Bitmap bitmap = new((int)(deltaLon * multiplier), (int)(deltaLat * multiplier), System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen previous = new (Color.White, 2);
using(GraphicsPath capPath = new ())
{
// A triangle
capPath.AddLines(new Point[]{new (-2, -2), new (0, 2), new (2, -2)});
previous.CustomEndCap = new CustomLineCap(null, capPath);
}
foreach ((ulong nodeId, Node? node) in r.Graph.Nodes)
{
Point startCoordinates = PixelCoordinatesFromGeoCoordinates(node, minCoordinates, multiplier, bitmap.Height);
//All neighbors
foreach ((ulong neighborNodeId, ulong wayId) in node.Neighbors.Where(kv => r.Graph.ContainsNode(kv.Key)))
{
Point endCoordinates = PixelCoordinatesFromGeoCoordinates(r.Graph.Nodes[neighborNodeId], minCoordinates, multiplier, bitmap.Height);
graphics.DrawLine(new Pen(Coloring.GetColorForWay(r.Graph.Ways[wayId]), 2), startCoordinates, endCoordinates);
}
//Previous
if (node.PreviousNodeId is not null)
{
Point previousCoordinates = PixelCoordinatesFromGeoCoordinates(r.Graph.Nodes[(ulong)node.PreviousNodeId], minCoordinates, multiplier, bitmap.Height);
graphics.DrawLine(previous, startCoordinates, previousCoordinates);
}
}
if (r.RouteFound)
{
Pen route = new(Color.BlueViolet, 2);
foreach (Step step in r.Steps)
{
Point startCoordinates = PixelCoordinatesFromGeoCoordinates(step.Node1, minCoordinates, multiplier, bitmap.Height);
Point endCoordinates = PixelCoordinatesFromGeoCoordinates(step.Node2, minCoordinates, multiplier, bitmap.Height);
graphics.DrawLine(route, startCoordinates, endCoordinates);
}
}
Directory.CreateDirectory(new FileInfo(outputPath).DirectoryName!);
bitmap.Save(outputPath);
Console.WriteLine(outputPath);
}
private static Point PixelCoordinatesFromGeoCoordinates
(Node node, ValueTuple<float, float> minCoordinates, float multiplier, int height) =>
PixelCoordinatesFromGeoCoordinates(node.Lat, node.Lon, minCoordinates, multiplier, height);
private static Point PixelCoordinatesFromGeoCoordinates(float lat, float lon,
ValueTuple<float, float> minCoordinates, float multiplier, int height)
{
float pLat = lat - minCoordinates.Item1;
float pLon = lon - minCoordinates.Item2;
return new Point((int)(pLon * multiplier), height - (int)(pLat * multiplier));
}
}