OSMServer/RenderPath/Renderer.cs

46 lines
1.7 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Pathfinding;
using System.Drawing;
using System.Drawing.Imaging;
using OSMDatastructure.Graph;
namespace RenderPath;
public class Renderer
{
[SuppressMessage("Interoperability", "CA1416:Plattformkompatibilität überprüfen")]
public static Bitmap DrawFromPath(List<PathNode> nodes)
{
float minLat = nodes.Min(node => node.coordinates.latitude);
float minLon = nodes.Min(node => node.coordinates.longitude);
float maxLat = nodes.Max(node => node.coordinates.latitude);
float maxLon = nodes.Max(node => node.coordinates.longitude);
float latDiff = maxLat - minLat;
float lonDiff = maxLon - minLon;
const int imageMaxSize = 10000;
float scaleFactor = latDiff > lonDiff ? imageMaxSize / latDiff : imageMaxSize / lonDiff;
int pixelsX = (int)(lonDiff * scaleFactor);
int pixelsY = (int)(latDiff * scaleFactor);
Bitmap ret = new Bitmap(pixelsX, pixelsY, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(ret);
Pen p = new Pen(Color.Black, 2);
g.Clear(Color.White);
for (int i = 0; i < nodes.Count - 1; i++)
{
Coordinates c1 = nodes[i].coordinates;
Coordinates c2 = nodes[i+1].coordinates;
Point p1 = new(Convert.ToInt32((c1.longitude - minLon) * scaleFactor), Convert.ToInt32((maxLat - c1.latitude) * scaleFactor));
Point p2 = new(Convert.ToInt32((c2.longitude - minLon) * scaleFactor), Convert.ToInt32((maxLat - c2.latitude) * scaleFactor));
g.DrawLine(p, p1, p2);
}
ret.Save(@"D:\render.png", ImageFormat.Bmp);
return ret;
}
}