-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_to_geojson.js
102 lines (90 loc) · 2.27 KB
/
format_to_geojson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const fs = require('fs');
const path = require('path');
const { convert } = require('./format_hours');
const inputFilePath = path.join(__dirname, 'output/tesla_stores.json');
const outputFilePath = path.join(__dirname, 'output/tesla_stores.geojson');
function setName(name) {
if (name.includes('Tesla')) return name;
return `Tesla ${name}`;
}
function formatHours(hours) {
let output = '';
for (const [key, value] of Object.entries(hours)) {
output += `${key}: ${value}, `;
}
return convert(output);
}
fs.readFile(inputFilePath, 'utf8', (error, data) => {
if (error) {
console.error(
"Une erreur s'est produite lors de la lecture du fichier JSON :",
error,
);
return;
}
try {
const stores = JSON.parse(data);
const geojsonFeatures = stores.map((store) => {
const {
name,
commonName,
address,
extendedAddress,
city,
phone,
email,
storeType,
hasRepearCenter,
services,
storeHours,
serviceHours,
coordinates,
} = store;
const feature = {
type: 'Feature',
properties: {
name: setName(name),
phone,
email,
address,
extendedAddress,
city,
storeHours: formatHours(storeHours),
serviceHours: formatHours(serviceHours),
hasRepearCenter,
storeType,
services,
},
geometry: {
type: 'Point',
coordinates: [
parseFloat(coordinates.longitude),
parseFloat(coordinates.latitude),
],
},
};
return feature;
});
const geojson = {
type: 'FeatureCollection',
name: 'Tesla Stores',
features: geojsonFeatures,
};
const outputData = JSON.stringify(geojson, null, 2);
fs.writeFile(outputFilePath, outputData, (error) => {
if (error) {
console.error(
"Une erreur s'est produite lors de l'écriture du fichier GeoJSON :",
error,
);
} else {
console.log('\x1b[32m \n\n✔ Terminé\n\n \x1b[0m');
}
});
} catch (error) {
console.error(
"Une erreur s'est produite lors de la transformation en GeoJSON :",
error,
);
}
});