refacto to use mapping engine class

This commit is contained in:
Tykayn 2023-07-30 18:37:07 +02:00 committed by tykayn
parent 7175a95ed1
commit 5cb3cc02b1
5 changed files with 357 additions and 244 deletions

View File

@ -4,10 +4,20 @@
import fs from 'node-fs' import fs from 'node-fs'
import minimist from 'minimist' import minimist from 'minimist'
import mappingConfigIRVE from 'mappings/configIRVE' import mappingConfigIRVE from 'mappings/configIRVE'
import mapping_engine from "./mappings/engine";
import {debugLog} from "./mappings/utils";
// let debugLog = custom_utils.debugLog
let mapping_engine = new mapping_engine(mappingConfigIRVE);
let use_mappping_engine = false;
let mini_arguments = minimist(process.argv.slice(2)) let mini_arguments = minimist(process.argv.slice(2))
let show_debug = 0
// let sourceFileChargemapJson = './chargemap_data/hurepoix.json'
let sourceFilePathGeoJson = './etalab_data/latest.json'
// wip filter // wip filter
let filterOnBoundingBox = true let filterOnBoundingBox = true
@ -21,17 +31,20 @@ let boundingBoxCoordinates = {
} }
let filterCoordinates = true let filterCoordinates = true
let enableFilterOnDepartment = 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']) {
sourceFilePathGeoJson = mini_arguments['source']
}
let filterZipCode = new RegExp(`^${filterDepartment}`) let filterZipCode = new RegExp(`^${filterDepartment}`)
let filterZipCodeAdresse = new RegExp(` ${filterDepartment}`) let filterZipCodeAdresse = new RegExp(` ${filterDepartment}`)
let filteredName = '' let filteredName = ''
if (enableFilterOnDepartment) { 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
@ -43,9 +56,6 @@ let limitConversionToFirstPoint = false
if (limitConversionToFirstPoint) { if (limitConversionToFirstPoint) {
pointCounterMax = 1 pointCounterMax = 1
} }
// let sourceFileChargemapJson = './chargemap_data/hurepoix.json'
let sourceFileIRVEGeoJson = './etalab_data/latest.json'
let defaultPropertiesOfPoint = { let defaultPropertiesOfPoint = {
'amenity': 'charging_station' 'amenity': 'charging_station'
} }
@ -62,61 +72,62 @@ let listOfBooleanKeys = Object.keys({
cable_t2_attache: 'socket:type2_cable', cable_t2_attache: 'socket:type2_cable',
}) })
function debugLog (message) {
if (!show_debug) {
return
}
console.log('debug: ', ...message)
}
function convertDataForIRVE (sourceFilePath, mapping, pointCounterMax, boundingBoxCoordinates) { let converted_geo_json = {
debugLog('convertDataFromChargemap from ', sourceFilePath)
let convertedGeoJson = {
type: 'FeatureCollection', type: 'FeatureCollection',
features: [] features: []
} }
/**
*
* @param sourceFilePath
* @param mapping
* @param pointCounterMax
* @param boundingBoxCoordinates
*/
function convertDataForIRVE(sourceFilePath, mapping, pointCounterMax, boundingBoxCoordinates) {
debugLog('convertDataFromChargemap from ', sourceFilePath)
fs.readFile(sourceFilePath, 'utf8', function (err, data) { fs.readFile(sourceFilePath, 'utf8', function (err, data) {
let pointcounter = 0 let point_counter = 0
if (err) { if (err) {
return debugLog(err) return debugLog(err)
} }
let dataTransformed = JSON.parse(data) let data_transformed = JSON.parse(data)
// debug('data keys ', Object.keys(dataTransformed)) // debug('data keys ', Object.keys(dataTransformed))
debugLog('debug: properties of point 0', dataTransformed.features[0]) debugLog('debug: properties of point 0', data_transformed.features[0])
if (dataTransformed.features) { if (data_transformed.features) {
debugLog('data found, features:', dataTransformed.features.length) debugLog('data found, features:', data_transformed.features.length)
// find interesting list of points to use // find interesting list of points to use
let listOfPoints = dataTransformed.features let list_of_points = data_transformed.features
// for each point from the data source, convert with the mapping // for each point from the data source, convert with the mapping
console.log('listOfPoints.length', listOfPoints.length) console.log('listOfPoints.length', list_of_points.length)
listOfPoints.forEach(featurePoint => { list_of_points.forEach(feature_point => {
let regextestresult = true let regex_filter_test_result = true
if (enableFilterOnDepartment) { if (enable_filter_on_department) {
console.log('filtre sur les départements activé') console.log('filtre sur les départements activé')
regextestresult = ( regex_filter_test_result = (
filterZipCode.test(featurePoint.properties.consolidated_code_postal) filterZipCode.test(feature_point.properties.consolidated_code_postal)
|| ||
filterZipCodeAdresse.test(featurePoint.properties.adresse_station) filterZipCodeAdresse.test(feature_point.properties.adresse_station)
) )
} else if (filterOnBoundingBox) { } else if (filterOnBoundingBox) {
console.log('filtre sur les coordonnées activé') console.log('filtre sur les coordonnées activé')
let x = featurePoint.properties.coordonneesXY[0] let x = feature_point.properties.coordonneesXY[0]
let xMin = boundingBoxCoordinates.xMin let xMin = boundingBoxCoordinates.xMin
let xMax = boundingBoxCoordinates.xMax let xMax = boundingBoxCoordinates.xMax
let yMin = boundingBoxCoordinates.yMin let yMin = boundingBoxCoordinates.yMin
let yMax = boundingBoxCoordinates.yMax let yMax = boundingBoxCoordinates.yMax
let y = featurePoint.properties.coordonneesXY[1] let y = feature_point.properties.coordonneesXY[1]
regextestresult = ( regex_filter_test_result = (
(x >= xMin && x <= xMax) (x >= xMin && x <= xMax)
&& &&
(y >= yMin && y <= yMax) (y >= yMin && y <= yMax)
@ -125,51 +136,54 @@ function convertDataForIRVE (sourceFilePath, mapping, pointCounterMax, boundingB
} }
// filter points depending on zipcode // filter points depending on zipcode
if (filterCoordinates && regextestresult) { if (filterCoordinates && regex_filter_test_result) {
debugLog('featurePoint.properties.consolidated_code_postal', featurePoint.properties.consolidated_code_postal) debugLog('featurePoint.properties.consolidated_code_postal', feature_point.properties.consolidated_code_postal)
// limit results number of points // limit results number of points
// if (pointcounter < pointCounterMax) { // if (pointcounter < pointCounterMax) {
debugLog('add point') debugLog('add point')
debugLog('featurePoint', featurePoint) debugLog('featurePoint', feature_point)
let mappedPoint = mapElementFromConf(featurePoint, mapping) let mapped_point = {};
debugLog('map one point', featurePoint, mappedPoint)
if (mappedPoint) { if (use_mappping_engine) {
convertedGeoJson.features.push(mappedPoint) 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)
} }
} }
// } // }
pointcounter++ point_counter++
}) })
// output new geojson // output new geojson
console.log('convertedGeoJson.features.length', convertedGeoJson.features.length) console.log('convertedGeoJson.features.length', converted_geo_json.features.length)
// write file on disk // write file on disk
let fileNameToWrite = 'my_converted_data_set' + filteredName + '.json' let fileNameToWrite = 'my_converted_data_set' + filteredName + '.json'
console.log('write file ', fileNameToWrite) console.log('write file ', fileNameToWrite)
writeJsonFile(fileNameToWrite, JSON.stringify(convertedGeoJson, null, 2)) writeJsonFile(fileNameToWrite, JSON.stringify(converted_geo_json, null, 2))
debugLog('mapped output:', convertedGeoJson.features) debugLog('mapped output:', converted_geo_json.features)
return convertedGeoJson return converted_geo_json
} }
}) })
} }
function isBooleanKey (pointKeyName) {
return listOfBooleanKeys.indexOf(pointKeyName) !== -1
}
/** /**
* 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 mapElementFromConf (featurePoint, mappingConfig) { {
let mappingKeys = Object.keys(mappingConfig) let mappingKeys = Object.keys(mappingConfig)
let featurePointPropertiesKeys = Object.keys(featurePoint.properties) let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
@ -206,7 +220,7 @@ function mapElementFromConf (featurePoint, mappingConfig) {
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(
@ -224,6 +238,4 @@ function writeJsonFile (fileName, fileContent) {
} }
console.log('pointCounterMax', pointCounterMax) console.log('pointCounterMax', pointCounterMax)
convertDataForIRVE(sourceFileIRVEGeoJson, mappingConfigIRVE, pointCounterMax, boundingBoxCoordinates) convertDataForIRVE(sourceFilePathGeoJson, mappingConfigIRVE, pointCounterMax, boundingBoxCoordinates)
// convertDataFromChargemap(sourceFileChargemapJson, mappingConfigIRVE)

View File

@ -2,7 +2,7 @@
D'après le jeu de données Etalab D'après le jeu de données Etalab
## liste des opérateurs ## liste des 310 opérateurs
270 AGENCY 270 AGENCY
2Ed Coutances 2Ed Coutances
2F Production 2F Production

View File

@ -5,6 +5,13 @@
*/ */
export default { export default {
config_name: "IRVE config",
config_author: "tykayn <contact@cipherbliss.com>",
default_properties_of_point: {
'amenity': 'charging_station'
},
tags: {
// ******* nombres // ******* nombres
nbre_pdc: 'capacity', nbre_pdc: 'capacity',
// ******* textes // ******* textes
@ -33,7 +40,13 @@ export default {
// TODO gestion des types dont on doit convertir la valeur // TODO gestion des types dont on doit convertir la valeur
// ******** champs booléens // ******** champs booléens
cable_t2_attache: 'socket:type2_cable', cable_t2_attache:
{
key_converted: 'socket:type2_cable',
// cable_t2_attache
truthy_value: '1'
}
,
prise_type_ef: 'socket:typee', prise_type_ef: 'socket:typee',
prise_type_2: 'socket:type2', prise_type_2: 'socket:type2',
prise_type_combo_ccs: 'socket:type2_combo', prise_type_combo_ccs: 'socket:type2_combo',
@ -49,22 +62,22 @@ export default {
key_converted: "wheelchair", key_converted: "wheelchair",
conditional_values: { conditional_values: {
"Accessibilité inconnue": { "Accessibilité inconnue": {
value_converted: "", // value_converted: "",
ignore_this_data: true, // ne pas ajouter de tag si la valeur est égale à Accessibilité inconnue. ignore_this_data: true, // ne pas ajouter de tag si la valeur est égale à Accessibilité inconnue.
// transform_function : (original_value) => original_value.toLowerCase(), // transform_function : (original_value) => original_value.toLowerCase(),
}, },
"Accessible mais non réservé PMR": { "Accessible mais non réservé PMR": {
value_converted: "" value_converted: "yes"
}, },
"Réservé PMR": { "Réservé PMR": {
value_converted: "" value_converted: "yes"
}, },
"Non accessible": { "Non accessible": {
value_converted: "no" value_converted: "no"
}, },
"Mo-Fr 08:30-12:00,Mo-Fr 14:00-19:00,Sat 09:00-18:30": { "Mo-Fr 08:30-12:00,Mo-Fr 14:00-19:00,Sat 09:00-18:30": {
value_converted: "Mo-Fr 08:30-12:00,Mo-Fr 14:00-19:00,Sat 09:00-18:30" value_converted: "Mo-Fr 08:30-12:00,Mo-Fr 14:00-19:00,Sat 09:00-18:30"
} },
"24/7": { "24/7": {
value_converted: "" value_converted: ""
} }
@ -81,7 +94,7 @@ export default {
// Mo-Fr 09:00-16:00 // Mo-Fr 09:00-16:00
// Mo-Fr 08:00-12:00,Mo-Fr 14:00-18:00,Th 08:00-18:00 // Mo-Fr 08:00-12:00,Mo-Fr 14:00-18:00,Th 08:00-18:00
} }
}, },
station_deux_roues: { station_deux_roues: {
key_converted: null, key_converted: null,
@ -95,11 +108,6 @@ export default {
] ]
} }
} }
} }
// station_deux_roues => },
// bicycle=yes
// scooter=yes
// motorcar=no
} }

View File

@ -0,0 +1,75 @@
import debugLog from 'utils'
export default class {
config = {}
constructor(mappingConfig){
this.setConfig(mappingConfig)
}
setConfig(mappingConfig){
this.config = mappingConfig
}
mapFeaturePoint(featurePointGeoJson){
let geoJSONConvertedPoint = {}
geoJSONConvertedPoint.properties = {... this.config.default_properties_of_point};
geoJSONConvertedPoint.type = featurePointGeoJson.type;
geoJSONConvertedPoint.geometry = featurePointGeoJson.geometry
let props = featurePointGeoJson.properties
props.forEach((key,value)=>{
})
return geoJSONConvertedPoint;
}
isBooleanKey (pointKeyName) {
return listOfBooleanKeys.indexOf(pointKeyName) !== -1
}
/**
* retuns the converted element from mapping config if present, null otherwise
*/
mapElementFromConf (featurePoint, mappingConfig) {
let mappingKeys = Object.keys(mappingConfig)
let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
debugLog('keys', mappingKeys, featurePointPropertiesKeys)
let newProperties = defaultPropertiesOfPoint
// 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])
let convertedValue = ''
if (isBooleanKey(pointKeyName)) {
convertedValue = featurePoint.properties[pointKeyName].toLowerCase() == 'true' ? 'yes' : 'no'
} else {
convertedValue = featurePoint.properties[pointKeyName]
}
if (convertedValue) {
newProperties[mappingConfig[pointKeyName]] = convertedValue
}
}
})
debugLog('basePoint', basePoint)
return basePoint
}
}

View File

@ -0,0 +1,18 @@
let show_debug = 0
/**
* faire un log
* @param message
*/
function debugLog (message) {
if (!show_debug) {
return
}
console.log('debug: ', ...message)
}
export default
{
debugLog
}