scripts/mapping_geojson_to_osm_tags/mappings/engine.ts

237 lines
9.4 KiB
TypeScript

import custom_utils from './utils'
import MappingConfigType from "./mapping-config.type";
const {debugLog} = custom_utils
let listOfBooleanKeys = [
"prise_type_ef",
"prise_type_2",
"prise_type_combo_ccs",
"prise_type_chademo",
"gratuit",
"paiement_acte",
"paiement_cb",
"cable_t2_attache"
]
export default class {
mapping_config: any = {}
constructor(mappingConfig: MappingConfigType) {
this.setConfig(mappingConfig)
}
setConfig(mappingConfig: MappingConfigType) {
this.mapping_config = mappingConfig
}
mapFeaturePoint(featurePointGeoJson: any) {
let geoJSONConvertedPoint: any = {}
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
}
/**
* TODO convert to mapping config property to transform_truthy
* @param pointKeyName
* @returns {boolean}
*/
isBooleanKey(pointKeyName: string): boolean {
return listOfBooleanKeys.indexOf(pointKeyName) !== -1
}
truthyValues = ['true', 'True', 'TRUE', '1', 1]
falsyValues = ['false', 'False', 'FALSE', '0', 0]
/**
* reduce number of features
* @param offsetCount
* @param listOfFeatures
*/
filterFeaturesByOffset(offsetCount: number, listOfFeatures: any): Array<any> {
let filteredList = listOfFeatures
// TODO
return filteredList
}
// filterFeaturesByPropertyRegex(bboxConfig:any, listOfFeatures:any) {
// debugLog('bboxConfig', bboxConfig)
// let filteredList = listOfFeatures
// // TODO
// return filteredList
// }
filterFeaturesByPropertyRegex(propertyName: string, criteriaRegex: any, listOfFeatures: any) {
let filteredList = listOfFeatures.filter((feature: any) => {
return criteriaRegex.test(feature?.properties[propertyName])
})
return filteredList
}
/**
* retuns the converted element from mapping config if present, null otherwise
*/
mapElementFromConf(featurePoint: any): any {
debugLog('mapElementFromConf: mapElementFromConf', featurePoint)
if (!this.mapping_config) {
throw new Error('no config was loaded in the mapping engine. use setConfig(my_mapping_config) on this instance of mapping engine before using this.')
}
debugLog('mapElementFromConf: config_name', this.mapping_config.config_name)
let mappingKeys = Object.keys(this.mapping_config.tags)
let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
debugLog('mapElementFromConf: ============= keys mappingKeys:', this.mapping_config.tags.length, mappingKeys.length)
debugLog('mapElementFromConf: ============= keys featurePointPropertiesKeys :', featurePoint.properties.length, featurePointPropertiesKeys.length)
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
// apply new properties if found in mapping config
featurePointPropertiesKeys.forEach(pointKeyName => {
debugLog('mapElementFromConf: convert', pointKeyName)
this.convertProperty(pointKeyName, mappingKeys, featurePoint, newProperties)
})
basePoint.properties = newProperties
debugLog('mapElementFromConf: basePoint', basePoint)
return basePoint
}
/**
* convertit une propriété en une autre selon la config de mapping
* @param pointKeyName
* @param mappingKeys
* @param featurePoint
* @param newProperties
*/
convertProperty(pointKeyName: string, mappingKeys: any, featurePoint: any, newProperties: any) {
let originalValue = featurePoint.properties[pointKeyName]
debugLog('convertProperty: pointKeyName', pointKeyName)
// debugLog('convertProperty: mappingKeys', mappingKeys)
if (mappingKeys.indexOf(pointKeyName) > 0) {
let valueConvertedFromMapping = featurePoint.properties[pointKeyName]
let keyConvertedFromMapping = mappingKeys[mappingKeys.indexOf(pointKeyName)]
debugLog('convertProperty: found element', pointKeyName, '=>', keyConvertedFromMapping, 'value : ', valueConvertedFromMapping)
let convertedValue = ''
let typeofValue = typeof valueConvertedFromMapping
let isStringValue = typeofValue === 'string'
debugLog('convertProperty: - typeofValue', typeofValue)
debugLog('convertProperty: - pointKeyName', pointKeyName)
debugLog('convertProperty: - valueConvertedFromMapping', valueConvertedFromMapping)
debugLog('typeof valueConvertedFromMapping === \'string\'', typeofValue)
let isConfigMappingObject = typeofValue === 'string'
debugLog('convertProperty: isStringValue?', isStringValue)
if (isStringValue) {
debugLog('convertProperty: -- string value')
if (this.isBooleanKey(pointKeyName)) {
let lowerValue = (valueConvertedFromMapping + '').toLowerCase()
debugLog('convertProperty: isBooleanKey: lowerValue', lowerValue)
convertedValue = this.truthyValues.indexOf(lowerValue) ? 'yes' : 'no'
} else {
convertedValue = valueConvertedFromMapping
}
debugLog('convertProperty: -- convertedValue', convertedValue)
if (convertedValue) {
newProperties[keyConvertedFromMapping] = convertedValue
}
} else {
debugLog('convertProperty: no string value')
}
// TODO handle config object for complex mapping
if (isConfigMappingObject) {
debugLog('convertProperty: is config object')
let newKey = ''
let configObject = valueConvertedFromMapping
if (configObject.key_converted) {
newKey = configObject.key_converted
}
/**
* conversion booléenne
*/
if (configObject.boolean_value_conversion) {
debugLog('convertProperty: is boolean_value_conversion' )
if (this.truthyValues.indexOf(originalValue) !== -1) {
convertedValue = 'yes'
}
if (this.falsyValues.indexOf(originalValue) !== -1) {
convertedValue = 'no'
}
}
/**
* gestion des valeurs conditionnelles
* nous pouvons renseigner une string ou un objet décrivant les transformations à réaliser
*/
if (configObject.conditional_values) {
let keysConditionnalValues: any = Object.keys(configObject.conditional_values)
let isFoundValue = keysConditionnalValues.indexOf(valueConvertedFromMapping)
if (isFoundValue !== -1) {
let conditionnalConfig: any = 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
}
}
}
}
}