scripts/mapping_geojson_to_osm_tags/mappings/engine.js
2023-08-05 10:57:07 +02:00

184 lines
8.7 KiB
JavaScript

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("./utils");
var debugLog = utils_1.default.debugLog;
var listOfBooleanKeys = [
"prise_type_ef",
"prise_type_2",
"prise_type_combo_ccs",
"prise_type_chademo",
"gratuit",
"paiement_acte",
"paiement_cb",
"cable_t2_attache"
];
var default_1 = /** @class */ (function () {
function default_1(mappingConfig) {
this.mapping_config = {};
this.truthyValues = ['true', 'True', 'TRUE', '1', 1];
this.falsyValues = ['false', 'False', 'FALSE', '0', 0];
this.setConfig(mappingConfig);
}
default_1.prototype.setConfig = function (mappingConfig) {
this.mapping_config = mappingConfig;
};
default_1.prototype.mapFeaturePoint = function (featurePointGeoJson) {
var geoJSONConvertedPoint = {};
geoJSONConvertedPoint.properties = __assign({}, this.mapping_config.default_properties_of_point);
geoJSONConvertedPoint.type = featurePointGeoJson.type;
geoJSONConvertedPoint.geometry = featurePointGeoJson.geometry;
var props = featurePointGeoJson.properties;
props.forEach(function (key, value) {
});
return geoJSONConvertedPoint;
};
/**
* TODO convert to mapping config property to transform_truthy
* @param pointKeyName
* @returns {boolean}
*/
default_1.prototype.isBooleanKey = function (pointKeyName) {
return listOfBooleanKeys.indexOf(pointKeyName) !== -1;
};
/**
* reduce number of features
* @param offsetCount
* @param listOfFeatures
*/
default_1.prototype.filterFeaturesByOffset = function (offsetCount, listOfFeatures) {
var filteredList = listOfFeatures;
// TODO
return filteredList;
};
// filterFeaturesByPropertyRegex(bboxConfig, listOfFeatures) {
// console.log('bboxConfig', bboxConfig)
// let filteredList = listOfFeatures
// // TODO
// return filteredList
// }
default_1.prototype.filterFeaturesByPropertyRegex = function (propertyName, criteriaRegex, listOfFeatures) {
var filteredList = listOfFeatures.filter(function (feature) {
return criteriaRegex.test(feature === null || feature === void 0 ? void 0 : feature.properties[propertyName]);
});
return filteredList;
};
/**
* retuns the converted element from mapping config if present, null otherwise
*/
default_1.prototype.mapElementFromConf = function (featurePoint) {
var _this = this;
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.');
}
console.log('mapping_config tags', this.mapping_config.tags);
debugLog('config_name', this.mapping_config.config_name);
var mappingKeys = Object.keys(this.mapping_config.tags);
// let mappingKeys = (this.mapping_config.tags)
var featurePointPropertiesKeys = Object.keys(featurePoint.properties);
debugLog('============= keys mappingKeys:', this.mapping_config.tags.length, mappingKeys.length);
debugLog('============= keys featurePointPropertiesKeys :', featurePoint.properties.length, featurePointPropertiesKeys.length);
var newProperties = Object.create(this.mapping_config.default_properties_of_point);
return;
// reinit properties of current point
var basePoint = Object.create(featurePoint);
basePoint.type = featurePoint.type;
basePoint.geometry = featurePoint.geometry;
// apply new properties if found in mapping config
featurePointPropertiesKeys.forEach(function (pointKeyName) {
_this.convertProperty(pointKeyName, mappingKeys, featurePoint, newProperties);
});
basePoint.properties = newProperties;
debugLog('basePoint', basePoint);
return basePoint;
};
default_1.prototype.convertProperty = function (pointKeyName, mappingKeys, featurePoint, newProperties) {
console.log('pointKeyName', pointKeyName);
if (!mappingKeys.indexOf(pointKeyName) !== -1) {
debugLog('found element', pointKeyName, '=>', this.mapping_config[pointKeyName], 'value : ', featurePoint.properties[pointKeyName]);
var convertedValue = '';
var valueConvertedFromMapping = featurePoint.properties[pointKeyName];
var typeofValue = typeof valueConvertedFromMapping;
var isStringValue = typeofValue === 'string';
debugLog('- pointKeyName', pointKeyName);
debugLog('- valueConvertedFromMapping', valueConvertedFromMapping);
// debugLog('typeof featurePoint.properties[pointKeyName] === \'string\'', typeofValue)
var isConfigMappingObject = typeofValue === 'string';
if (isStringValue) {
debugLog('-- string value');
if (this.isBooleanKey(pointKeyName)) {
var lowerValue = (valueConvertedFromMapping + '').toLowerCase();
debugLog('isBooleanKey: lowerValue', lowerValue);
convertedValue = this.truthyValues.indexOf(lowerValue) ? 'yes' : 'no';
}
else {
convertedValue = valueConvertedFromMapping;
}
debugLog('-- convertedValue', convertedValue);
if (convertedValue) {
newProperties[this.mapping_config[pointKeyName]] = convertedValue;
}
}
else if (isConfigMappingObject) {
var newKey = '';
var 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) {
var keysConditionnalValues = Object.keys(configObject.conditional_values);
var isFoundValue = keysConditionnalValues.indexOf(valueConvertedFromMapping);
if (isFoundValue !== -1) {
var conditionnalConfig = keysConditionnalValues[isFoundValue];
if (conditionnalConfig.tags_to_add) {
// on peut définir un ensemble de tags à rajouter
newProperties.push.apply(newProperties, 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;
}
}
}
};
return default_1;
}());
exports.default = default_1;