"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * convertisseur de données de bornes de recharge électrique à partir de données Chargemap et open data Etalab */ var fs = require("fs"); var minimist = require("minimist"); var configIRVE_1 = require("./mappings/converters/configIRVE"); var mappingConfigIRVE_simple_1 = require("./mappings/converters/mappingConfigIRVE_simple"); var engine_1 = require("./mappings/engine"); var utils_ts_1 = require("./mappings/utils.ts"); var debugLog = utils_ts_1.default.debugLog; var use_mappping_engine = false; use_mappping_engine = true; var Mapping_engine = new engine_1.default(configIRVE_1.default); var mini_arguments = minimist(process.argv.slice(2)); var sourceFilePathGeoJson = './etalab_data/small.json'; // wip filter var filterOnBoundingBox = true; filterOnBoundingBox = false; var boundingBoxCoordinates = { xMin: 1.91, xMax: 2.38, yMin: 48.7, yMax: 48.4, }; var filterCoordinates = true; var enable_filter_on_department = true; enable_filter_on_department = false; var filterDepartment = 91; if (mini_arguments['department']) { filterDepartment = mini_arguments['department']; enable_filter_on_department = true; } if (mini_arguments['source']) { sourceFilePathGeoJson = mini_arguments['source']; } if (mini_arguments['engine']) { use_mappping_engine = mini_arguments['engine']; } var filterZipCode = new RegExp("^".concat(filterDepartment)); var filterZipCodeAdresse = new RegExp(" ".concat(filterDepartment)); var filteredName = ''; if (enable_filter_on_department) { filteredName = '_filtered_zipcode_' + filterDepartment; } else if (filterOnBoundingBox) { filteredName = '_filtered_bbox_' + boundingBoxCoordinates.xMin + '-' + boundingBoxCoordinates.xMax + '_' + boundingBoxCoordinates.yMin + '-' + boundingBoxCoordinates.yMax; } var pointCounterMax = 1000000; var limitConversionToFirstPoint = false; // limitConversionToFirstPoint = true if (limitConversionToFirstPoint) { pointCounterMax = 1; } var defaultPropertiesOfPoint = { 'amenity': 'charging_station' }; var converted_geo_json = { type: 'FeatureCollection', features: [] }; /** * * @param sourceFilePath * @param mapping * @param pointCounterMax * @param boundingBoxCoordinates */ function convertDataForIRVE(sourceFilePath, mapping, pointCounterMax, boundingBoxCoordinates) { debugLog('convertDataFromChargemap from ', sourceFilePath); fs.readFile(sourceFilePath, 'utf8', function (err, data) { var point_counter = 0; if (err) { return debugLog(err); } var data_transformed = JSON.parse(data); // debug('data keys ', Object.keys(dataTransformed)) debugLog('debug: properties of point 0', data_transformed.features[0]); if (data_transformed.features) { debugLog('data found, features:', data_transformed.features.length); // find interesting list of points to use var list_of_points = data_transformed.features; // for each point from the data source, convert with the mapping console.log('listOfPoints.length', list_of_points.length); list_of_points.forEach(function (feature_point) { var regex_filter_test_result = true; if (enable_filter_on_department) { debugLog('filtre sur les départements activé'); regex_filter_test_result = (filterZipCode.test(feature_point.properties.consolidated_code_postal) || filterZipCodeAdresse.test(feature_point.properties.adresse_station)); } else if (filterOnBoundingBox) { debugLog('filtre sur les coordonnées activé'); var x = feature_point.properties.coordonneesXY[0]; var xMin = boundingBoxCoordinates.xMin; var xMax = boundingBoxCoordinates.xMax; var yMin = boundingBoxCoordinates.yMin; var yMax = boundingBoxCoordinates.yMax; var y = feature_point.properties.coordonneesXY[1]; regex_filter_test_result = ((x >= xMin && x <= xMax) && (y >= yMin && y <= yMax)); } // filter points depending on zipcode if (filterCoordinates && regex_filter_test_result) { debugLog('featurePoint.properties.consolidated_code_postal', feature_point.properties.consolidated_code_postal); // limit results number of points // if (pointcounter < pointCounterMax) { debugLog('add point'); debugLog('featurePoint', feature_point); var mapped_point = {}; if (use_mappping_engine) { mapped_point = Mapping_engine.mapElementFromConf(feature_point); } else { mapped_point = mapElementFromConfSimple(feature_point, mapping); } debugLog('map one point', feature_point, mapped_point); if (mapped_point) { converted_geo_json.features.push(mapped_point); } } // } point_counter++; }); // output new geojson console.log('convertedGeoJson.features.length', converted_geo_json.features.length); // write file on disk var fileNameToWrite = 'my_converted_data_set' + filteredName + '.json'; console.log('write file ', fileNameToWrite); utils_ts_1.default.writeFile(fileNameToWrite, JSON.stringify(converted_geo_json, null, 2)); debugLog('mapped output:', converted_geo_json.features); return converted_geo_json; } }); } /** * retuns the converted element from mapping config if present, null otherwise */ function mapElementFromConfSimple(featurePoint, mappingConfig) { var mappingKeys = Object.keys(mappingConfig); var featurePointPropertiesKeys = Object.keys(featurePoint.properties); debugLog('keys', mappingKeys, featurePointPropertiesKeys); var newProperties = defaultPropertiesOfPoint; // reinit properties of current point var 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(function (pointKeyName) { if (mappingKeys.indexOf(pointKeyName) !== -1) { debugLog('found element', pointKeyName, '=>', mappingConfig[pointKeyName], 'value : ', featurePoint.properties[pointKeyName]); var convertedValue = ''; if (utils_ts_1.default.isBooleanKey(pointKeyName)) { var copyOfValue = '' + featurePoint.properties[pointKeyName]; if (typeof copyOfValue === typeof Object && copyOfValue.key_converted) { copyOfValue = copyOfValue.key_converted; } convertedValue = copyOfValue.toLowerCase() == 'true' ? 'yes' : 'no'; } else { convertedValue = featurePoint.properties[pointKeyName]; } if (convertedValue) { newProperties[mappingConfig[pointKeyName]] = convertedValue; } } }); debugLog('basePoint', basePoint); return basePoint; } if (use_mappping_engine) { console.log(' - using mapping engine'); console.log(' - pointCounterMax', pointCounterMax); Mapping_engine.setConfig(configIRVE_1.default); convertDataForIRVE(sourceFilePathGeoJson, configIRVE_1.default, pointCounterMax, boundingBoxCoordinates); } else { configIRVE_1.default = mappingConfigIRVE_simple_1.default; convertDataForIRVE(sourceFilePathGeoJson, configIRVE_1.default, pointCounterMax, boundingBoxCoordinates); }