ASP.NET Core 10 API and React client that visualize the canton of Thurgau's hourly traffic counts by vehicle class (data.tg.ch, CC0). - Background refresh from data.tg.ch every 6 h into a per-day JSON cache, so history grows beyond the source's two-week window - Road geometry per station direction from swisstopo swissTLM3D, oriented by geocoding the direction's target town; roundabout arms follow the road outward only - Leaflet map with canvas animation: dots drive along the road in both directions, density by hourly count, colour by vehicle class, right-hand traffic; play/pause, time slider and day selection - Station panel with per-class counts and Chart.js charts (hourly, weekday/weekend profile, daily totals, class mix) - swisstopo base map switcher (grey, colour, aerial) - Dockerfile and docker-compose with a data volume - xUnit tests for mapping, cache, geometry, geocoding and analysis Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
45 lines
2 KiB
C#
45 lines
2 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;
|
|
|
|
public virtual async Task<StationGeometry> BuildAsync(Station station, CancellationToken cancellationToken)
|
|
{
|
|
var directions = new List<ResolvedDirection>();
|
|
foreach (var direction in station.Directions)
|
|
{
|
|
var target = DirectionTarget.Parse(direction);
|
|
directions.Add(new ResolvedDirection(direction, target, await FindPlaceAsync(target.Place, station.Location, cancellationToken)));
|
|
}
|
|
|
|
var targetLocation = directions.Select(d => d.TargetLocation).FirstOrDefault(l => l is not null);
|
|
var isRoundaboutArm = station.Name.Contains("Kreisel", StringComparison.OrdinalIgnoreCase) && targetLocation is not null;
|
|
|
|
var segments = await swisstopoClient.GetRoadSegmentsAsync(station.Location, SearchRadiusMeters, cancellationToken);
|
|
var roadPath = (isRoundaboutArm
|
|
? RoadPathBuilder.BuildArmPath(station.Location, segments, targetLocation!.Value, 2 * HalfPathLengthMeters)
|
|
: 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));
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|