scripts/mapping_geojson_to_osm_tags/mappings/engine.mjs
2023-07-31 19:27:17 +02:00

157 lines
4.9 KiB
JavaScript

import custom_utils from './utils.mjs'
const { debugLog } = custom_utils
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',
})
export default class {
mapping_config = {}
constructor (mappingConfig) {
this.setConfig(mappingConfig)
}
setConfig (mappingConfig) {
this.mapping_config = mappingConfig
}
mapFeaturePoint (featurePointGeoJson) {
let geoJSONConvertedPoint = {}
geoJSONConvertedPoint.properties = { ...this.mapping_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
}
truthyValues = ['true', 'True', 'TRUE', '1', 1]
falsyValues = ['false', 'False', 'FALSE', '0', 0]
/**
* retuns the converted element from mapping config if present, null otherwise
*/
mapElementFromConf (featurePoint) {
console.log('config_name', this.mapping_config.config_name)
let mappingKeys = Object.keys(this.mapping_config.tags)
let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
console.log('keys', mappingKeys, featurePointPropertiesKeys)
let newProperties = Object.create(this.mapping_config.default_properties_of_point)
// 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, '=>', this.mapping_config[pointKeyName], 'value : ', featurePoint.properties[pointKeyName])
let convertedValue = ''
let valueConvertedFromMapping = featurePoint.properties[pointKeyName]
let typeofValue = typeof valueConvertedFromMapping
let isStringValue = typeofValue === 'string'
console.log('typeof featurePoint.properties[pointKeyName] === \'string\'', typeofValue)
let isConfigMappingObject = typeofValue === 'string'
if (isStringValue) {
console.log('string value')
if (this.isBooleanKey(pointKeyName)) {
let lowerValue = (valueConvertedFromMapping + '').toLowerCase()
convertedValue = this.truthyValues.indexOf(lowerValue) ? 'yes' : 'no'
} else {
convertedValue = valueConvertedFromMapping
}
if (convertedValue) {
newProperties[this.mapping_config[pointKeyName]] = convertedValue
}
} else if (isConfigMappingObject) {
let newKey = ''
let configObject = valueConvertedFromMapping
if (configObject.key_converted) {
newKey = configObject.key_converted
}
/**
* gestion des valeurs conditionnelles
* nous pouvons renseigner une string ou un objet décrivant les transformations à réaliser
*/
if (configObject.conditional_values) {
let keysConditionnalValues = Object.keys(configObject.conditional_values)
let isFoundValue = keysConditionnalValues.indexOf(valueConvertedFromMapping)
if (isFoundValue !== -1) {
let conditionnalConfig = keysConditionnalValues[isFoundValue]
if (conditionnalConfig.tags_to_add) {
// on peut définir un ensemble de tags à rajouter
newProperties.push(...conditionnalConfig.tags_to_add)
}
if (conditionnalConfig.truthy_value) {
// convertir la valeur, si elle est truthy, la transformer en ce que donne la propriété truthy_value
// exemple: le jeu de données dit que la colonne cable_t2_attache vaut "True", mais on veut le convertir en "1".
// on met donc truthy_value: '1'
if (this.truthyValues.indexOf(valueConvertedFromMapping) !== -1) {
convertedValue = conditionnalConfig.truthy_value
}
}
if (conditionnalConfig.falsy_value) {
if (this.falsyValues.indexOf(valueConvertedFromMapping) !== -1) {
convertedValue = conditionnalConfig.falsy_value
}
}
if (conditionnalConfig.transform_function) {
// une transformation de la valeur
// apply transformation to value
convertedValue = conditionnalConfig.transform_function(valueConvertedFromMapping)
}
// use the value converted
else if (conditionnalConfig.value_converted) {
convertedValue = conditionnalConfig.value_converted
}
}
}
if (newKey && !configObject.ignore_this_data) {
newProperties[newKey] = convertedValue
}
}
}
})
debugLog('basePoint', basePoint)
return basePoint
}
}