165 lines
5.1 KiB
JavaScript
165 lines
5.1 KiB
JavaScript
/**
|
|
* 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;
|
|
|
|
let filterCoordinates = true;
|
|
let filterDepartment = 91;
|
|
let filterZipCode = new RegExp(`^${filterDepartment}`);
|
|
|
|
|
|
let pointCounterMax = 10;
|
|
|
|
// let limitConversionToFirstPoint = false;
|
|
// if (limitConversionToFirstPoint) {
|
|
// pointCounterMax = 1
|
|
// }
|
|
|
|
// limitConversionToFirstPoint = false;
|
|
|
|
// let sourceFileChargemapJson = './chargemap_data/hurepoix.json'
|
|
let sourceFileIRVEGeoJson = './etalab_data/latest.json'
|
|
/**
|
|
* plan de conversion des clés du jeu de données vers les tags OSM
|
|
*/
|
|
let mappingConfigIRVE = {
|
|
contact_operateur: "email", // ici, on souhaite convertir la clé contact_operateur=bidule en email=bidule
|
|
amenity: "amenity", // conserver le tag de base
|
|
capacity: "capacity", // conserver le tag de base
|
|
nom_amenageur: "operator",
|
|
telephone_operateur: "phone",
|
|
nbre_pdc: "chargin_point:count",
|
|
id_station_itinerance: "ref:EU:EVSE",
|
|
id_station_local: "ref",
|
|
// consolidated_code_postal: "zipcode",
|
|
}
|
|
|
|
|
|
function debugLog(message) {
|
|
if (!show_debug) {
|
|
return
|
|
}
|
|
console.log('debug: ', ...message)
|
|
}
|
|
|
|
function convertDataForIRVE(sourceFilePath, mapping, pointCounterMax) {
|
|
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
|
|
|
|
console.log('listOfPoints.length', listOfPoints.length)
|
|
listOfPoints.forEach(featurePoint => {
|
|
|
|
let regextestresult = filterZipCode.test(featurePoint.properties.consolidated_code_postal)
|
|
|
|
// filter points depending on zipcode
|
|
if (filterCoordinates && regextestresult) {
|
|
|
|
console.log('featurePoint.properties.consolidated_code_postal', featurePoint.properties.consolidated_code_postal)
|
|
|
|
// limit results number of points
|
|
// if (pointcounter < pointCounterMax) {
|
|
|
|
console.log('add point')
|
|
|
|
debugLog('featurePoint', featurePoint)
|
|
|
|
let mappedPoint = mapElementFromConf(featurePoint, mapping)
|
|
debugLog('map one point', featurePoint, mappedPoint)
|
|
if (mappedPoint) {
|
|
convertedGeoJson.features.push(mappedPoint)
|
|
}
|
|
}
|
|
// }
|
|
pointcounter++;
|
|
|
|
})
|
|
// output new geojson
|
|
console.log('convertedGeoJson.features.length', convertedGeoJson.features.length)
|
|
// write file on disk
|
|
writeJsonFile('my_converted_data_set.json', JSON.stringify(convertedGeoJson, null, 2))
|
|
|
|
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)
|
|
|
|
debugLog('keys', mappingKeys, featurePointPropertiesKeys)
|
|
|
|
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) {
|
|
debugLog('found element', pointKeyName, '=>', mappingConfig[pointKeyName], 'value : ', featurePoint.properties[pointKeyName])
|
|
newProperties[mappingConfig[pointKeyName]] = featurePoint.properties[pointKeyName]
|
|
}
|
|
})
|
|
|
|
debugLog('basePoint', basePoint)
|
|
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!`);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
console.log('pointCounterMax', pointCounterMax)
|
|
convertDataForIRVE(sourceFileIRVEGeoJson, mappingConfigIRVE, pointCounterMax)
|
|
|
|
// convertDataFromChargemap(sourceFileChargemapJson, mappingConfigIRVE)
|