-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (56 loc) · 1.86 KB
/
app.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
const express = require("express");
const xlsx = require("xlsx");
const path = require("path");
const app = express();
const filePath = path.join(__dirname, "data", "postcode-lat-long.xlsx");
const workbook = xlsx.readFile(filePath);
const sheetName = workbook.SheetNames[0];
const data = xlsx.utils.sheet_to_json(workbook.Sheets[sheetName]);
data.forEach((row) => {
row.lat = parseFloat(row.lat);
row.long = parseFloat(row.long);
});
function calculateDistance(lat1, lon1, lat2, lon2) {
const R = 6371;
const dLat = ((lat2 - lat1) * Math.PI) / 180;
const dLon = ((lon2 - lon1) * Math.PI) / 180;
const a =
Math.sin(dLat / 2) ** 2 +
Math.cos((lat1 * Math.PI) / 180) *
Math.cos((lat2 * Math.PI) / 180) *
Math.sin(dLon / 2) ** 2;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
function postcodesWithinRadius(postcode, radius) {
const start = data.find((row) => row.pcd === postcode);
if (!start) {
return { error: `Postcode ${postcode} not found in the dataset.` };
}
const { lat: lat1, long: lon1 } = start;
const results = data
.map((row) => ({
postcode: row.pcd,
distance: calculateDistance(lat1, lon1, row.lat, row.long),
}))
.filter((row) => row.distance <= radius);
return results;
}
app.get("/find-postcodes", (req, res) => {
const { postcode, radius } = req.query;
if (!postcode || !radius) {
return res
.status(400)
.json({ error: "Please provide 'postcode' and 'radius' parameters." });
}
const numericRadius = parseFloat(radius);
if (isNaN(numericRadius)) {
return res.status(400).json({ error: "'radius' must be a numeric value." });
}
const result = postcodesWithinRadius(postcode, numericRadius);
res.json(result);
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});