Skip to content

Instantly share code, notes, and snippets.

@myke11j
Created July 12, 2018 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myke11j/9ba21b4a120a924312ac09f7da2ad47b to your computer and use it in GitHub Desktop.
Save myke11j/9ba21b4a120a924312ac09f7da2ad47b to your computer and use it in GitHub Desktop.

Instructions

Simple running above file via node will show the name of organisation with address and rounded distance from Centre London in KMs.

node findNearestOffices.js

By deafult, it fetches offices within 100km of Centre London, but it is configurable. For eg, to find offices within 500km of Centre London, pass it as env variable like,

OFFSET_DISTANCE=500 && node findNearestOffices.js

'use strict'
const https = require('https');
const milesToKm = 1.609344; // in Kms
const londonCoords = [51.515419, -0.141099];
const API_URL = 'https://success.spidergap.com/partners.json?inf_contact_key=e1630715cd03ec4f60b3b9267d9d79586f50da9776b71adcafe8af8556562912';
const offsetDist = process.env.OFFSET_DISTANCE || 100;
/**
* @desc Find distance between two locations in Kms
* @param {*} Object
*/
const getDistance = (source, target) => {
const {
lat1, long1
} = source;
const {
lat2, long2
} = target
const radlat1 = Math.PI * lat1/180
const radlat2 = Math.PI * lat2/180
const theta = long1-long2
const radtheta = Math.PI * theta/180
let dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
dist = dist * milesToKm
return dist
}
/**
* @desc Service for fetching data from Spidergap dataset
*/
const getSpiderGapData = () => new Promise((resolve, reject) => {
console.log('Fetching data for SpiderGap Sample Dataset');
https.get(API_URL, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk
});
res.on('end', (chunk) => {
return resolve(JSON.parse(data));
});
}).on('error', (e) => {
console.error(e);
return reject(e);
});
});
/**
*
* @param {Number} offset - Number of kms within source
* @param {*} done - Callback, returns list of sorted office name with address
*/
const fetchOfficesByDistance = async (offset, done) => {
try {
const dataset = await getSpiderGapData();
const result = [];
dataset.map((partner) => {
const {
offices
} = partner;
offices.map((office) => {
const targetCoords = office.coordinates
.split(',')
.map(x => parseFloat(x));
const distance = getDistance(
{ lat1: londonCoords[0], long1: londonCoords[1] },
{ lat2: targetCoords[0], long2: targetCoords[1] }
);
if (distance < offset) {
result.push({
name: partner.organization,
address: office.location,
distance
})
}
});
});
return done(result.sort(x => x.name));
} catch (error) {
throw new Error(error)
}
};
fetchOfficesByDistance(offsetDist, (listOfOffices) => {
console.log(`List of offices within ${offsetDist} kms are: \n`);
listOfOffices.forEach(office => console.log(`${office.name}, ${office.address} ${Math.floor(office.distance)} KMs\n`));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment