fix(counties): make the cluster and the entry panel agree on a US county

An operator asked why K1SEI showed "Middlesex" in Info (F2) and "Lower
Connecticut River Valley" in the cluster. Two sources: the entry panel has
a callbook answer, a spot carries only a callsign so the cluster derives one
from the FCC licence ZIP through GeoNames — and GeoNames has followed the
Census in replacing Connecticut's counties with the 2022 planning regions.
No award, callbook or log uses those, so every CT station matched nothing:
new county for ever, and counting toward nothing.

Measured against a full ULS import (1 556 444 US callsigns), 23 223 resolved
to a name the USA-CA reference does not contain. Three causes, three fixes:

  - Spelling. "City and County of San Francisco", "Baltimore (city)",
    "Nome (CA)", plus counties renamed since the award list was drawn
    (Kusilvak, Oglala Lakota, the Valdez-Cordova split) and Alaska's four
    "X City and Borough", whose reference codes read "JUNEAUCITYAND" because
    the county-type suffix strip eats the wrong end. Normalised in
    award.USCountyKey, which both sides already go through. 7 515 callsigns,
    no re-download needed.

  - Doña Ana, NM shipped into the reference as "NM/DO̱AANA" — mangled by a
    non-UTF-8 CSV line, a code nothing could ever produce, so that county was
    unwinnable and silent about it. Row repaired, cntygen now refuses such a
    line, and a test makes every one of the 3 102 references reproduce its own
    code from its own name.

  - Connecticut. A planning region is drawn from towns in several counties, so
    no name maps to a name — only the ZIP can resolve it. cmd/ctzipgen builds
    the table from the Census 2020 crosswalk, filling PO-box-only ZIPs from the
    nearest resolved centroid; all 11 ZIPs GeoNames still labels with a real
    county agree with the result. 15 037 callsigns, applied at import, so the
    store now carries a rules version and Settings says when a re-download is
    needed.

Alignment itself is the last piece: a spot now shows the county the station is
logged with when we have one, and falls back to the ZIP-derived county only for
stations never worked.
This commit is contained in:
2026-08-14 00:42:27 +02:00
parent fbd62fda30
commit 30143b01bf
15 changed files with 1122 additions and 15 deletions
+13 -4
View File
@@ -749,9 +749,14 @@ func EmissionOf(mode string) string { return emissionOf(mode) }
// abbreviating "Saint"→"St." and hyphenating "Matanuska-Susitna". The rules:
// - if cnty already carries "ST,County", split on the first comma; else take
// the state from the STATE column;
// - upper-case; drop periods and apostrophes; hyphens→space; strip a trailing
// County/Parish/Borough/Census Area/Municipality; fold Saint(e)→St(e);
// collapse whitespace;
// - upper-case; fold accents (the FIPS list writes "Doña Ana", callbooks and
// logs write "Dona Ana"); drop periods and apostrophes; hyphens→space;
// expand the parenthetical shapes GeoNames uses for county-equivalents
// ("Baltimore (city)", "Nome (CA)") and drop its "City and County of"
// style prefixes; strip a trailing County/Parish/Borough/Census Area/
// Municipality; fold Saint(e)→St(e); collapse whitespace;
// - fold the counties renamed since the USA-CA list was drawn onto the name
// the award still uses, so a QSO there counts for something (see renamed);
// - require a 2-letter state and a non-empty county, else no match ("").
func USCountyKey(state, cnty string) string {
s := strings.TrimSpace(cnty)
@@ -764,10 +769,11 @@ func USCountyKey(state, cnty string) string {
} else {
st, co = strings.TrimSpace(state), s
}
co = strings.ToUpper(co)
co = strings.ToUpper(foldAccents(co))
co = strings.ReplaceAll(co, ".", "")
co = strings.ReplaceAll(co, "'", "")
co = strings.ReplaceAll(co, "-", " ")
co = expandCountyEquivalent(co)
for _, suf := range []string{" COUNTY", " PARISH", " BOROUGH", " CENSUS AREA", " MUNICIPALITY"} {
if strings.HasSuffix(co, suf) {
co = strings.TrimSuffix(co, suf)
@@ -788,6 +794,9 @@ func USCountyKey(state, cnty string) string {
if len(st) != 2 || co == "" {
return ""
}
if alias, ok := renamed[st+"/"+co]; ok {
co = alias
}
// Separator is "/", NOT ",": the QSOFIELDS matcher splits a field value on
// commas/semicolons (n-fer POTA "US-1,US-2"), which would shatter "AL,AUTAUGA"
// into two non-matching tokens. The stored ADIF cnty keeps its comma; only
+120
View File
@@ -0,0 +1,120 @@
package award
import "strings"
// Name normalisation for USCountyKey. It exists because the county a QSO
// carries and the county the USA-CA reference list carries come from different
// hands and rarely agree letter for letter:
//
// - the reference is the FIPS list ("Doña Ana County", "Baltimore city");
// - a callbook writes what the licensee typed;
// - and OpsLog's own offline resolver reads GeoNames, which spells the
// county-equivalents its own way ("Baltimore (city)", "Nome (CA)",
// "City and County of San Francisco").
//
// Measured against a full FCC ULS import (1.56 M US callsigns), the shapes
// handled here account for ~7 300 stations whose county matched nothing at all
// — they showed as a new county for ever and counted for no award.
//
// Everything here is SPELLING. A county that genuinely no longer exists under
// that name is a different problem, handled by renamed below.
// foldAccents strips the diacritics that separate a FIPS name from the ASCII
// every log and callbook actually holds. Only the letters that occur in US
// place names are listed — this is not a general Unicode folder, and it must
// not become one: silently folding arbitrary marks would let two distinct
// references collapse onto one key.
var accents = strings.NewReplacer(
"ñ", "n", "Ñ", "N",
"á", "a", "Á", "A",
"é", "e", "É", "E",
"í", "i", "Í", "I",
"ó", "o", "Ó", "O",
"ú", "u", "Ú", "U",
"ü", "u", "Ü", "U",
"ç", "c", "Ç", "C",
)
func foldAccents(s string) string {
// Fast path: US county names are ASCII with a single exception (Doña Ana),
// so the common case must not pay for the replacer.
for i := 0; i < len(s); i++ {
if s[i] >= 0x80 {
return accents.Replace(s)
}
}
return s
}
// expandCountyEquivalent rewrites GeoNames' parenthetical and prefixed spellings
// of county-equivalents into the plain FIPS form, which the rest of USCountyKey
// then normalises as usual.
//
// Input is already upper-cased with periods and hyphens removed.
//
// BALTIMORE (CITY) → BALTIMORE CITY (FIPS "Baltimore city")
// NOME (CA) → NOME CENSUS AREA (suffix stripped after)
// CITY AND COUNTY OF SAN FRANCISCO → SAN FRANCISCO
//
// The truncated "(CITY" is not a typo here: GeoNames really does ship
// "Colonial Heights (city" with the closing parenthesis missing.
func expandCountyEquivalent(co string) string {
switch {
case strings.HasSuffix(co, " (CITY)"):
co = strings.TrimSuffix(co, " (CITY)") + " CITY"
case strings.HasSuffix(co, " (CITY"):
co = strings.TrimSuffix(co, " (CITY") + " CITY"
case strings.HasSuffix(co, " (CA)"):
co = strings.TrimSuffix(co, " (CA)") + " CENSUS AREA"
}
// "City and County of X" and "City and Borough of X" are the same place as
// FIPS's bare "X" (San Francisco, Denver, Honolulu, Juneau, Sitka). Note the
// order: the longer prefixes must be tried before "CITY OF ".
for _, p := range []string{
"CITY AND COUNTY OF ",
"CITY AND BOROUGH OF ",
"MUNICIPALITY OF ",
"BOROUGH OF ",
"CITY OF ",
} {
if strings.HasPrefix(co, p) {
return strings.TrimPrefix(co, p)
}
}
return co
}
// renamed folds a county renamed or re-drawn since the CQ USA-CA list was
// published onto the name the award still counts. Keys and values are finished
// USCountyKey county parts (upper-case, no spaces), looked up as "ST/COUNTY".
//
// This deliberately does NOT track the FIPS list. USA-CA is a fixed target of
// ~3 100 counties; when a state renames one, the award does not reissue its
// list, so a QSO with the new name would otherwise match nothing and count for
// nothing. Folding it onto the old name is the only outcome that scores.
//
// Connecticut is absent on purpose. Its 2022 planning regions are not renamed
// counties: a region is drawn from towns belonging to several different
// counties, so there is no name that maps to another name. That one is resolved
// from the station's ZIP, in internal/uls.
var renamed = map[string]string{
// Wade Hampton Census Area, AK → Kusilvak Census Area (2015).
"AK/KUSILVAK": "WADEHAMPTON",
// Shannon County, SD → Oglala Lakota County (2015).
"SD/OGLALALAKOTA": "SHANNON",
// Valdez-Cordova Census Area, AK was split in two (2019). Both halves fold
// back to the parent the award still lists.
"AK/CHUGACH": "VALDEZCORDOVA",
"AK/COPPERRIVER": "VALDEZCORDOVA",
// Alaska's four "X City and Borough". The reference codes for these look
// wrong and are not: the trailing " BOROUGH" is stripped as a county type,
// leaving "JUNEAU CITY AND". Nothing else in the chain produces that shape
// — a log that simply says "Juneau", and GeoNames' "City and Borough of
// Juneau", both arrive here as "JUNEAU" and would match nothing. Rather
// than re-key four published references, bend the bare name onto them.
"AK/JUNEAU": "JUNEAUCITYAND",
"AK/SITKA": "SITKACITYAND",
"AK/WRANGELL": "WRANGELLCITYAND",
"AK/YAKUTAT": "YAKUTATCITYAND",
}
+1 -1
View File
@@ -1943,7 +1943,7 @@ func usCounties() []Ref {
ref("NM/COLFAX", "Colfax County, NM", 291),
ref("NM/CURRY", "Curry County, NM", 291),
ref("NM/DEBACA", "De Baca County, NM", 291),
ref("NM/DO̱AANA", "Do̱a Ana County, NM", 291),
ref("NM/DONAANA", "Doña Ana County, NM", 291),
ref("NM/EDDY", "Eddy County, NM", 291),
ref("NM/GRANT", "Grant County, NM", 291),
ref("NM/GUADALUPE", "Guadalupe County, NM", 291),
+118
View File
@@ -0,0 +1,118 @@
package awardref
import (
"strings"
"testing"
"unicode/utf8"
"hamlog/internal/award"
)
// TestUSCountyRefsSelfConsistent is the guard that was missing when the Doña Ana
// row shipped mangled: its code was "NM/DO̱AANA", which no log, no callbook and
// no county database could ever produce, so that county was unwinnable and
// nothing said so.
//
// Every reference must reproduce its own code from its own display name through
// award.USCountyKey — the same function cmd/cntygen used to build it. That ties
// the generated file to the matcher: change the normalisation rules and this
// fails the moment one of the 3 102 references stops matching itself.
func TestUSCountyRefsSelfConsistent(t *testing.T) {
refs := usCounties()
if len(refs) < 3000 {
t.Fatalf("only %d counties — the generated list looks truncated", len(refs))
}
for _, r := range refs {
if !utf8.ValidString(r.Name) || !utf8.ValidString(r.Code) {
t.Errorf("%s (%q): not valid UTF-8", r.Code, r.Name)
continue
}
// Name is "County Name, ST".
i := strings.LastIndex(r.Name, ", ")
if i < 0 {
t.Errorf("%s: name %q is not \"County, ST\"", r.Code, r.Name)
continue
}
name, st := r.Name[:i], r.Name[i+2:]
if got := award.USCountyKey(st, name); got != r.Code {
t.Errorf("USCountyKey(%q, %q) = %q, want %q", st, name, got, r.Code)
}
}
}
// TestUSCountyRefsUnique catches a normalisation rule loose enough to collapse
// two real counties onto one key — the failure mode that would silently merge
// them in every award total.
func TestUSCountyRefsUnique(t *testing.T) {
seen := map[string]string{}
for _, r := range usCounties() {
if prev, dup := seen[r.Code]; dup {
t.Errorf("code %s is shared by %q and %q", r.Code, prev, r.Name)
continue
}
seen[r.Code] = r.Name
}
}
// TestUSCountyResolverShapes pins the spellings OpsLog's own offline resolver
// produces (GeoNames, via internal/uls) against the reference. These are the
// real strings that were failing to match, with the number of US callsigns each
// covers in a full FCC ULS import.
func TestUSCountyResolverShapes(t *testing.T) {
valid := map[string]bool{}
for _, r := range usCounties() {
valid[r.Code] = true
}
cases := []struct {
st, county, want string
}{
{"CA", "City and County of San Francisco", "CA/SANFRANCISCO"}, // 4 217 calls
{"NM", "Doña Ana", "NM/DONAANA"}, // 1 214
{"NM", "Dona Ana", "NM/DONAANA"}, // what logs hold
{"MD", "Baltimore (city)", "MD/BALTIMORECITY"}, // 885
{"MO", "St. Louis (city)", "MO/STLOUISCITY"}, // 574
{"MO", "St. Louis", "MO/STLOUIS"}, // the county, distinct
{"AK", "Nome (CA)", "AK/NOME"}, // 252
{"AK", "Yukon-Koyukuk (CA)", "AK/YUKONKOYUKUK"}, // 84
{"AK", "City and Borough of Wrangell", "AK/WRANGELLCITYAND"}, // 27
{"AK", "Juneau", "AK/JUNEAUCITYAND"}, // a log that just says Juneau
{"AK", "Kusilvak", "AK/WADEHAMPTON"}, // renamed 2015
{"SD", "Oglala Lakota County", "SD/SHANNON"}, // renamed 2015
{"AK", "Chugach Census Area", "AK/VALDEZCORDOVA"}, // split 2019
{"AK", "Copper River Census Area", "AK/VALDEZCORDOVA"},
{"CO", "City and County of Denver", "CO/DENVER"},
{"HI", "City and County of Honolulu", "HI/HONOLULU"},
}
for _, c := range cases {
got := award.USCountyKey(c.st, c.county)
if got != c.want {
t.Errorf("USCountyKey(%q, %q) = %q, want %q", c.st, c.county, got, c.want)
continue
}
if !valid[got] {
t.Errorf("USCountyKey(%q, %q) = %q, which is not in the reference list", c.st, c.county, got)
}
}
}
// TestUSCountyKeepsVirginiaCitiesOut guards the other direction: cmd/cntygen
// drops Virginia's independent cities and Carson City on purpose, because CQ's
// USA-CA does not count them. Normalising their names must not smuggle them
// back in as some neighbouring county.
func TestUSCountyKeepsVirginiaCitiesOut(t *testing.T) {
valid := map[string]bool{}
for _, r := range usCounties() {
valid[r.Code] = true
}
for _, c := range []string{"Virginia Beach (city)", "City of Alexandria", "Lynchburg (city)"} {
if k := award.USCountyKey("VA", c); valid[k] {
t.Errorf("%q resolved to %s, which USA-CA does not count", c, k)
}
}
// Charles City and James City ARE counties, despite the name.
for _, c := range []string{"Charles City", "James City"} {
if k := award.USCountyKey("VA", c); !valid[k] {
t.Errorf("%q resolved to %s, which is missing from the reference", c, k)
}
}
}
+46
View File
@@ -2265,6 +2265,52 @@ func (r *Repo) WorkedCountyKeys(ctx context.Context, keyFn func(state, cnty stri
return out, rows.Err()
}
// CallCounties returns callsign → "STATE,County" for every US station already
// logged with a county, newest QSO winning.
//
// It exists so the cluster shows the SAME county the entry panel does. The
// entry panel gets its county from a callbook, which knows where the licensee
// actually lives; a spot carries only a callsign, so the cluster falls back to
// deriving one from the FCC licence address, which is right about 98% of the
// time. Where the log already holds a callbook's answer for that station, the
// two panels disagreeing is pure noise — this is what lets the better answer
// win in both.
//
// One DISTINCT scan when the cluster snapshot is rebuilt, then map lookups per
// spot, like the worked-county and worked-grid sets beside it.
func (r *Repo) CallCounties(ctx context.Context) (map[string]string, error) {
rows, err := r.db.QueryContext(ctx,
`SELECT callsign, COALESCE(state,''), COALESCE(cnty,'') FROM qso
WHERE dxcc IN (291,110,6) AND cnty IS NOT NULL AND cnty != ''
ORDER BY qso_date, time_on`)
if err != nil {
return nil, err
}
defer rows.Close()
out := make(map[string]string, 1024)
for rows.Next() {
var call, state, cnty string
if err := rows.Scan(&call, &state, &cnty); err != nil {
return nil, err
}
call = strings.ToUpper(strings.TrimSpace(call))
cnty = strings.TrimSpace(cnty)
if call == "" || cnty == "" {
continue
}
// Store the ADIF "STATE,County" shape whatever the log holds, so the
// caller has both halves without a second column.
if !strings.Contains(cnty, ",") {
if state = strings.TrimSpace(state); state == "" {
continue
}
cnty = state + "," + cnty
}
out[call] = cnty
}
return out, rows.Err()
}
// CountyWorked reports whether one US county has already been worked.
//
// It exists so the entry panel can flag a new county without loading the whole
+440
View File
@@ -0,0 +1,440 @@
// Code generated by cmd/ctzipgen. DO NOT EDIT.
package uls
// ctCounty maps a Connecticut ZIP to its legal county.
//
// GeoNames reports Connecticut's 2022 planning regions instead, which no
// award, callbook or log uses. See cmd/ctzipgen for how this was built and
// why a name-to-name mapping cannot work.
var ctCounty = map[string]string{
"06001": "Hartford",
"06002": "Hartford",
"06006": "Hartford",
"06010": "Hartford",
"06011": "Hartford",
"06013": "Hartford",
"06016": "Hartford",
"06018": "Litchfield",
"06019": "Hartford",
"06020": "Hartford",
"06021": "Litchfield",
"06022": "Hartford",
"06023": "Hartford",
"06024": "Litchfield",
"06025": "Hartford",
"06026": "Hartford",
"06027": "Hartford",
"06028": "Hartford",
"06029": "Tolland",
"06030": "Hartford",
"06031": "Litchfield",
"06032": "Hartford",
"06033": "Hartford",
"06034": "Hartford",
"06035": "Hartford",
"06037": "Hartford",
"06039": "Litchfield",
"06040": "Hartford",
"06041": "Hartford",
"06042": "Hartford",
"06043": "Tolland",
"06045": "Hartford",
"06050": "Hartford",
"06051": "Hartford",
"06052": "Hartford",
"06053": "Hartford",
"06057": "Litchfield",
"06058": "Litchfield",
"06059": "Hartford",
"06060": "Hartford",
"06061": "Litchfield",
"06062": "Hartford",
"06063": "Litchfield",
"06064": "Hartford",
"06065": "Hartford",
"06066": "Tolland",
"06067": "Hartford",
"06068": "Litchfield",
"06069": "Litchfield",
"06070": "Hartford",
"06071": "Tolland",
"06072": "Tolland",
"06073": "Hartford",
"06074": "Hartford",
"06075": "Tolland",
"06076": "Tolland",
"06077": "Tolland",
"06078": "Hartford",
"06079": "Litchfield",
"06080": "Hartford",
"06081": "Hartford",
"06082": "Hartford",
"06083": "Hartford",
"06084": "Tolland",
"06085": "Hartford",
"06087": "Hartford",
"06088": "Hartford",
"06089": "Hartford",
"06090": "Hartford",
"06091": "Hartford",
"06092": "Hartford",
"06093": "Hartford",
"06094": "Litchfield",
"06095": "Hartford",
"06096": "Hartford",
"06098": "Litchfield",
"06101": "Hartford",
"06102": "Hartford",
"06103": "Hartford",
"06104": "Hartford",
"06105": "Hartford",
"06106": "Hartford",
"06107": "Hartford",
"06108": "Hartford",
"06109": "Hartford",
"06110": "Hartford",
"06111": "Hartford",
"06112": "Hartford",
"06114": "Hartford",
"06115": "Hartford",
"06117": "Hartford",
"06118": "Hartford",
"06119": "Hartford",
"06120": "Hartford",
"06123": "Hartford",
"06126": "Hartford",
"06127": "Hartford",
"06128": "Hartford",
"06129": "Hartford",
"06131": "Hartford",
"06132": "Hartford",
"06133": "Hartford",
"06134": "Hartford",
"06137": "Hartford",
"06138": "Hartford",
"06140": "Hartford",
"06141": "Hartford",
"06142": "Hartford",
"06143": "Hartford",
"06144": "Hartford",
"06145": "Hartford",
"06146": "Hartford",
"06147": "Hartford",
"06150": "Hartford",
"06151": "Hartford",
"06152": "Hartford",
"06153": "Hartford",
"06154": "Hartford",
"06155": "Hartford",
"06156": "Hartford",
"06160": "Hartford",
"06161": "Hartford",
"06167": "Hartford",
"06176": "Hartford",
"06180": "Hartford",
"06183": "Hartford",
"06199": "Hartford",
"06226": "Windham",
"06230": "Windham",
"06231": "Tolland",
"06232": "Tolland",
"06233": "Windham",
"06234": "Windham",
"06235": "Windham",
"06237": "Tolland",
"06238": "Tolland",
"06239": "Windham",
"06241": "Windham",
"06242": "Windham",
"06243": "Windham",
"06244": "Windham",
"06245": "Windham",
"06246": "Windham",
"06247": "Windham",
"06248": "Tolland",
"06249": "New London",
"06250": "Tolland",
"06251": "Tolland",
"06254": "New London",
"06255": "Windham",
"06256": "Windham",
"06258": "Windham",
"06259": "Windham",
"06260": "Windham",
"06262": "Windham",
"06263": "Windham",
"06264": "Windham",
"06265": "Tolland",
"06266": "Windham",
"06267": "Windham",
"06268": "Tolland",
"06269": "Tolland",
"06277": "Windham",
"06278": "Windham",
"06279": "Tolland",
"06280": "Windham",
"06281": "Windham",
"06282": "Windham",
"06320": "New London",
"06330": "New London",
"06331": "Windham",
"06332": "Windham",
"06333": "New London",
"06334": "New London",
"06335": "New London",
"06336": "New London",
"06338": "New London",
"06339": "New London",
"06340": "New London",
"06349": "New London",
"06350": "New London",
"06351": "New London",
"06353": "New London",
"06354": "Windham",
"06355": "New London",
"06357": "New London",
"06359": "New London",
"06360": "New London",
"06365": "New London",
"06370": "New London",
"06371": "New London",
"06372": "New London",
"06373": "Windham",
"06374": "Windham",
"06375": "New London",
"06376": "New London",
"06377": "Windham",
"06378": "New London",
"06379": "New London",
"06380": "New London",
"06382": "New London",
"06383": "New London",
"06384": "New London",
"06385": "New London",
"06387": "Windham",
"06388": "New London",
"06389": "New London",
"06401": "New Haven",
"06403": "New Haven",
"06404": "Fairfield",
"06405": "New Haven",
"06408": "New Haven",
"06409": "Middlesex",
"06410": "New Haven",
"06411": "New Haven",
"06412": "Middlesex",
"06413": "Middlesex",
"06414": "Middlesex",
"06415": "New London",
"06416": "Middlesex",
"06417": "Middlesex",
"06418": "New Haven",
"06419": "Middlesex",
"06420": "New London",
"06422": "Middlesex",
"06423": "Middlesex",
"06424": "Middlesex",
"06426": "Middlesex",
"06437": "New Haven",
"06438": "Middlesex",
"06439": "New London",
"06440": "Fairfield",
"06441": "Middlesex",
"06442": "Middlesex",
"06443": "New Haven",
"06444": "Hartford",
"06447": "Hartford",
"06450": "New Haven",
"06451": "New Haven",
"06455": "Middlesex",
"06456": "Middlesex",
"06457": "Middlesex",
"06459": "Middlesex",
"06460": "New Haven",
"06461": "New Haven",
"06467": "Hartford",
"06468": "Fairfield",
"06469": "Middlesex",
"06470": "Fairfield",
"06471": "New Haven",
"06472": "New Haven",
"06473": "New Haven",
"06474": "New London",
"06475": "Middlesex",
"06477": "New Haven",
"06478": "New Haven",
"06479": "Hartford",
"06480": "Middlesex",
"06481": "Middlesex",
"06482": "Fairfield",
"06483": "New Haven",
"06484": "Fairfield",
"06487": "New Haven",
"06488": "New Haven",
"06489": "Hartford",
"06491": "Fairfield",
"06492": "New Haven",
"06493": "New Haven",
"06494": "New Haven",
"06495": "New Haven",
"06498": "Middlesex",
"06501": "New Haven",
"06502": "New Haven",
"06503": "New Haven",
"06504": "New Haven",
"06505": "New Haven",
"06506": "New Haven",
"06507": "New Haven",
"06508": "New Haven",
"06509": "New Haven",
"06510": "New Haven",
"06511": "New Haven",
"06512": "New Haven",
"06513": "New Haven",
"06514": "New Haven",
"06515": "New Haven",
"06516": "New Haven",
"06517": "New Haven",
"06518": "New Haven",
"06519": "New Haven",
"06520": "New Haven",
"06521": "New Haven",
"06524": "New Haven",
"06525": "New Haven",
"06530": "New Haven",
"06531": "New Haven",
"06532": "New Haven",
"06533": "New Haven",
"06534": "New Haven",
"06535": "New Haven",
"06536": "New Haven",
"06537": "New Haven",
"06538": "New Haven",
"06540": "New Haven",
"06601": "Fairfield",
"06602": "Fairfield",
"06604": "Fairfield",
"06605": "Fairfield",
"06606": "Fairfield",
"06607": "Fairfield",
"06608": "Fairfield",
"06610": "Fairfield",
"06611": "Fairfield",
"06612": "Fairfield",
"06614": "Fairfield",
"06615": "Fairfield",
"06673": "Fairfield",
"06699": "Fairfield",
"06701": "New Haven",
"06702": "New Haven",
"06703": "New Haven",
"06704": "New Haven",
"06705": "New Haven",
"06706": "New Haven",
"06708": "New Haven",
"06710": "New Haven",
"06712": "New Haven",
"06716": "New Haven",
"06720": "New Haven",
"06721": "New Haven",
"06722": "New Haven",
"06723": "New Haven",
"06724": "New Haven",
"06725": "New Haven",
"06726": "New Haven",
"06749": "New Haven",
"06750": "Litchfield",
"06751": "Litchfield",
"06752": "Litchfield",
"06753": "Litchfield",
"06754": "Litchfield",
"06755": "Litchfield",
"06756": "Litchfield",
"06757": "Litchfield",
"06758": "Litchfield",
"06759": "Litchfield",
"06762": "New Haven",
"06763": "Litchfield",
"06770": "New Haven",
"06776": "Litchfield",
"06777": "Litchfield",
"06778": "Litchfield",
"06779": "Litchfield",
"06781": "Litchfield",
"06782": "Litchfield",
"06783": "Litchfield",
"06784": "Fairfield",
"06785": "Litchfield",
"06786": "Litchfield",
"06787": "Litchfield",
"06790": "Litchfield",
"06791": "Litchfield",
"06792": "Litchfield",
"06793": "Litchfield",
"06794": "Litchfield",
"06795": "Litchfield",
"06796": "Litchfield",
"06798": "Litchfield",
"06801": "Fairfield",
"06804": "Fairfield",
"06807": "Fairfield",
"06810": "Fairfield",
"06811": "Fairfield",
"06812": "Fairfield",
"06813": "Fairfield",
"06814": "Fairfield",
"06816": "Fairfield",
"06817": "Fairfield",
"06820": "Fairfield",
"06824": "Fairfield",
"06825": "Fairfield",
"06828": "Fairfield",
"06829": "Fairfield",
"06830": "Fairfield",
"06831": "Fairfield",
"06836": "Fairfield",
"06838": "Fairfield",
"06840": "Fairfield",
"06850": "Fairfield",
"06851": "Fairfield",
"06852": "Fairfield",
"06853": "Fairfield",
"06854": "Fairfield",
"06855": "Fairfield",
"06856": "Fairfield",
"06857": "Fairfield",
"06858": "Fairfield",
"06860": "Fairfield",
"06870": "Fairfield",
"06875": "Fairfield",
"06876": "Fairfield",
"06877": "Fairfield",
"06878": "Fairfield",
"06879": "Fairfield",
"06880": "Fairfield",
"06881": "Fairfield",
"06883": "Fairfield",
"06888": "Fairfield",
"06889": "Fairfield",
"06890": "Fairfield",
"06896": "Fairfield",
"06897": "Fairfield",
"06901": "Fairfield",
"06902": "Fairfield",
"06903": "Fairfield",
"06904": "Fairfield",
"06905": "Fairfield",
"06906": "Fairfield",
"06907": "Fairfield",
"06910": "Fairfield",
"06911": "Fairfield",
"06912": "Fairfield",
"06913": "Fairfield",
"06914": "Fairfield",
"06920": "Fairfield",
"06921": "Fairfield",
"06922": "Fairfield",
"06926": "Fairfield",
"06927": "Fairfield",
}
+70
View File
@@ -0,0 +1,70 @@
package uls
import (
"testing"
"hamlog/internal/award"
)
// The eight counties Connecticut still has for every purpose except the Census.
var ctLegalCounties = map[string]bool{
"Fairfield": true, "Hartford": true, "Litchfield": true, "Middlesex": true,
"New Haven": true, "New London": true, "Tolland": true, "Windham": true,
}
// TestCTCountyOnlyLegalCounties makes sure no planning region ever creeps back
// into the table — a regenerated file that quietly picked up "Capitol Region"
// would put every Hartford-area station back out of reach of the award, which
// is exactly the failure this table exists to end.
func TestCTCountyOnlyLegalCounties(t *testing.T) {
if len(ctCounty) < 400 {
t.Fatalf("only %d Connecticut ZIPs — the table looks truncated", len(ctCounty))
}
for zip, county := range ctCounty {
if !ctLegalCounties[county] {
t.Errorf("ZIP %s → %q, which is not one of Connecticut's eight counties", zip, county)
}
if len(zip) != 5 {
t.Errorf("ZIP %q is not five digits", zip)
}
}
}
// TestCTCountyMatchesAward closes the loop: every county in the table has to
// produce a key the USA-CA reference actually lists. A county the resolver can
// name but the award cannot match is worth no more than the planning region it
// replaced.
func TestCTCountyMatchesAward(t *testing.T) {
want := map[string]bool{
"CT/FAIRFIELD": true, "CT/HARTFORD": true, "CT/LITCHFIELD": true,
"CT/MIDDLESEX": true, "CT/NEWHAVEN": true, "CT/NEWLONDON": true,
"CT/TOLLAND": true, "CT/WINDHAM": true,
}
for zip, county := range ctCounty {
if k := award.USCountyKey("CT", county); !want[k] {
t.Errorf("ZIP %s → %q → key %q, not a USA-CA county", zip, county, k)
}
}
}
// TestCTCountyKnownZips pins a ZIP in each county against ground truth, so a
// regeneration that silently shifted the whole table is caught. 06419 is the
// case that started this: Killingworth is in Middlesex, and GeoNames calls it
// "Lower Connecticut River Valley".
func TestCTCountyKnownZips(t *testing.T) {
for zip, want := range map[string]string{
"06419": "Middlesex", // Killingworth
"06001": "Hartford", // Avon
"06106": "Hartford", // Hartford
"06510": "New Haven", // New Haven
"06880": "Fairfield", // Westport
"06340": "New London", // Groton
"06226": "Windham", // Willimantic
"06238": "Tolland", // Coventry
"06759": "Litchfield", // Litchfield
} {
if got := ctCounty[zip]; got != want {
t.Errorf("ctCounty[%s] = %q, want %q", zip, got, want)
}
}
}
+46 -2
View File
@@ -34,6 +34,7 @@ import (
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
@@ -114,6 +115,33 @@ func (s *Store) Count() int {
return n
}
// RulesVersion is bumped whenever a correction applied DURING import changes
// the county a callsign resolves to. The value is stamped into uls_meta, so a
// database built by an older OpsLog can be told apart from one built by this
// one — the rows themselves carry no clue, and the fix cannot be applied
// retroactively because the ZIP each county was derived from is not stored.
//
// 1 — original.
// 2 — Connecticut ZIPs mapped back to their legal county instead of the
// 2022 planning region GeoNames now reports (see ctcounty_gen.go).
const RulesVersion = 2
// NeedsRefresh reports that the store was built before a correction that
// changes its answers, so the operator should download it again. False for an
// empty store: there is nothing stale about data that was never fetched.
func (s *Store) NeedsRefresh() bool {
s.mu.RLock()
defer s.mu.RUnlock()
var n int
if s.db.QueryRow(`SELECT COUNT(*) FROM uls_callsign`).Scan(&n); n == 0 {
return false
}
var v string
s.db.QueryRow(`SELECT value FROM uls_meta WHERE key='rules_version'`).Scan(&v)
got, _ := strconv.Atoi(v)
return got < RulesVersion
}
// UpdatedAt returns when the store was last imported (zero if never).
func (s *Store) UpdatedAt() time.Time {
s.mu.RLock()
@@ -285,6 +313,10 @@ func (s *Store) rebuild(ctx context.Context, amatZip string, zipmap map[string]z
time.Now().UTC().Format(time.RFC3339)); err != nil {
return err
}
if _, err := tx.ExecContext(ctx, `INSERT OR REPLACE INTO uls_meta(key,value) VALUES('rules_version',?)`,
strconv.Itoa(RulesVersion)); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return err
}
@@ -333,9 +365,21 @@ func parseGeoNames(zipPath string) (map[string]zipRow, error) {
}
lat := parseFloat(f[9])
lon := parseFloat(f[10])
state := strings.ToUpper(strings.TrimSpace(f[4]))
county := strings.TrimSpace(f[5])
// Connecticut: GeoNames reports the 2022 planning regions, which are
// county-equivalents for the Census and nothing at all for amateur
// radio — no award, no callbook and no log uses them, so every CT
// station resolved to a name that matched nothing. Substitute the legal
// county for the ZIP. See cmd/ctzipgen.
if state == "CT" {
if c, ok := ctCounty[zip5]; ok {
county = c
}
}
out[zip5] = zipRow{
state: strings.ToUpper(strings.TrimSpace(f[4])),
county: strings.TrimSpace(f[5]),
state: state,
county: county,
lat: lat,
lon: lon,
}