-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #985 from City-of-Helsinki/UHF-11027
UHF-11027: Fix ploughing schedule component
- Loading branch information
Showing
9 changed files
with
291 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
public/modules/custom/helfi_kymp_content/src/StreetDataService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_kymp_content; | ||
|
||
use Drupal\Core\TypedData\TypedDataManagerInterface; | ||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
||
/** | ||
* Service for fetching street data from kartta.hel.fi. | ||
*/ | ||
class StreetDataService { | ||
|
||
public const API_URL = 'https://kartta.hel.fi/ws/geoserver/avoindata/wfs'; | ||
|
||
/** | ||
* Constructs a new StreetDataService instance. | ||
*/ | ||
public function __construct( | ||
protected readonly ClientInterface $client, | ||
protected readonly TypedDataManagerInterface $typedDataManager, | ||
#[Autowire(service: 'logger.channel.helfi_kymp_content')] | ||
protected readonly LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
/** | ||
* Gets street data. | ||
* | ||
* @return array<int|string, \Drupal\Core\TypedData\ComplexDataInterface> | ||
* Street data. | ||
*/ | ||
public function getStreetData(): array { | ||
$query = http_build_query([ | ||
'request' => 'GetFeature', | ||
'service' => 'WFS', | ||
'version' => '1.1.0', | ||
'typeName' => 'avoindata:YLRE_Katualue_alue', | ||
'propertyname' => 'avoindata:kadun_nimi,avoindata:yllapitoluokka,avoindata:pituus', | ||
]); | ||
$uri = sprintf('%s?%s', self::API_URL, $query); | ||
|
||
try { | ||
$content = $this->client->request('GET', $uri); | ||
$xmlResult = $content->getBody()->getContents(); | ||
if (!$xmlResult) { | ||
return []; | ||
} | ||
} | ||
catch (GuzzleException $e) { | ||
$this->logger->error("Errors while fetching street data from kartta.hel.fi: {$e->getMessage()}"); | ||
return []; | ||
} | ||
|
||
$internal_errors = libxml_use_internal_errors(TRUE); | ||
$doc = new \DOMDocument(encoding: 'UTF-8'); | ||
$doc->preserveWhiteSpace = FALSE; | ||
$doc->loadXML($xmlResult); | ||
$errors = libxml_get_errors(); | ||
libxml_use_internal_errors($internal_errors); | ||
|
||
if ($errors) { | ||
$this->logger->error('Errors while parsing street data xml string.'); | ||
return []; | ||
} | ||
|
||
$data = []; | ||
foreach ($doc->firstChild->firstChild->childNodes->getIterator() as $street_data) { | ||
if (!$street_data instanceof \DOMElement) { | ||
continue; | ||
} | ||
|
||
$id = $street_data->getAttribute('gml:id'); | ||
if (!$id) { | ||
continue; | ||
} | ||
|
||
$single_street = [ | ||
'id' => $id, | ||
]; | ||
|
||
foreach ($street_data->childNodes->getIterator() as $field) { | ||
switch ($field->nodeName) { | ||
case 'avoindata:kadun_nimi': | ||
$single_street['street_name'] = $field->nodeValue; | ||
break; | ||
|
||
case 'avoindata:pituus': | ||
$single_street['length'] = $field->nodeValue; | ||
break; | ||
|
||
case 'avoindata:yllapitoluokka': | ||
// Turn field value from III or II to 3 or 2 etc. | ||
$single_street['maintenance_class'] = strlen($field->nodeValue); | ||
break; | ||
} | ||
} | ||
|
||
$street_data_definition = $this->typedDataManager->createDataDefinition('street_data'); | ||
/** @var \Drupal\Core\TypedData\ComplexDataInterface $street_data */ | ||
$street_data = $this->typedDataManager->create($street_data_definition); | ||
$street_data->setValue($single_street); | ||
$data[$id] = $street_data; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
public/modules/custom/helfi_kymp_content/tests/fixtures/street_data.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<wfs:FeatureCollection xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wfs="http://www.opengis.net/wfs" xmlns:gml="http://www.opengis.net/gml" xmlns:avoindata="https://www.hel.fi/avoindata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" numberOfFeatures="7717" timeStamp="2024-11-28T11:58:56.213+02:00" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd https://www.hel.fi/avoindata https://kartta.hel.fi/ws/geoserver/avoindata/wfs?service=WFS&version=1.1.0&request=DescribeFeatureType&typeName=avoindata%3AYLRE_Katualue_alue"> | ||
<gml:featureMembers> | ||
<avoindata:YLRE_Katualue_alue gml:id="YLRE_Katualue_alue.228"> | ||
<avoindata:kadun_nimi>Suovakuja</avoindata:kadun_nimi> | ||
<avoindata:kayttotarkoitus>Asuntokatu</avoindata:kayttotarkoitus> | ||
<avoindata:yllapitoluokka>I</avoindata:yllapitoluokka> | ||
<avoindata:pituus>139.5</avoindata:pituus> | ||
</avoindata:YLRE_Katualue_alue> | ||
<avoindata:YLRE_Katualue_alue gml:id="YLRE_Katualue_alue.797"> | ||
<avoindata:kadun_nimi>Rauhankatu</avoindata:kadun_nimi> | ||
<avoindata:kayttotarkoitus>Asuntokatu</avoindata:kayttotarkoitus> | ||
<avoindata:yllapitoluokka>II</avoindata:yllapitoluokka> | ||
<avoindata:pituus>130.3</avoindata:pituus> | ||
</avoindata:YLRE_Katualue_alue> | ||
<avoindata:YLRE_Katualue_alue gml:id="YLRE_Katualue_alue.809"> | ||
<avoindata:kadun_nimi>Kruunuhaankatu</avoindata:kadun_nimi> | ||
<avoindata:kayttotarkoitus>Asuntokatu</avoindata:kayttotarkoitus> | ||
<avoindata:yllapitoluokka>III</avoindata:yllapitoluokka> | ||
<avoindata:pituus>72.4</avoindata:pituus> | ||
</avoindata:YLRE_Katualue_alue> | ||
<avoindata:YLRE_Katualue_alue gml:id="YLRE_Katualue_alue.448"> | ||
<avoindata:kadun_nimi>Sammonkatu</avoindata:kadun_nimi> | ||
<avoindata:kayttotarkoitus>Asuntokatu</avoindata:kayttotarkoitus> | ||
<avoindata:yllapitoluokka>III</avoindata:yllapitoluokka> | ||
<avoindata:pituus>75.3</avoindata:pituus> | ||
</avoindata:YLRE_Katualue_alue> | ||
</gml:featureMembers> | ||
</wfs:FeatureCollection> |
Oops, something went wrong.