2023-08-02 16:59:24 +02:00
|
|
|
import custom_utils from './utils'
|
2023-08-05 12:01:24 +02:00
|
|
|
import MappingConfigType from "./mapping-config.type";
|
2023-08-02 13:20:50 +02:00
|
|
|
|
|
|
|
const {debugLog} = custom_utils
|
|
|
|
|
2023-08-05 09:55:14 +02:00
|
|
|
let listOfBooleanKeys = [
|
|
|
|
"prise_type_ef",
|
|
|
|
"prise_type_2",
|
|
|
|
"prise_type_combo_ccs",
|
|
|
|
"prise_type_chademo",
|
|
|
|
"gratuit",
|
|
|
|
"paiement_acte",
|
|
|
|
"paiement_cb",
|
|
|
|
"cable_t2_attache"
|
|
|
|
]
|
2023-08-02 13:20:50 +02:00
|
|
|
|
|
|
|
export default class {
|
2023-08-05 09:55:14 +02:00
|
|
|
mapping_config: any = {}
|
2023-08-02 13:20:50 +02:00
|
|
|
|
2023-08-05 12:01:24 +02:00
|
|
|
constructor(mappingConfig: MappingConfigType) {
|
2023-08-02 13:20:50 +02:00
|
|
|
this.setConfig(mappingConfig)
|
|
|
|
}
|
|
|
|
|
2023-08-05 12:01:24 +02:00
|
|
|
setConfig(mappingConfig: MappingConfigType) {
|
2023-08-02 13:20:50 +02:00
|
|
|
this.mapping_config = mappingConfig
|
|
|
|
}
|
|
|
|
|
2023-08-05 12:01:24 +02:00
|
|
|
mapFeaturePoint(featurePointGeoJson: any) {
|
2023-08-02 13:20:50 +02:00
|
|
|
|
2023-08-05 09:55:14 +02:00
|
|
|
let geoJSONConvertedPoint: any = {}
|
2023-08-02 13:20:50 +02:00
|
|
|
geoJSONConvertedPoint.properties = {...this.mapping_config.default_properties_of_point}
|
|
|
|
geoJSONConvertedPoint.type = featurePointGeoJson.type
|
|
|
|
geoJSONConvertedPoint.geometry = featurePointGeoJson.geometry
|
|
|
|
|
2023-08-05 11:52:25 +02:00
|
|
|
// let props = featurePointGeoJson.properties
|
2023-08-02 13:20:50 +02:00
|
|
|
|
2023-08-05 11:52:25 +02:00
|
|
|
// props.forEach((key, value) => {
|
|
|
|
//
|
|
|
|
// })
|
2023-08-02 13:20:50 +02:00
|
|
|
|
|
|
|
return geoJSONConvertedPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO convert to mapping config property to transform_truthy
|
|
|
|
* @param pointKeyName
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2023-08-05 12:01:24 +02:00
|
|
|
isBooleanKey(pointKeyName: string): boolean {
|
2023-08-02 13:20:50 +02:00
|
|
|
|
|
|
|
return listOfBooleanKeys.indexOf(pointKeyName) !== -1
|
|
|
|
}
|
|
|
|
|
2023-08-05 15:11:22 +02:00
|
|
|
truthyValues = ['true', 'True', 'TRUE', '1', 'yes', 1]
|
|
|
|
falsyValues = ['false', 'False', 'FALSE', '0', 'no', 0]
|
2023-08-02 13:20:50 +02:00
|
|
|
|
2023-08-05 09:55:14 +02:00
|
|
|
/**
|
|
|
|
* reduce number of features
|
|
|
|
* @param offsetCount
|
|
|
|
* @param listOfFeatures
|
|
|
|
*/
|
2023-08-05 12:01:24 +02:00
|
|
|
filterFeaturesByOffset(offsetCount: number, listOfFeatures: any): Array<any> {
|
2023-08-02 13:20:50 +02:00
|
|
|
let filteredList = listOfFeatures
|
|
|
|
// TODO
|
|
|
|
return filteredList
|
|
|
|
}
|
|
|
|
|
2023-08-05 12:01:24 +02:00
|
|
|
// filterFeaturesByPropertyRegex(bboxConfig:any, listOfFeatures:any) {
|
2023-08-05 11:52:25 +02:00
|
|
|
// debugLog('bboxConfig', bboxConfig)
|
2023-08-05 09:55:14 +02:00
|
|
|
// let filteredList = listOfFeatures
|
|
|
|
// // TODO
|
|
|
|
// return filteredList
|
|
|
|
// }
|
2023-08-02 13:20:50 +02:00
|
|
|
|
2023-08-05 12:01:24 +02:00
|
|
|
filterFeaturesByPropertyRegex(propertyName: string, criteriaRegex: any, listOfFeatures: any) {
|
2023-08-05 12:05:52 +02:00
|
|
|
let filteredList = listOfFeatures.filter((feature: any) => {
|
2023-08-02 13:20:50 +02:00
|
|
|
return criteriaRegex.test(feature?.properties[propertyName])
|
|
|
|
})
|
|
|
|
return filteredList
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retuns the converted element from mapping config if present, null otherwise
|
|
|
|
*/
|
2023-08-05 12:01:24 +02:00
|
|
|
mapElementFromConf(featurePoint: any): any {
|
2023-08-05 14:24:19 +02:00
|
|
|
// debugLog('mapElementFromConf: mapElementFromConf', featurePoint)
|
2023-08-02 13:20:50 +02:00
|
|
|
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.')
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:52:25 +02:00
|
|
|
debugLog('mapElementFromConf: config_name', this.mapping_config.config_name)
|
2023-08-02 13:20:50 +02:00
|
|
|
let mappingKeys = Object.keys(this.mapping_config.tags)
|
|
|
|
let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
|
|
|
|
|
2023-08-05 11:52:25 +02:00
|
|
|
debugLog('mapElementFromConf: ============= keys mappingKeys:', this.mapping_config.tags.length, mappingKeys.length)
|
|
|
|
debugLog('mapElementFromConf: ============= keys featurePointPropertiesKeys :', featurePoint.properties.length, featurePointPropertiesKeys.length)
|
2023-08-02 13:20:50 +02:00
|
|
|
|
|
|
|
let newProperties = Object.create(this.mapping_config.default_properties_of_point)
|
|
|
|
|
2023-08-05 11:52:25 +02:00
|
|
|
|
2023-08-02 13:20:50 +02:00
|
|
|
// 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 => {
|
|
|
|
|
2023-08-05 11:52:25 +02:00
|
|
|
debugLog('mapElementFromConf: convert', pointKeyName)
|
2023-08-02 13:20:50 +02:00
|
|
|
this.convertProperty(pointKeyName, mappingKeys, featurePoint, newProperties)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
basePoint.properties = newProperties
|
|
|
|
|
2023-08-05 14:24:19 +02:00
|
|
|
// debugLog('mapElementFromConf: basePoint', basePoint)
|
2023-08-02 13:20:50 +02:00
|
|
|
return basePoint
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:52:25 +02:00
|
|
|
/**
|
|
|
|
* convertit une propriété en une autre selon la config de mapping
|
|
|
|
* @param pointKeyName
|
|
|
|
* @param mappingKeys
|
|
|
|
* @param featurePoint
|
|
|
|
* @param newProperties
|
|
|
|
*/
|
2023-08-05 12:01:24 +02:00
|
|
|
convertProperty(pointKeyName: string, mappingKeys: any, featurePoint: any, newProperties: any) {
|
2023-08-05 13:45:31 +02:00
|
|
|
let originalValue = featurePoint.properties[pointKeyName]
|
2023-08-05 14:24:19 +02:00
|
|
|
let mappingValueObject: any = '';
|
2023-08-05 13:45:31 +02:00
|
|
|
|
2023-08-05 14:24:19 +02:00
|
|
|
if (mappingKeys.indexOf(pointKeyName) > 0) {
|
|
|
|
mappingValueObject = this.mapping_config.tags[pointKeyName]
|
|
|
|
debugLog('convertProperty: mappingValueObject ', mappingValueObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
debugLog(' ------ convertProperty: pointKeyName', pointKeyName)
|
2023-08-05 12:05:52 +02:00
|
|
|
// debugLog('convertProperty: mappingKeys', mappingKeys)
|
2023-08-05 14:24:19 +02:00
|
|
|
/**
|
|
|
|
* only use existing keys
|
|
|
|
*/
|
2023-08-05 12:05:52 +02:00
|
|
|
if (mappingKeys.indexOf(pointKeyName) > 0) {
|
|
|
|
let valueConvertedFromMapping = featurePoint.properties[pointKeyName]
|
|
|
|
let keyConvertedFromMapping = mappingKeys[mappingKeys.indexOf(pointKeyName)]
|
2023-08-05 15:11:22 +02:00
|
|
|
let mappingConfigOfTag = this.mapping_config.tags[pointKeyName]
|
2023-08-05 12:05:52 +02:00
|
|
|
|
2023-08-05 15:11:22 +02:00
|
|
|
console.log('========== mappingConfigOfTag', mappingConfigOfTag)
|
2023-08-05 12:05:52 +02:00
|
|
|
debugLog('convertProperty: found element', pointKeyName, '=>', keyConvertedFromMapping, 'value : ', valueConvertedFromMapping)
|
2023-08-02 13:20:50 +02:00
|
|
|
let convertedValue = ''
|
|
|
|
|
2023-08-05 15:11:22 +02:00
|
|
|
let typeOfConfigForKey = typeof mappingConfigOfTag
|
|
|
|
let isStringValue = typeOfConfigForKey === 'string'
|
|
|
|
let isConfigMappingObject = typeOfConfigForKey === 'object'
|
|
|
|
|
|
|
|
debugLog('convertProperty: - typeofValue', typeOfConfigForKey)
|
2023-08-05 11:52:25 +02:00
|
|
|
debugLog('convertProperty: - pointKeyName', pointKeyName)
|
|
|
|
debugLog('convertProperty: - valueConvertedFromMapping', valueConvertedFromMapping)
|
2023-08-05 15:11:22 +02:00
|
|
|
debugLog('typeof valueConvertedFromMapping === \'string\'', typeOfConfigForKey)
|
|
|
|
|
|
|
|
|
|
|
|
debugLog('convertProperty: isStringValue?', valueConvertedFromMapping, isStringValue)
|
|
|
|
console.log('convertProperty: isStringValue?', valueConvertedFromMapping, isStringValue)
|
|
|
|
|
|
|
|
console.log('mappingConfigOfTag', mappingConfigOfTag)
|
|
|
|
console.log('typeOfConfigForKey', typeOfConfigForKey)
|
2023-08-02 13:20:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (isStringValue) {
|
2023-08-05 14:24:19 +02:00
|
|
|
console.log('convertProperty: -- string value')
|
2023-08-05 11:52:25 +02:00
|
|
|
debugLog('convertProperty: -- string value')
|
2023-08-02 13:20:50 +02:00
|
|
|
if (this.isBooleanKey(pointKeyName)) {
|
|
|
|
let lowerValue = (valueConvertedFromMapping + '').toLowerCase()
|
2023-08-05 11:52:25 +02:00
|
|
|
debugLog('convertProperty: isBooleanKey: lowerValue', lowerValue)
|
2023-08-02 13:20:50 +02:00
|
|
|
convertedValue = this.truthyValues.indexOf(lowerValue) ? 'yes' : 'no'
|
|
|
|
|
|
|
|
} else {
|
2023-08-05 15:11:22 +02:00
|
|
|
console.log('convertProperty: -- simple conversion : ', pointKeyName, '_', originalValue, '=>', valueConvertedFromMapping)
|
2023-08-02 13:20:50 +02:00
|
|
|
convertedValue = valueConvertedFromMapping
|
|
|
|
}
|
2023-08-05 11:52:25 +02:00
|
|
|
debugLog('convertProperty: -- convertedValue', convertedValue)
|
2023-08-02 13:20:50 +02:00
|
|
|
if (convertedValue) {
|
2023-08-05 12:05:52 +02:00
|
|
|
newProperties[keyConvertedFromMapping] = convertedValue
|
2023-08-02 13:20:50 +02:00
|
|
|
}
|
2023-08-05 12:01:24 +02:00
|
|
|
} else {
|
2023-08-05 11:52:25 +02:00
|
|
|
debugLog('convertProperty: no string value')
|
2023-08-02 13:20:50 +02:00
|
|
|
}
|
2023-08-05 12:09:14 +02:00
|
|
|
|
|
|
|
if (isConfigMappingObject) {
|
2023-08-05 15:11:22 +02:00
|
|
|
let configObject = mappingConfigOfTag
|
2023-08-05 12:09:14 +02:00
|
|
|
|
2023-08-05 14:24:19 +02:00
|
|
|
debugLog('convertProperty: is config object', configObject)
|
2023-08-05 15:11:22 +02:00
|
|
|
let newKey: any = '' + pointKeyName
|
|
|
|
let remove_original_key = false;
|
2023-08-05 14:24:19 +02:00
|
|
|
|
2023-08-05 12:09:14 +02:00
|
|
|
if (configObject.key_converted) {
|
|
|
|
newKey = configObject.key_converted
|
|
|
|
}
|
2023-08-05 15:11:22 +02:00
|
|
|
// else {
|
|
|
|
// newKey = pointKeyName
|
|
|
|
// }
|
2023-08-05 12:09:14 +02:00
|
|
|
|
2023-08-05 13:45:31 +02:00
|
|
|
/**
|
|
|
|
* conversion booléenne
|
|
|
|
*/
|
2023-08-05 14:24:19 +02:00
|
|
|
if (mappingValueObject.boolean_value_conversion) {
|
|
|
|
debugLog('convertProperty: is boolean_value_conversion')
|
|
|
|
debugLog('convertProperty: ==========> original value', originalValue)
|
2023-08-05 13:45:31 +02:00
|
|
|
if (this.truthyValues.indexOf(originalValue) !== -1) {
|
|
|
|
convertedValue = 'yes'
|
|
|
|
}
|
|
|
|
if (this.falsyValues.indexOf(originalValue) !== -1) {
|
|
|
|
convertedValue = 'no'
|
|
|
|
}
|
2023-08-05 14:24:19 +02:00
|
|
|
} else {
|
|
|
|
debugLog('convertProperty: is NOT having boolean_value_conversion', mappingValueObject)
|
2023-08-05 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2023-08-05 12:09:14 +02:00
|
|
|
/**
|
|
|
|
* gestion des valeurs conditionnelles
|
|
|
|
* nous pouvons renseigner une string ou un objet décrivant les transformations à réaliser
|
|
|
|
*/
|
|
|
|
if (configObject.conditional_values) {
|
2023-08-05 15:11:22 +02:00
|
|
|
console.log('convertProperty: conditional_values__________',
|
|
|
|
configObject.conditional_values)
|
|
|
|
|
2023-08-05 12:09:14 +02:00
|
|
|
let keysConditionnalValues: any = Object.keys(configObject.conditional_values)
|
2023-08-05 15:11:22 +02:00
|
|
|
|
|
|
|
let isFoundValue = keysConditionnalValues.indexOf(originalValue)
|
|
|
|
console.log('isFoundValue', isFoundValue)
|
|
|
|
console.log('keysConditionnalValues', keysConditionnalValues)
|
2023-08-05 12:09:14 +02:00
|
|
|
if (isFoundValue !== -1) {
|
2023-08-05 15:11:22 +02:00
|
|
|
let conditionnalConfig: any = configObject.conditional_values[keysConditionnalValues[isFoundValue]]
|
2023-08-05 12:09:14 +02:00
|
|
|
|
|
|
|
if (conditionnalConfig.tags_to_add) {
|
|
|
|
// on peut définir un ensemble de tags à rajouter
|
2023-08-05 15:11:22 +02:00
|
|
|
let tagKeys = Object.keys(conditionnalConfig.tags_to_add)
|
|
|
|
console.log('conditionnalConfig.tags_to_add', conditionnalConfig.tags_to_add)
|
|
|
|
conditionnalConfig.tags_to_add.forEach((object: any, pair: any) => {
|
|
|
|
console.log('object', object)
|
|
|
|
console.log('pair', pair)
|
|
|
|
let key: any = Object.keys(object)
|
|
|
|
key = key[0]
|
|
|
|
let value = object[key]
|
|
|
|
|
|
|
|
console.log('key', key)
|
|
|
|
console.log('value', value)
|
|
|
|
newProperties[key] = value
|
|
|
|
})
|
2023-08-05 12:09:14 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2023-08-05 13:45:31 +02:00
|
|
|
|
2023-08-05 12:09:14 +02:00
|
|
|
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
|
|
|
|
}
|
2023-08-05 15:11:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
if (configObject.remove_original_key) {
|
|
|
|
remove_original_key = true
|
2023-08-05 12:09:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 14:24:19 +02:00
|
|
|
debugLog('convertProperty: convertedValue ==========> {', newKey, ':', convertedValue, '}')
|
2023-08-05 15:11:22 +02:00
|
|
|
console.log(' =============== remove_original_key', remove_original_key)
|
|
|
|
if (!remove_original_key && newKey && !configObject.ignore_this_data) {
|
2023-08-05 14:24:19 +02:00
|
|
|
|
|
|
|
debugLog('convertProperty: added')
|
2023-08-05 12:09:14 +02:00
|
|
|
newProperties[newKey] = convertedValue
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 13:20:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|