-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrendermap.js
More file actions
81 lines (72 loc) · 3.12 KB
/
rendermap.js
File metadata and controls
81 lines (72 loc) · 3.12 KB
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
async function loadCoordinates() {
const response = await fetch("./coordinates.json");
const json = await response.json();
return json;
};
function sum(array) {
const convertedValues = array.map((surface) => {
const valueInt = parseInt(surface);
if (valueInt === NaN) { return 0 } else { return valueInt }
});
const result = convertedValues.reduce((total, current) => { return total + current; }, 0);
return result
}
function sumSurfaces(surfacesArray) {
const result = sum(surfacesArray);
if (result === 0 || isNaN(result)) { return "No documented surface yet." } else { return `${result} m²` }
}
function sumPowers(powersArray) {
const result = sum(powersArray);
if (result === 0 || isNaN(result)) { return "No documented power yet." } else { return `min. ${result} MW` }
}
function formatNames(namesArray) {
const string = namesArray.join(", \n<br>");
return string
}
function parseDelivery(deliveryString) {
if (deliveryString === "" || deliveryString === "à venir" || deliveryString === "NR") { return "No documented delivery date yet" } else { return deliveryString }
}
async function dcProjectsMap() {
const dcProjects = await loadCoordinates();
const franceCenterLat = 46.227638;
const franceCenterLng = 2.213749;
const container = document.getElementById("map-container");
const map = document.createElement("div");
map.setAttribute("id", "map");
container?.append(map);
const options = {
center: L.latLng(franceCenterLat, franceCenterLng),
zoom: 6
};
const myMap = L.map("map", options);
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 19,
attribution: "© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a>"
}).addTo(myMap);
const flattenedArray = dcProjects.flat(1);
const groupedDcProjects = Object.groupBy(flattenedArray, ({ lat }) => lat);
const iterableGroups = Object.entries(groupedDcProjects);
const orderedDcProjects = iterableGroups.map((projects) => {
const projectNames = projects[1].map((project) => {
const name = `${project["dc_project"]["name"]} (${parseDelivery(project["dc_project"]["planned_delivery"])})`;
return name
});
const formattedNames = formatNames(projectNames);
const projectSurfaces = projects[1].map((project) => project["dc_project"]["surface"]);
const totalSurface = sumSurfaces(projectSurfaces);
const projectPowers = projects[1].map((project) => project["dc_project"]["power"]);
const totalPower = sumPowers(projectPowers);
const projectLatitude = projects[1][0]["lat"];
const projectLongitude = projects[1][0]["lon"];
const coordinates = [projectLatitude, projectLongitude];
const result = { number: projectNames.length, name: formattedNames, surface: totalSurface, power: totalPower, coordinates: coordinates };
return result;
});
orderedDcProjects.forEach((project) => {
const marker = L.marker(project.coordinates).addTo(myMap);
marker.bindPopup(
`<b>Number of data centers</b>: ${project.number}<br><b>DC project name(s) (planned delivery date):</b><br> ${project.name}<br><b>DC project total surface</b>: ${project.surface}<br><b>DC project total power</b>: ${project.power}`
);
});
}
dcProjectsMap();