mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
addresses reference: remove the load Adressess references command (has moved to the Vendee app)
This commit is contained in:
parent
1b1eb45d15
commit
57a35f88be
@ -36,7 +36,6 @@
|
||||
"doctrine/doctrine-migrations-bundle": "^3.0",
|
||||
"doctrine/orm": "^2.7",
|
||||
"symfony/asset": "4.*",
|
||||
"symfony/http-client": "^4.4 || ^5",
|
||||
"symfony/monolog-bundle": "^3.5",
|
||||
"symfony/security-bundle": "4.*",
|
||||
"symfony/translation": "4.*",
|
||||
|
@ -1,198 +0,0 @@
|
||||
<?php
|
||||
namespace Chill\MainBundle\Command;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Champs-Libres
|
||||
*
|
||||
*/
|
||||
class LoadAddressReferenceCommand extends Command
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
private $entityManager;
|
||||
|
||||
/**
|
||||
* @var HttpClientInterface
|
||||
*/
|
||||
private $client;
|
||||
|
||||
|
||||
private $wfsBaseUrl = 'https://carto.vendee.fr/arcgis/services/Applications/CHILL/MapServer/WFSServer?SERVICE=WFS&REQUEST=GetFeature&version=2.0.0&TYPENAME=CHILL:Adresses';
|
||||
|
||||
/**
|
||||
* LoadAddressReferenceCommand constructor.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entityManager, HttpClientInterface $client)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->client = $client;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see \Symfony\Component\Console\Command\Command::configure()
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setName('chill:main:address-reference:populate')
|
||||
->setDescription('Load or update reference addresses in db. This command does not delete or update existing address reference,'.
|
||||
'but will add new address reference entities based on their timestamp');
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see \Symfony\Component\Console\Command\Command::execute()
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
|
||||
$maxCount = $this->countWFSFeatures($this->wfsBaseUrl, $output);
|
||||
$output->writeln('Number of addresses to be fetched: ' . $maxCount);
|
||||
$totalCount = 0;
|
||||
$batchSize = 100;
|
||||
|
||||
$success = 0;
|
||||
$em = $this->entityManager;
|
||||
|
||||
while($totalCount < $maxCount) {
|
||||
|
||||
try {
|
||||
$addressReferenceArray = $this->fetchWFS($totalCount, $batchSize, $output);
|
||||
$success = $success + 1;
|
||||
|
||||
foreach($addressReferenceArray as $a) {
|
||||
if (static::isAddressReferenceNew($a)) {
|
||||
$em->persist($a);
|
||||
}
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
$em->clear();
|
||||
|
||||
} catch (\Exception $ex) {
|
||||
$output->writeln('<warning>Cannot fetch WFS. Error message is: '.$ex->getMessage().'</warning>');
|
||||
}
|
||||
|
||||
$totalCount = $totalCount + $batchSize;
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
|
||||
$output->writeln($success * $batchSize . '/'. $maxCount . 'reference address were loaded in the database.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function isAddressReferenceNew(AddressReference $a): bool
|
||||
{
|
||||
$cond = true;
|
||||
// TODO: write this function
|
||||
|
||||
return $cond;
|
||||
}
|
||||
|
||||
|
||||
private function countWFSFeatures(string $wfsBaseUrl, OutputInterface $output): int
|
||||
{
|
||||
$maxCountWfsUrl = $wfsBaseUrl . '&resultType=hits';
|
||||
try {
|
||||
$response = $this->client->request('GET', $maxCountWfsUrl);
|
||||
} catch (\Exception $ex) {
|
||||
$output->writeln('<warning>Cannot get WFS request working with '.$maxCountWfsUrl.' : '.$ex->getMessage().'</warning>');
|
||||
}
|
||||
if ($response->getStatusCode() === 200) {
|
||||
$content = $response->getContent();
|
||||
$count = explode('"', explode('numberMatched="', $content)[1])[0];
|
||||
} else {
|
||||
$output->writeln('<error>Error getting WFS at ' . $maxCountWfsUrl . '. Status code is ' . $response->getStatusCode() . '.</error>');
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/*
|
||||
* //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, OutputInterface $output): array
|
||||
{
|
||||
|
||||
$countryCode = 'FR';
|
||||
|
||||
$addressReferenceArray = array();
|
||||
|
||||
$wfsBaseUrl = $this->wfsBaseUrl;
|
||||
$wfsUrl = $wfsBaseUrl . '&startIndex=' . $startIndex . '&count=' . $count . '&OUTPUTFORMAT=geojson';
|
||||
$output->writeln('Start fetching WFS data @: ' . $wfsUrl);
|
||||
|
||||
try {
|
||||
$response = $this->client->request('GET', $wfsUrl);
|
||||
} catch (\Exception $ex) {
|
||||
$output->writeln('<warning>Cannot get WFS request working with '.$wfsUrl.' : '.$ex->getMessage().'</warning>');
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() === 200) {
|
||||
|
||||
$content = $response->toArray();
|
||||
|
||||
foreach ($content['features'] as $key => $value) {
|
||||
|
||||
if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERY_VERBOSE) {
|
||||
$output->writeln('The address is ...');
|
||||
$output->write($value);
|
||||
}
|
||||
|
||||
$a = new AddressReference();
|
||||
|
||||
$x = $value['geometry']['coordinates'][0];
|
||||
$y = $value['geometry']['coordinates'][1];
|
||||
|
||||
$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
|
||||
|
||||
$point = Point::fromLonLat($lon, $lat);
|
||||
|
||||
$em = $this->entityManager;
|
||||
|
||||
$country = $em
|
||||
->getRepository('ChillMainBundle:Country')
|
||||
->findOneBy(array('countryCode' => $countryCode));
|
||||
|
||||
$postcode = $em
|
||||
->getRepository('ChillMainBundle:PostalCode')
|
||||
->findOneBy(array('code' => $value['properties']['CODE_POST'], 'country' => $country));
|
||||
|
||||
$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($value['properties']['SOURCE']);
|
||||
|
||||
$addressReferenceArray[] = $a;
|
||||
}
|
||||
|
||||
} else {
|
||||
$output->writeln('<error>Error getting WFS at ' . $wfsUrl . '. Status code is ' . $response->getStatusCode() . '.</error>');
|
||||
}
|
||||
|
||||
return $addressReferenceArray;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -26,8 +26,7 @@
|
||||
],
|
||||
"require": {
|
||||
"league/csv": "^9.6",
|
||||
"phpoffice/phpspreadsheet": "~1.2",
|
||||
"symfony/http-client": "^4.4 || ^5"
|
||||
"phpoffice/phpspreadsheet": "~1.2"
|
||||
},
|
||||
"require-dev": {
|
||||
},
|
||||
|
@ -66,9 +66,3 @@ services:
|
||||
- "@chill.main.security.authorization.helper"
|
||||
- "@security.token_storage"
|
||||
|
||||
Chill\MainBundle\Command\:
|
||||
resource: '../Command/'
|
||||
autowire: true
|
||||
autoconfigure: true
|
||||
tags:
|
||||
- { name: console.command }
|
||||
|
Loading…
x
Reference in New Issue
Block a user