From fdcf253ca7434549816a908f5cd31eadae159b7e Mon Sep 17 00:00:00 2001 From: Ovidiu U Date: Fri, 12 Jun 2026 09:26:56 +0100 Subject: [PATCH] Skip placeholder-coordinate postcodes (lat >= 90) in ONSPD import ONS marks non-geographic postcodes (no grid reference) with a placeholder latitude of 99.999999. The "Latest Centroids" export shipped ~12,789 such rows, which were imported as real postcodes pointing at lat 99.99 and would poison nearest-station distance maths. Drop them at ingest, with a test. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/Console/Commands/ImportPostcodes.php | 7 +++++++ tests/Feature/Console/ImportPostcodesTest.php | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/Console/Commands/ImportPostcodes.php b/app/Console/Commands/ImportPostcodes.php index 722564e..ae9bb00 100644 --- a/app/Console/Commands/ImportPostcodes.php +++ b/app/Console/Commands/ImportPostcodes.php @@ -109,6 +109,13 @@ final class ImportPostcodes extends Command continue; } + // ONS marks non-geographic postcodes (no grid reference) with a + // placeholder latitude of 99.999999 — drop them so they don't + // poison nearest-station distance maths with a bogus location. + if (abs((float) $lat) >= 90) { + continue; + } + $pcd = strtoupper(preg_replace('/\s+/', '', (string) $row[$columns[$pcdColumn]])); if ($pcd === '' || strlen($pcd) < 5) { diff --git a/tests/Feature/Console/ImportPostcodesTest.php b/tests/Feature/Console/ImportPostcodesTest.php index 22e44ed..5b79341 100644 --- a/tests/Feature/Console/ImportPostcodesTest.php +++ b/tests/Feature/Console/ImportPostcodesTest.php @@ -63,6 +63,21 @@ CSV; ->and(Postcode::find('BT11AA'))->toBeNull(); }); +it('skips postcodes with placeholder coordinates (no grid reference)', function (): void { + $csv = <<<'CSV' +pcd,pcds,doterm,lat,long +"SW1A1AA","SW1A 1AA","",51.501009,-0.141588 +"GIR0AA","GIR 0AA","",99.999999,0.000000 +CSV; + + $path = writeOnspdFixture($csv); + + $this->artisan('postcodes:import', ['--file' => $path])->assertSuccessful(); + + expect(Postcode::count())->toBe(1) + ->and(Postcode::find('GIR0AA'))->toBeNull(); +}); + it('accepts ArcGIS ONSPD exports that use PCD7 instead of PCD', function (): void { $csv = <<<'CSV' OBJECTID,PCD7,PCD8,PCDS,DOTERM,LAT,LONG,x,y