-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLitteralisCommunicationExtractor.php
More file actions
188 lines (154 loc) · 7.35 KB
/
LitteralisCommunicationExtractor.php
File metadata and controls
188 lines (154 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
declare(strict_types=1);
namespace App\Infrastructure\Integration\Litteralis;
use App\Application\Integration\Litteralis\DTO\LitteralisCredentials;
use App\Infrastructure\Integration\IntegrationReport\CommonRecordEnum;
use App\Infrastructure\Integration\IntegrationReport\Reporter;
/**
* Extrait les données du flux WFS Litteralis "Communication" (une feature = une mesure),
* les regroupe par emprise et produit le même format que LitteralisExtractor pour réutilisation
* du LitteralisTransformer.
*
* @see LitteralisClient (couche LIcommunication via LitteralisCommunicationClientFactory)
* @see LitteralisTransformer
*/
final class LitteralisCommunicationExtractor
{
private array $clients = [];
/** Filtres CQL sur le champ "mesure" (singulier) du flux Communication. */
private const MESURE_ILIKE_ITEMS = [
'%circulation interdite%',
'%limitation de vitesse%',
'%interruption de circulation%',
'%interdiction de stationnement%',
];
public function __construct(
private LitteralisCommunicationClientFactory $clientFactory,
) {
}
public function configure(array $enabledOrgs, LitteralisCredentials $credentials): void
{
$this->clients = [];
foreach ($enabledOrgs as $name) {
$orgCredentials = $credentials->getCredentials($name);
if (empty($orgCredentials)) {
throw new \RuntimeException(\sprintf(
'Organization "%s": missing credentials (check APP_LITTERALIS_ORG_%s_CREDENTIALS)',
$name,
strtoupper($name),
));
}
$client = $this->clientFactory->create($orgCredentials);
$this->clients[$name] = $client;
}
}
private function getClient(string $name): LitteralisClient
{
if (empty($this->clients[$name])) {
throw new \RuntimeException(\sprintf('Organization with name "%s" is not enabled for Communication flux', $name));
}
return $this->clients[$name];
}
/**
* Même contrat que LitteralisExtractor::extractFeaturesByRegulation.
* Retourne un tableau [ uniqueIdentifier => [ feature synthétique par emprise ] ]
* avec des propriétés "mesures" et "parametresmesures" construites à partir des champs
* "mesure" et "parametresmesure" du flux Communication.
*
* @return array<string, list<array{geometry: array, properties: array}>>
*/
public function extractFeaturesByRegulation(string $name, \DateTimeInterface $laterThan, Reporter $reporter): array
{
$client = $this->getClient($name);
$quotedItems = array_map(fn (string $i) => \sprintf("'%s'", $i), self::MESURE_ILIKE_ITEMS);
$mesureFilter = \sprintf('mesure ILIKE %s', implode(' OR mesure ILIKE ', $quotedItems));
$arreteFinFilter = \sprintf("arretefin IS NULL OR arretefin >= '%s'", $laterThan->format(\DateTimeInterface::ISO8601));
$cqlFilter = \sprintf('(%s) AND (%s)', $mesureFilter, $arreteFinFilter);
$numTotalFeatures = $client->count(null, $reporter);
$reporter->addCount(LitteralisRecordEnum::COUNT_TOTAL_FEATURES->value, $numTotalFeatures);
$numMatchingFeatures = $client->count($cqlFilter, $reporter);
$reporter->addCount(LitteralisRecordEnum::COUNT_MATCHING_FEATURES->value, $numMatchingFeatures);
$features = $client->fetchAllPaginated($cqlFilter, $reporter);
// Grouper par (idarrete, idemprise) : dans le flux Communication une feature = une mesure
$byRegulationAndEmprise = [];
$numExtractedFeatures = 0;
foreach ($features as $feature) {
$props = $feature['properties'] ?? [];
$identifier = $props['arretesrcid'] ?? '';
$idemprise = $props['idemprise'] ?? null;
if (empty($feature['geometry'])) {
$reporter->addWarning(LitteralisRecordEnum::WARNING_MISSING_GEOMETRY->value, [
CommonRecordEnum::ATTR_REGULATION_ID->value => $identifier,
CommonRecordEnum::ATTR_URL->value => $props['shorturl'] ?? '',
CommonRecordEnum::ATTR_DETAILS->value => ['idemprise' => $idemprise],
]);
continue;
}
$feature['geometry']['crs'] = [
'type' => 'name',
'properties' => ['name' => 'EPSG:4326'],
];
$collectiviteagenceid = $props['collectiviteagenceid'] ?? $props['idagence'] ?? '';
$empriseKey = \sprintf('%s#%s#%s', $collectiviteagenceid, $identifier, $idemprise);
if (!isset($byRegulationAndEmprise[$empriseKey])) {
$byRegulationAndEmprise[$empriseKey] = [];
}
$byRegulationAndEmprise[$empriseKey][] = $feature;
++$numExtractedFeatures;
}
// Construire une feature synthétique par emprise (format attendu par LitteralisTransformer)
$featuresByRegulation = [];
foreach ($byRegulationAndEmprise as $empriseKey => $measureFeatures) {
$first = $measureFeatures[0];
$firstProps = $first['properties'];
$mesures = [];
$parametresmesuresParts = [];
foreach ($measureFeatures as $mf) {
$p = $mf['properties'];
$mesureName = trim((string) ($p['mesure'] ?? ''));
if ($mesureName === '') {
continue;
}
$mesures[] = $mesureName;
$parametresmesure = trim((string) ($p['parametresmesure'] ?? ''));
if ($parametresmesure !== '') {
$items = $this->parseSeparatedString($parametresmesure, ';');
foreach ($items as $item) {
$parametresmesuresParts[] = $mesureName . ' | ' . $item;
}
}
}
$syntheticProperties = $firstProps;
$syntheticProperties['mesures'] = implode(';', $mesures);
$syntheticProperties['parametresmesures'] = implode(' ; ', $parametresmesuresParts);
// parametresemprise : présent dans le flux Communication
$syntheticProperties['parametresemprise'] = $syntheticProperties['parametresemprise'] ?? '';
$syntheticFeature = [
'geometry' => $first['geometry'],
'properties' => $syntheticProperties,
];
$uniqueIdentifier = \sprintf('%s#%s', $firstProps['collectiviteagenceid'] ?? $firstProps['idagence'] ?? '', $firstProps['arretesrcid']);
$featuresByRegulation[$uniqueIdentifier][] = $syntheticFeature;
}
$reporter->addCount(LitteralisRecordEnum::COUNT_EXTRACTED_FEATURES->value, $numExtractedFeatures, [
'regulationsCount' => \count($featuresByRegulation),
]);
$reporter->onExtract(json_encode($featuresByRegulation, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
return $featuresByRegulation;
}
private function parseSeparatedString(string $string, string $sep): array
{
if ($string === '') {
return [];
}
$rawValues = explode($sep, $string);
$values = [];
foreach ($rawValues as $v) {
$cleaned = trim($v);
if ($cleaned !== '') {
$values[] = $cleaned;
}
}
return $values;
}
}