scripts/mapping_geojson_to_osm_tags/convert_to_osm_tags.mjs

244 lines
7.9 KiB
JavaScript
Raw Normal View History

/**
* convertisseur de données de bornes de recharge électrique à partir de données Chargemap et open data Etalab
*/
2023-07-29 22:16:36 +02:00
import fs from 'node-fs'
2023-07-29 23:16:36 +02:00
import minimist from 'minimist'
2023-07-30 18:41:47 +02:00
import mappingConfigIRVE from './mappings/configIRVE.mjs'
import mapping_engine from "./mappings/engine.mjs";
import custom_utils from "./mappings/utils.mjs";
const {debugLog} = custom_utils;
2023-07-30 18:37:07 +02:00
// let debugLog = custom_utils.debugLog
let use_mappping_engine = false;
2023-07-30 18:41:47 +02:00
let Mapping_engine = new mapping_engine(mappingConfigIRVE);
2023-07-29 23:16:36 +02:00
let mini_arguments = minimist(process.argv.slice(2))
2023-07-29 17:19:56 +02:00
2023-07-30 18:37:07 +02:00
// let sourceFileChargemapJson = './chargemap_data/hurepoix.json'
let sourceFilePathGeoJson = './etalab_data/latest.json'
2023-07-29 22:10:50 +02:00
// wip filter
let filterOnBoundingBox = true
2023-07-29 22:16:36 +02:00
filterOnBoundingBox = false
2023-07-29 22:10:50 +02:00
2023-07-29 22:16:36 +02:00
let boundingBoxCoordinates = {
2023-07-30 18:37:07 +02:00
xMin: 1.91,
xMax: 2.38,
yMin: 48.7,
yMax: 48.4,
2023-07-29 22:10:50 +02:00
}
2023-07-29 22:16:36 +02:00
let filterCoordinates = true
2023-07-30 18:37:07 +02:00
let enable_filter_on_department = true
2023-07-29 23:16:36 +02:00
let filterDepartment = 91
if (mini_arguments['department']) {
2023-07-30 18:37:07 +02:00
filterDepartment = mini_arguments['department']
}
if (mini_arguments['source']) {
sourceFilePathGeoJson = mini_arguments['source']
2023-07-29 23:16:36 +02:00
}
2023-07-29 22:16:36 +02:00
let filterZipCode = new RegExp(`^${filterDepartment}`)
let filterZipCodeAdresse = new RegExp(` ${filterDepartment}`)
let filteredName = ''
2023-07-29 17:19:56 +02:00
2023-07-30 18:37:07 +02:00
if (enable_filter_on_department) {
filteredName = '_filtered_zipcode_' + filterDepartment
} else if (filterOnBoundingBox) {
2023-07-30 18:37:07 +02:00
filteredName = '_filtered_bbox_' + boundingBoxCoordinates.xMin + '-' + boundingBoxCoordinates.xMax + '_' + boundingBoxCoordinates.yMin + '-' + boundingBoxCoordinates.yMax
2023-07-29 22:10:50 +02:00
}
2023-07-29 22:16:36 +02:00
let pointCounterMax = 1000000
2023-07-29 17:19:56 +02:00
2023-07-29 22:16:36 +02:00
let limitConversionToFirstPoint = false
2023-07-29 22:10:50 +02:00
// limitConversionToFirstPoint = true;
if (limitConversionToFirstPoint) {
2023-07-30 18:37:07 +02:00
pointCounterMax = 1
2023-07-29 22:10:50 +02:00
}
let defaultPropertiesOfPoint = {
2023-07-30 18:37:07 +02:00
'amenity': 'charging_station'
}
let listOfBooleanKeys = Object.keys({
2023-07-30 18:37:07 +02:00
prise_type_ef: 'socket:typee',
prise_type_2: 'socket:type2',
prise_type_combo_ccs: 'socket:type2_combo',
prise_type_chademo: 'socket:chademo',
gratuit: 'fee',
paiement_acte: 'authentication:none',
paiement_cb: 'payment:credit_cards',
cable_t2_attache: 'socket:type2_cable',
})
2023-07-30 18:37:07 +02:00
let converted_geo_json = {
type: 'FeatureCollection',
features: []
}
2023-07-30 18:37:07 +02:00
/**
*
* @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) {
let point_counter = 0
if (err) {
return debugLog(err)
}
let 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
let 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(feature_point => {
let regex_filter_test_result = true
if (enable_filter_on_department) {
console.log('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) {
console.log('filtre sur les coordonnées activé')
let x = feature_point.properties.coordonneesXY[0]
let xMin = boundingBoxCoordinates.xMin
let xMax = boundingBoxCoordinates.xMax
let yMin = boundingBoxCoordinates.yMin
let yMax = boundingBoxCoordinates.yMax
let 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)
let mapped_point = {};
if (use_mappping_engine) {
2023-07-30 18:41:47 +02:00
Mapping_engine.setConfig(mapping)
mapped_point = Mapping_engine.mapFeaturePoint(feature_point)
2023-07-30 18:37:07 +02:00
} 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
let fileNameToWrite = 'my_converted_data_set' + filteredName + '.json'
console.log('write file ', fileNameToWrite)
writeJsonFile(fileNameToWrite, JSON.stringify(converted_geo_json, null, 2))
debugLog('mapped output:', converted_geo_json.features)
return converted_geo_json
}
})
}
2023-07-30 18:37:07 +02:00
/**
* retuns the converted element from mapping config if present, null otherwise
*/
2023-07-30 18:37:07 +02:00
function mapElementFromConfSimple(featurePoint, mappingConfig)
{
let mappingKeys = Object.keys(mappingConfig)
let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
debugLog('keys', mappingKeys, featurePointPropertiesKeys)
let newProperties = defaultPropertiesOfPoint
// reinit properties of current point
let 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(pointKeyName => {
if (mappingKeys.indexOf(pointKeyName) !== -1) {
debugLog('found element', pointKeyName, '=>', mappingConfig[pointKeyName], 'value : ', featurePoint.properties[pointKeyName])
let convertedValue = ''
if (isBooleanKey(pointKeyName)) {
convertedValue = featurePoint.properties[pointKeyName].toLowerCase() == 'true' ? 'yes' : 'no'
} else {
convertedValue = featurePoint.properties[pointKeyName]
}
if (convertedValue) {
newProperties[mappingConfig[pointKeyName]] = convertedValue
}
}
})
debugLog('basePoint', basePoint)
return basePoint
}
2023-07-30 18:37:07 +02:00
function writeJsonFile(fileName, fileContent) {
debugLog('write file ', fileName)
return fs.writeFile(
`./output/${fileName}`,
fileContent,
'utf8',
(err) => {
if (err) {
debugLog(`Error writing file: ${err}`)
} else {
debugLog(`File ${fileName} is written successfully!`)
}
}
)
}
2023-07-29 17:19:56 +02:00
console.log('pointCounterMax', pointCounterMax)
2023-07-30 18:37:07 +02:00
convertDataForIRVE(sourceFilePathGeoJson, mappingConfigIRVE, pointCounterMax, boundingBoxCoordinates)