From b4a1e824acbb9acc155bb308208c2a22c3a623d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 10 Jan 2025 22:52:08 +0100 Subject: [PATCH 1/3] Add service and command to import French addresses from BAN Introduce a service to handle the import of French addresses from the Base Adresse Nationale (BAN) dataset. Add a new console command `chill:main:address-ref-from-ban` to trigger the import by department numbers, with an option to send a report email for unmatched addresses. --- .../Command/LoadAddressesFRFromBANCommand.php | 48 ++++++++ .../Import/AddressReferenceFromBAN.php | 106 ++++++++++++++++++ .../config/services/command.yaml | 6 + 3 files changed, 160 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Command/LoadAddressesFRFromBANCommand.php create mode 100644 src/Bundle/ChillMainBundle/Service/Import/AddressReferenceFromBAN.php diff --git a/src/Bundle/ChillMainBundle/Command/LoadAddressesFRFromBANCommand.php b/src/Bundle/ChillMainBundle/Command/LoadAddressesFRFromBANCommand.php new file mode 100644 index 000000000..8483c0be1 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Command/LoadAddressesFRFromBANCommand.php @@ -0,0 +1,48 @@ +setName('chill:main:address-ref-from-ban') + ->addArgument('departementNo', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'a list of departement numbers') + ->addOption('send-report-email', 's', InputOption::VALUE_REQUIRED, 'Email address where a list of unimported addresses can be send'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + dump(__METHOD__); + foreach ($input->getArgument('departementNo') as $departementNo) { + $output->writeln('Import addresses for '.$departementNo); + + $this->addressReferenceFromBAN->import($departementNo, $input->hasOption('send-report-email') ? $input->getOption('send-report-email') : null); + } + + return Command::SUCCESS; + } +} diff --git a/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceFromBAN.php b/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceFromBAN.php new file mode 100644 index 000000000..b45760592 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceFromBAN.php @@ -0,0 +1,106 @@ +client->request('GET', $url); + + if (200 !== $response->getStatusCode()) { + throw new \Exception('Could not download CSV: '.$response->getStatusCode()); + } + + $path = sys_get_temp_dir().'/'.$departementNo.'.csv.gz'; + $file = fopen($path, 'w'); + + if (false === $file) { + throw new \Exception('Could not create temporary file'); + } + + foreach ($this->client->stream($response) as $chunk) { + fwrite($file, $chunk->getContent()); + } + + fclose($file); + + // re-open it to read it + $csvDecompressed = gzopen($path, 'r'); + + $csv = Reader::createFromStream($csvDecompressed); + $csv->setDelimiter(';')->setHeaderOffset(0); + $stmt = Statement::create() + ->process($csv, [ + 'id', + 'id_fantoir', + 'numero', + 'rep', + 'nom_voie', + 'code_postal', + 'code_insee', + 'nom_commune', + 'code_insee_ancienne_commune', + 'nom_ancienne_commune', + 'x', + 'y', + 'lon', + 'lat', + 'type_position', + 'alias', + 'nom_ld', + 'libelle_acheminement', + 'nom_afnor', + 'source_position', + 'source_nom_voie', + 'certification_commune', + 'cad_parcelles', + ]); + + foreach ($stmt as $record) { + $this->baseImporter->importAddress( + $record['id'], + $record['code_insee'], + $record['code_postal'], + $record['nom_voie'], + $record['numero'].' '.$record['rep'], + 'BAN.'.$departementNo, + (float) $record['lat'], + (float) $record['lon'], + 4326 + ); + } + + $this->baseImporter->finalize(sendAddressReportToEmail: $sendAddressReportToEmail); + + $this->addressToReferenceMatcher->checkAddressesMatchingReferences(); + + fclose($csvDecompressed); + unlink($path); + } +} diff --git a/src/Bundle/ChillMainBundle/config/services/command.yaml b/src/Bundle/ChillMainBundle/config/services/command.yaml index 94cb2cf97..158bf3fab 100644 --- a/src/Bundle/ChillMainBundle/config/services/command.yaml +++ b/src/Bundle/ChillMainBundle/config/services/command.yaml @@ -47,6 +47,12 @@ services: tags: - { name: console.command } + Chill\MainBundle\Command\LoadAddressesFRFromBANCommand: + autoconfigure: true + autowire: true + tags: + - { name: console.command } + Chill\MainBundle\Command\LoadAddressesBEFromBestAddressCommand: autoconfigure: true autowire: true From 6de4861b98d967124b35e4081c25e5bfc39f6d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 10 Jan 2025 23:00:40 +0100 Subject: [PATCH 2/3] Fix email attachment handling in address import reports Updated attachment logic to use in-memory file contents and apply a `.gz` suffix to filenames. This ensures better file handling and resolves potential issues with attaching files directly from a path. --- .../Service/Import/AddressReferenceBaseImporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceBaseImporter.php b/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceBaseImporter.php index 3f4c0aca1..adb49549b 100644 --- a/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceBaseImporter.php +++ b/src/Bundle/ChillMainBundle/Service/Import/AddressReferenceBaseImporter.php @@ -311,7 +311,7 @@ final class AddressReferenceBaseImporter $email = (new Email()) ->addTo($sendAddressReportToEmail) ->subject('Addresses that could not be imported') - ->attachFromPath($attachmentPath); + ->attach(file_get_contents($attachmentPath), sprintf('%s.gz', $path)); try { $this->mailer->send($email); From feebcf66627537deb842af40dc88f1b4f86bac98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 13 Jan 2025 17:08:45 +0100 Subject: [PATCH 3/3] Add changie [ci-skip] --- .changes/unreleased/Feature-20250113-170824.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/unreleased/Feature-20250113-170824.yaml diff --git a/.changes/unreleased/Feature-20250113-170824.yaml b/.changes/unreleased/Feature-20250113-170824.yaml new file mode 100644 index 000000000..fb1c6fe26 --- /dev/null +++ b/.changes/unreleased/Feature-20250113-170824.yaml @@ -0,0 +1,5 @@ +kind: Feature +body: Add address importer from french Base d'Adresse Nationale (BAN) +time: 2025-01-13T17:08:24.034500095+01:00 +custom: + Issue: ""