mirror of
https://forge.chapril.org/tykayn/libre-charge-map
synced 2025-10-09 17:02:46 +02:00
add stats page
This commit is contained in:
parent
c23d4bd404
commit
fca7661ad8
12 changed files with 767 additions and 6 deletions
201
make_stats.js
Normal file
201
make_stats.js
Normal file
|
@ -0,0 +1,201 @@
|
|||
|
||||
import fs from 'fs';
|
||||
import fetch from 'node-fetch';
|
||||
import * as cheerio from 'cheerio';
|
||||
|
||||
let stats = {};
|
||||
|
||||
// Fonction pour télécharger un fichier
|
||||
|
||||
async function downloadOpenDataEtalab(url, outputPath) {
|
||||
|
||||
console.log('Téléchargement des fichiers...');
|
||||
|
||||
try {
|
||||
// Télécharger le fichier Etalab
|
||||
const response = await fetch(url);
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
fs.writeFileSync(outputPath, buffer);
|
||||
console.log('Fichier Etalab téléchargé avec succès');
|
||||
} catch (error) {
|
||||
console.error('Erreur lors du téléchargement:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Fonction pour compter les features dans un fichier GeoJSON
|
||||
async function compterFeaturesInOpenDataEtalab() {
|
||||
|
||||
|
||||
try {
|
||||
// Lire le fichier GeoJSON
|
||||
const donnees = fs.readFileSync('js/opendata.json', 'utf8');
|
||||
const geojson = JSON.parse(donnees);
|
||||
|
||||
// Vérifier que c'est bien un GeoJSON avec des features
|
||||
if (!geojson.features) {
|
||||
throw new Error('Le fichier ne contient pas de features GeoJSON valides');
|
||||
}
|
||||
|
||||
// Compter le nombre de features
|
||||
const nombreFeatures = geojson.features.length;
|
||||
|
||||
let count_capacity = 0;
|
||||
let count_nb_pdc_fuckedup = 0;
|
||||
let count_nb_pdc_ok = 0;
|
||||
let unique_id_station_itinerance = []
|
||||
let count_id_station_itinerance_duplicate = 0;
|
||||
geojson.features.forEach(feature => {
|
||||
if (feature.properties.id_station_itinerance) {
|
||||
if (!unique_id_station_itinerance.includes(feature.properties.id_station_itinerance)) {
|
||||
unique_id_station_itinerance.push(feature.properties.id_station_itinerance)
|
||||
} else {
|
||||
count_id_station_itinerance_duplicate++
|
||||
}
|
||||
}
|
||||
if (feature.properties.nbre_pdc) {
|
||||
let nbre_pdc = parseInt(feature.properties.nbre_pdc);
|
||||
if (isNaN(nbre_pdc)) {
|
||||
nbre_pdc = 1;
|
||||
count_nb_pdc_fuckedup++
|
||||
} else {
|
||||
count_nb_pdc_ok++
|
||||
}
|
||||
count_capacity += nbre_pdc;
|
||||
}
|
||||
});
|
||||
// Créer l'objet de statistiques
|
||||
stats = {
|
||||
...stats,
|
||||
countStationsOpenData: nombreFeatures,
|
||||
countPointsDeChargeOpenData: count_capacity,
|
||||
count_nb_pdc_fuckedupOpenData: count_nb_pdc_fuckedup,
|
||||
count_nb_pdc_okOpenData: count_nb_pdc_ok,
|
||||
count_id_station_itinerance_duplicate: count_id_station_itinerance_duplicate,
|
||||
count_unique_id_station_itinerance: unique_id_station_itinerance.length,
|
||||
dateGeneration: new Date().toISOString()
|
||||
};
|
||||
|
||||
|
||||
} catch (erreur) {
|
||||
console.error('Erreur lors du traitement du fichier js/opendata.json:', erreur.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Nouvelle fonction pour télécharger les données Overpass et les sauvegarder
|
||||
async function downloadOverpassData(outputPath) {
|
||||
const overpassUrl = 'https://overpass-api.de/api/interpreter';
|
||||
const query = `
|
||||
[out:json][timeout:200];
|
||||
area["ISO3166-1"="FR"][admin_level=2]->.france;
|
||||
(
|
||||
nwr(area.france)["amenity"="charging_station"];
|
||||
);
|
||||
out center;
|
||||
`;
|
||||
const response = await fetch(overpassUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: 'data=' + encodeURIComponent(query)
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Erreur lors de la requête Overpass : ${response.status} ${response.statusText}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2));
|
||||
console.log('Fichier Overpass téléchargé avec succès');
|
||||
}
|
||||
|
||||
async function getAvereData() {
|
||||
const url = 'https://www.avere-france.org/publications/?publication-type%5B%5D=barometres-recharge';
|
||||
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Erreur lors du téléchargement de la page AVERE : ${response.status} ${response.statusText}`);
|
||||
}
|
||||
const html = await response.text();
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
// Chercher le texte du type "[Baromètre] 163 656 points de recharge ouverts au public fin mars 2025"
|
||||
let count = 0;
|
||||
$('.posts-list .card .card-title').each((i, el) => {
|
||||
const text = $(el).text();
|
||||
const match = text.match(/(\d[\d\s]+) points de recharge/);
|
||||
if (match) {
|
||||
// On prend le premier nombre trouvé (le plus récent)
|
||||
count = parseInt(match[1].replace(/\s/g, ''), 10);
|
||||
return false; // break
|
||||
}
|
||||
});
|
||||
|
||||
stats = {
|
||||
...stats,
|
||||
countStationsAVERE: count
|
||||
};
|
||||
console.log(`Nombre de points de recharge AVERE : ${count}`);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération des données AVERE:', error.message);
|
||||
stats = {
|
||||
...stats,
|
||||
countStationsAVERE: 0
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Nouvelle fonction pour compter les features dans un fichier OSM téléchargé
|
||||
function compterFeaturesInOpenStreetMapFromFile() {
|
||||
const inputPath = 'js/openstreetmap.json';
|
||||
try {
|
||||
const donnees = fs.readFileSync(inputPath, 'utf8');
|
||||
const data = JSON.parse(donnees);
|
||||
const nombreFeaturesInOSM = data.elements.length;
|
||||
|
||||
let countPointsDeChargeOSM = 0;
|
||||
data.elements.forEach(element => {
|
||||
if (element.type === 'node' && element.tags.capacity) {
|
||||
let capa = parseInt(element.tags.capacity)
|
||||
if (isNaN(capa)) {
|
||||
capa = 1;
|
||||
}
|
||||
countPointsDeChargeOSM += capa;
|
||||
}
|
||||
});
|
||||
|
||||
stats = {
|
||||
...stats,
|
||||
countStationsOSM: nombreFeaturesInOSM,
|
||||
countPointsDeChargeOSM: countPointsDeChargeOSM
|
||||
};
|
||||
} catch (erreur) {
|
||||
console.error('Erreur lors du traitement du fichier OSM:', erreur.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function saveStatsFile() {
|
||||
// Écrire les stats dans un fichier JSON
|
||||
fs.writeFileSync('js/stats.json', JSON.stringify(stats, null, 2));
|
||||
|
||||
console.log(`Statistiques générées avec succès:`, stats);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const shouldDownload = process.argv.includes('--wget');
|
||||
|
||||
if (shouldDownload) {
|
||||
console.log('on télécharge l\'open data etalab')
|
||||
await downloadOpenDataEtalab('https://www.data.gouv.fr/fr/datasets/r/7eee8f09-5d1b-4f48-a304-5e99e8da1e26', 'js/opendata.json');
|
||||
await downloadOverpassData('js/openstreetmap.json');
|
||||
}
|
||||
|
||||
compterFeaturesInOpenStreetMapFromFile();
|
||||
await compterFeaturesInOpenDataEtalab();
|
||||
await getAvereData();
|
||||
|
||||
saveStatsFile();
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue