osm-commerces/templates/admin/stats.html.twig

399 lines
14 KiB
Twig
Raw Normal View History

{% extends 'base.html.twig' %}
{% block title %}{{ 'display.stats'|trans }}- {{ stats.zone }}
{{ stats.name }} {% endblock %}
2025-06-01 19:52:56 +02:00
{% block stylesheets %}
{{ parent() }}
2025-06-03 14:58:23 +02:00
<link href='{{ asset('js/maplibre/maplibre-gl.css') }}' rel='stylesheet' />
<style>
.completion-circle {
fill-opacity: 0.6;
stroke: #fff;
stroke-width: 3;
}
2025-06-06 12:17:03 +02:00
#distribution_completion {
height: 300px;
margin: 20px 0;
}
</style>
2025-06-01 19:52:56 +02:00
{% endblock %}
{% block body %}
<div class="container">
2025-05-29 16:50:25 +02:00
<div class="mt-4 p-4">
<div class="row">
<div class="col-md-6 col-12">
<h1 class="title">{{ 'display.stats'|trans }} - {{ stats.zone }}
{{ stats.name }} - {{ stats.completionPercent }}% complété</h1>
</div>
<div class="col-md-6 col-12">
<a href="{{ path('app_admin_labourer', {'zip_code': stats.zone}) }}" class="btn btn-primary" id="labourer">Labourer les mises à jour</a>
<button id="openInJOSM" class="btn btn-secondary ms-2">
<i class="bi bi-map"></i> Ouvrir dans JOSM
</button>
</div>
</div>
2025-06-03 12:51:20 +02:00
<div class="row">
<div class="col-md-3 col-12">
{{ stats.getCompletionPercent() }} % complété sur les critères donnés.
</div>
<div class="col-md-3 col-12">
2025-06-04 00:46:46 +02:00
<i class="bi bi-building"></i> {{ stats.places | length}} commerces dans la zone.
2025-06-03 12:51:20 +02:00
</div>
<div class="col-md-3 col-12">
<i class="bi bi-clock"></i> {{ stats.getAvecHoraires() }} commerces avec horaires.
</div>
<div class="col-md-3 col-12">
<i class="bi bi-map"></i> {{ stats.getAvecAdresse() }} commerces avec adresse.
</div>
<div class="col-md-3 col-12">
<i class="bi bi-globe"></i> {{ stats.getAvecSite() }} commerces avec site web renseigné.
</div>
<div class="col-md-3 col-12">
<i class="bi bi-arrow-up-right"></i>
{{ stats.getAvecAccessibilite() }} commerces avec accessibilité PMR renseignée.
</div>
<div class="col-md-3 col-12">
<i class="bi bi-chat-dots"></i> {{ stats.getAvecNote() }} commerces avec note renseignée.
</div>
</div>
<div id="maploader">
<div class="spinner-border" role="status">
<i class="bi bi-load bi-spin"></i>
<span class="visually-hidden">Chargement de la carte...</span>
</div>
</div>
2025-06-04 00:46:46 +02:00
<div id="map" class="mt-4" style="height: 400px;"></div>
2025-06-03 12:51:20 +02:00
</div>
<div class="card mt-4">
2025-06-06 12:17:03 +02:00
<div id="distribution_completion" class="mt-4 mb-4"></div>
<div class="row">
<div class="col-md-6 col-12">
<h1 class="card-title p-4">Tableau des {{ stats.places |length }} lieux</h1>
</div>
<div class="col-md-6 col-12">
2025-06-04 00:46:46 +02:00
<a class="btn btn-primary pull-right mt-4" href="{{ path('app_admin_export_csv', {'zip_code': stats.zone}) }}" class="btn btn-primary">
<i class="bi bi-filetype-csv"></i>
Exporter en CSV
</a>
</div>
</div>
2025-06-06 13:26:44 +02:00
<table class="table table-bordered table-striped table-hover table-responsive js-sort-table">
2025-06-03 11:37:27 +02:00
{% include 'admin/stats/table-head.html.twig' %}
<tbody>
{% for commerce in stats.places %}
2025-06-03 11:37:27 +02:00
{% include 'admin/stats/row.html.twig' %}
{% endfor %}
</tbody>
</table>
</div>
2025-06-03 12:51:20 +02:00
<h2>requête overpass</h2>
<pre class="p-4 bg-light">
2025-06-03 12:51:20 +02:00
{{query_places|raw}}
</pre>
</div>
</div>
2025-06-06 13:26:44 +02:00
<!-- Bouton caché pour JOSM -->
<a id="josmButton" style="display: none;"></a>
2025-06-03 14:58:23 +02:00
<script src='{{ asset('js/maplibre/maplibre-gl.js') }}'></script>
<script src='{{ asset('js/turf/turf.min.js') }}'></script>
2025-06-06 12:29:53 +02:00
<script src="{{ asset('js/chartjs/chart.umd.js') }}"></script>
2025-06-06 13:26:44 +02:00
<script src="{{ asset('js/app.js') }}"></script>
2025-06-01 19:52:56 +02:00
<script>
2025-06-03 12:51:20 +02:00
const request = `{{query_places|raw}}`;
const zip_code = `{{stats.zone}}`;
let mapElements = [];
2025-06-06 13:26:44 +02:00
let map_is_loaded = false;
let features = [];
2025-06-06 12:29:53 +02:00
function getCompletionColor(completion) {
if (completion === undefined || completion === null) {
return '#808080'; // Gris pour pas d'information
}
// Convertir le pourcentage en couleur verte (0% = blanc, 100% = vert foncé)
const intensity = Math.floor((completion / 100) * 255);
return `rgb(0, ${intensity}, 0)`;
}
function calculateCompletion(element) {
let completed = 0;
let total = 0;
// Critères à vérifier
const criteria = [
'name',
'addr:street',
'addr:housenumber',
'addr:postcode',
'addr:city',
'opening_hours',
'website',
'wheelchair',
'phone'
];
criteria.forEach(criterion => {
if (element.tags && element.tags[criterion]) {
completed++;
}
total++;
});
return total > 0 ? (completed / total) * 100 : 0;
}
function createPopupContent(element) {
console.log("createPopupContent",element);
let tagstable = '<table class="table table-bordered"><tr><th>Clé</th><th>Valeur</th></tr>';
if (element.tags) {
for (const tag in element.tags) {
tagstable += `<tr><td>${tag}</td><td>${element.tags[tag]}</td></tr>`;
}
}
tagstable += '</table>';
return `
<a href="/admin/placeType/${element.type}/${element.id}">
<h3>${element.tags?.name || 'Sans nom'}</h3>
</a>
<br>
<a href="https://openstreetmap.org/${element.type}/${element.id}" target="_blank">OSM</a>
${tagstable}
`;
}
2025-06-06 12:17:03 +02:00
function calculateCompletionDistribution(elements) {
// Créer des buckets de 10% (0-10%, 10-20%, etc.)
const buckets = Array(11).fill(0);
elements.forEach(element => {
const completion = calculateCompletion(element);
const bucketIndex = Math.floor(completion / 10);
buckets[bucketIndex]++;
});
return buckets;
}
function createCompletionChart(elements) {
const distribution = calculateCompletionDistribution(elements);
const ctx = document.createElement('canvas');
document.getElementById('distribution_completion').appendChild(ctx);
new Chart(ctx, {
type: 'line',
data: {
labels: ['0-10%', '10-20%', '20-30%', '30-40%', '40-50%', '50-60%', '60-70%', '70-80%', '80-90%', '90-100%'],
datasets: [{
label: 'Nombre de commerces',
data: distribution,
backgroundColor: 'rgba(0, 128, 0, 0.1)',
borderColor: 'rgba(0, 128, 0, 1)',
borderWidth: 2,
tension: 0.4,
fill: true,
pointBackgroundColor: 'rgba(0, 128, 0, 1)',
pointBorderColor: '#fff',
pointBorderWidth: 2,
pointRadius: 4,
pointHoverRadius: 6
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Nombre de commerces'
}
},
x: {
title: {
display: true,
text: 'Taux de complétion'
}
}
},
plugins: {
title: {
display: true,
text: 'Distribution du taux de complétion des commerces'
}
}
}
});
}
2025-06-06 13:26:44 +02:00
async function loadPlaces(map) {
map_is_loaded = false;
try {
const response = await fetch('https://overpass-api.de/api/interpreter', {
method: 'POST',
body: request
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log('Lieux chargés:', data.elements);
mapElements = data.elements;
map_is_loaded = true;
2025-06-06 13:26:44 +02:00
// Créer le graphique de distribution
createCompletionChart(data.elements);
2025-06-06 12:29:53 +02:00
2025-06-06 13:26:44 +02:00
// Mettre à jour les cercles
features = [];
data.elements.forEach(element => {
const lat = element.lat || (element.center && element.center.lat);
const lon = element.lon || (element.center && element.center.lon);
2025-06-03 12:51:20 +02:00
2025-06-06 13:26:44 +02:00
if (lat && lon) {
const completion = calculateCompletion(element);
const color = getCompletionColor(completion);
// Créer un cercle de 20 mètres de rayon (plus petit)
const circle = turf.circle([lon, lat], 0.04, { steps: 64, units: 'kilometers' });
circle.properties = {
color: color,
completion: completion,
name: element.tags?.name || 'Sans nom',
popupContent: createPopupContent(element),
center: [lon, lat] // Stocker le centre comme un tableau [lon, lat]
};
features.push(circle);
}
});
// Mettre à jour la source des cercles
map.getSource('completion-circles').setData({
'type': 'FeatureCollection',
'features': features
});
// Calculer les bounds pour tous les points
const points = features.map(f => f.properties.center);
if (points.length > 0) {
const bounds = new maplibregl.LngLatBounds(points[0], points[0]);
points.forEach(point => bounds.extend(point));
// Vérifier que les bounds sont valides avant de les utiliser
if (bounds?._sw && bounds?._ne) {
map.fitBounds(bounds, {
padding: 50,
maxZoom: 15
2025-06-03 12:51:20 +02:00
});
2025-06-06 13:26:44 +02:00
} else {
console.warn('Bounds invalides, utilisation des coordonnées par défaut');
2025-06-03 12:51:20 +02:00
}
}
2025-06-06 13:26:44 +02:00
document.getElementById('maploader').classList.add('d-none');
} catch (error) {
console.error('Erreur lors du chargement des lieux:', error);
document.getElementById('maploader').classList.add('d-none');
}
}
function openInJOSM() {
const selectedElements = document.querySelectorAll('input[type="checkbox"]:checked');
if (selectedElements.length === 0) {
alert('Veuillez sélectionner au moins un élément');
return;
}
2025-06-06 13:26:44 +02:00
const osmElements = Array.from(selectedElements).map(checkbox => {
return JSON.parse(checkbox.value);
});
2025-06-06 12:17:03 +02:00
2025-06-06 13:26:44 +02:00
window.openInJOSM(map, map_is_loaded, osmElements);
}
document.addEventListener('DOMContentLoaded', function() {
console.log('DOMContentLoaded');
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v2/style.json?key={{ maptiler_token }}',
center: [2.3488, 48.8534],
zoom: 12
});
document.getElementById('openInJOSM').addEventListener('click', openInJOSM);
2025-06-06 13:26:44 +02:00
map.on('load', () => {
// Créer une source pour les cercles
map.addSource('completion-circles', {
'type': 'geojson',
'data': {
'type': 'FeatureCollection',
'features': []
}
});
// Ajouter une couche pour les cercles
map.addLayer({
'id': 'completion-circles',
'type': 'fill',
'source': 'completion-circles',
'paint': {
'fill-color': ['get', 'color'],
'fill-opacity': 0.6,
'fill-outline-color': '#fff'
}
});
// Ajouter une couche pour la bordure
map.addLayer({
'id': 'completion-circles-outline',
'type': 'line',
'source': 'completion-circles',
'paint': {
2025-06-06 11:54:39 +02:00
'line-color': 'rgba(100,100,100,0.5)',
'line-width': 2
}
});
// Ajouter les popups sur les cercles
map.on('click', 'completion-circles', (e) => {
const properties = e.features[0].properties;
new maplibregl.Popup()
.setLngLat(e.lngLat)
.setHTML(properties.popupContent)
.addTo(map);
});
// Changer le curseur au survol des cercles
map.on('mouseenter', 'completion-circles', () => {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'completion-circles', () => {
map.getCanvas().style.cursor = '';
});
2025-06-06 13:26:44 +02:00
// Charger les lieux
loadPlaces(map);
2025-06-01 19:52:56 +02:00
});
2025-05-28 16:24:34 +02:00
2025-06-01 19:52:56 +02:00
sortTable();
2025-06-06 13:26:44 +02:00
colorHeadingTable();
2025-06-01 19:52:56 +02:00
});
</script>
{% endblock %}