From 3ec7cda79011eb8e3f95817467b2cb73884f40a4 Mon Sep 17 00:00:00 2001 From: Ovidiu U Date: Wed, 22 Apr 2026 12:36:39 +0100 Subject: [PATCH] feat: derive outcode centroids from postcodes during import --- app/Console/Commands/ImportPostcodes.php | 6 ++++++ tests/Feature/Console/ImportPostcodesTest.php | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/app/Console/Commands/ImportPostcodes.php b/app/Console/Commands/ImportPostcodes.php index c4fdd90..cfa6c7d 100644 --- a/app/Console/Commands/ImportPostcodes.php +++ b/app/Console/Commands/ImportPostcodes.php @@ -109,7 +109,13 @@ final class ImportPostcodes extends Command fclose($handle); + DB::statement( + 'INSERT INTO outcodes (outcode, lat, lng) + SELECT outcode, AVG(lat), AVG(lng) FROM postcodes GROUP BY outcode' + ); + $this->info("Imported {$imported} postcodes."); + $this->info('Derived '.DB::table('outcodes')->count().' outcode centroids.'); return self::SUCCESS; } diff --git a/tests/Feature/Console/ImportPostcodesTest.php b/tests/Feature/Console/ImportPostcodesTest.php index b8288ca..3d60e68 100644 --- a/tests/Feature/Console/ImportPostcodesTest.php +++ b/tests/Feature/Console/ImportPostcodesTest.php @@ -1,5 +1,6 @@ toBe(1) ->and(Postcode::find('BT11AA'))->toBeNull(); }); + +it('derives outcode centroids as the average of member postcodes', function (): void { + $csv = <<<'CSV' +pcd,pcds,doterm,lat,long +"PE71AA","PE7 1AA","",52.500000,-0.200000 +"PE71AB","PE7 1AB","",52.600000,-0.220000 +"M11AD","M1 1AD","",53.480957,-2.237428 +CSV; + + $path = writeOnspdFixture($csv); + + $this->artisan('postcodes:import', ['--file' => $path])->assertSuccessful(); + + $pe7 = Outcode::find('PE7'); + + expect($pe7)->not->toBeNull() + ->and(round((float) $pe7->lat, 6))->toBe(52.550000) + ->and(round((float) $pe7->lng, 6))->toBe(-0.210000) + ->and(Outcode::find('M1'))->not->toBeNull(); +});