mirror of
https://forge.chapril.org/tykayn/libre-charge-map
synced 2025-06-20 01:34:43 +02:00
226 lines
No EOL
6.8 KiB
JavaScript
226 lines
No EOL
6.8 KiB
JavaScript
import fs from 'fs';
|
|
import fetch from 'node-fetch';
|
|
import * as cheerio from 'cheerio';
|
|
import lcm_config from './js/lcm_config.js';
|
|
|
|
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;
|
|
let validationErrors = {}
|
|
|
|
// Initialiser le compteur pour chaque tag
|
|
let tagsCountOSM = {};
|
|
lcm_config.tags_to_display_in_popup.forEach(tag => {
|
|
tagsCountOSM[tag] = 0;
|
|
});
|
|
|
|
data.elements.forEach(element => {
|
|
if (element.type === 'node' && element.tags && element.tags.capacity) {
|
|
let capa = parseInt(element.tags.capacity)
|
|
if (isNaN(capa)) {
|
|
capa = 1;
|
|
}
|
|
countPointsDeChargeOSM += capa;
|
|
}
|
|
// Compter les tags présents
|
|
if (element.tags) {
|
|
|
|
let tags = Object.keys(element.tags)
|
|
|
|
lcm_config.tags_to_display_in_popup.forEach(tag => {
|
|
if (element.tags[tag] !== undefined && element.tags[tag] !== null && element.tags[tag] !== "") {
|
|
tagsCountOSM[tag]++;
|
|
}
|
|
});
|
|
|
|
}
|
|
});
|
|
|
|
stats = {
|
|
...stats,
|
|
countStationsOSM: nombreFeaturesInOSM,
|
|
countPointsDeChargeOSM: countPointsDeChargeOSM,
|
|
tagsCountOSM,
|
|
validationErrors
|
|
};
|
|
} 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();
|
|
})(); |