Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 18 additions & 48 deletions backend/controllers/getCongestion.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,22 @@
const axios = require("axios");
const API_URL = "https://route.ls.hereapi.com/routing/7.2/calculateroute.json";
const getColorForPercentage = require("../utils/getColorForPercentage");

async function getCongestionData(route, results, departureTime) {
let promises = [];
for (let i = 0; i < route.length; i++) {
let p = axios.get(API_URL, {
params: {
waypoint0: `${route[i].begin.lat},${route[i].begin.lng}`,
waypoint1: `${route[i].end.lat},${route[i].end.lng}`,
mode: "fastest;car;traffic:enabled",
apiKey: process.env.here_api_key,
departure: departureTime,
},
});
promises.push(p);
}
try {
let responses = await Promise.all(promises);
let result = [];
for (let i = 0; i < route.length; i++) {
let { baseTime, travelTime } = responses[
i
].data.response.route[0].summary;
let congestionValue = travelTime / baseTime;
let congestionColor =
route[i].transport.mode === "pedestrian"
? "green"
: getCongestionColor(congestionValue);

result = [...result, { ...route[i], congestionColor }];
}
results.push(result);
} catch (err) {
console.log(err);
}
}
async function calculateCongestion(routes, departureTime) {
let results = [];
for (let i = 0; i < routes.length; i++) {
await getCongestionData(routes[i], results, departureTime);
}
return results;
async function calculateCongestion(congestionParams) {
const congestionData =
congestionParams && congestionParams.length
? congestionParams.map((param, index) => {
const { baseDuration, duration } = param
? param
: { baseDuration: 1, duration: 1 };
const congestionValue =
duration / baseDuration < 1 ? 1 : duration / baseDuration;
const normalizedCongestion = Math.min(congestionValue - 1, 1);
return {
congestionValue,
congestionColor: getColorForPercentage(normalizedCongestion),
};
})
: [];
return congestionData;
}

function getCongestionColor(congestionValue) {
if (congestionValue < 1.25) return "green";
else if (congestionValue >= 1.25 && congestionValue < 1.5) return "blue";
else if (congestionValue >= 1.5 && congestionValue < 2) return "orange";
else if (congestionValue >= 2) return "red";
}
module.exports = calculateCongestion;
53 changes: 39 additions & 14 deletions backend/controllers/getPM2_5.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.updateDatabase = async () => {
let lat1 = 28.08652,
lat2 = 28.921631,
long1 = 76.730347,
long2 = 77.631226;
long2 = 77.631226; // bouding coordinates for delhi region
const api_url = `https://api.waqi.info/map/bounds/?latlng=${lat1},${long1},${lat2},${long2}&token=${process.env.waqi_api_key}`;
await axios
.get(api_url)
Expand Down Expand Up @@ -41,7 +41,7 @@ exports.updateDatabase = async () => {
// console.log(database);
};

exports.getPM2_5 = (lat, lng) => {
exports.getPM2_5 = (lat, lng, currentDatabase) => {
const x1 = lat;
const y1 = lng;

Expand All @@ -58,7 +58,6 @@ exports.getPM2_5 = (lat, lng) => {

return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
}
const currentDatabase = [...database]; // use contant value for a particular calculation as database is updated every 10 seconds
for (let i = 0; i < currentDatabase.length; i++) {
const x2 = currentDatabase[i].latitude;
const y2 = currentDatabase[i].longitude;
Expand All @@ -84,19 +83,45 @@ exports.getPMColor = (routes = [], minPm, maxPm) => {
routes.forEach((route) => {
colorizedPmRoutes = [
...colorizedPmRoutes,
route && route.length > 0
? route.map((section) => {
const normalizedPm =
maxPm > minPm ? (section.pmValue - minPm) / (maxPm - minPm) : -1;
return {
...section,
normalizedPm,
pmColor: getColorForPercentage(normalizedPm),
};
})
: {},
{
...route,
sections:
route.sections && route.sections.length > 0
? route.sections.map((section) => {
const newSpans = section.spans.map((span) => {
const normalizedPm =
maxPm > minPm
? (span.pmValue - minPm) / (maxPm - minPm)
: -1;
return {
...span,
pmColor: getColorForPercentage(normalizedPm),
};
});
return {
...section,
spans: newSpans,
};
})
: [],
},
];
});
// console.log(colorizedPmRoutes)
return colorizedPmRoutes;
};

exports.calculatePmValuesList = async (locationsList) => {
const currentDatabase = [...database]; // use contant value for a particular calculation as database is updated every 10 seconds
const promises = locationsList.map((location) => {
return new Promise((resolve, reject) =>
resolve(this.getPM2_5(location.lat, location.lng, currentDatabase))
);
});
try {
const responses = await Promise.all(promises);
return responses;
} catch (err) {
console.log(err);
}
};
Loading