2023-07-29 14:43:01 +02:00
|
|
|
/**
|
|
|
|
* convertisseur de données de bornes de recharge électrique à partir de données Chargemap et open data Etalab
|
|
|
|
*/
|
|
|
|
import fs from 'node-fs';
|
|
|
|
|
|
|
|
|
|
|
|
let show_debug = 0;
|
2023-07-29 17:19:56 +02:00
|
|
|
|
2023-07-29 22:10:50 +02:00
|
|
|
|
|
|
|
// wip filter
|
|
|
|
let filterOnBoundingBox = true
|
|
|
|
|
|
|
|
let boundingBoxCoordinates ={
|
|
|
|
xMin: 1.91,
|
|
|
|
yMin: 48.7,
|
|
|
|
xMax: 2.38,
|
|
|
|
yMax: 48.4,
|
|
|
|
}
|
|
|
|
|
2023-07-29 17:19:56 +02:00
|
|
|
let filterCoordinates = true;
|
2023-07-29 22:10:50 +02:00
|
|
|
let enableFilterOnDepartment = false;
|
|
|
|
let filterDepartment = 91;
|
2023-07-29 17:19:56 +02:00
|
|
|
let filterZipCode = new RegExp(`^${filterDepartment}`);
|
2023-07-29 21:59:51 +02:00
|
|
|
let filterZipCodeAdresse = new RegExp(` ${filterDepartment}`);
|
|
|
|
let filteredName =''
|
2023-07-29 17:19:56 +02:00
|
|
|
|
2023-07-29 22:10:50 +02:00
|
|
|
if(enableFilterOnDepartment){
|
2023-07-29 21:59:51 +02:00
|
|
|
filteredName = '_filtered_zipcode_'+filterDepartment
|
|
|
|
}
|
2023-07-29 22:10:50 +02:00
|
|
|
if(filterOnBoundingBox){
|
|
|
|
filteredName = '_filtered_bbox_'+boundingBoxCoordinates.xMin+'-'+boundingBoxCoordinates.xMax+'_'+boundingBoxCoordinates.yMin+'-'+boundingBoxCoordinates.yMax
|
|
|
|
}
|
2023-07-29 21:59:51 +02:00
|
|
|
let pointCounterMax = 1000000;
|
2023-07-29 17:19:56 +02:00
|
|
|
|
2023-07-29 22:10:50 +02:00
|
|
|
let limitConversionToFirstPoint = false;
|
|
|
|
// limitConversionToFirstPoint = true;
|
|
|
|
if (limitConversionToFirstPoint) {
|
|
|
|
pointCounterMax = 1
|
|
|
|
}
|
2023-07-29 17:19:56 +02:00
|
|
|
|
|
|
|
|
2023-07-29 14:43:01 +02:00
|
|
|
// let sourceFileChargemapJson = './chargemap_data/hurepoix.json'
|
2023-07-29 18:47:20 +02:00
|
|
|
let sourceFileIRVEGeoJson = './etalab_data/latest.json'
|
2023-07-29 14:43:01 +02:00
|
|
|
/**
|
|
|
|
* plan de conversion des clés du jeu de données vers les tags OSM
|
2023-07-29 19:03:52 +02:00
|
|
|
* détail dans le tableau
|
|
|
|
* https://wiki.openstreetmap.org/wiki/France/data.gouv.fr/Bornes_de_Recharge_pour_V%C3%A9hicules_%C3%89lectriques
|
2023-07-29 14:43:01 +02:00
|
|
|
*/
|
|
|
|
let mappingConfigIRVE = {
|
2023-07-29 18:47:20 +02:00
|
|
|
amenity: "amenity", // conserver le tag de base
|
|
|
|
capacity: "capacity", // conserver le tag de base
|
2023-07-29 14:43:01 +02:00
|
|
|
nom_amenageur: "operator",
|
2023-07-29 19:03:52 +02:00
|
|
|
siren_amenageur: "owner:ref:FR:SIREN",
|
|
|
|
nom_operateur: "operator",
|
2023-07-29 14:43:01 +02:00
|
|
|
telephone_operateur: "phone",
|
2023-07-29 19:03:52 +02:00
|
|
|
contact_operateur: "email", // ici, on souhaite convertir la clé contact_operateur=bidule en email=bidule
|
|
|
|
nbre_pdc: "capacity",
|
2023-07-29 14:43:01 +02:00
|
|
|
id_station_itinerance: "ref:EU:EVSE",
|
|
|
|
id_station_local: "ref",
|
2023-07-29 19:03:52 +02:00
|
|
|
prise_type_ef: "socket:typee",
|
|
|
|
prise_type_2: "socket:type2",
|
|
|
|
prise_type_combo_ccs: "socket:type2_combo",
|
|
|
|
prise_type_chademo: "socket:chademo",
|
|
|
|
gratuit: "fee",
|
|
|
|
paiement_acte: "authentication:none",
|
|
|
|
paiement_cb: "payment:credit_cards",
|
|
|
|
reservation: "reservation",
|
|
|
|
accessibilite_pmr: "wheelchair",
|
|
|
|
date_mise_en_service: "start_date",
|
|
|
|
observations: "note",
|
|
|
|
date_maj: "source:date",
|
|
|
|
nom_station: "name",
|
|
|
|
nom_enseigne: "network",
|
|
|
|
cable_t2_attache: "socket:type2_cable",
|
|
|
|
|
|
|
|
// TODO gestion des types dont on doit convertir la valeur
|
|
|
|
// station_deux_roues =>
|
|
|
|
// ajout de trois tags:
|
|
|
|
// bicycle=yes
|
|
|
|
// scooter=yes
|
|
|
|
// motorcar=no
|
|
|
|
|
2023-07-29 17:24:34 +02:00
|
|
|
// consolidated_code_postal: "zipcode",
|
2023-07-29 14:43:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function debugLog(message) {
|
|
|
|
if (!show_debug) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log('debug: ', ...message)
|
|
|
|
}
|
|
|
|
|
2023-07-29 17:19:56 +02:00
|
|
|
function convertDataForIRVE(sourceFilePath, mapping, pointCounterMax) {
|
2023-07-29 14:43:01 +02:00
|
|
|
debugLog('convertDataFromChargemap from ', sourceFilePath);
|
|
|
|
|
|
|
|
let convertedGeoJson = {
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: []
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
|
|
|
|
let pointcounter = 0;
|
|
|
|
if (err) {
|
|
|
|
return debugLog(err);
|
|
|
|
}
|
|
|
|
let dataTransformed = JSON.parse(data);
|
|
|
|
// debug('data keys ', Object.keys(dataTransformed))
|
|
|
|
|
|
|
|
debugLog('debug: properties of point 0', dataTransformed.features[0])
|
|
|
|
if (dataTransformed.features) {
|
|
|
|
|
|
|
|
debugLog('data found, features:', dataTransformed.features.length)
|
|
|
|
|
|
|
|
// find interesting list of points to use
|
|
|
|
let listOfPoints = dataTransformed.features
|
|
|
|
// for each point from the data source, convert with the mapping
|
|
|
|
|
2023-07-29 17:19:56 +02:00
|
|
|
console.log('listOfPoints.length', listOfPoints.length)
|
2023-07-29 14:43:01 +02:00
|
|
|
listOfPoints.forEach(featurePoint => {
|
|
|
|
|
2023-07-29 22:10:50 +02:00
|
|
|
let regextestresult = true;
|
|
|
|
|
|
|
|
if(enableFilterOnDepartment){
|
|
|
|
console.log('filtre sur les départements activé')
|
|
|
|
regextestresult = (
|
|
|
|
filterZipCode.test(featurePoint.properties.consolidated_code_postal)
|
|
|
|
||
|
|
|
|
filterZipCodeAdresse.test(featurePoint.properties.adresse_station)
|
|
|
|
)
|
|
|
|
}else if(filterOnBoundingBox){
|
|
|
|
console.log('filtre sur les coordonnées activé')
|
|
|
|
|
|
|
|
let x = coordonneesXY[0]
|
|
|
|
let xMin = boundingBoxCoordinates.xMin
|
|
|
|
let xMax = boundingBoxCoordinates.xMax
|
|
|
|
let yMin = boundingBoyCoordinates.yMin
|
|
|
|
let yMax = boundingBoyCoordinates.yMay
|
|
|
|
|
|
|
|
let y = coordonneesXY[1]
|
|
|
|
regextestresult = (
|
|
|
|
filterZipCode.test(x >= xMin && x <= xMax)
|
|
|
|
&&
|
|
|
|
filterZipCode.test(y >= yMin && y <= yMax)
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-29 21:59:51 +02:00
|
|
|
|
2023-07-29 17:19:56 +02:00
|
|
|
|
|
|
|
// filter points depending on zipcode
|
|
|
|
if (filterCoordinates && regextestresult) {
|
|
|
|
|
2023-07-29 21:59:51 +02:00
|
|
|
debugLog('featurePoint.properties.consolidated_code_postal', featurePoint.properties.consolidated_code_postal)
|
2023-07-29 14:43:01 +02:00
|
|
|
|
2023-07-29 17:19:56 +02:00
|
|
|
// limit results number of points
|
|
|
|
// if (pointcounter < pointCounterMax) {
|
|
|
|
|
2023-07-29 21:59:51 +02:00
|
|
|
debugLog('add point')
|
2023-07-29 17:19:56 +02:00
|
|
|
|
|
|
|
debugLog('featurePoint', featurePoint)
|
2023-07-29 14:43:01 +02:00
|
|
|
|
|
|
|
let mappedPoint = mapElementFromConf(featurePoint, mapping)
|
|
|
|
debugLog('map one point', featurePoint, mappedPoint)
|
|
|
|
if (mappedPoint) {
|
|
|
|
convertedGeoJson.features.push(mappedPoint)
|
|
|
|
}
|
|
|
|
}
|
2023-07-29 17:19:56 +02:00
|
|
|
// }
|
2023-07-29 14:43:01 +02:00
|
|
|
pointcounter++;
|
|
|
|
|
|
|
|
})
|
|
|
|
// output new geojson
|
2023-07-29 17:19:56 +02:00
|
|
|
console.log('convertedGeoJson.features.length', convertedGeoJson.features.length)
|
2023-07-29 14:43:01 +02:00
|
|
|
// write file on disk
|
2023-07-29 21:59:51 +02:00
|
|
|
let fileNameToWrite = 'my_converted_data_set'+filteredName+'.json';
|
|
|
|
console.log('write file ',fileNameToWrite )
|
|
|
|
writeJsonFile( fileNameToWrite, JSON.stringify(convertedGeoJson, null, 2))
|
2023-07-29 14:43:01 +02:00
|
|
|
|
|
|
|
debugLog('mapped output:', convertedGeoJson.features)
|
|
|
|
|
|
|
|
return convertedGeoJson;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retuns the converted element from mapping config if present, null otherwise
|
|
|
|
*/
|
|
|
|
|
|
|
|
function mapElementFromConf(featurePoint, mappingConfig) {
|
|
|
|
let mappingKeys = Object.keys(mappingConfig)
|
|
|
|
let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
|
|
|
|
|
2023-07-29 17:19:56 +02:00
|
|
|
debugLog('keys', mappingKeys, featurePointPropertiesKeys)
|
2023-07-29 14:43:01 +02:00
|
|
|
|
|
|
|
let newProperties = {}
|
|
|
|
|
|
|
|
// reinit properties of current point
|
|
|
|
let basePoint = Object.create(featurePoint)
|
|
|
|
basePoint.type = featurePoint.type
|
|
|
|
basePoint.geometry = featurePoint.geometry
|
|
|
|
basePoint.properties = newProperties
|
|
|
|
|
|
|
|
// apply new properties if found in mapping config
|
|
|
|
featurePointPropertiesKeys.forEach(pointKeyName => {
|
|
|
|
|
|
|
|
if (mappingKeys.indexOf(pointKeyName) !== -1) {
|
2023-07-29 17:19:56 +02:00
|
|
|
debugLog('found element', pointKeyName, '=>', mappingConfig[pointKeyName], 'value : ', featurePoint.properties[pointKeyName])
|
2023-07-29 14:43:01 +02:00
|
|
|
newProperties[mappingConfig[pointKeyName]] = featurePoint.properties[pointKeyName]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-07-29 17:19:56 +02:00
|
|
|
debugLog('basePoint', basePoint)
|
2023-07-29 14:43:01 +02:00
|
|
|
return basePoint
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function writeJsonFile(fileName, fileContent) {
|
|
|
|
debugLog('write file ', fileName);
|
|
|
|
|
|
|
|
return fs.writeFile(
|
|
|
|
`./output/${fileName}`,
|
|
|
|
fileContent,
|
|
|
|
"utf8",
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
debugLog(`Error writing file: ${err}`);
|
|
|
|
} else {
|
|
|
|
debugLog(`File ${fileName} is written successfully!`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-29 17:19:56 +02:00
|
|
|
|
|
|
|
console.log('pointCounterMax', pointCounterMax)
|
|
|
|
convertDataForIRVE(sourceFileIRVEGeoJson, mappingConfigIRVE, pointCounterMax)
|
2023-07-29 14:43:01 +02:00
|
|
|
|
2023-07-29 21:59:51 +02:00
|
|
|
// convertDataFromChargemap(sourceFileChargemapJson, mappingConfigIRVE)
|