mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-14 06:14:23 +00:00
addresses: import reference address from WFS using httpClientInterface
This commit is contained in:
parent
830cda64e0
commit
73ee77b8b2
@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Chill\MainBundle\Command;
|
namespace Chill\MainBundle\Command;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManager;
|
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
use Chill\MainBundle\Doctrine\Model\Point;
|
use Chill\MainBundle\Doctrine\Model\Point;
|
||||||
use Chill\MainBundle\Entity\AddressReference;
|
use Chill\MainBundle\Entity\AddressReference;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -18,18 +18,22 @@ class LoadAddressReferenceCommand extends Command
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var EntityManager
|
* @var EntityManagerInterface
|
||||||
*/
|
*/
|
||||||
private $entityManager;
|
private $entityManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* LoadAddressReferenceCommand constructor.
|
* @var HttpClientInterface
|
||||||
*
|
|
||||||
* @param EntityManager $entityManager
|
|
||||||
*/
|
*/
|
||||||
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();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,8 +56,7 @@ class LoadAddressReferenceCommand extends Command
|
|||||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$addressReferenceArray = $this->fetchWFS(1, 2, $output);
|
||||||
$addressReferenceArray = $this->fetchWFS(1, 10);
|
|
||||||
$em = $this->entityManager;
|
$em = $this->entityManager;
|
||||||
|
|
||||||
foreach($addressReferenceArray as $a) {
|
foreach($addressReferenceArray as $a) {
|
||||||
@ -64,11 +67,7 @@ class LoadAddressReferenceCommand extends Command
|
|||||||
|
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isAddressReferenceNew(AddressReference $a): bool
|
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
|
* //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()
|
* @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';
|
$dataSource = 'BAL GeoVendee';
|
||||||
$countryCode = 'FR';
|
$countryCode = 'FR';
|
||||||
|
|
||||||
$addressReferenceArray = array();
|
$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(
|
$response = $this->client->request('GET', $wfsUrl);
|
||||||
$wfsUrl . '&version=2.0.0&TYPENAME=CHILL:Adresses&startIndex=' . $startIndex . '&count=' . $count . '&OUTPUTFORMAT=geojson'
|
|
||||||
);
|
|
||||||
|
|
||||||
$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];
|
$a = new AddressReference();
|
||||||
$y = $value['geometry']['coordinates'][1];
|
|
||||||
|
|
||||||
$lon = -1.4 + 0.01 * rand(-5, 5); //TODO temp: get the coordinates in EPSG:4326
|
$x = $value['geometry']['coordinates'][0];
|
||||||
$lat = 46.5 + 0.01 * rand(-5, 5); //TODO temp: get the coordinates in EPSG:4326
|
$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
|
$em = $this->entityManager;
|
||||||
->getRepository('ChillMainBundle:Country')
|
|
||||||
->findOneBy(array('countryCode' => $countryCode));
|
|
||||||
|
|
||||||
$postcode = $em
|
$country = $em
|
||||||
->getRepository('ChillMainBundle:PostalCode')
|
->getRepository('ChillMainBundle:Country')
|
||||||
->findOneBy(array('code' => $value['properties']['CODE_POST'], 'country' => $country));
|
->findOneBy(array('countryCode' => $countryCode));
|
||||||
|
|
||||||
$a->setRefId($value['properties']['OBJECTID']);
|
$postcode = $em
|
||||||
$a->setStreet($value['properties']['NOM_AFNOR']);
|
->getRepository('ChillMainBundle:PostalCode')
|
||||||
$a->setStreetNumber($value['properties']['NUMERO']);
|
->findOneBy(array('code' => $value['properties']['CODE_POST'], 'country' => $country));
|
||||||
$a->setPoint($point);
|
|
||||||
$a->setPostcode($postcode);
|
|
||||||
$a->setMunicipalityCode($value['properties']['CODE_INSEE']);
|
|
||||||
$a->setSource($dataSource);
|
|
||||||
|
|
||||||
$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;
|
return $addressReferenceArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,3 +65,10 @@ services:
|
|||||||
- "@security.authorization_checker"
|
- "@security.authorization_checker"
|
||||||
- "@chill.main.security.authorization.helper"
|
- "@chill.main.security.authorization.helper"
|
||||||
- "@security.token_storage"
|
- "@security.token_storage"
|
||||||
|
|
||||||
|
Chill\MainBundle\Command\:
|
||||||
|
resource: '../Command/'
|
||||||
|
autowire: true
|
||||||
|
autoconfigure: true
|
||||||
|
tags:
|
||||||
|
- { name: console.command }
|
||||||
|
@ -44,9 +44,3 @@ services:
|
|||||||
$entityManager: '@doctrine.orm.entity_manager'
|
$entityManager: '@doctrine.orm.entity_manager'
|
||||||
tags:
|
tags:
|
||||||
- { name: console.command }
|
- { name: console.command }
|
||||||
|
|
||||||
Chill\MainBundle\Command\LoadAddressReferenceCommand:
|
|
||||||
arguments:
|
|
||||||
$entityManager: '@doctrine.orm.entity_manager'
|
|
||||||
tags:
|
|
||||||
- { name: console.command }
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user