From ba25c181f5c228419af4811ce9d0bdb7f5c39be7 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 27 Jun 2024 10:07:24 +0200 Subject: [PATCH] DX import Luxembourg address command --- .changes/unreleased/DX-20240627-100653.yaml | 6 ++ .../LoadAddressesLUFromBDAddressCommand.php | 40 +++++++ .../Service/Import/AddressReferenceLU.php | 102 ++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 .changes/unreleased/DX-20240627-100653.yaml create mode 100644 src/Bundle/ChillMainBundle/Command/LoadAddressesLUFromBDAddressCommand.php create mode 100644 src/Bundle/ChillMainBundle/Service/Import/AddressReferenceLU.php diff --git a/.changes/unreleased/DX-20240627-100653.yaml b/.changes/unreleased/DX-20240627-100653.yaml new file mode 100644 index 000000000..538cf2697 --- /dev/null +++ b/.changes/unreleased/DX-20240627-100653.yaml @@ -0,0 +1,6 @@ +kind: DX +body: Add a command for reading official address DB from Luxembourg and update chill + addresses +time: 2024-06-27T10:06:53.098022939+02:00 +custom: + Issue: "" diff --git a/src/Bundle/ChillMainBundle/Command/LoadAddressesLUFromBDAddressCommand.php b/src/Bundle/ChillMainBundle/Command/LoadAddressesLUFromBDAddressCommand.php new file mode 100644 index 000000000..2477a486d --- /dev/null +++ b/src/Bundle/ChillMainBundle/Command/LoadAddressesLUFromBDAddressCommand.php @@ -0,0 +1,40 @@ +setName('chill:main:address-ref-lux'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->addressImporter->import(); + + return Command::SUCCESS; + } +} diff --git a/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceLU.php b/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceLU.php new file mode 100644 index 000000000..c29921847 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceLU.php @@ -0,0 +1,102 @@ +client->request('GET', $downloadUrl); + + if (200 !== $response->getStatusCode()) { + throw new \Exception('Could not download CSV: '.$response->getStatusCode()); + } + + $tmpname = tempnam(sys_get_temp_dir(), 'php-add-'); + $file = fopen($tmpname, 'r+b'); + + foreach ($this->client->stream($response) as $chunk) { + fwrite($file, $chunk->getContent()); + } + + fclose($file); + + $uncompressedStream = gzopen($tmpname, 'r'); + + $csv = Reader::createFromStream($uncompressedStream); + $csv->setDelimiter(';'); + $csv->setHeaderOffset(0); + + $this->process_postal_code($csv); + + $this->process_address($csv); + + $this->addressToReferenceMatcher->checkAddressesMatchingReferences(); + + gzclose($uncompressedStream); + } + + private function process_address(Reader $csv): void + { + $stmt = Statement::create()->process($csv); + foreach ($stmt as $record) { + $this->addressBaseImporter->importAddress( + $record['id_geoportail'], + $record['code_postal'], + $record['code_postal'], + $record['rue'], + $record['numero'], + 'bd addresses', + (float) $record['lat_wgs84'], + (float) $record['lon_wgs84'], + 4326 + ); + } + + $this->addressBaseImporter->finalize(); + } + + private function process_postal_code(Reader $csv): void + { + $stmt = Statement::create()->process($csv); + $arr_postal_codes = []; + foreach ($stmt as $record) { + if (false === \in_array($record['code_postal'], $arr_postal_codes, true)) { + $this->postalCodeBaseImporter->importCode( + 'LU', + trim((string) $record['localite']), + trim((string) $record['code_postal']), + trim((string) $record['code_postal']), + 'bd addresses', + (float) $record['lat_wgs84'], + (float) $record['lon_wgs84'], + 4326 + ); + \array_push($arr_postal_codes, $record['code_postal']); + } + } + } +} + + +