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>
86 lines
3.6 KiB
C#
86 lines
3.6 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
using Microsoft.Extensions.Options;
|
|
using Schaad.OpenData.Watercraft.Models;
|
|
|
|
namespace Schaad.OpenData.Watercraft.Source;
|
|
|
|
public partial class ThurgauWatercraftClient(HttpClient httpClient, IOptions<WatercraftOptions> options)
|
|
{
|
|
private const string SearchText = "immatrikulierten Wasserfahrzeuge";
|
|
private const string TitlePrefix = "Bestand der bei der Schifffahrtskontrolle";
|
|
|
|
public virtual async Task<IReadOnlyList<VesselDataset>> FindDatasetsAsync(CancellationToken cancellationToken)
|
|
{
|
|
var where = Uri.EscapeDataString($"search(\"{SearchText}\")");
|
|
var url = $"{options.Value.CatalogUrl}?where={where}&limit=100";
|
|
using var response = await httpClient.GetAsync(url, cancellationToken);
|
|
response.EnsureSuccessStatusCode();
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
|
|
using var document = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken);
|
|
|
|
var datasets = new List<VesselDataset>();
|
|
foreach (var result in document.RootElement.GetProperty("results").EnumerateArray())
|
|
{
|
|
if (ToDataset(result) is { } dataset)
|
|
{
|
|
datasets.Add(dataset);
|
|
}
|
|
}
|
|
|
|
return datasets
|
|
.GroupBy(d => d.Year)
|
|
.Select(g => g.MaxBy(d => d.Modified ?? DateTimeOffset.MinValue)!)
|
|
.OrderBy(d => d.Year)
|
|
.ToList();
|
|
}
|
|
|
|
public virtual async Task<VesselYear> FetchAsync(VesselDataset dataset, CancellationToken cancellationToken)
|
|
{
|
|
var url = $"{options.Value.CatalogUrl}/{Uri.EscapeDataString(dataset.DatasetId)}/exports/json";
|
|
using var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
|
response.EnsureSuccessStatusCode();
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
|
|
|
|
var records = new List<ThurgauVesselRecord>();
|
|
await foreach (var record in JsonSerializer.DeserializeAsyncEnumerable<ThurgauVesselRecord>(stream, cancellationToken: cancellationToken))
|
|
{
|
|
if (record is not null)
|
|
{
|
|
records.Add(record);
|
|
}
|
|
}
|
|
|
|
return ThurgauVesselMapper.Map(dataset, records);
|
|
}
|
|
|
|
private static VesselDataset? ToDataset(JsonElement result)
|
|
{
|
|
if (!result.TryGetProperty("dataset_id", out var idElement)
|
|
|| idElement.GetString() is not { } id
|
|
|| !result.TryGetProperty("metas", out var metas)
|
|
|| !metas.TryGetProperty("default", out var meta))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var title = meta.TryGetProperty("title", out var titleElement) ? titleElement.GetString() ?? "" : "";
|
|
var yearMatch = TrailingYear().Match(title);
|
|
if (!title.StartsWith(TitlePrefix, StringComparison.OrdinalIgnoreCase) || !yearMatch.Success)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
DateTimeOffset? modified = meta.TryGetProperty("modified", out var modifiedElement)
|
|
&& modifiedElement.ValueKind == JsonValueKind.String
|
|
&& DateTimeOffset.TryParse(modifiedElement.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var parsed)
|
|
? parsed
|
|
: null;
|
|
|
|
return new VesselDataset(id, int.Parse(yearMatch.Groups[1].Value, CultureInfo.InvariantCulture), modified);
|
|
}
|
|
|
|
[GeneratedRegex(@"(\d{4})\s*$")]
|
|
private static partial Regex TrailingYear();
|
|
}
|