OpenDataVisualizer/Schaad.OpenData.Vehicles/Source/ThurgauVehicleMapper.cs
Claudio Schaad 688775e742 Add Thurgau vehicle stock map (#3)
Choropleth of the 80 Thurgau municipalities from the road vehicle stock dataset (djs-stv-17), with population from sk-stat-56 and outlines from swisstopo. Also fixes the broken ENV line in the Dockerfile.

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>
2026-11-06 21:43:23 +00:00

42 lines
1.9 KiB
C#

using System.Globalization;
using Schaad.OpenData.Vehicles.Models;
namespace Schaad.OpenData.Vehicles.Source;
public static class ThurgauVehicleMapper
{
public const string OtherBrand = "Andere";
public const string Unknown = "Unbekannt";
public static IReadOnlyList<StockEntry> MapStock(IEnumerable<ThurgauStockRecord> records) =>
records
.Select(r => (Record: r, Year: ParseYear(r.Year), Id: Clean(r.MunicipalityId)))
.Where(r => r.Year is not null && r.Id is not null && r.Record.Count > 0)
.Select(r => new StockEntry(
r.Year!.Value,
r.Id!,
Clean(r.Record.Municipality) ?? r.Id!,
Clean(r.Record.District) ?? Unknown,
Clean(r.Record.Group) ?? Unknown,
Clean(r.Record.Type) ?? Unknown,
Clean(r.Record.Brand) ?? OtherBrand,
(int)Math.Round(r.Record.Count!.Value)))
.ToList();
public static IReadOnlyList<PopulationEntry> MapPopulation(IEnumerable<ThurgauPopulationRecord> records) =>
records
.Select(r => (Year: ParseYear(r.Year), Id: Clean(r.MunicipalityId), Population: ParseInt(r.Population)))
.Where(r => r.Year is not null && r.Id is not null && r.Population > 0)
.Select(r => new PopulationEntry(r.Year!.Value, r.Id!, r.Population!.Value))
.ToList();
private static int? ParseYear(string? value) =>
value is { Length: >= 4 } && int.TryParse(value.AsSpan(0, 4), NumberStyles.None, CultureInfo.InvariantCulture, out var year)
? year
: null;
private static int? ParseInt(string? value) =>
double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var number) ? (int)Math.Round(number) : null;
private static string? Clean(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
}