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 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 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 entries, Dictionary groups, Dictionary types, Dictionary 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 OrderedGroups(VehicleSnapshot snapshot) => ByFrequency(snapshot.Stock, e => e.Group).ToList(); private static List 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 OrderedBrands(VehicleSnapshot snapshot) => ByFrequency(snapshot.Stock.Where(e => e.Group == PassengerCarGroup), e => e.Brand) .OrderBy(brand => brand == ThurgauVehicleMapper.OtherBrand) .ToList(); private static IEnumerable ByFrequency(IEnumerable entries, Func 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 IndexOf(IEnumerable 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 entries) { private readonly Dictionary> 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; } } }