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>
139 lines
5.8 KiB
C#
139 lines
5.8 KiB
C#
using Schaad.OpenData.Vehicles.Models;
|
|
using Schaad.OpenData.Vehicles.Source;
|
|
using Schaad.OpenData.Vehicles.Storage;
|
|
|
|
namespace Schaad.OpenData.Vehicles.Analysis;
|
|
|
|
public static class VehicleAnalysis
|
|
{
|
|
public const string CantonId = "TG";
|
|
public const string CantonName = "Kanton Thurgau";
|
|
public const string PassengerCarGroup = "Personenwagen";
|
|
|
|
public static VehicleOverview BuildOverview(VehicleSnapshot snapshot)
|
|
{
|
|
var population = new PopulationLookup(snapshot.Population);
|
|
var municipalityIds = snapshot.Stock.Select(e => e.MunicipalityId).Distinct(StringComparer.Ordinal).ToList();
|
|
var years = snapshot.Years
|
|
.Select(year => new VehicleYearInfo(
|
|
year,
|
|
ReferenceDate(year),
|
|
snapshot.Stock.Where(e => e.Year == year).Sum(e => e.Count),
|
|
municipalityIds.Sum(id => population.Find(year, id))))
|
|
.ToList();
|
|
|
|
return new VehicleOverview(years, OrderedGroups(snapshot), OrderedTypes(snapshot), OrderedBrands(snapshot));
|
|
}
|
|
|
|
public static IReadOnlyList<VehicleYearView> BuildYears(VehicleSnapshot snapshot)
|
|
{
|
|
var population = new PopulationLookup(snapshot.Population);
|
|
var groups = IndexOf(OrderedGroups(snapshot));
|
|
var types = IndexOf(OrderedTypes(snapshot).Select(t => t.Name));
|
|
var brands = IndexOf(OrderedBrands(snapshot));
|
|
|
|
return snapshot.Years
|
|
.Select(year =>
|
|
{
|
|
var entries = snapshot.Stock.Where(e => e.Year == year).ToList();
|
|
var municipalities = entries
|
|
.GroupBy(e => e.MunicipalityId, StringComparer.Ordinal)
|
|
.Select(g =>
|
|
{
|
|
var first = g.First();
|
|
return Summarize(first.MunicipalityId, first.Municipality, first.District, population.Find(year, g.Key), g, groups, types, brands);
|
|
})
|
|
.OrderBy(m => m.Name, StringComparer.Ordinal)
|
|
.ToList();
|
|
var canton = Summarize(CantonId, CantonName, null, municipalities.Sum(m => m.Population), entries, groups, types, brands);
|
|
return new VehicleYearView(year, ReferenceDate(year), canton, municipalities);
|
|
})
|
|
.ToList();
|
|
}
|
|
|
|
public static IReadOnlyList<MunicipalityShape> BuildShapes(VehicleSnapshot snapshot)
|
|
{
|
|
var names = snapshot.Stock
|
|
.GroupBy(e => e.MunicipalityId, StringComparer.Ordinal)
|
|
.ToDictionary(g => g.Key, g => g.MaxBy(e => e.Year)!, StringComparer.Ordinal);
|
|
|
|
return snapshot.Outlines
|
|
.Where(o => names.ContainsKey(o.Id))
|
|
.Select(o => new MunicipalityShape(o.Id, names[o.Id].Municipality, names[o.Id].District, o.Polygons))
|
|
.OrderBy(s => s.Name, StringComparer.Ordinal)
|
|
.ToList();
|
|
}
|
|
|
|
private static AreaSummary Summarize(
|
|
string id,
|
|
string name,
|
|
string? district,
|
|
int population,
|
|
IEnumerable<StockEntry> entries,
|
|
Dictionary<string, int> groups,
|
|
Dictionary<string, int> types,
|
|
Dictionary<string, int> brands)
|
|
{
|
|
var groupCounts = new int[groups.Count];
|
|
var typeCounts = new int[types.Count];
|
|
var brandCounts = new int[brands.Count];
|
|
var count = 0;
|
|
foreach (var entry in entries)
|
|
{
|
|
count += entry.Count;
|
|
groupCounts[groups[entry.Group]] += entry.Count;
|
|
typeCounts[types[entry.Type]] += entry.Count;
|
|
if (entry.Group == PassengerCarGroup)
|
|
{
|
|
brandCounts[brands[entry.Brand]] += entry.Count;
|
|
}
|
|
}
|
|
|
|
return new AreaSummary(id, name, district, population, count, groupCounts, typeCounts, brandCounts);
|
|
}
|
|
|
|
private static List<string> OrderedGroups(VehicleSnapshot snapshot) =>
|
|
ByFrequency(snapshot.Stock, e => e.Group).ToList();
|
|
|
|
private static List<VehicleTypeInfo> OrderedTypes(VehicleSnapshot snapshot)
|
|
{
|
|
var groupOfType = snapshot.Stock
|
|
.GroupBy(e => e.Type, StringComparer.Ordinal)
|
|
.ToDictionary(g => g.Key, g => ByFrequency(g, e => e.Group).First(), StringComparer.Ordinal);
|
|
return ByFrequency(snapshot.Stock, e => e.Type).Select(type => new VehicleTypeInfo(type, groupOfType[type])).ToList();
|
|
}
|
|
|
|
private static List<string> OrderedBrands(VehicleSnapshot snapshot) =>
|
|
ByFrequency(snapshot.Stock.Where(e => e.Group == PassengerCarGroup), e => e.Brand)
|
|
.OrderBy(brand => brand == ThurgauVehicleMapper.OtherBrand)
|
|
.ToList();
|
|
|
|
private static IEnumerable<string> ByFrequency(IEnumerable<StockEntry> entries, Func<StockEntry, string> key) =>
|
|
entries
|
|
.GroupBy(key, StringComparer.Ordinal)
|
|
.OrderByDescending(g => g.Sum(e => e.Count))
|
|
.ThenBy(g => g.Key, StringComparer.Ordinal)
|
|
.Select(g => g.Key);
|
|
|
|
private static Dictionary<string, int> IndexOf(IEnumerable<string> keys) =>
|
|
keys.Select((key, index) => (key, index)).ToDictionary(p => p.key, p => p.index, StringComparer.Ordinal);
|
|
|
|
private static DateOnly ReferenceDate(int year) => new(year, 12, 31);
|
|
|
|
private sealed class PopulationLookup(IEnumerable<PopulationEntry> entries)
|
|
{
|
|
private readonly Dictionary<string, List<PopulationEntry>> byMunicipality = entries
|
|
.GroupBy(e => e.MunicipalityId, StringComparer.Ordinal)
|
|
.ToDictionary(g => g.Key, g => g.OrderBy(e => e.Year).ToList(), StringComparer.Ordinal);
|
|
|
|
public int Find(int year, string municipalityId)
|
|
{
|
|
if (!byMunicipality.TryGetValue(municipalityId, out var list))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return (list.LastOrDefault(e => e.Year <= year) ?? list[0]).Population;
|
|
}
|
|
}
|
|
}
|