From 242ae6e3622101caa53050056400d2653eb7b195 Mon Sep 17 00:00:00 2001 From: Andy Lulham Date: Fri, 1 Aug 2025 13:18:30 +0100 Subject: [PATCH 1/3] Import additional hospital data --- README.md | 1 + .../ImportAdditionalHospitalsCommand.php | 42 +++++++++++++++++++ app/Console/Commands/ImportDataCommand.php | 1 + 3 files changed, 44 insertions(+) create mode 100644 app/Console/Commands/ImportAdditionalHospitalsCommand.php diff --git a/README.md b/README.md index 1c60e34..93da651 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ php artisan import:data | Dentists (England) | [Unknown (CampaignLab)](https://github.com/CampaignLab/New-Constituency-Almanac/blob/main/data/dentists%20england%20mapped.csv) | [dentists-england.csv](https://github.com/CampaignLab/constituency-explorer/blob/refs/heads/main/database/fixtures/dentists-england.csv) | [`import:dentists`](https://github.com/CampaignLab/constituency-explorer/blob/main/app/Console/Commands/ImportDentistsCommand.php) | | Hospitals (England) | [Unknown (CampaignLab)](https://github.com/CampaignLab/New-Constituency-Almanac/blob/main/data/english%20hospitals%20by%20constituency.csv) | [hospitals-england.csv](https://github.com/CampaignLab/constituency-explorer/blob/refs/heads/main/database/fixtures/hospitals-england.csv) | [`import:english-hospitals`](https://github.com/CampaignLab/constituency-explorer/blob/main/app/Console/Commands/ImportEnglishHospitalsCommand.php) | | Hospitals (Scotland) | [Unknown (CampaignLab)](https://github.com/CampaignLab/New-Constituency-Almanac/blob/main/data/hospitals%20in%20scotland%20by%20constituency.csv) | [hospitals-scotland.csv](https://github.com/CampaignLab/constituency-explorer/blob/refs/heads/main/database/fixtures/hospitals-scotland.csv) | [`import:scottish-hospitals`](https://github.com/CampaignLab/constituency-explorer/blob/main/app/Console/Commands/ImportScottishHospitalsCommand.php) | +| Hospitals (additional) | [Unknown (CampaignLab)](https://github.com/CampaignLab/constituency-explorer/issues/58) | [hospitals-scotland.csv](https://github.com/CampaignLab/constituency-explorer/blob/refs/heads/main/database/fixtures/additional_hospitals_20250618.csv) | [`import:additional-hospitals`](https://github.com/CampaignLab/constituency-explorer/blob/main/app/Console/Commands/ImportAdditionalHospitalsCommand.php) | | Schools (England) | [Gov.uk](https://get-information-schools.service.gov.uk/Downloads) | [schools-england.csv](https://github.com/CampaignLab/constituency-explorer/blob/refs/heads/main/database/fixtures/schools-england.csv) | [`import:english-schools`](https://github.com/CampaignLab/constituency-explorer/blob/main/app/Console/Commands/ImportEnglishSchoolsCommand.php) | | Schools (Scotland) | [Gov.uk](https://www.data.gov.uk/dataset/9a6f9d86-9698-4a5d-a2c8-89f3b212c52c/scottish-school-roll-and-locations) | [schools-scotland.xlsx](https://github.com/CampaignLab/constituency-explorer/blob/refs/heads/main/database/fixtures/schools-scotland.xlsx) | [`import:scottish-schools`](https://github.com/CampaignLab/constituency-explorer/blob/main/app/Console/Commands/ImportScottishSchoolsCommand.php) | | Community centres | - | [community-centres.csv](https://github.com/CampaignLab/constituency-explorer/blob/refs/heads/main/database/fixtures/community-centres.csv) | [`import:community-centres`](https://github.com/CampaignLab/constituency-explorer/blob/main/app/Console/Commands/ImportCommunityCentres.php) | diff --git a/app/Console/Commands/ImportAdditionalHospitalsCommand.php b/app/Console/Commands/ImportAdditionalHospitalsCommand.php new file mode 100644 index 0000000..efd7994 --- /dev/null +++ b/app/Console/Commands/ImportAdditionalHospitalsCommand.php @@ -0,0 +1,42 @@ +constituencies = Constituency::all()->keyBy('gss_code'); + } + + public function importRow($row) + { + $gss_code = $row['Mapped: codes.parliamentary_constituency']; + if (empty($gss_code)) { + return null; + } + + $constituency = $this->constituencies[$gss_code] ?? null; + + if (! $constituency) { + $this->warn("Constituency not found: {$gss_code}"); + return null; + } + + return Hospital::create([ + 'constituency_id' => $constituency->id, + 'name' => $row['name'], + 'address' => [trim($row['postcode'])], + 'latitude' => $row['Mapped: latitude'], + 'longitude' => $row['Mapped: longitude'], + ]); + } +} diff --git a/app/Console/Commands/ImportDataCommand.php b/app/Console/Commands/ImportDataCommand.php index 331d6c8..eb347d7 100644 --- a/app/Console/Commands/ImportDataCommand.php +++ b/app/Console/Commands/ImportDataCommand.php @@ -60,6 +60,7 @@ public function handle() $this->call(ImportDentistsCommand::class); $this->call(ImportEnglishHospitalsCommand::class); $this->call(ImportScottishHospitalsCommand::class); + $this->call(ImportAdditionalHospitalsCommand::class); $this->call(ImportEnglishSchoolsCommand::class); $this->call(ImportScottishSchoolsCommand::class); $this->call(ImportCommunityCentres::class); From d461f727a2d6b1ddfff72a5a4194cb91f7d1867b Mon Sep 17 00:00:00 2001 From: Andy Lulham Date: Fri, 1 Aug 2025 13:18:48 +0100 Subject: [PATCH 2/3] Add a warning if hospital looks like a duplicate --- app/Console/Commands/ImportAdditionalHospitalsCommand.php | 5 +++++ app/Console/Commands/ImportEnglishHospitalsCommand.php | 5 +++++ app/Console/Commands/ImportScottishHospitalsCommand.php | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/app/Console/Commands/ImportAdditionalHospitalsCommand.php b/app/Console/Commands/ImportAdditionalHospitalsCommand.php index efd7994..e174158 100644 --- a/app/Console/Commands/ImportAdditionalHospitalsCommand.php +++ b/app/Console/Commands/ImportAdditionalHospitalsCommand.php @@ -31,6 +31,11 @@ public function importRow($row) return null; } + $existing = Hospital::where('name', $row['name'])->where('constituency_id', $constituency->id)->first(); + if ($existing) { + $this->warn("Hospital already exists: {$row['name']} ({$constituency->name})"); + } + return Hospital::create([ 'constituency_id' => $constituency->id, 'name' => $row['name'], diff --git a/app/Console/Commands/ImportEnglishHospitalsCommand.php b/app/Console/Commands/ImportEnglishHospitalsCommand.php index b921273..7658ab7 100644 --- a/app/Console/Commands/ImportEnglishHospitalsCommand.php +++ b/app/Console/Commands/ImportEnglishHospitalsCommand.php @@ -31,6 +31,11 @@ public function importRow($row) return null; } + $existing = Hospital::where('name', $row['Name'])->where('constituency_id', $constituency->id)->first(); + if ($existing) { + $this->warn("Hospital already exists: {$row['Name']} ({$constituency->name})"); + } + return Hospital::create([ 'constituency_id' => $constituency->id, 'name' => $row['Name'], diff --git a/app/Console/Commands/ImportScottishHospitalsCommand.php b/app/Console/Commands/ImportScottishHospitalsCommand.php index cd71182..faa570f 100644 --- a/app/Console/Commands/ImportScottishHospitalsCommand.php +++ b/app/Console/Commands/ImportScottishHospitalsCommand.php @@ -32,6 +32,11 @@ public function importRow($row) return null; } + $existing = Hospital::where('name', $row['Location Name'])->where('constituency_id', $constituency->id)->first(); + if ($existing) { + $this->warn("Hospital already exists: {$row['Location Name']} ({$constituency->name})"); + } + return Hospital::create([ 'constituency_id' => $constituency->id, 'name' => $row['Location Name'], From 9b48bf23aaccc9bc31cbf17f86439edd77e0f534 Mon Sep 17 00:00:00 2001 From: Andy Lulham Date: Fri, 1 Aug 2025 13:19:17 +0100 Subject: [PATCH 3/3] Reject row if hospital looks like a duplicate --- app/Console/Commands/ImportAdditionalHospitalsCommand.php | 1 + app/Console/Commands/ImportEnglishHospitalsCommand.php | 1 + app/Console/Commands/ImportScottishHospitalsCommand.php | 1 + 3 files changed, 3 insertions(+) diff --git a/app/Console/Commands/ImportAdditionalHospitalsCommand.php b/app/Console/Commands/ImportAdditionalHospitalsCommand.php index e174158..1afe9f0 100644 --- a/app/Console/Commands/ImportAdditionalHospitalsCommand.php +++ b/app/Console/Commands/ImportAdditionalHospitalsCommand.php @@ -34,6 +34,7 @@ public function importRow($row) $existing = Hospital::where('name', $row['name'])->where('constituency_id', $constituency->id)->first(); if ($existing) { $this->warn("Hospital already exists: {$row['name']} ({$constituency->name})"); + return null; } return Hospital::create([ diff --git a/app/Console/Commands/ImportEnglishHospitalsCommand.php b/app/Console/Commands/ImportEnglishHospitalsCommand.php index 7658ab7..d1c69be 100644 --- a/app/Console/Commands/ImportEnglishHospitalsCommand.php +++ b/app/Console/Commands/ImportEnglishHospitalsCommand.php @@ -34,6 +34,7 @@ public function importRow($row) $existing = Hospital::where('name', $row['Name'])->where('constituency_id', $constituency->id)->first(); if ($existing) { $this->warn("Hospital already exists: {$row['Name']} ({$constituency->name})"); + return null; } return Hospital::create([ diff --git a/app/Console/Commands/ImportScottishHospitalsCommand.php b/app/Console/Commands/ImportScottishHospitalsCommand.php index faa570f..d65bf8b 100644 --- a/app/Console/Commands/ImportScottishHospitalsCommand.php +++ b/app/Console/Commands/ImportScottishHospitalsCommand.php @@ -35,6 +35,7 @@ public function importRow($row) $existing = Hospital::where('name', $row['Location Name'])->where('constituency_id', $constituency->id)->first(); if ($existing) { $this->warn("Hospital already exists: {$row['Location Name']} ({$constituency->name})"); + return null; } return Hospital::create([