feat: postal codes from GeoNames for the ISTAT municipalities (#45)

The municipalities now come with their postal codes, so the lookup from
postal code works on a new company without loading anything. GeoNames
(CC BY 4.0) lists places rather than municipalities: a postal code goes
to a municipality only when the name and the province abbreviation match
exactly one of them (the name alone within Sardinia, whose abbreviations
changed), which covers 7,750 of 7,894 municipalities; the rest are mostly
renamed or merged ones that users complete by hand.

tools/Khaemhat.PlacesTool rebuilds both lists from the ISTAT sheet and
the GeoNames file, framework only, so the yearly update is repeatable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-12 09:16:07 +01:00
1 parent 321aae7e81
commit a9eda510f2
15 files changed
+8192 -7936

No files matched your search

@@ -23,6 +23,7 @@ public sealed class DependencyRulesTests
["Khaemhat.Web"] = ["Khaemhat.Application", "Khaemhat.Data.Sqlite", "Khaemhat.Data.PostgreSql", "Khaemhat.Fiscal.Italy"],
["Khaemhat.LicenseTool"] = ["Khaemhat.Core", "Khaemhat.Infrastructure"],
["Khaemhat.DataModelTool"] = ["Khaemhat.Data"],
["Khaemhat.PlacesTool"] = [],
};
// The only packages, and where each may be referenced.
@@ -33,12 +33,26 @@ public sealed class ItalianPlacesTests
}
[TestMethod]
[DataRow("L219", "Torino", "TO", "Piemonte", "001272")]
[DataRow("H501", "Roma", "RM", "Lazio", "058091")]
[DataRow("A952", "Bolzano", "BZ", "Trentino-Alto Adige/Südtirol", "021008")]
public void Municipality_HasTheCadastralCodeOfTheTaxCode(string code, string name, string province, string region, string istat)
[DataRow("L219", "Torino", "TO", "Piemonte", "001272", "10100,10121-10156")]
[DataRow("A952", "Bolzano", "BZ", "Trentino-Alto Adige/Südtirol", "021008", "39100")]
[DataRow("B354", "Cagliari", "CA", "Sardegna", "118006", "09100,09121-09131,09134")]
[DataRow("I138", "Sanremo", "IM", "Liguria", "008055", null)]
public void Municipality_HasTheCadastralCodeOfTheTaxCode_AndThePostalCodesFound(string code, string name, string province, string region, string istat, string? postalCodes)
{
Assert.AreEqual(new MunicipalityTemplate(code, name, province, region, istat, null), Places.Municipalities.Single(m => m.Code == code));
Assert.AreEqual(new MunicipalityTemplate(code, name, province, region, istat, postalCodes), Places.Municipalities.Single(m => m.Code == code));
}
[TestMethod]
public void PostalCodes_CoverAlmostEveryMunicipality_AndAreFiveDigitsOrRanges()
{
var withCodes = Places.Municipalities.Where(m => m.PostalCodes is not null).ToList();
Assert.IsGreaterThan(7700, withCodes.Count);
Assert.Contains("GeoNames", Places.Source);
foreach (var municipality in withCodes)
{
Assert.MatchesRegex(@"^[0-9]{5}(-[0-9]{5})?(,[0-9]{5}(-[0-9]{5})?)*$", municipality.PostalCodes!, municipality.Code);
}
}
[TestMethod]
@@ -123,16 +123,15 @@ public sealed class CodeRulesTests
var tables = services.GetRequiredService<TableService>();
var autoFill = services.GetRequiredService<AutoFillService>();
// The base data brings every municipality of the ISTAT list, without postal codes: the users add them.
// The base data brings every municipality of the ISTAT list with the GeoNames postal codes; the few missing
// ones are written by the users.
var municipalities = await tables.ActiveAsync<Municipality>(Root, company);
Assert.HasCount(7894, municipalities);
Assert.IsEmpty(await autoFill.PlacesByPostalCodeAsync(Root, company, "10128"));
var turinRow = municipalities.Single(m => m.Code == "L219");
turinRow.PostalCodes = "10121-10156";
await tables.SaveAsync(Root, company, turinRow);
var pinerolo = municipalities.Single(m => m.Code == "G674");
pinerolo.PostalCodes = "10064";
await tables.SaveAsync(Root, company, pinerolo);
Assert.IsEmpty(await autoFill.PlacesByPostalCodeAsync(Root, company, "18038"));
var sanremo = municipalities.Single(m => m.Code == "I138");
sanremo.PostalCodes = "18038";
await tables.SaveAsync(Root, company, sanremo);
Assert.AreEqual("Sanremo", (await autoFill.PlacesByPostalCodeAsync(Root, company, "18038")).Single().Name);
var turin = (await autoFill.PlacesByPostalCodeAsync(Root, company, "10128")).Single();
Assert.AreEqual(new PlaceSuggestion("L219", "Torino", "TO", "Piemonte", "10128"), turin);