New Schaad.OpenData.Watercraft library that discovers the yearly "immatrikulierte Wasserfahrzeuge" datasets (2021–2025) on data.tg.ch, caches them and aggregates boats per harbour location by type, propulsion, fuel, emission group and use, with length/power bins. API under /api/watercraft, new page /schifffahrt/thurgau with donut markers per location, year playback, toggleable categories and a location/canton panel. Swisstopo base layers moved to map/baseLayers.ts for both maps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
5.1 KiB
C#
146 lines
5.1 KiB
C#
using Schaad.OpenData.Watercraft.Models;
|
||
using Schaad.OpenData.Watercraft.Storage;
|
||
|
||
namespace Schaad.OpenData.Watercraft.Analysis;
|
||
|
||
public static class WatercraftAnalysis
|
||
{
|
||
public const string CantonName = "Kanton Thurgau";
|
||
|
||
private const double LicenseFreePowerKw = 6;
|
||
private const double LicenseFreeSailAreaM2 = 15;
|
||
|
||
private static readonly double[] LengthLimitsM = [4, 6, 8, 10, 12];
|
||
private static readonly double[] PowerLimitsKw = [6, 25, 75, 150, 300];
|
||
|
||
public static IReadOnlyList<string> LengthBinLabels { get; } =
|
||
["unter 4 m", "4–6 m", "6–8 m", "8–10 m", "10–12 m", "ab 12 m"];
|
||
|
||
public static IReadOnlyList<string> PowerBinLabels { get; } =
|
||
[VesselDimension.NoEngine, "bis 6 kW", "6–25 kW", "25–75 kW", "75–150 kW", "150–300 kW", "über 300 kW"];
|
||
|
||
public static WatercraftOverview BuildOverview(WatercraftSnapshot snapshot) => new(
|
||
snapshot.Years.Select(y => new YearInfo(y.Year, y.DatasetId, y.ReferenceDate, y.Vessels.Count)).ToList(),
|
||
BuildDimensions(snapshot),
|
||
LengthBinLabels,
|
||
PowerBinLabels);
|
||
|
||
public static IReadOnlyList<DimensionView> BuildDimensions(WatercraftSnapshot snapshot)
|
||
{
|
||
var vessels = snapshot.Years.SelectMany(y => y.Vessels).ToList();
|
||
return VesselDimension.All
|
||
.Select(dimension => new DimensionView(
|
||
dimension.Key,
|
||
dimension.Label,
|
||
vessels
|
||
.GroupBy(dimension.Categorize)
|
||
.OrderByDescending(g => g.Count())
|
||
.ThenBy(g => g.Key, StringComparer.Ordinal)
|
||
.Select(g => g.Key)
|
||
.ToList()))
|
||
.ToList();
|
||
}
|
||
|
||
public static IReadOnlyList<YearView> BuildYears(WatercraftSnapshot snapshot)
|
||
{
|
||
var dimensions = BuildDimensions(snapshot);
|
||
return snapshot.Years.Select(year => BuildYear(year, dimensions)).ToList();
|
||
}
|
||
|
||
public static YearView BuildYear(VesselYear year, IReadOnlyList<DimensionView> dimensions)
|
||
{
|
||
var locations = year.Vessels
|
||
.GroupBy(v => v.Location, StringComparer.OrdinalIgnoreCase)
|
||
.Select(g => Summarize(g.First().Location, WatercraftLocations.Find(g.Key), g.ToList(), dimensions))
|
||
.OrderByDescending(l => l.Count)
|
||
.ThenBy(l => l.Name, StringComparer.Ordinal)
|
||
.ToList();
|
||
|
||
return new YearView(year.Year, year.ReferenceDate, Summarize(CantonName, null, year.Vessels, dimensions), locations);
|
||
}
|
||
|
||
private static LocationSummary Summarize(string name, GeoPoint? position, IReadOnlyList<Vessel> vessels, IReadOnlyList<DimensionView> dimensions)
|
||
{
|
||
var categories = new Dictionary<string, int[]>();
|
||
foreach (var view in dimensions)
|
||
{
|
||
var dimension = VesselDimension.All.First(d => d.Key == view.Key);
|
||
var counts = new int[view.Categories.Count];
|
||
foreach (var vessel in vessels)
|
||
{
|
||
var index = IndexOf(view.Categories, dimension.Categorize(vessel));
|
||
if (index >= 0)
|
||
{
|
||
counts[index]++;
|
||
}
|
||
}
|
||
|
||
categories[view.Key] = counts;
|
||
}
|
||
|
||
var lengthBins = new int[LengthBinLabels.Count];
|
||
var powerBins = new int[PowerBinLabels.Count];
|
||
foreach (var vessel in vessels)
|
||
{
|
||
if (vessel.LengthCm is { } lengthCm && lengthCm > 0)
|
||
{
|
||
lengthBins[Bin(lengthCm / 100, LengthLimitsM)]++;
|
||
}
|
||
|
||
powerBins[vessel.PowerKw is { } power && power > 0 ? 1 + Bin(power, PowerLimitsKw, true) : 0]++;
|
||
}
|
||
|
||
return new LocationSummary(
|
||
name,
|
||
position?.Lat,
|
||
position?.Lon,
|
||
vessels.Count,
|
||
categories,
|
||
lengthBins,
|
||
powerBins,
|
||
Median(vessels.Where(v => v.LengthCm > 0).Select(v => v.LengthCm!.Value / 100)),
|
||
Median(vessels.Where(v => v.PowerKw > 0).Select(v => v.PowerKw!.Value)),
|
||
vessels.Count(RequiresLicense));
|
||
}
|
||
|
||
public static bool RequiresLicense(Vessel vessel) =>
|
||
vessel.PowerKw > LicenseFreePowerKw || vessel.SailAreaM2 > LicenseFreeSailAreaM2;
|
||
|
||
private static int IndexOf(IReadOnlyList<string> categories, string category)
|
||
{
|
||
for (var i = 0; i < categories.Count; i++)
|
||
{
|
||
if (categories[i] == category)
|
||
{
|
||
return i;
|
||
}
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
private static int Bin(double value, double[] limits, bool upperInclusive = false)
|
||
{
|
||
for (var i = 0; i < limits.Length; i++)
|
||
{
|
||
if (upperInclusive ? value <= limits[i] : value < limits[i])
|
||
{
|
||
return i;
|
||
}
|
||
}
|
||
|
||
return limits.Length;
|
||
}
|
||
|
||
private static double? Median(IEnumerable<double> values)
|
||
{
|
||
var sorted = values.Order().ToList();
|
||
if (sorted.Count == 0)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var middle = sorted.Count / 2;
|
||
return sorted.Count % 2 == 1 ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2;
|
||
}
|
||
}
|