434 lines
No EOL
22 KiB
PHP
434 lines
No EOL
22 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Entity\Stats;
|
|
use App\Entity\CityFollowUp;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
class FollowUpService
|
|
{
|
|
public function generateCityFollowUps(Stats $stats, Motocultrice $motocultrice, EntityManagerInterface $em): void
|
|
{
|
|
$insee_code = $stats->getZone();
|
|
$elements = $motocultrice->followUpCity($insee_code) ?? [];
|
|
$themes = self::getFollowUpThemes();
|
|
$types = [];
|
|
foreach ($themes as $type => $label) {
|
|
if ($type === 'fire_hydrant') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['emergency'] ?? null) === 'fire_hydrant') ?? [];
|
|
} elseif ($type === 'charging_station') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'charging_station') ?? [];
|
|
} elseif ($type === 'toilets') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'toilets') ?? [];
|
|
} elseif ($type === 'bus_stop') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['highway'] ?? null) === 'bus_stop') ?? [];
|
|
} elseif ($type === 'defibrillator') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['emergency'] ?? null) === 'defibrillator') ?? [];
|
|
} elseif ($type === 'camera') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['man_made'] ?? null) === 'surveillance') ?? [];
|
|
} elseif ($type === 'recycling') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'recycling') ?? [];
|
|
} elseif ($type === 'substation') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['power'] ?? null) === 'substation') ?? [];
|
|
} elseif ($type === 'laboratory') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['healthcare'] ?? null) === 'laboratory') ?? [];
|
|
} elseif ($type === 'school') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'school') ?? [];
|
|
} elseif ($type === 'police') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'police') ?? [];
|
|
} elseif ($type === 'healthcare') {
|
|
$objects = array_filter($elements, function($el) {
|
|
return isset($el['tags']['healthcare'])
|
|
|| ($el['tags']['amenity'] ?? null) === 'doctors'
|
|
|| ($el['tags']['amenity'] ?? null) === 'pharmacy'
|
|
|| ($el['tags']['amenity'] ?? null) === 'hospital'
|
|
|| ($el['tags']['amenity'] ?? null) === 'clinic'
|
|
|| ($el['tags']['amenity'] ?? null) === 'social_facility';
|
|
}) ?? [];
|
|
} elseif ($type === 'bicycle_parking') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'bicycle_parking') ?? [];
|
|
} elseif ($type === 'advertising_board') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['advertising'] ?? null) === 'board' && ($el['tags']['message'] ?? null) === 'political') ?? [];
|
|
} elseif ($type === 'building') {
|
|
$objects = array_filter($elements, fn($el) => ($el['type'] ?? null) === 'way' && !empty($el['tags']['building'])) ?? [];
|
|
} elseif ($type === 'email') {
|
|
$objects = array_filter($elements, fn($el) => !empty($el['tags']['email'] ?? null) || !empty($el['tags']['contact:email'] ?? null)) ?? [];
|
|
} elseif ($type === 'bench') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'bench') ?? [];
|
|
} elseif ($type === 'waste_basket') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'waste_basket') ?? [];
|
|
} elseif ($type === 'street_lamp') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['highway'] ?? null) === 'street_lamp') ?? [];
|
|
} elseif ($type === 'drinking_water') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'drinking_water') ?? [];
|
|
} elseif ($type === 'tree') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['natural'] ?? null) === 'tree') ?? [];
|
|
} elseif ($type === 'places') {
|
|
$objects = [];
|
|
} elseif ($type === 'power_pole') {
|
|
$objects = array_filter($elements, fn($el) => ($el['tags']['power'] ?? null) === 'pole') ?? [];
|
|
} else {
|
|
$objects = [];
|
|
}
|
|
$types[$type] = [
|
|
'label' => $label,
|
|
'objects' => $objects
|
|
];
|
|
}
|
|
$now = new \DateTime();
|
|
$persisted = 0;
|
|
foreach ($types as $type => $data) {
|
|
// Suivi du nombre
|
|
$followupCount = new CityFollowUp();
|
|
$followupCount->setName($type . '_count')
|
|
->setMeasure($type === 'places' ? $stats->getPlacesCount() : count($data['objects']))
|
|
->setDate($now)
|
|
->setStats($stats);
|
|
$em->persist($followupCount);
|
|
$persisted++;
|
|
if ($persisted % 100 === 0) {
|
|
$em->flush();
|
|
$em->clear();
|
|
}
|
|
|
|
// Suivi de la complétion personnalisé (exemples)
|
|
$completed = [];
|
|
if ($type === 'fire_hydrant') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['ref'] ?? null);
|
|
});
|
|
} elseif ($type === 'charging_station') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['charging_station:output'] ?? null) && !empty($el['tags']['capacity'] ?? null);
|
|
});
|
|
} elseif ($type === 'toilets') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return ($el['tags']['wheelchair'] ?? null) === 'yes';
|
|
});
|
|
} elseif ($type === 'bus_stop') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['shelter'] ?? null);
|
|
});
|
|
} elseif ($type === 'defibrillator') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['indoor'] ?? null);
|
|
});
|
|
} elseif ($type === 'camera') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['surveillance:type'] ?? null);
|
|
});
|
|
} elseif ($type === 'recycling') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['recycling_type'] ?? null);
|
|
});
|
|
} elseif ($type === 'substation') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['substation'] ?? null);
|
|
});
|
|
} elseif ($type === 'laboratory') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['website'] ?? null) || !empty($el['tags']['contact:website'] ?? null);
|
|
});
|
|
} elseif ($type === 'school') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['ref:UAI'] ?? null) && !empty($el['tags']['isced:level'] ?? null) && !empty($el['tags']['school:FR'] ?? null);
|
|
});
|
|
} elseif ($type === 'police') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['phone'] ?? null) || !empty($el['tags']['website'] ?? null);
|
|
});
|
|
} elseif ($type === 'healthcare') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
$tags = $el['tags'] ?? [];
|
|
return !empty($tags['name'] ?? null)
|
|
|| !empty($tags['contact:phone'] ?? null)
|
|
|| !empty($tags['phone'] ?? null)
|
|
|| !empty($tags['email'] ?? null)
|
|
|| !empty($tags['contact:email'] ?? null);
|
|
});
|
|
} elseif ($type === 'bicycle_parking') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
return !empty($el['tags']['capacity'] ?? null) || !empty($el['tags']['covered'] ?? null);
|
|
});
|
|
} elseif ($type === 'advertising_board') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
// On considère complet si le tag "operator" ou "ref" est présent
|
|
return !empty($el['tags']['operator'] ?? null) || !empty($el['tags']['ref'] ?? null);
|
|
});
|
|
} elseif ($type === 'building') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
// Complet si ref:FR:RNB est rempli
|
|
return !empty($el['tags']['ref:FR:RNB'] ?? null);
|
|
});
|
|
} elseif ($type === 'email') {
|
|
$completed = $data['objects']; // Possède déjà un email ou contact:email
|
|
} elseif ($type === 'bench') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
// Complet si le tag "material" ou "backrest" est présent
|
|
return !empty($el['tags']['material'] ?? null) || !empty($el['tags']['backrest'] ?? null);
|
|
});
|
|
} elseif ($type === 'waste_basket') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
// Complet si le tag "material" ou "ref" est présent
|
|
return !empty($el['tags']['material'] ?? null) || !empty($el['tags']['ref'] ?? null);
|
|
});
|
|
} elseif ($type === 'street_lamp') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
// Complet si le tag "lamp_type" ou "ref" est présent
|
|
return !empty($el['tags']['lamp_type'] ?? null) || !empty($el['tags']['ref'] ?? null);
|
|
});
|
|
} elseif ($type === 'drinking_water') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
// Complet si le tag "covered" ou "ref" est présent
|
|
return !empty($el['tags']['covered'] ?? null) || !empty($el['tags']['ref'] ?? null);
|
|
});
|
|
} elseif ($type === 'tree') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
$tags = $el['tags'] ?? [];
|
|
$hasTaxonomy = !empty($tags['species'] ?? null)
|
|
|| !empty($tags['genus'] ?? null)
|
|
|| !empty($tags['taxon'] ?? null)
|
|
|| !empty($tags['taxon:binomial'] ?? null);
|
|
$hasLeaf = !empty($tags['leaf_type'] ?? null)
|
|
|| !empty($tags['leaf_cycle'] ?? null)
|
|
|| !empty($tags['leaf_shape'] ?? null)
|
|
|| !empty($tags['leaf_color'] ?? null)
|
|
|| !empty($tags['leaf_fall'] ?? null);
|
|
return $hasTaxonomy && $hasLeaf;
|
|
});
|
|
} elseif ($type === 'power_pole') {
|
|
$completed = array_filter($data['objects'], function($el) {
|
|
$tags = $el['tags'] ?? [];
|
|
return !empty($tags['ref'] ?? null)
|
|
|| !empty($tags['material'] ?? null)
|
|
|| !empty($tags['height'] ?? null)
|
|
|| !empty($tags['operator'] ?? null);
|
|
});
|
|
}
|
|
if ($type === 'places') {
|
|
$completion = $stats->getCompletionPercent();
|
|
} else {
|
|
$completion = count($data['objects']) > 0 ? round(count($completed) / count($data['objects']) * 100) : 0;
|
|
}
|
|
$followupCompletion = new CityFollowUp();
|
|
$followupCompletion->setName($type . '_completion')
|
|
->setMeasure($completion)
|
|
->setDate($now)
|
|
->setStats($stats);
|
|
$em->persist($followupCompletion);
|
|
$persisted++;
|
|
if ($persisted % 100 === 0) {
|
|
$em->flush();
|
|
$em->clear();
|
|
}
|
|
}
|
|
$em->flush();
|
|
$em->clear();
|
|
|
|
// Suppression des mesures redondantes (même valeur consécutive, sauf la dernière)
|
|
$repo = $em->getRepository(CityFollowUp::class);
|
|
foreach (array_keys(self::getFollowUpThemes()) as $type) {
|
|
foreach (['_count', '_completion'] as $suffix) {
|
|
$name = $type . $suffix;
|
|
$followups = $repo->createQueryBuilder('f')
|
|
->where('f.stats = :stats')
|
|
->andWhere('f.name = :name')
|
|
->setParameter('stats', $stats)
|
|
->setParameter('name', $name)
|
|
->orderBy('f.date', 'ASC')
|
|
->getQuery()->getResult();
|
|
$toDelete = [];
|
|
$prev = null;
|
|
$n = count($followups);
|
|
foreach ($followups as $i => $fu) {
|
|
// Si seulement 2 mesures, ne rien supprimer
|
|
if ($n == 2) {
|
|
break;
|
|
}
|
|
if ($prev && $fu->getMeasure() === $prev->getMeasure() && $i < $n - 1) {
|
|
$toDelete[] = $prev;
|
|
}
|
|
$prev = $fu;
|
|
}
|
|
foreach ($toDelete as $del) {
|
|
$em->remove($del);
|
|
}
|
|
}
|
|
}
|
|
$em->flush();
|
|
$em->clear();
|
|
}
|
|
|
|
public function generateGlobalFollowUps(EntityManagerInterface $em): void
|
|
{
|
|
$statsGlobal = $em->getRepository(\App\Entity\Stats::class)->findOneBy(['zone' => '00000']);
|
|
if (!$statsGlobal) {
|
|
$statsGlobal = new \App\Entity\Stats();
|
|
$statsGlobal->setZone('00000');
|
|
$statsGlobal->setName('toutes les villes');
|
|
$em->persist($statsGlobal);
|
|
$em->flush();
|
|
}
|
|
$now = new \DateTime();
|
|
$themes = array_keys(self::getFollowUpThemes());
|
|
$allStats = $em->getRepository(\App\Entity\Stats::class)->findAll();
|
|
$allStats = array_filter($allStats, fn($s) => $s->getZone() !== '00000');
|
|
$cityCount = count($allStats);
|
|
$globalCompletionSum = 0;
|
|
$globalCompletionCount = 0;
|
|
foreach ($themes as $theme) {
|
|
$sumCount = 0;
|
|
$sumCompletion = 0;
|
|
$nbCompletion = 0;
|
|
foreach ($allStats as $stats) {
|
|
$latestCount = null;
|
|
$latestCompletion = null;
|
|
foreach ($stats->getCityFollowUps() as $fu) {
|
|
if ($fu->getName() === $theme . '_count') {
|
|
if ($latestCount === null || $fu->getDate() > $latestCount->getDate()) {
|
|
$latestCount = $fu;
|
|
}
|
|
}
|
|
if ($fu->getName() === $theme . '_completion') {
|
|
if ($latestCompletion === null || $fu->getDate() > $latestCompletion->getDate()) {
|
|
$latestCompletion = $fu;
|
|
}
|
|
}
|
|
}
|
|
if ($latestCount) $sumCount += $latestCount->getMeasure();
|
|
if ($latestCompletion) {
|
|
$sumCompletion += $latestCompletion->getMeasure();
|
|
$nbCompletion++;
|
|
}
|
|
}
|
|
$fuCount = new \App\Entity\CityFollowUp();
|
|
$fuCount->setName($theme . '_count')
|
|
->setMeasure($sumCount)
|
|
->setDate($now)
|
|
->setStats($statsGlobal);
|
|
$em->persist($fuCount);
|
|
$completionAvg = $nbCompletion > 0 ? round($sumCompletion / $nbCompletion, 1) : 0;
|
|
$fuCompletion = new \App\Entity\CityFollowUp();
|
|
$fuCompletion->setName($theme . '_completion')
|
|
->setMeasure($completionAvg)
|
|
->setDate($now)
|
|
->setStats($statsGlobal);
|
|
$em->persist($fuCompletion);
|
|
if ($theme !== 'places' && $nbCompletion > 0) {
|
|
$globalCompletionSum += $completionAvg;
|
|
$globalCompletionCount++;
|
|
}
|
|
}
|
|
$sumPlaces = 0;
|
|
foreach ($allStats as $stats) {
|
|
$sumPlaces += $stats->getPlacesCount() ?? 0;
|
|
}
|
|
$fuPlaces = new \App\Entity\CityFollowUp();
|
|
$fuPlaces->setName('places_count')
|
|
->setMeasure($sumPlaces)
|
|
->setDate($now)
|
|
->setStats($statsGlobal);
|
|
$em->persist($fuPlaces);
|
|
$globalCompletionAvg = $globalCompletionCount > 0 ? round($globalCompletionSum / $globalCompletionCount, 1) : 0;
|
|
$fuGlobalCompletion = new \App\Entity\CityFollowUp();
|
|
$fuGlobalCompletion->setName('global_completion_average')
|
|
->setMeasure($globalCompletionAvg)
|
|
->setDate($now)
|
|
->setStats($statsGlobal);
|
|
$em->persist($fuGlobalCompletion);
|
|
$fuCityCount = new \App\Entity\CityFollowUp();
|
|
$fuCityCount->setName('city_count')
|
|
->setMeasure($cityCount)
|
|
->setDate($now)
|
|
->setStats($statsGlobal);
|
|
$em->persist($fuCityCount);
|
|
$em->flush();
|
|
}
|
|
|
|
public static function getFollowUpThemes(): array
|
|
{
|
|
return [
|
|
'fire_hydrant' => 'Bornes incendie',
|
|
'charging_station' => 'Bornes de recharge',
|
|
'toilets' => 'Toilettes publiques',
|
|
'bus_stop' => 'Arrêts de bus',
|
|
'defibrillator' => 'Défibrillateurs',
|
|
'camera' => 'Caméras de surveillance',
|
|
'recycling' => 'Points de recyclage',
|
|
'substation' => 'Sous-stations électriques',
|
|
'laboratory' => "Laboratoires d'analyse",
|
|
'school' => 'Écoles',
|
|
'police' => 'Commissariats',
|
|
'healthcare' => 'Lieux de santé',
|
|
'bicycle_parking' => 'Parkings vélos',
|
|
'advertising_board' => 'Panneaux électoraux',
|
|
'building' => 'Bâtiments',
|
|
'email' => 'Objets avec email',
|
|
'bench' => 'Bancs',
|
|
'waste_basket' => 'Poubelles',
|
|
'street_lamp' => 'Lampadaires',
|
|
'drinking_water' => 'Eau potable',
|
|
'tree' => 'Arbres',
|
|
'places' => 'Lieux',
|
|
'power_pole' => 'Poteaux électriques',
|
|
];
|
|
}
|
|
|
|
public static function getFollowUpIcons(): array
|
|
{
|
|
return [
|
|
'fire_hydrant' => 'bi-droplet',
|
|
'charging_station' => 'bi-lightning-charge',
|
|
'toilets' => 'bi-toilet',
|
|
'bus_stop' => 'bi-bus-front',
|
|
'defibrillator' => 'bi-heart-pulse',
|
|
'camera' => 'bi-camera-video',
|
|
'recycling' => 'bi-recycle',
|
|
'substation' => 'bi-plug',
|
|
'laboratory' => 'bi-beaker',
|
|
'school' => 'bi-mortarboard',
|
|
'police' => 'bi-shield-lock',
|
|
'healthcare' => 'bi-hospital',
|
|
'bicycle_parking' => 'bi-bicycle',
|
|
'advertising_board' => 'bi-easel',
|
|
'building' => 'bi-building',
|
|
'email' => 'bi-envelope-at',
|
|
'bench' => 'bi-badge-wc',
|
|
'waste_basket' => 'bi-trash',
|
|
'street_lamp' => 'bi-lightbulb',
|
|
'drinking_water' => 'bi-droplet-half',
|
|
'tree' => 'bi-tree',
|
|
'places' => 'bi-geo-alt',
|
|
'power_pole' => 'bi-signpost',
|
|
];
|
|
}
|
|
|
|
public static function getFollowUpOverpassQueries(): array
|
|
{
|
|
return [
|
|
'fire_hydrant' => 'nwr["emergency"="fire_hydrant"](area.searchArea);',
|
|
'charging_station' => 'nwr["amenity"="charging_station"](area.searchArea);',
|
|
'toilets' => 'nwr["amenity"="toilets"](area.searchArea);',
|
|
'bus_stop' => 'nwr["highway"="bus_stop"](area.searchArea);',
|
|
'defibrillator' => 'nwr["emergency"="defibrillator"](area.searchArea);',
|
|
'camera' => 'nwr["man_made"="surveillance"](area.searchArea);',
|
|
'recycling' => 'nwr["amenity"="recycling"](area.searchArea);',
|
|
'substation' => 'nwr["power"="substation"](area.searchArea);',
|
|
'laboratory' => 'nwr["healthcare"="laboratory"](area.searchArea);',
|
|
'school' => 'nwr["amenity"="school"](area.searchArea);',
|
|
'police' => 'nwr["amenity"="police"](area.searchArea);',
|
|
'healthcare' => 'nwr["healthcare"](area.searchArea);nwr["amenity"="doctors"](area.searchArea);nwr["amenity"="pharmacy"](area.searchArea);nwr["amenity"="hospital"](area.searchArea);nwr["amenity"="clinic"](area.searchArea);nwr["amenity"="social_facility"](area.searchArea);',
|
|
'bicycle_parking' => 'nwr["amenity"="bicycle_parking"](area.searchArea);',
|
|
'advertising_board' => 'nwr["advertising"="board"]["message"="political"](area.searchArea);',
|
|
'building' => 'way["building"](area.searchArea);',
|
|
'email' => 'nwr["email"](area.searchArea);nwr["contact:email"](area.searchArea);',
|
|
'bench' => 'nwr["amenity"="bench"](area.searchArea);',
|
|
'waste_basket' => 'nwr["amenity"="waste_basket"](area.searchArea);',
|
|
'street_lamp' => 'nwr["highway"="street_lamp"](area.searchArea);',
|
|
'drinking_water' => 'nwr["amenity"="drinking_water"](area.searchArea);',
|
|
'tree' => 'nwr["natural"="tree"](area.searchArea);',
|
|
'places' => ''
|
|
];
|
|
}
|
|
}
|