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..499751c7c --- /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 0; + } +} diff --git a/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceLU.php b/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceLU.php new file mode 100644 index 000000000..d70b5085d --- /dev/null +++ b/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceLU.php @@ -0,0 +1,97 @@ +client->request('GET', $downloadUrl); + + if (200 !== $response->getStatusCode()) { + throw new \Exception('Could not download CSV: '.$response->getStatusCode()); + } + + $file = tmpfile(); + + foreach ($this->client->stream($response) as $chunk) { + fwrite($file, $chunk->getContent()); + } + + fseek($file, 0); + + $csv = Reader::createFromStream($file); + $csv->setDelimiter(';'); + $csv->setHeaderOffset(0); + + $this->process_postal_code($csv); + + $this->process_address($csv); + + $this->addressToReferenceMatcher->checkAddressesMatchingReferences(); + + fclose($file); + } + + 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.lux', + (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 === \array_key_exists($record['code_postal'], $arr_postal_codes)) { + $this->postalCodeBaseImporter->importCode( + 'LU', + trim((string) $record['localite']), + trim((string) $record['code_postal']), + trim((string) $record['code_postal']), + 'bd-addresses.lux', + (float) $record['lat_wgs84'], + (float) $record['lon_wgs84'], + 4326 + ); + $arr_postal_codes[$record['code_postal']] = 1; + } + } + } +} diff --git a/src/Bundle/ChillMainBundle/config/services/command.yaml b/src/Bundle/ChillMainBundle/config/services/command.yaml index 8a2327c0b..94cb2cf97 100644 --- a/src/Bundle/ChillMainBundle/config/services/command.yaml +++ b/src/Bundle/ChillMainBundle/config/services/command.yaml @@ -59,6 +59,12 @@ services: tags: - { name: console.command } + Chill\MainBundle\Command\LoadAddressesLUFromBDAddressCommand: + autoconfigure: true + autowire: true + tags: + - { name: console.command } + Chill\MainBundle\Command\ExecuteCronJobCommand: autoconfigure: true autowire: true