Skip to content

Instantly share code, notes, and snippets.

@skamenetskiy
Last active August 29, 2015 14:26
Show Gist options
  • Save skamenetskiy/93623473d4b926dcba74 to your computer and use it in GitHub Desktop.
Save skamenetskiy/93623473d4b926dcba74 to your computer and use it in GitHub Desktop.
{"latitude": "52.986375", "user_id": 12, "name": "Christina McArdle", "longitude": "-6.043701"}
{"latitude": "51.92893", "user_id": 1, "name": "Alice Cahill", "longitude": "-10.27699"}
{"latitude": "51.8856167", "user_id": 2, "name": "Ian McArdle", "longitude": "-10.4240951"}
{"latitude": "52.3191841", "user_id": 3, "name": "Jack Enright", "longitude": "-8.5072391"}
{"latitude": "53.807778", "user_id": 28, "name": "Charlie Halligan", "longitude": "-7.714444"}
{"latitude": "53.4692815", "user_id": 7, "name": "Frank Kehoe", "longitude": "-9.436036"}
{"latitude": "54.0894797", "user_id": 8, "name": "Eoin Ahearn", "longitude": "-6.18671"}
{"latitude": "53.038056", "user_id": 26, "name": "Stephen McArdle", "longitude": "-7.653889"}
{"latitude": "54.1225", "user_id": 27, "name": "Enid Gallagher", "longitude": "-8.143333"}
{"latitude": "53.1229599", "user_id": 6, "name": "Theresa Enright", "longitude": "-6.2705202"}
{"latitude": "52.2559432", "user_id": 9, "name": "Jack Dempsey", "longitude": "-7.1048927"}
{"latitude": "52.240382", "user_id": 10, "name": "Georgina Gallagher", "longitude": "-6.972413"}
{"latitude": "53.2451022", "user_id": 4, "name": "Ian Kehoe", "longitude": "-6.238335"}
{"latitude": "53.1302756", "user_id": 5, "name": "Nora Dempsey", "longitude": "-6.2397222"}
{"latitude": "53.008769", "user_id": 11, "name": "Richard Finnegan", "longitude": "-6.1056711"}
{"latitude": "53.1489345", "user_id": 31, "name": "Alan Behan", "longitude": "-6.8422408"}
{"latitude": "53", "user_id": 13, "name": "Olive Ahearn", "longitude": "-7"}
{"latitude": "51.999447", "user_id": 14, "name": "Helen Cahill", "longitude": "-9.742744"}
{"latitude": "52.966", "user_id": 15, "name": "Michael Ahearn", "longitude": "-6.463"}
{"latitude": "52.366037", "user_id": 16, "name": "Ian Larkin", "longitude": "-8.179118"}
{"latitude": "54.180238", "user_id": 17, "name": "Patricia Cahill", "longitude": "-5.920898"}
{"latitude": "53.0033946", "user_id": 39, "name": "Lisa Ahearn", "longitude": "-6.3877505"}
{"latitude": "52.228056", "user_id": 18, "name": "Bob Larkin", "longitude": "-7.915833"}
{"latitude": "54.133333", "user_id": 24, "name": "Rose Enright", "longitude": "-6.433333"}
{"latitude": "55.033", "user_id": 19, "name": "Enid Cahill", "longitude": "-8.112"}
{"latitude": "53.521111", "user_id": 20, "name": "Enid Enright", "longitude": "-9.831111"}
{"latitude": "51.802", "user_id": 21, "name": "David Ahearn", "longitude": "-9.442"}
{"latitude": "54.374208", "user_id": 22, "name": "Charlie McArdle", "longitude": "-8.371639"}
{"latitude": "53.74452", "user_id": 29, "name": "Oliver Ahearn", "longitude": "-7.11167"}
{"latitude": "53.761389", "user_id": 30, "name": "Nick Enright", "longitude": "-7.2875"}
{"latitude": "54.080556", "user_id": 23, "name": "Eoin Gallagher", "longitude": "-6.361944"}
{"latitude": "52.833502", "user_id": 25, "name": "David Behan", "longitude": "-8.522366"}
#!/usr/local/bin/node
/**
* Head office coordinates (Dublin, Ireland)
* @type {GpsPoint}
*/
var headOffice = new GpsPoint(53.3381985, -6.2592576);
/**
* Maximum distance from the head office (in kilometers)
* @type {Number}
*/
var maximumDistance = 100;
/**
* Gps point object
* @param latitude
* @param longitude
* @constructor
*/
function GpsPoint(latitude, longitude) {
/**
* Point object
* @param coordinate
* @constructor
*/
function Point(coordinate) {
/**
* Returns radians
* @returns {Number}
*/
this.toRadians = function () {
return coordinate * Math.PI / 180;
};
/**
* Returns original value
* @returns {String}
*/
this.getCoordinate = function () {
return coordinate;
};
/**
* Override: toString
* @returns {String}
*/
this.toString = function () {
return this.getCoordinate();
};
}
// Convert latitude and longitude to Point objects
latitude = new Point(latitude);
longitude = new Point(longitude);
/**
* Getter: latitude
* @returns {GpsPoint.Point}
*/
this.getLatitude = function () {
return latitude;
};
/**
* Getter: longitude
* @returns {GpsPoint.Point}
*/
this.getLongitude = function () {
return longitude;
};
}
/**
* Returns the earth radius
* @returns {Number}
*/
GpsPoint.getEarthRadius = function () {
return 6371000;
};
/**
* Calculates distance (in kilometers) to GPS point
* @param {GpsPoint} point
* @returns {Number}
*/
GpsPoint.prototype.getDistanceTo = function (point) {
/**
* This gps point radians
* @type {Number}
*/
var thisLatitudeRadian = this.getLatitude().toRadians(),
thisLongitudeRadian = this.getLongitude().toRadians();
/**
* Target gps point radians
* @type {Number}
*/
var pointLatitudeRadian = point.getLatitude().toRadians(),
pointLongitudeRadian = point.getLongitude().toRadians();
/**
* The difference between this and the target gps points
* @type {Number}
*/
var latitudeDifference = pointLatitudeRadian - thisLatitudeRadian,
longitudeDifference = pointLongitudeRadian - thisLongitudeRadian;
/**
* The math
* @type {Number}
*/
var a = Math.sin(latitudeDifference / 2) * Math.sin(latitudeDifference / 2) +
Math.cos(thisLatitudeRadian) * Math.cos(pointLatitudeRadian) *
Math.sin(longitudeDifference / 2) * Math.sin(longitudeDifference / 2),
b = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return (GpsPoint.getEarthRadius() * b) / 1000;
};
/**
* Customer object
* @param customerData
* @constructor
*/
function Customer(customerData) {
/**
* Customer id
* @type {Number}
*/
var id = parseInt(customerData.user_id);
/**
* Customer gps position
* @type {GpsPoint}
*/
var position = new GpsPoint(
parseFloat(customerData.latitude),
parseFloat(customerData.longitude)
);
/**
* Returns customer ID
* @returns {Number}
*/
this.getId = function () {
return id
};
/**
* Returns customer name
* @returns {String}
*/
this.getName = function () {
return customerData.name;
};
/**
* Returns customer gps position
* @returns {GpsPoint}
*/
this.getGpsPoint = function () {
return position
};
}
/**
* CustomerList object
* @constructor
*/
function CustomerList() {
}
/**
* Sort ascending pseudo-constant
* @type {Number}
*/
CustomerList.SORT_ASC = 1;
/**
* Sort descending pseudo-constant
* @type {number}
*/
CustomerList.SORT_DESC = -1;
/**
* Extending Array prototype
* @type {Array}
*/
CustomerList.prototype = [];
/**
* Sorts the list by customer id
* @param [direction]
* @returns {CustomerList}
*/
CustomerList.prototype.sortByCustomerId = function (direction) {
direction = direction || CustomerList.SORT_ASC;
return this.sort(function (a, b) {
if (a.getId() == b.getId()) {
return 0;
}
return a.getId() > b.getId() ? direction : -direction;
})
};
/**
* Customers list
* @type {CustomerList|Array}
*/
var list = new CustomerList;
// Read the file line by line
require('readline')
.createInterface({
input: require('fs').createReadStream('./customers.json'),
output: process.stdout,
terminal: false
})
// Execute on every line
.on('line', function (line) {
/**
* Customer object
* @type {Customer}
*/
var customer = new Customer(JSON.parse(line));
// Check that the distance between the customer and the head office is less than or equals the maximum distance
if (customer.getGpsPoint().getDistanceTo(headOffice) <= maximumDistance) {
list.push(customer);
}
})
// Execute when all the lines are read
.on('close', function () {
// Sort the list by customer id
list.sortByCustomerId()
// Output the result
.forEach(function (customer) {
process.stdout.write(
'Customer ID: ' + customer.getId() +
'; Customer name: ' + customer.getName() + '\n'
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment