fix simple config

This commit is contained in:
Tykayn 2023-07-30 19:18:17 +02:00 committed by tykayn
parent 4c6dfbd198
commit 431ba2e558
7 changed files with 7393 additions and 4971 deletions

View File

@ -3,43 +3,43 @@
*/ */
import fs from 'node-fs' import fs from 'node-fs'
import minimist from 'minimist' import minimist from 'minimist'
import mappingConfigIRVE from './mappings/configIRVE.mjs' import mappingConfigIRVE from './mappings/converters/configIRVE.mjs'
import mapping_engine from "./mappings/engine.mjs"; import mappingConfigIRVE_simple from './mappings/converters/mappingConfigIRVE_simple.mjs'
import custom_utils from "./mappings/utils.mjs"; import mapping_engine from './mappings/engine.mjs'
import custom_utils from './mappings/utils.mjs'
const {debugLog} = custom_utils; const { debugLog } = custom_utils
const { isBooleanKey } = custom_utils
// let debugLog = custom_utils.debugLog // let debugLog = custom_utils.debugLog
let use_mappping_engine = false; let use_mappping_engine = false
let Mapping_engine = new mapping_engine(mappingConfigIRVE); let Mapping_engine = new mapping_engine(mappingConfigIRVE)
let mini_arguments = minimist(process.argv.slice(2)) let mini_arguments = minimist(process.argv.slice(2))
// let sourceFileChargemapJson = './chargemap_data/hurepoix.json' // let sourceFileChargemapJson = './chargemap_data/hurepoix.json'
let sourceFilePathGeoJson = './etalab_data/latest.json' let sourceFilePathGeoJson = './etalab_data/latest.json'
// wip filter // wip filter
let filterOnBoundingBox = true let filterOnBoundingBox = true
filterOnBoundingBox = false filterOnBoundingBox = false
let boundingBoxCoordinates = { let boundingBoxCoordinates = {
xMin: 1.91, xMin: 1.91,
xMax: 2.38, xMax: 2.38,
yMin: 48.7, yMin: 48.7,
yMax: 48.4, yMax: 48.4,
} }
let filterCoordinates = true let filterCoordinates = true
let enable_filter_on_department = true let enable_filter_on_department = true
let filterDepartment = 91 let filterDepartment = 91
if (mini_arguments['department']) { if (mini_arguments['department']) {
filterDepartment = mini_arguments['department'] filterDepartment = mini_arguments['department']
} }
if (mini_arguments['source']) { if (mini_arguments['source']) {
sourceFilePathGeoJson = mini_arguments['source'] sourceFilePathGeoJson = mini_arguments['source']
} }
let filterZipCode = new RegExp(`^${filterDepartment}`) let filterZipCode = new RegExp(`^${filterDepartment}`)
@ -47,38 +47,26 @@ let filterZipCodeAdresse = new RegExp(` ${filterDepartment}`)
let filteredName = '' let filteredName = ''
if (enable_filter_on_department) { if (enable_filter_on_department) {
filteredName = '_filtered_zipcode_' + filterDepartment filteredName = '_filtered_zipcode_' + filterDepartment
} else if (filterOnBoundingBox) { } else if (filterOnBoundingBox) {
filteredName = '_filtered_bbox_' + boundingBoxCoordinates.xMin + '-' + boundingBoxCoordinates.xMax + '_' + boundingBoxCoordinates.yMin + '-' + boundingBoxCoordinates.yMax filteredName = '_filtered_bbox_' + boundingBoxCoordinates.xMin + '-' + boundingBoxCoordinates.xMax + '_' + boundingBoxCoordinates.yMin + '-' + boundingBoxCoordinates.yMax
} }
let pointCounterMax = 1000000 let pointCounterMax = 1000000
let limitConversionToFirstPoint = false let limitConversionToFirstPoint = false
// limitConversionToFirstPoint = true; limitConversionToFirstPoint = true
if (limitConversionToFirstPoint) { if (limitConversionToFirstPoint) {
pointCounterMax = 1 pointCounterMax = 1
} }
let defaultPropertiesOfPoint = { let defaultPropertiesOfPoint = {
'amenity': 'charging_station' 'amenity': 'charging_station'
} }
let listOfBooleanKeys = Object.keys({
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',
cable_t2_attache: 'socket:type2_cable',
})
let converted_geo_json = { let converted_geo_json = {
type: 'FeatureCollection', type: 'FeatureCollection',
features: [] features: []
} }
/** /**
* *
* @param sourceFilePath * @param sourceFilePath
@ -86,158 +74,166 @@ let converted_geo_json = {
* @param pointCounterMax * @param pointCounterMax
* @param boundingBoxCoordinates * @param boundingBoxCoordinates
*/ */
function convertDataForIRVE(sourceFilePath, mapping, pointCounterMax, boundingBoxCoordinates) { function convertDataForIRVE (sourceFilePath, mapping, pointCounterMax, boundingBoxCoordinates) {
debugLog('convertDataFromChargemap from ', sourceFilePath) debugLog('convertDataFromChargemap from ', sourceFilePath)
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
let point_counter = 0
if (err) {
return debugLog(err)
}
let data_transformed = JSON.parse(data)
// debug('data keys ', Object.keys(dataTransformed))
fs.readFile(sourceFilePath, 'utf8', function (err, data) { debugLog('debug: properties of point 0', data_transformed.features[0])
let point_counter = 0 if (data_transformed.features) {
if (err) {
return debugLog(err)
}
let data_transformed = JSON.parse(data)
// debug('data keys ', Object.keys(dataTransformed))
debugLog('debug: properties of point 0', data_transformed.features[0]) debugLog('data found, features:', data_transformed.features.length)
if (data_transformed.features) {
debugLog('data found, features:', data_transformed.features.length) // find interesting list of points to use
let list_of_points = data_transformed.features
// for each point from the data source, convert with the mapping
// find interesting list of points to use console.log('listOfPoints.length', list_of_points.length)
let list_of_points = data_transformed.features list_of_points.forEach(feature_point => {
// for each point from the data source, convert with the mapping
console.log('listOfPoints.length', list_of_points.length) let regex_filter_test_result = true
list_of_points.forEach(feature_point => {
let regex_filter_test_result = true if (enable_filter_on_department) {
debugLog('filtre sur les départements activé')
regex_filter_test_result = (
filterZipCode.test(feature_point.properties.consolidated_code_postal)
||
filterZipCodeAdresse.test(feature_point.properties.adresse_station)
)
} else if (filterOnBoundingBox) {
debugLog('filtre sur les coordonnées activé')
if (enable_filter_on_department) { let x = feature_point.properties.coordonneesXY[0]
console.log('filtre sur les départements activé') let xMin = boundingBoxCoordinates.xMin
regex_filter_test_result = ( let xMax = boundingBoxCoordinates.xMax
filterZipCode.test(feature_point.properties.consolidated_code_postal) let yMin = boundingBoxCoordinates.yMin
|| let yMax = boundingBoxCoordinates.yMax
filterZipCodeAdresse.test(feature_point.properties.adresse_station)
)
} else if (filterOnBoundingBox) {
console.log('filtre sur les coordonnées activé')
let x = feature_point.properties.coordonneesXY[0] let y = feature_point.properties.coordonneesXY[1]
let xMin = boundingBoxCoordinates.xMin regex_filter_test_result = (
let xMax = boundingBoxCoordinates.xMax (x >= xMin && x <= xMax)
let yMin = boundingBoxCoordinates.yMin &&
let yMax = boundingBoxCoordinates.yMax (y >= yMin && y <= yMax)
)
let y = feature_point.properties.coordonneesXY[1] }
regex_filter_test_result = (
(x >= xMin && x <= xMax)
&&
(y >= yMin && y <= yMax)
)
} // filter points depending on zipcode
if (filterCoordinates && regex_filter_test_result) {
// filter points depending on zipcode debugLog('featurePoint.properties.consolidated_code_postal', feature_point.properties.consolidated_code_postal)
if (filterCoordinates && regex_filter_test_result) {
debugLog('featurePoint.properties.consolidated_code_postal', feature_point.properties.consolidated_code_postal) // limit results number of points
// if (pointcounter < pointCounterMax) {
// limit results number of points debugLog('add point')
// if (pointcounter < pointCounterMax) {
debugLog('add point') debugLog('featurePoint', feature_point)
debugLog('featurePoint', feature_point) let mapped_point = {}
let mapped_point = {}; if (use_mappping_engine) {
Mapping_engine.setConfig(mapping)
mapped_point = Mapping_engine.mapFeaturePoint(feature_point)
} else {
mapped_point = mapElementFromConfSimple(feature_point, mapping)
}
debugLog('map one point', feature_point, mapped_point)
if (mapped_point) {
converted_geo_json.features.push(mapped_point)
}
}
// }
point_counter++
if (use_mappping_engine) { })
Mapping_engine.setConfig(mapping) // output new geojson
mapped_point = Mapping_engine.mapFeaturePoint(feature_point) console.log('convertedGeoJson.features.length', converted_geo_json.features.length)
} else { // write file on disk
mapped_point = mapElementFromConfSimple(feature_point, mapping) let fileNameToWrite = 'my_converted_data_set' + filteredName + '.json'
} console.log('write file ', fileNameToWrite)
debugLog('map one point', feature_point, mapped_point) writeJsonFile(fileNameToWrite, JSON.stringify(converted_geo_json, null, 2))
if (mapped_point) {
converted_geo_json.features.push(mapped_point)
}
}
// }
point_counter++
}) debugLog('mapped output:', converted_geo_json.features)
// output new geojson
console.log('convertedGeoJson.features.length', converted_geo_json.features.length)
// write file on disk
let fileNameToWrite = 'my_converted_data_set' + filteredName + '.json'
console.log('write file ', fileNameToWrite)
writeJsonFile(fileNameToWrite, JSON.stringify(converted_geo_json, null, 2))
debugLog('mapped output:', converted_geo_json.features) return converted_geo_json
}
return converted_geo_json })
}
})
} }
/** /**
* retuns the converted element from mapping config if present, null otherwise * retuns the converted element from mapping config if present, null otherwise
*/ */
function mapElementFromConfSimple(featurePoint, mappingConfig) function mapElementFromConfSimple (featurePoint, mappingConfig) {
{ let mappingKeys = Object.keys(mappingConfig)
let mappingKeys = Object.keys(mappingConfig) let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
debugLog('keys', mappingKeys, featurePointPropertiesKeys) debugLog('keys', mappingKeys, featurePointPropertiesKeys)
let newProperties = defaultPropertiesOfPoint let newProperties = defaultPropertiesOfPoint
// reinit properties of current point // reinit properties of current point
let basePoint = Object.create(featurePoint) let basePoint = Object.create(featurePoint)
basePoint.type = featurePoint.type basePoint.type = featurePoint.type
basePoint.geometry = featurePoint.geometry basePoint.geometry = featurePoint.geometry
basePoint.properties = newProperties basePoint.properties = newProperties
// apply new properties if found in mapping config // apply new properties if found in mapping config
featurePointPropertiesKeys.forEach(pointKeyName => { featurePointPropertiesKeys.forEach(pointKeyName => {
if (mappingKeys.indexOf(pointKeyName) !== -1) { if (mappingKeys.indexOf(pointKeyName) !== -1) {
debugLog('found element', pointKeyName, '=>', mappingConfig[pointKeyName], 'value : ', featurePoint.properties[pointKeyName]) debugLog('found element', pointKeyName, '=>', mappingConfig[pointKeyName], 'value : ', featurePoint.properties[pointKeyName])
let convertedValue = '' let convertedValue = ''
if (isBooleanKey(pointKeyName)) { if (isBooleanKey(pointKeyName)) {
convertedValue = featurePoint.properties[pointKeyName].toLowerCase() == 'true' ? 'yes' : 'no' let copyOfValue = '' + featurePoint.properties[pointKeyName]
} else { if (typeof copyOfValue === typeof Object && copyOfValue.key_converted) {
convertedValue = featurePoint.properties[pointKeyName] copyOfValue = copyOfValue.key_converted
} }
convertedValue = copyOfValue.toLowerCase() == 'true' ? 'yes' : 'no'
} else {
convertedValue = featurePoint.properties[pointKeyName]
}
if (convertedValue) { if (convertedValue) {
newProperties[mappingConfig[pointKeyName]] = convertedValue newProperties[mappingConfig[pointKeyName]] = convertedValue
} }
} }
}) })
debugLog('basePoint', basePoint) debugLog('basePoint', basePoint)
return basePoint return basePoint
} }
function writeJsonFile(fileName, fileContent) { function writeJsonFile (fileName, fileContent) {
debugLog('write file ', fileName) debugLog('write file ', fileName)
return fs.writeFile( return fs.writeFile(
`./output/${fileName}`, `./output/${fileName}`,
fileContent, fileContent,
'utf8', 'utf8',
(err) => { (err) => {
if (err) { if (err) {
debugLog(`Error writing file: ${err}`) debugLog(`Error writing file: ${err}`)
} else { } else {
debugLog(`File ${fileName} is written successfully!`) debugLog(`File ${fileName} is written successfully!`)
} }
} }
) )
} }
console.log('pointCounterMax', pointCounterMax) console.log('pointCounterMax', pointCounterMax)
convertDataForIRVE(sourceFilePathGeoJson, mappingConfigIRVE, pointCounterMax, boundingBoxCoordinates)
if (!use_mappping_engine) {
convertDataForIRVE(sourceFilePathGeoJson, mappingConfigIRVE_simple, pointCounterMax, boundingBoxCoordinates)
} else {
convertDataForIRVE(sourceFilePathGeoJson, mappingConfigIRVE, pointCounterMax, boundingBoxCoordinates)
}

View File

@ -0,0 +1,43 @@
/**
* plan de conversion des clés du jeu de données vers les tags OSM
* détail dans le tableau
* https://wiki.openstreetmap.org/wiki/France/data.gouv.fr/Bornes_de_Recharge_pour_V%C3%A9hicules_%C3%89lectriques
*/
export default {
// ******* nombres
nbre_pdc: 'capacity',
// ******* textes
amenity: 'amenity', // conserver le tag de base
capacity: 'capacity', // conserver le tag de base
nom_amenageur: 'operator',
siren_amenageur: 'owner:ref:FR:SIREN',
nom_operateur: 'operator',
telephone_operateur: 'phone',
contact_operateur: 'email', // ici, on souhaite convertir la clé contact_operateur=bidule en email=bidule
id_station_itinerance: 'ref:EU:EVSE',
id_station_local: 'ref',
gratuit: 'fee',
paiement_acte: 'authentication:none',
reservation: 'reservation',
observations: 'note',
nom_station: 'name',
nom_enseigne: 'network',
// ******* dates
date_mise_en_service: 'start_date',
date_maj: 'source:date',
// ******** champs booléens
prise_type_ef: 'socket:typee',
prise_type_2: 'socket:type2',
prise_type_combo_ccs: 'socket:type2_combo',
prise_type_chademo: 'socket:chademo',
// ******** champs plus complexes
horaires: 'opening_hours', // déjà au bon format
}

View File

@ -12,7 +12,33 @@ function debugLog (message) {
} }
console.log('debug: ', ...message) console.log('debug: ', ...message)
} }
let listOfBooleanKeys = Object.keys({
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',
cable_t2_attache: 'socket:type2_cable',
})
/**
*
* @param pointKeyName
* @returns {boolean}
*/
function isBooleanKey(pointKeyName) {
return listOfBooleanKeys.indexOf(pointKeyName) !== -1
}
export default export default
{ {
debugLog debugLog,
isBooleanKey
} }

File diff suppressed because one or more lines are too long