Feature: command to load addresses from France from bano.openstreetmap.fr

This commit is contained in:
2022-07-30 23:10:19 +02:00
parent 9b3b9f2552
commit 84cda8845d
6 changed files with 376 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Service\Import;
use Exception;
use League\Csv\Reader;
use League\Csv\Statement;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use UnexpectedValueException;
use function is_int;
class AddressReferenceFromBano
{
private AddressReferenceBaseImporter $baseImporter;
private HttpClientInterface $client;
public function __construct(HttpClientInterface $client, AddressReferenceBaseImporter $baseImporter)
{
$this->client = $client;
$this->baseImporter = $baseImporter;
}
public function import(string $departementNo): void
{
if (!is_numeric($departementNo) || !is_int((int) $departementNo)) {
throw new UnexpectedValueException('Could not parse this department number');
}
$url = "https://bano.openstreetmap.fr/data/bano-{$departementNo}.csv";
$response = $this->client->request('GET', $url);
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(',');
$stmt = Statement::create()
->process($csv, [
'refId',
'streetNumber',
'street',
'postcode',
'city',
'_o',
'lat',
'lon',
]);
foreach ($stmt as $record) {
$this->baseImporter->importAddress(
$record['refId'],
substr($record['refId'], 0, 5), // extract insee from reference
$record['postcode'],
$record['street'],
$record['streetNumber'],
'BANO.' . $departementNo,
(float) $record['lat'],
(float) $record['lon'],
4326
);
}
$this->baseImporter->finalize();
fclose($file);
}
}