From 73ee77b8b21b712e1b9888ee93f1166e3dee0a6f Mon Sep 17 00:00:00 2001 From: nobohan Date: Tue, 18 May 2021 13:57:39 +0200 Subject: [PATCH] addresses: import reference address from WFS using httpClientInterface --- .../Command/LoadAddressReferenceCommand.php | 91 ++++++++++--------- .../ChillMainBundle/config/services.yaml | 11 ++- .../config/services/command.yaml | 6 -- 3 files changed, 58 insertions(+), 50 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Command/LoadAddressReferenceCommand.php b/src/Bundle/ChillMainBundle/Command/LoadAddressReferenceCommand.php index 91621a3dd..a488685bf 100644 --- a/src/Bundle/ChillMainBundle/Command/LoadAddressReferenceCommand.php +++ b/src/Bundle/ChillMainBundle/Command/LoadAddressReferenceCommand.php @@ -1,13 +1,13 @@ 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 getting WFS at ' . $wfsUrl . '. Status code is ' . $response->getStatusCode() . '.'); } return $addressReferenceArray; } } + + diff --git a/src/Bundle/ChillMainBundle/config/services.yaml b/src/Bundle/ChillMainBundle/config/services.yaml index a6afa4336..eb07a16e2 100644 --- a/src/Bundle/ChillMainBundle/config/services.yaml +++ b/src/Bundle/ChillMainBundle/config/services.yaml @@ -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 } diff --git a/src/Bundle/ChillMainBundle/config/services/command.yaml b/src/Bundle/ChillMainBundle/config/services/command.yaml index fc35f1e06..5df80c2ad 100644 --- a/src/Bundle/ChillMainBundle/config/services/command.yaml +++ b/src/Bundle/ChillMainBundle/config/services/command.yaml @@ -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 }