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') ?? []; } elseif ($type === 'manhole') { $objects = array_filter($elements, fn($el) => ($el['tags']['manhole'] ?? null) === 'manhole') ?? []; } elseif ($type === 'little_free_library') { $objects = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'public_bookcase') ?? []; } elseif ($type === 'playground') { $objects = array_filter($elements, fn($el) => ($el['tags']['leisure'] ?? null) === 'playground') ?? []; } else { $objects = []; } $types[$type] = ['objects' => $objects, 'label' => $label]; } $types['places'] = ['objects' => [], 'label' => 'Lieux']; $now = new \DateTime(); $persisted = 0; foreach ($types as $type => $data) { // Suivi du nombre $measureCount = $type === 'places' ? $stats->getPlacesCount() : count($data['objects']); if ($measureCount !== null) { $followupCount = new CityFollowUp(); $followupCount->setName($type . '_count') ->setMeasure($measureCount) ->setDate($now) ->setStats($stats); $em->persist($followupCount); $persisted++; if ($persisted % 100 === 0) { $em->flush(); $em->clear(); } } // Suivi de la complétion basé sur les tags attendus $completionTags = self::getFollowUpCompletionTags(); $expectedTags = $completionTags[$type] ?? []; $completed = []; $partialCompletions = []; if (!empty($expectedTags)) { foreach ($data['objects'] as $el) { $tags = $el['tags'] ?? []; $filled = 0; foreach ($expectedTags as $tag) { if ($tag === 'phone') { if (!empty($tags['phone'] ?? null) || !empty($tags['contact:phone'] ?? null)) { $filled++; } } elseif ($tag === 'email') { if (!empty($tags['email'] ?? null) || !empty($tags['contact:email'] ?? null)) { $filled++; } } else { if (!empty($tags[$tag] ?? null)) { $filled++; } } } $percent = count($expectedTags) > 0 ? ($filled / count($expectedTags)) : 0; $partialCompletions[] = $percent; if ($percent === 1.0) { $completed[] = $el; } } } // ... fallback pour les types sans tags attendus else { $completed = []; $partialCompletions = array_fill(0, count($data['objects']), 0); } if ($type === 'places') { $completion = $stats->getCompletionPercent(); } else { $completion = count($partialCompletions) > 0 ? round(array_sum($partialCompletions) / count($partialCompletions) * 100) : 0; } if ($completion !== null) { $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) - désactivée définitivement // (aucune suppression de CityFollowUp) } public function generateGlobalFollowUps(EntityManagerInterface $em): void { // Vérifier l'existence d'un objet Stats pour la zone '00000' $statsGlobal = $em->getRepository(\App\Entity\Stats::class)->findOneBy(['zone' => '00000']); if ($statsGlobal) { // Si déjà existant, on le réutilise // (optionnel : mettre à jour le nom si besoin) if ($statsGlobal->getName() !== 'toutes les villes') { $statsGlobal->setName('toutes les villes'); $em->persist($statsGlobal); $em->flush(); } } else { // Sinon, on le crée // $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', 'rnb' => 'RNB', 'email' => 'Objets avec email', 'bench' => 'Bancs', 'waste_basket' => 'Poubelles', 'street_lamp' => 'Lampadaires', 'drinking_water' => 'Eau potable', 'tree' => 'Arbres', 'places' => 'Lieux', 'power_pole' => 'Poteaux électriques', 'manhole' => "Bouche d'égout", 'little_free_library' => "Micro bibliothèque", 'playground' => "Parc à jeux pour enfants", ]; } 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', 'rnb' => 'bi-key', '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', 'manhole' => 'bi-droplet-half', 'little_free_library' => 'bi-book', 'playground' => 'bi-emoji-smile', ]; } public static function getFollowUpOverpassQueries(): array { return [ 'places' => 'nw["amenity"~"^(library|townhall|restaurant|fast_food|cafe|fuel|pharmacy|bank|bar|hospital|post_office|clinic|pub|car_wash|ice_cream|driving_school|cinema|car_rental|nightclub|bureau_de_change|studio|internet_cafe|money_transfer|casino|vehicle_inspection|frozen_food|boat_rental|coworking_space|workshop|personal_service|camping|dancing_school|training|ski_school|ski_rental|dive_centre|driver_training|nursing_home|funeral_hall|doctors|dentist|theatre|kindergarten|language_school|stripclub|veterinary|convenience|supermarket|clothes|hairdresser|car_repair|bakery|beauty|car|hardware|mobile_phone|butcher|furniture|car_parts|alcohol|florist|scooter|variety_store|electronics|shoes|optician|jewelry|mall|gift|doityourself|greengrocer|books|bicycle|chemist|department_store|laundry|travel_agency|stationery|pet|sports|confectionery|tyres|cosmetics|computer|tailor|tobacco|storage_rental|dry_cleaning|trade|copyshop|motorcycle|funeral_directors|beverages|newsagent|garden_centre|massage|pastry|interior_decoration|general|deli|toys|houseware|wine|seafood|pawnbroker|tattoo|paint|wholesale|photo|second_hand|bed|kitchen|outdoor|fabric|antiques|coffee|gas|e-cigarette|perfumery|craft|hearing_aids|money_lender|appliance|electrical|tea|motorcycle_repair|boutique|baby_goods|bag|musical_instrument|dairy|pet_grooming|music|carpet|rental|fashion_accessories|cheese|chocolate|medical_supply|leather|sewing|cannabis|locksmith|games|video_games|hifi|window_blind|caravan|tool_hire|household_linen|bathroom_furnishing|shoe_repair|watches|nutrition_supplements|fishing|erotic|frame|grocery|boat|repair|weapons|gold_buyer|lighting|pottery|security|groundskeeping|herbalist|curtain|health_food|flooring|printer_ink|anime|camera|scuba_diving|candles|printing|garden_furniture|food|estate_agent|insurance|it|accountant|employment_agency|tax_advisor|financial|advertising_agency|logistics|newspaper|financial_advisor|consulting|travel_agent|coworking|moving_company|lawyer|architect|construction_company|credit_broker|graphic_design|property_management|cleaning)$"](area.searchArea); nw["shop"]["shop"!~"vacant"](area.searchArea); nw["tourism"~"^(hotel|hostel|motel|wilderness_hut|yes|chalet|gallery|guest_house|museum|zoo|theme_park|aquarium|alpine_hut|apartment)$"](area.searchArea); nw["healthcare"](area.searchArea); nw["information"="office"](area.searchArea); nw["office"](area.searchArea);', '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);nwr["public_transport"="platform"](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' => 'nwr["building"](area.searchArea);', 'rnb' => 'nwr["ref:FR:RNB"](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);', 'power_pole' => 'nwr["power"="pole"](area.searchArea);', 'manhole' => 'nwr["manhole"](area.searchArea);', 'little_free_library' => 'nwr["amenity"="public_bookcase"](area.searchArea);', 'playground' => 'nwr["leisure"="playground"](area.searchArea);', ]; } /** * Calcule la progression sur 7 jours pour un type donné */ public static function calculate7DayProgression(Stats $stats, string $type): array { $followups = $stats->getCityFollowUps(); $now = new \DateTime(); $refDate = clone $now; $refDate->modify('-7 days'); // Récupérer toutes les mesures pour ce type $countData = []; $completionData = []; foreach ($followups as $fu) { if ($fu->getName() === $type . '_count') { $countData[] = [ 'date' => $fu->getDate(), 'value' => $fu->getMeasure() ]; } if ($fu->getName() === $type . '_completion') { $completionData[] = [ 'date' => $fu->getDate(), 'value' => $fu->getMeasure() ]; } } // Trier par date usort($countData, fn($a, $b) => $a['date'] <=> $b['date']); usort($completionData, fn($a, $b) => $a['date'] <=> $b['date']); $countDelta = self::calculateDelta($countData, $refDate); $completionDelta = self::calculateDelta($completionData, $refDate); return [ 'count' => $countDelta, 'completion' => $completionDelta ]; } /** * Calcule le delta pour une série de données */ private static function calculateDelta(array $data, \DateTime $refDate): ?float { if (empty($data)) { return null; } $last = end($data)['value']; // Chercher la mesure exacte à la date de référence $exactRef = null; foreach (array_reverse($data) as $point) { if ($point['date'] <= $refDate) { $exactRef = $point['value']; break; } } // Si on a trouvé une mesure exacte, l'utiliser if ($exactRef !== null) { return $last - $exactRef; } // Sinon, chercher les deux mesures les plus proches pour faire une interpolation $beforeRef = null; $afterRef = null; $beforeDate = null; $afterDate = null; // Chercher la mesure juste avant la date de référence foreach (array_reverse($data) as $point) { if ($point['date'] < $refDate) { $beforeRef = $point['value']; $beforeDate = $point['date']; break; } } // Chercher la mesure juste après la date de référence foreach ($data as $point) { if ($point['date'] > $refDate) { $afterRef = $point['value']; $afterDate = $point['date']; break; } } // Si on a les deux mesures, faire une interpolation linéaire if ($beforeRef !== null && $afterRef !== null && $beforeDate !== null && $afterDate !== null) { $timeDiff = $afterDate->getTimestamp() - $beforeDate->getTimestamp(); $refTimeDiff = $refDate->getTimestamp() - $beforeDate->getTimestamp(); $ratio = $refTimeDiff / $timeDiff; $interpolatedRef = $beforeRef + ($afterRef - $beforeRef) * $ratio; return $last - $interpolatedRef; } // Si on n'a qu'une mesure avant, l'utiliser if ($beforeRef !== null) { return $last - $beforeRef; } // Si on n'a qu'une mesure après, l'utiliser if ($afterRef !== null) { return $last - $afterRef; } // Si aucune mesure n'est disponible, retourner null return null; } /** * Retourne pour chaque thématique la liste des tags attendus pour la complétion */ public static function getFollowUpCompletionTags(): array { return [ 'fire_hydrant' => ['ref', 'colour'], 'charging_station' => ['charging_station:output', 'capacity'], 'toilets' => ['wheelchair', 'access'], 'bus_stop' => ['shelter', 'network', 'highway', 'public_transport'], 'defibrillator' => ['indoor', 'access'], 'camera' => ['surveillance:type'], 'recycling' => ['recycling_type'], 'substation' => ['substation'], 'laboratory' => ['website', 'contact:website', 'name', 'phone'], 'school' => ['ref:UAI', 'isced:level', 'school:FR'], 'police' => ['phone', 'website'], 'healthcare' => ['name', 'contact:phone', 'phone', 'email', 'contact:email'], 'bicycle_parking' => ['capacity', 'covered'], 'advertising_board' => ['operator', 'contact:phone'], 'building' => ['building'], 'rnb' => ['building', 'ref:FR:RNB'], 'email' => ['name', 'phone'], 'bench' => ['material', 'backrest'], 'waste_basket' => ['waste', 'recycling_type'], 'street_lamp' => ['lamp_type', 'height'], 'drinking_water' => ['covered', 'ref'], 'tree' => ['species', 'leaf_type', 'leaf_cycle'], 'power_pole' => ['ref', 'material'], 'places' => ['name', 'address', 'opening_hours', 'website', 'phone', 'wheelchair', 'siret'], 'manhole' => ['manhole', 'location'], 'little_free_library' => ['amenity', 'operator'], 'playground' => ['playground', 'operator'], ]; } }