Choropleth of the 80 Thurgau municipalities from the road vehicle stock dataset (djs-stv-17): vehicles per 1000 inhabitants by vehicle group or share of a car brand among passenger cars, with year controls and a municipality panel. Population comes from sk-stat-56, outlines from swisstopo swissBOUNDARIES3D. Also fixes the broken ENV line in the Dockerfile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
42 lines
1.9 KiB
C#
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();
|
|
}
|