- Thurgau hourly traffic counts (data.tg.ch) refreshed every 6 h into a per-day JSON cache - Road geometry per station direction from swisstopo swissTLM3D; roundabout arms run outward from the roundabout centre - Leaflet map with animated vehicles by class, time controls and base map switcher (grey, colour, aerial) - Station panel with per-class counts and Chart.js charts - Start page with map tiles; traffic map at /verkehr/thurgau - Dockerfile and docker-compose, xUnit tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Claudio Schaad <c.schaad@pog.ch> Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
77 lines
3.4 KiB
C#
77 lines
3.4 KiB
C#
using Schaad.OpenData.Traffic.Models;
|
|
|
|
namespace Schaad.OpenData.Traffic.Geometry;
|
|
|
|
public class StationGeometryService(SwisstopoClient swisstopoClient)
|
|
{
|
|
private const double HalfPathLengthMeters = 300;
|
|
private const double SearchRadiusMeters = 650;
|
|
private const double MetersPerDegreeLat = 111_320;
|
|
private const double MinimumArmOffsetMeters = 3;
|
|
private const double OutwardTargetMeters = 3000;
|
|
private const string RoundaboutMarker = "Kreisel";
|
|
|
|
public virtual async Task<StationGeometry> BuildAsync(Station station, IReadOnlyList<Station> stations, CancellationToken cancellationToken)
|
|
{
|
|
var isRoundaboutArm = station.Name.Contains(RoundaboutMarker, StringComparison.OrdinalIgnoreCase);
|
|
var outwardTarget = isRoundaboutArm ? OutwardTarget(station, stations) : null;
|
|
|
|
var directions = new List<ResolvedDirection>();
|
|
foreach (var direction in station.Directions)
|
|
{
|
|
var target = DirectionTarget.Parse(direction);
|
|
var location = outwardTarget ?? await FindPlaceAsync(target.Place, station.Location, cancellationToken);
|
|
directions.Add(new ResolvedDirection(direction, target, location));
|
|
}
|
|
|
|
var targetLocation = directions.Select(d => d.TargetLocation).FirstOrDefault(l => l is not null);
|
|
var segments = await swisstopoClient.GetRoadSegmentsAsync(station.Location, SearchRadiusMeters, cancellationToken);
|
|
var roadPath = (isRoundaboutArm && targetLocation is { } armTarget
|
|
? RoadPathBuilder.BuildArmPath(station.Location, segments, armTarget, 2 * HalfPathLengthMeters, station.Address)
|
|
: null)
|
|
?? RoadPathBuilder.BuildRoadPath(station.Location, segments, HalfPathLengthMeters);
|
|
|
|
var path = roadPath ?? RoadPathBuilder.BuildStraightPath(station.Location, targetLocation, HalfPathLengthMeters);
|
|
|
|
return new StationGeometry(station.Code, roadPath is not null, RoadPathBuilder.Orient(station.Location, path, directions));
|
|
}
|
|
|
|
internal static GeoPoint? OutwardTarget(Station station, IReadOnlyList<Station> stations)
|
|
{
|
|
var key = RoundaboutKey(station.Name);
|
|
var siblings = stations.Where(s => RoundaboutKey(s.Name) == key).ToList();
|
|
if (siblings.Count < 2)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var center = new GeoPoint(siblings.Average(s => s.Location.Lat), siblings.Average(s => s.Location.Lon));
|
|
var deltaLat = station.Location.Lat - center.Lat;
|
|
var deltaLon = station.Location.Lon - center.Lon;
|
|
var meters = Math.Sqrt(
|
|
Math.Pow(deltaLat * MetersPerDegreeLat, 2)
|
|
+ Math.Pow(deltaLon * MetersPerDegreeLat * Math.Cos(center.Lat * Math.PI / 180), 2));
|
|
if (meters < MinimumArmOffsetMeters)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var scale = OutwardTargetMeters / meters;
|
|
return new GeoPoint(center.Lat + deltaLat * scale, center.Lon + deltaLon * scale);
|
|
}
|
|
|
|
private static string RoundaboutKey(string name) => name.Split(" - ", 2)[0].Trim();
|
|
|
|
private async Task<GeoPoint?> FindPlaceAsync(string place, GeoPoint near, CancellationToken cancellationToken)
|
|
{
|
|
foreach (var part in place.Split('/', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
if (await swisstopoClient.FindPlaceAsync(part, near, cancellationToken) is { } location)
|
|
{
|
|
return location;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|