addresses: import reference address from WFS using httpClientInterface

This commit is contained in:
nobohan 2021-05-18 13:57:39 +02:00
parent 830cda64e0
commit 73ee77b8b2
3 changed files with 58 additions and 50 deletions

View File

@ -1,13 +1,13 @@
<?php
namespace Chill\MainBundle\Command;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Chill\MainBundle\Doctrine\Model\Point;
use Chill\MainBundle\Entity\AddressReference;
use Doctrine\ORM\EntityManagerInterface;
/**
*
@ -18,18 +18,22 @@ class LoadAddressReferenceCommand extends Command
{
/**
* @var EntityManager
* @var EntityManagerInterface
*/
private $entityManager;
/**
* LoadAddressReferenceCommand constructor.
*
* @param EntityManager $entityManager
* @var HttpClientInterface
*/
public function __construct(EntityManager $entityManager)
private $client;
/**
* LoadAddressReferenceCommand constructor.
*/
public function __construct(EntityManagerInterface $entityManager, HttpClientInterface $client)
{
$this->entityManager=$entityManager;
$this->entityManager = $entityManager;
$this->client = $client;
parent::__construct();
}
@ -52,8 +56,7 @@ class LoadAddressReferenceCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int
{
$addressReferenceArray = $this->fetchWFS(1, 10);
$addressReferenceArray = $this->fetchWFS(1, 2, $output);
$em = $this->entityManager;
foreach($addressReferenceArray as $a) {
@ -64,11 +67,7 @@ class LoadAddressReferenceCommand extends Command
$em->flush();
return 0;
}
public static function isAddressReferenceNew(AddressReference $a): bool
@ -84,55 +83,63 @@ class LoadAddressReferenceCommand extends Command
* //TODO move this function in App and make the command generic by accepting a fetcher as an argument
* @see \Symfony\Component\Console\Command\Command::execute()
*/
private function fetchWFS(int $startIndex, int $count ): array
private function fetchWFS(int $startIndex, int $count, OutputInterface $output): array
{
$dataSource = 'BAL GeoVendee';
$countryCode = 'FR';
$addressReferenceArray = array();
$wfsUrl = 'https://carto.vendee.fr/arcgis/services/Applications/CHILL/MapServer/WFSServer?SERVICE=WFS&REQUEST=GetFeature';
$wfsBaseUrl = 'https://carto.vendee.fr/arcgis/services/Applications/CHILL/MapServer/WFSServer?SERVICE=WFS&REQUEST=GetFeature&version=2.0.0';
$wfsUrl = $wfsBaseUrl . '&TYPENAME=CHILL:Adresses&startIndex=' . $startIndex . '&count=' . $count . '&OUTPUTFORMAT=geojson';
$wfsContent = file_get_contents(
$wfsUrl . '&version=2.0.0&TYPENAME=CHILL:Adresses&startIndex=' . $startIndex . '&count=' . $count . '&OUTPUTFORMAT=geojson'
);
$response = $this->client->request('GET', $wfsUrl);
$jsonContent = json_decode($wfsContent, true);
if ($response->getStatusCode() === 200) {
foreach ($jsonContent['features'] as $key => $value) {
$content = $response->toArray();
$a = new AddressReference();
foreach ($content['features'] as $key => $value) {
$x = $value['geometry']['coordinates'][0];
$y = $value['geometry']['coordinates'][1];
$a = new AddressReference();
$lon = -1.4 + 0.01 * rand(-5, 5); //TODO temp: get the coordinates in EPSG:4326
$lat = 46.5 + 0.01 * rand(-5, 5); //TODO temp: get the coordinates in EPSG:4326
$x = $value['geometry']['coordinates'][0];
$y = $value['geometry']['coordinates'][1];
$point = Point::fromLonLat($lon, $lat);
$lon = -1.4 + 0.01 * rand(-5, 5); //TODO temp: get the coordinates in EPSG:4326
$lat = 46.5 + 0.01 * rand(-5, 5); //TODO temp: get the coordinates in EPSG:4326
$em = $this->entityManager;
$point = Point::fromLonLat($lon, $lat);
$country = $em
->getRepository('ChillMainBundle:Country')
->findOneBy(array('countryCode' => $countryCode));
$em = $this->entityManager;
$postcode = $em
->getRepository('ChillMainBundle:PostalCode')
->findOneBy(array('code' => $value['properties']['CODE_POST'], 'country' => $country));
$country = $em
->getRepository('ChillMainBundle:Country')
->findOneBy(array('countryCode' => $countryCode));
$a->setRefId($value['properties']['OBJECTID']);
$a->setStreet($value['properties']['NOM_AFNOR']);
$a->setStreetNumber($value['properties']['NUMERO']);
$a->setPoint($point);
$a->setPostcode($postcode);
$a->setMunicipalityCode($value['properties']['CODE_INSEE']);
$a->setSource($dataSource);
$postcode = $em
->getRepository('ChillMainBundle:PostalCode')
->findOneBy(array('code' => $value['properties']['CODE_POST'], 'country' => $country));
$addressReferenceArray[] = $a;
$a->setRefId($value['properties']['OBJECTID']);
$a->setStreet($value['properties']['NOM_AFNOR']);
$a->setStreetNumber($value['properties']['NUMERO']);
$a->setPoint($point);
$a->setPostcode($postcode);
$a->setMunicipalityCode($value['properties']['CODE_INSEE']);
$a->setSource($dataSource);
$addressReferenceArray[] = $a;
}
} else {
$output->writeln('<error>Error getting WFS at ' . $wfsUrl . '. Status code is ' . $response->getStatusCode() . '.</error>');
}
return $addressReferenceArray;
}
}

View File

@ -31,14 +31,14 @@ services:
- [ setContainer, ["@service_container"]]
tags:
- { name: twig.extension }
chill.main.twig.widget:
class: Chill\MainBundle\Templating\Widget\WidgetRenderingTwig
arguments:
- "@event_dispatcher"
tags:
- { name: twig.extension }
chill.main.twig.csv_cell:
class: Chill\MainBundle\Templating\CSVCellTwig
tags:
@ -65,3 +65,10 @@ services:
- "@security.authorization_checker"
- "@chill.main.security.authorization.helper"
- "@security.token_storage"
Chill\MainBundle\Command\:
resource: '../Command/'
autowire: true
autoconfigure: true
tags:
- { name: console.command }

View File

@ -44,9 +44,3 @@ services:
$entityManager: '@doctrine.orm.entity_manager'
tags:
- { name: console.command }
Chill\MainBundle\Command\LoadAddressReferenceCommand:
arguments:
$entityManager: '@doctrine.orm.entity_manager'
tags:
- { name: console.command }