-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
326 lines (294 loc) · 14.3 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* eslint-disable unused-imports/no-unused-vars */
/* eslint-disable no-undef */
/* eslint-disable @ptvgroup/linguijs/string-is-marked-for-translation */
/* eslint-disable no-prototype-builtins */
const api_key = "YOUR_API_KEY";
const tileURL = "https://api.myptv.com/rastermaps/v1/image-tiles/{z}/{x}/{y}?size={tileSize}";
const routesURL = "https://api.myptv.com/routing/v1/routes";
const predefinedProfilesURL = "https://api.myptv.com/data/v1/vehicle-profiles/predefined";
$(document).ready(function () {
const coordinate = L.latLng(49, 8.4);
const map = new L.Map("map", {
center: coordinate,
zoom: 13,
zoomControl: false,
});
L.control.zoom({ position: "bottomright" }).addTo(map);
L.tileLayer(tileURL, {
attribution: "© " + new Date().getFullYear() + ", PTV Group, HERE",
tileSize: 256,
trackResize: false,
},
[{ header: "ApiKey", value: api_key }]
).addTo(map);
// Adding Waypoints to the Map
map.on("click", onMapClick);
function onMapClick(e) {
const marker = L.marker(e.latlng).addTo(map);
marker.on("contextmenu", removeMarker);
calculateRoute();
}
function removeMarker(e) {
map.eachLayer((layer) => {
if (layer instanceof L.Marker && layer._latlng === e.latlng) {
layer.remove();
}
});
calculateRoute();
}
// Adding controls to manipulate the emission calculation
fetch(predefinedProfilesURL, {method: "GET", headers: {apiKey: api_key}})
.then((response) => response.json())
.then((response, error) => {
if (error) {
console.log(error);
} else {
profiles = response.profiles.filter((p) => p.name !== "PEDESTRIAN" && p.name !== "BICYCLE");
addControls();
}
});
function addControls() {
const routingControl = L.control({ position: "topleft" });
routingControl.onAdd = function (map) {
const div = L.DomUtil.create("div", "routing-control");
const html = `
<h2>Emission Calculation</h2>
<div>
<h3>Emission Options</h3>
<div>
<label for="emissionCalculationMethod" style="display: block;">Emission Calculation Method</label>
<select name="emissionCalculationMethod" id="emissionCalculationMethod" style="display: block; width: 100%;">
<option value="ISO14083_2023">ISO14083_2023</option>
<option value="EN16258_2012">EN16258_2012</option>
<option value="FRENCH_CO2E_DECREE_2017_639">FRENCH_CO2E_DECREE_2017_639</option>
</select>
</div>
<div>
<label for="emissionVersion" style="display: block;">ISO14083 Factors Version</label>
<select name="emissionVersion" id="emissionVersion" style="display: block; width: 100%;">
<option value="INITIAL">INITIAL</option>
<option value="VERSION_2">VERSION_2</option>
<option value="LATEST">LATEST</option>
</select>
</div>
<div>
<label for="defaultConsumption" style="display: block;">Default Consumption</label>
<input type="checkbox" name="defaultConsumption" id="defaultConsumption" style="display: block;"/>
</div>
<h3>Vehicle Options</h3>
<div>
<label for="vehicleProfile" style="display: block;">Vehicle Profile</label>
<select name="vehicleProfile" id="vehicleProfile" style="display: block; width: 100%;">
${profiles.map((profile) => `<option value="${profile.name}">${profile.name}</option>`)}
</select>
</div>
<div>
<label for="engineType" id="engineTypeLabel" style="display: block;">Engine Type</label>
<select name="engineType" id="engineType" style="display: block; width: 100%;">
<option value="COMBUSTION">COMBUSTION</option>
<option value="HYBRID">HYBRID</option>
<option value="ELECTRIC">ELECTRIC</option>
</select>
</div>
<div>
<label for="fuelType" id="fuelTypeLabel" style="display: block;">Fuel Type</label>
<select name="fuelType" id="fuelType" style="display: block; width: 100%;">
<option value="DIESEL">DIESEL</option>
<option value="GASOLINE">GASOLINE</option>
<option value="COMPRESSED_NATURAL_GAS">COMPRESSED_NATURAL_GAS</option>
<option value="LIQUEFIED_PETROLEUM_GAS">LIQUEFIED_PETROLEUM_GAS</option>
<option value="LIQUEFIED_NATURAL_GAS">LIQUEFIED_NATURAL_GAS</option>
<option value="CNG_GASOLINE">CNG_GASOLINE</option>
<option value="LPG_GASOLINE">LPG_GASOLINE</option>
<option value="ETHANOL">ETHANOL</option>
<option value="NONE">NONE</option>
</select>
</div>
<div>
<label for="emissionStandards" id="emissionStandardsLabel" style="display: block;">Emission standards</label>
<select name="emissionStandards" id="emissionStandards" style="display: block; width: 100%;">
<option value="EURO_0">EURO_0</option>
<option value="EURO_1">EURO_1</option>
<option value="EURO_2">EURO_2</option>
<option value="EURO_3">EURO_3</option>
<option value="EURO_4">EURO_4</option>
<option value="EURO_5">EURO_5</option>
<option value="EURO_EEV">EURO_EEV</option>
<option value="EURO_6" selected>EURO_6</option>
<option value="EURO_6C">EURO_6C</option>
<option value="EURO_6D_TEMP">EURO_6D_TEMP</option>
<option value="EURO_6D">EURO_6D</option>
<option value="EURO_6E">EURO_6E</option>
<option value="EURO_7">EURO_7</option>
<option value="NONE">NONE</option>
</select>
</div>
<div>
<label for="electricityType" id="electricityTypeLabel" style="display: block;">Electricity Type</label>
<select name="electricityType" id="electricityType" style="display: block; width: 100%;">
<option value="BATTERY">BATTERY</option>
<option value="HYDROGEN_FUEL_CELL">HYDROGEN_FUEL_CELL</option>
<option value="NONE" selected>NONE</option>
</select>
</div>
<div>
<label for="averageFuelConsumption" id="averageFuelConsumptionLabel" style="display: block;">Fuel Consumption (l / 100km)</label>
<input type="number" id="averageFuelConsumption" name="averageFuelConsumption" style= "width: 100%;"
min="1" step="1" value="35" max="100">
</div>
<div>
<label for="averageElectricityConsumption" id="averageElectricityConsumptionLabel" style="display: block;">Electric Consumption (kWh / 100km)</label>
<input type="number" id="averageElectricityConsumption" name="averageElectricityConsumption" style= "width: 100%;"
min="1" step="1" value="15" max="100">
</div>
<div>
<label for="bioFuelRatio" id="bioFuelRatioLabel" style="display: block;">Bio fuel ratio [0..100]</label>
<input type="number" id="bioFuelRatio" name="bioFuelRatio" style= "width: 100%;"
min="1" step="1" value="0" max="100">
</div>
<div>
<label for="dualFuelRatio" id="dualFuelRatioLabel" style="display: block;">Dual fuel ratio [1..99]</label>
<input type="number" id="dualFuelRatio" name="dualFuelRatio" style= "width: 100%;"
min="1" step="1" value="1" max="99">
</div>
<div>
<label for="hybridRatio" id="hybridRatioLabel" style="display: block;">Hybrid ratio [1..99]</label>
<input type="number" id="hybridRatio" name="hybridRatio" style= "width: 100%;"
min="1" step="1" value="1" max="99">
</div>
</div>
`;
div.innerHTML = html;
L.DomEvent.disableScrollPropagation(div);
L.DomEvent.disableClickPropagation(div);
return div;
};
routingControl.addTo(map);
document.getElementById("emissionCalculationMethod").addEventListener("change", onEmissionCalculationMethodChanged);
document.getElementById("vehicleProfile").addEventListener("change", onVehicleProfileChanged);
const routeCalculationTriggers = ["emissionVersion", "defaultConsumption", "engineType", "fuelType",
"emissionStandards", "electricityType", "averageFuelConsumption", "averageElectricityConsumption", "dualFuelRatio",
"bioFuelRatio", "hybridRatio"];
routeCalculationTriggers.forEach(id => document.getElementById(id).addEventListener("change", calculateRoute));
}
// Call the Routing API
function calculateRoute() {
const waypoints = [];
map.eachLayer((layer) => {
if (layer instanceof L.Marker) {
waypoints.push(layer._latlng);
}
});
if (waypoints.length > 1) {
fetch(routesURL + getQuery(waypoints),
{ method: "GET", headers: { "Content-Type": "application/json", apiKey: api_key } }
).then((response) =>
response.json().then((result) => {
clearResults();
displayPolyline(JSON.parse(result.polyline));
displayResults(result);
})
);
} else {
clearResults();
}
}
function getQuery(waypoints) {
let query = "?results=POLYLINE";
query += "&emissionOptions[calculationMethods]=" + document.getElementById("emissionCalculationMethod").value;
query += "&emissionOptions[iso14083EmissionFactorsVersion]=" + document.getElementById("emissionVersion").value;
query += "&emissionOptions[defaultConsumption]=" + document.getElementById("defaultConsumption").checked;
query += "&profile=" + document.getElementById("vehicleProfile").value;
query += "&vehicle[averageElectricityConsumption]=" + document.getElementById("averageElectricityConsumption").value;
query += "&vehicle[engineType]=" + document.getElementById("engineType").value;
query += "&vehicle[electricityType]=" + document.getElementById("electricityType").value;
query += "&vehicle[dualFuelRatio]=" + document.getElementById("dualFuelRatio").value;
query += "&vehicle[emissionStandard]=" + document.getElementById("emissionStandards").value;
query += "&vehicle[bioFuelRatio]=" + document.getElementById("bioFuelRatio").value;
query += "&vehicle[hybridRatio]=" + document.getElementById("hybridRatio").value;
query += "&vehicle[averageFuelConsumption]=" + document.getElementById("averageFuelConsumption").value;
query += "&vehicle[fuelType]=" + document.getElementById("fuelType").value;
waypoints.forEach((waypoint) => {
query += "&waypoints=" + waypoint.lat + "," + waypoint.lng;
});
return query;
}
var polylineLayer = null;
function displayPolyline(polyline) {
polylineLayer = L.geoJSON(polyline,
{style: { color: "#2882C8", weight: 5, opacity: 0.65 }}
).addTo(map);
map.fitBounds(polylineLayer.getBounds());
}
function onEmissionCalculationMethodChanged() {
const calculationMethod = document.getElementById("emissionCalculationMethod").value;
document.getElementById("emissionVersion").disabled = calculationMethod !== "ISO14083_2023";
document.getElementById("defaultConsumption").disabled = calculationMethod === "FRENCH_CO2E_DECREE_2017_639";
calculateRoute();
}
function onVehicleProfileChanged() {
const profileName = document.getElementById("vehicleProfile").value;
const profile = profiles.find((e) => e.name === profileName);
if (profile?.vehicle?.fuelType && profile?.vehicle?.averageFuelConsumption) {
document.getElementById("fuelType").value = profile.vehicle.fuelType;
document.getElementById("averageFuelConsumption").value = profile.vehicle.averageFuelConsumption;
}
calculateRoute();
}
// How to display the result
const resultControl = L.control({ position: "topright" });
resultControl.onAdd = function (map) {
const div = L.DomUtil.create("div", "result-control-left");
const html = `
<h2>Summary</h2>
<div id="summaryTableWrapper">
<table id="summaryTable"></table>
</div>
<h2>Emissions</h2>
<div id="emissionsReportTableWrapper">
<table id="emissionsCostsTable"></table>
</div>
`;
div.innerHTML = html;
L.DomEvent.disableScrollPropagation(div);
L.DomEvent.disableClickPropagation(div);
return div;
};
resultControl.addTo(map);
const units = {
fuelConsumption: "kg",
co2eTankToWheel: "kg",
co2eWellToWheel: "kg",
energyUseTankToWheel: "MJ",
energyUseWellToWheel: "MJ",
electricityConsumption: "kWh",
};
function displayResults(result) {
$("#summaryTable")
.append(getRow(["Distance", convertDistance(result.distance)]))
.append(getRow(["Travel Time", convertTime(result.travelTime)]));
$("#emissionsCostsTable")
.append(getRow(["<b>Type</b>", "<b>Amount</b>"]));
Object.keys(result.emissions).forEach((emissionResultKey) => {
const emissionResult = result.emissions[emissionResultKey];
Object.keys(emissionResult).forEach((key) =>
$("#emissionsCostsTable")
.append(getRow([key, `${emissionResult[key].toFixed(4)} ${units[key]}`]))
)
});
}
function clearResults() {
if (polylineLayer) {
map.removeLayer(polylineLayer);
}
$("#summaryTable").empty();
$("#emissionsCostsTable").empty();
}
function getRow(columns) {
let row = "";
columns.forEach((col) => {
row += "<td>" + col + "</td>";
});
return $.parseHTML("<tr>" + row + "</tr>");
}
});