Add find nearest feature

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2024-06-30 21:23:22 +01:00
parent 2a9050940d
commit 9031eb53c6
3 changed files with 17 additions and 11 deletions

14
package-lock.json generated
View File

@ -3138,12 +3138,12 @@
} }
}, },
"node_modules/braces": { "node_modules/braces": {
"version": "3.0.2", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz",
"integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"fill-range": "^7.0.1" "fill-range": "^7.1.1"
}, },
"engines": { "engines": {
"node": ">=8" "node": ">=8"
@ -4297,9 +4297,9 @@
} }
}, },
"node_modules/fill-range": { "node_modules/fill-range": {
"version": "7.0.1", "version": "7.1.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz",
"integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"to-regex-range": "^5.0.1" "to-regex-range": "^5.0.1"

View File

@ -179,10 +179,11 @@ async function getReasonCode(code) {
} }
async function getNearestStations(lat, long) { async function getNearestStations(lat, long) {
logger.debug(`ldbService.getNearestStations: Fetching nearest stations`) logger.debug(`ldbService.getNearestStations: Fetching nearest stations`)
let pipeline = findStationsByDistancePipeline(4, lat, long) let pipeline = findStationsByDistancePipeline(4, lat, long)
try { try {
return await db.queryAggregate(pipeline) return await db.queryAggregate("stations", pipeline)
} catch (err) { } catch (err) {
logger.error(err, `ldbService.getNearestStations`) logger.error(err, `ldbService.getNearestStations`)
} }

View File

@ -1,11 +1,13 @@
export function findStationsByDistancePipeline(count: number, latitude: number, longitude: number) { export function findStationsByDistancePipeline(count: number, latitude: string, longitude: string) {
return [ const numericLatitude = parseFloat(latitude)
const numericLongitude = parseFloat(longitude)
const pipeline = [
{ {
'$geoNear': { '$geoNear': {
'near': { 'near': {
'type': 'Point', 'type': 'Point',
'coordinates': [ 'coordinates': [
longitude, latitude numericLongitude, numericLatitude
] ]
}, },
'distanceField': 'distance' 'distanceField': 'distance'
@ -39,4 +41,7 @@ export function findStationsByDistancePipeline(count: number, latitude: number,
} }
} }
] ]
//console.log(JSON.stringify(pipeline))
return pipeline
} }