mapping-geojson-osm/wip/chargemap/chargemap.ts
2023-08-11 18:41:32 +02:00

83 lines
2.2 KiB
TypeScript

import fs from "fs";
let sourceFilePath: string = './chargemap_data/hurepoix.json'
/**
makes a geojson from chargemap layer data
*/
function convertChargemapFile(sourceFilePath: string) {
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
let data_transformed: any = JSON.parse(data)
let base_dataset: any = {type: 'FeatureCollection', features: []}
let base_point: any = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: []
},
properties: {
amenity: "charging_station"
}
}
console.log('data_transformed.items.length', data_transformed.items.length)
data_transformed.items.forEach((item: any) => {
let new_point: any = {...base_point}
if (item.type == 'point') {
new_point.geometry.coordinates = [
item.properties?.lat,
item.properties?.lng,
]
} else if (item.type == 'pool') {
new_point.geometry.coordinates = [
item.pool.gps_coordinates.lon,
item.pool.gps_coordinates.lat,
]
console.log('new_point.geometry.coordinates', new_point.geometry.coordinates)
}
base_dataset.features.push(new_point)
})
console.log('base_dataset.features.length', base_dataset.features.length)
if (base_dataset) {
writeFile('hurepoix_geojson.json', JSON.stringify(base_dataset, null, 2))
}
})
}
/**
* crée un fichier dans le dossier par défaut, output
* @param fileName
* @param fileContent
*/
function writeFile(fileName: string, fileContent: any) {
let output_folder = 'output';
return fs.writeFile(
`./${output_folder}/${fileName}`,
fileContent,
'utf8',
(err) => {
if (err) {
console.log(`Error writing file: ${err}`)
} else {
console.log(`File ${fileName} is written successfully!`)
}
}
)
}
convertChargemapFile(sourceFilePath)