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>
58 lines
2.1 KiB
C#
58 lines
2.1 KiB
C#
using System.Collections.Frozen;
|
|
using System.Collections.Immutable;
|
|
using Schaad.OpenData.Traffic.Models;
|
|
|
|
namespace Schaad.OpenData.Traffic.Storage;
|
|
|
|
public sealed class TrafficSnapshot
|
|
{
|
|
public TrafficSnapshot(
|
|
IEnumerable<Station> stations,
|
|
IEnumerable<HourlyCount> counts,
|
|
IEnumerable<StationGeometry> geometry,
|
|
DateTimeOffset? lastRefresh)
|
|
{
|
|
Stations = stations.ToFrozenDictionary(s => s.Code);
|
|
CountsByDate = counts
|
|
.GroupBy(c => c.Date)
|
|
.ToFrozenDictionary(g => g.Key, g => g.ToImmutableArray());
|
|
Geometry = geometry.ToFrozenDictionary(g => g.StationCode);
|
|
Dates = [.. CountsByDate.Keys.Order()];
|
|
LastRefresh = lastRefresh;
|
|
}
|
|
|
|
public static TrafficSnapshot Empty { get; } = new([], [], [], null);
|
|
|
|
public FrozenDictionary<int, Station> Stations { get; }
|
|
|
|
public FrozenDictionary<DateOnly, ImmutableArray<HourlyCount>> CountsByDate { get; }
|
|
|
|
public FrozenDictionary<int, StationGeometry> Geometry { get; }
|
|
|
|
public ImmutableArray<DateOnly> Dates { get; }
|
|
|
|
public DateTimeOffset? LastRefresh { get; }
|
|
|
|
public TrafficSnapshot Merge(TrafficImport import, DateTimeOffset refreshedAt, out IReadOnlyList<DateOnly> changedDays)
|
|
{
|
|
var importedDays = import.Counts.Select(c => c.Date).ToHashSet();
|
|
changedDays = [.. importedDays.Order()];
|
|
|
|
var stations = Stations.Values
|
|
.Where(s => import.Stations.All(i => i.Code != s.Code))
|
|
.Concat(import.Stations);
|
|
var counts = CountsByDate
|
|
.Where(kv => !importedDays.Contains(kv.Key))
|
|
.SelectMany(kv => kv.Value)
|
|
.Concat(import.Counts);
|
|
|
|
return new TrafficSnapshot(stations, counts, Geometry.Values, refreshedAt);
|
|
}
|
|
|
|
public TrafficSnapshot WithGeometry(IEnumerable<StationGeometry> geometry)
|
|
{
|
|
var added = geometry.ToList();
|
|
var merged = Geometry.Values.Where(g => added.All(a => a.StationCode != g.StationCode)).Concat(added);
|
|
return new TrafficSnapshot(Stations.Values, CountsByDate.Values.SelectMany(c => c), merged, LastRefresh);
|
|
}
|
|
}
|