From d57e744db3961519dd086a2ea9337ae3d4f01bc6 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Mon, 31 Jul 2023 19:09:48 +0200 Subject: [PATCH] handle engine convert fields --- .../mappings/converters/configIRVE.mjs | 2 +- .../mappings/engine.mjs | 165 ++++++++++++------ .../mappings/infer_domain_from_csv.mjs | 32 ++++ 3 files changed, 147 insertions(+), 52 deletions(-) create mode 100644 mapping_geojson_to_osm_tags/mappings/infer_domain_from_csv.mjs diff --git a/mapping_geojson_to_osm_tags/mappings/converters/configIRVE.mjs b/mapping_geojson_to_osm_tags/mappings/converters/configIRVE.mjs index 6cf8c930..0fb8ac7d 100644 --- a/mapping_geojson_to_osm_tags/mappings/converters/configIRVE.mjs +++ b/mapping_geojson_to_osm_tags/mappings/converters/configIRVE.mjs @@ -100,7 +100,7 @@ export default { key_converted: null, conditional_values: { // ajout de trois tags si la valeur est yes - yes: { + "yes": { tags_to_add: [ {bicycle: "yes"}, {scooter: "yes"}, diff --git a/mapping_geojson_to_osm_tags/mappings/engine.mjs b/mapping_geojson_to_osm_tags/mappings/engine.mjs index 64560cb9..20eddb1a 100644 --- a/mapping_geojson_to_osm_tags/mappings/engine.mjs +++ b/mapping_geojson_to_osm_tags/mappings/engine.mjs @@ -1,75 +1,138 @@ import debugLog from './utils.mjs' export default class { - config = {} - constructor(mappingConfig){ + config = {} - this.setConfig(mappingConfig) - } - setConfig(mappingConfig){ - this.config = mappingConfig - } + constructor (mappingConfig) { - mapFeaturePoint(featurePointGeoJson){ + this.setConfig(mappingConfig) + } - let geoJSONConvertedPoint = {} - geoJSONConvertedPoint.properties = {... this.config.default_properties_of_point}; - geoJSONConvertedPoint.type = featurePointGeoJson.type; - geoJSONConvertedPoint.geometry = featurePointGeoJson.geometry + setConfig (mappingConfig) { + this.config = mappingConfig + } - let props = featurePointGeoJson.properties + mapFeaturePoint (featurePointGeoJson) { - props.forEach((key,value)=>{ + let geoJSONConvertedPoint = {} + geoJSONConvertedPoint.properties = { ...this.config.default_properties_of_point } + geoJSONConvertedPoint.type = featurePointGeoJson.type + geoJSONConvertedPoint.geometry = featurePointGeoJson.geometry - }) + let props = featurePointGeoJson.properties - return geoJSONConvertedPoint; - } + props.forEach((key, value) => { + }) - isBooleanKey (pointKeyName) { + return geoJSONConvertedPoint + } - return listOfBooleanKeys.indexOf(pointKeyName) !== -1 - } + isBooleanKey (pointKeyName) { - /** - * 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) + return listOfBooleanKeys.indexOf(pointKeyName) !== -1 + } - debugLog('keys', mappingKeys, featurePointPropertiesKeys) + truthyValues = ['true', 'True', 'TRUE', '1', 1] + falsyValues = ['false', 'False', 'FALSE', '0', 0] - let newProperties = defaultPropertiesOfPoint + /** + * 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) - // reinit properties of current point - let basePoint = Object.create(featurePoint) - basePoint.type = featurePoint.type - basePoint.geometry = featurePoint.geometry - basePoint.properties = newProperties + debugLog('keys', mappingKeys, featurePointPropertiesKeys) - // apply new properties if found in mapping config - featurePointPropertiesKeys.forEach(pointKeyName => { + let newProperties = defaultPropertiesOfPoint - if (mappingKeys.indexOf(pointKeyName) !== -1) { - debugLog('found element', pointKeyName, '=>', mappingConfig[pointKeyName], 'value : ', featurePoint.properties[pointKeyName]) - let convertedValue = '' - if (isBooleanKey(pointKeyName)) { + // reinit properties of current point + let basePoint = Object.create(featurePoint) + basePoint.type = featurePoint.type + basePoint.geometry = featurePoint.geometry + basePoint.properties = newProperties - convertedValue = featurePoint.properties[pointKeyName].toLowerCase() == 'true' ? 'yes' : 'no' - } else { - convertedValue = featurePoint.properties[pointKeyName] - } + // apply new properties if found in mapping config + featurePointPropertiesKeys.forEach(pointKeyName => { - if (convertedValue) { - newProperties[mappingConfig[pointKeyName]] = convertedValue - } - } - }) + if (mappingKeys.indexOf(pointKeyName) !== -1) { + debugLog('found element', pointKeyName, '=>', mappingConfig[pointKeyName], 'value : ', featurePoint.properties[pointKeyName]) + let convertedValue = '' - debugLog('basePoint', basePoint) - return basePoint - } + 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) { + if (isBooleanKey(pointKeyName)) { + convertedValue = (valueConvertedFromMapping + '').toLowerCase() == 'true' ? 'yes' : 'no' + } else { + convertedValue = valueConvertedFromMapping + } + + if (convertedValue) { + newProperties[mappingConfig[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 + } } \ No newline at end of file diff --git a/mapping_geojson_to_osm_tags/mappings/infer_domain_from_csv.mjs b/mapping_geojson_to_osm_tags/mappings/infer_domain_from_csv.mjs new file mode 100644 index 00000000..38e591d3 --- /dev/null +++ b/mapping_geojson_to_osm_tags/mappings/infer_domain_from_csv.mjs @@ -0,0 +1,32 @@ + + +let show_debug = 0 + +/** + * faire un log + * @param message + */ +function debugLog (message) { + if (!show_debug) { + return + } + console.log('debug: ', ...message) +} + + +/** + * + * @param pointKeyName + * @returns {boolean} + */ +function openCSV(filePath) { + + console.log('open csv filePath') + return +} + +export default +{ + debugLog, + isBooleanKey +}