mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
DX import Luxembourg address command
This commit is contained in:
parent
47f4cfddbb
commit
ba25c181f5
6
.changes/unreleased/DX-20240627-100653.yaml
Normal file
6
.changes/unreleased/DX-20240627-100653.yaml
Normal file
@ -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: ""
|
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Command;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Service\Import\AddressReferenceLU;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
|
class LoadAddressesLUFromBDAddressCommand extends Command
|
||||||
|
{
|
||||||
|
protected static $defaultDescription = 'Import LUX addresses from BD addresses (see https://data.public.lu/fr/datasets/adresses-georeferencees-bd-adresses/)';
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly AddressReferenceLU $addressImporter,
|
||||||
|
) {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this->setName('chill:main:address-ref-lux');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
$this->addressImporter->import();
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
102
src/Bundle/ChillMainBundle/Service/Import/AddressReferenceLU.php
Normal file
102
src/Bundle/ChillMainBundle/Service/Import/AddressReferenceLU.php
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Service\Import;
|
||||||
|
|
||||||
|
use League\Csv\Reader;
|
||||||
|
use League\Csv\Statement;
|
||||||
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||||
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
|
|
||||||
|
class AddressReferenceLU
|
||||||
|
{
|
||||||
|
private const RELEASE = 'https://data.public.lu/fr/datasets/r/5cadc5b8-6a7d-4283-87bc-f9e58dd771f7';
|
||||||
|
|
||||||
|
public function __construct(private readonly HttpClientInterface $client, private readonly AddressReferenceBaseImporter $addressBaseImporter, private readonly PostalCodeBaseImporter $postalCodeBaseImporter, private readonly AddressToReferenceMatcher $addressToReferenceMatcher) {}
|
||||||
|
|
||||||
|
public function import(): void
|
||||||
|
{
|
||||||
|
$downloadUrl = self::RELEASE;
|
||||||
|
|
||||||
|
$response = $this->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']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user