Skip to content

Commit

Permalink
feat: ajout liens fichiers styles dans metadata TMS #587
Browse files Browse the repository at this point in the history
feat: ajout liens fichiers styles dans metadata TMS #587
  • Loading branch information
ocruze committed Feb 3, 2025
1 parent bc72388 commit 9c257d3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
12 changes: 12 additions & 0 deletions src/Constants/EntrepotApi/ConfigurationMetadataTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Constants\EntrepotApi;

final readonly class ConfigurationMetadataTypes
{
public const ISO19115_2003 = 'ISO19115:2003';
public const FGDC = 'FGDC';
public const TC211 = 'TC211';
public const _19139 = '19139';
public const Other = 'Other';
}
17 changes: 0 additions & 17 deletions src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace App\Controller;

use App\Services\CswMetadataHelper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -29,18 +26,4 @@ public function app(UrlGeneratorInterface $urlGenerator): Response
'app_root' => $appRoot,
]);
}

/*#[Route(
'/test/xml',
name: 'cartesgouvfr_app_test_xml',
)]
public function testXML(ParameterBagInterface $parameterBagInterface, CswMetadataHelper $helper)
{
$filepath = $parameterBagInterface->get('assets_directory') . '/data/test_maria.xml';
$content = file_get_contents($filepath);
$md = $helper->fromXml($content);
return new JsonResponse("coucou");
}*/
}
62 changes: 61 additions & 1 deletion src/Controller/Entrepot/StyleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Controller\Entrepot;

use App\Constants\EntrepotApi\CommonTags;
use App\Constants\EntrepotApi\ConfigurationMetadataTypes;
use App\Constants\EntrepotApi\ConfigurationTypes;
use App\Controller\ApiControllerInterface;
use App\Exception\ApiException;
use App\Exception\CartesApiException;
Expand All @@ -20,7 +22,9 @@

#[Route(
'/api/datastores/{datastoreId}/style',
name: 'cartesgouvfr_api_style_'
name: 'cartesgouvfr_api_style_',
options: ['expose' => true],
condition: 'request.isXmlHttpRequest()'
)]
class StyleController extends AbstractController implements ApiControllerInterface
{
Expand Down Expand Up @@ -72,6 +76,7 @@ public function add(string $datastoreId, string $offeringId, Request $request):
$this->_addUrls($datastore, $styles);

$this->updateStyles($datastoreId, $configuration['_id'], $styles);
$this->updateStylesTmsMetadata($datastoreId, $configuration, $offeringId, $styles);

try {
$this->cartesMetadataApiService->updateStyleFiles($datastoreId, $datasheetName);
Expand Down Expand Up @@ -135,6 +140,7 @@ public function remove(string $datastoreId, string $offeringId, Request $request
}

$this->updateStyles($datastoreId, $configuration['_id'], $styles);
$this->updateStylesTmsMetadata($datastoreId, $configuration, $offeringId, $styles);

try {
$this->cartesMetadataApiService->updateStyleFiles($datastoreId, $datasheetName);
Expand Down Expand Up @@ -269,4 +275,58 @@ private function updateStyles(string $datastoreId, string $configurationId, arra
$extra = ['styles' => $styles];
$this->configurationApiService->modify($datastoreId, $configurationId, ['extra' => $extra]);
}

/**
* @param array<mixed> $configuration
* @param array<mixed> $styles
*/
private function updateStylesTmsMetadata(string $datastoreId, array $configuration, string $offeringId, array $styles): void
{
if (ConfigurationTypes::WMTSTMS !== $configuration['type']) {
return;
}

$requestBody = [
'type' => $configuration['type'],
'name' => $configuration['name'],
'type_infos' => [
'title' => $configuration['type_infos']['title'],
'abstract' => $configuration['type_infos']['abstract'],
'keywords' => $configuration['type_infos']['keywords'],
'used_data' => $configuration['type_infos']['used_data'],
],
];

if (isset($configuration['attribution']['title']) && isset($configuration['attribution']['url'])) {
$requestBody['attribution'] = [
'title' => $configuration['attribution']['title'],
'url' => $configuration['attribution']['url'],
];
}

$styleUrlRegex = '/^https?:\/\/[^\s\/$.?#].[^\s]*\/style\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\.json$/';

$metadataList = $configuration['metadata'];

// suppression des fichiers de style venant de cartes.gouv.fr (voir styleUrlRegex)
$metadataList = array_values(array_filter($metadataList, function ($metadata) use ($styleUrlRegex) {
return !('application/json' === $metadata['format'] && preg_match($styleUrlRegex, $metadata['url']));
}));

// ajout des fichiers de style à jour dans la metadata de la configuration
foreach ($styles as $style) {
foreach ($style['layers'] as $layer) {
$metadataList[] = [
'format' => 'application/json',
'url' => $layer['url'],
'type' => ConfigurationMetadataTypes::Other,
];
}
}

$requestBody['metadata'] = $metadataList;

$this->configurationApiService->replace($datastoreId, $configuration['_id'], $requestBody);
$this->configurationApiService->syncOffering($datastoreId, $offeringId);
}
}

0 comments on commit 9c257d3

Please sign in to comment.