Skip to content

Instantly share code, notes, and snippets.

@evdb
Created August 27, 2013 11:58
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 evdb/6352637 to your computer and use it in GitHub Desktop.
Save evdb/6352637 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/node
var _ = require('underscore'),
rest = require('restler'),
async = require('async');
// Get the mapit ids from command line
var ids = _.rest(process.argv, 2);
console.log(ids);
// Run in series to avoid restler bug with getting the same content back for each request.
async.mapSeries(
ids,
getCentreForMapitArea,
function (err, results) {
var average = averageCentres(results);
console.log("LonLat: %s,%s", average.lon, average.lat);
}
);
function getCentreForMapitArea (areaID, cb) {
var done = false;
var url = "http://za-pombola.staging.mysociety.org/mapit/area/" + areaID + "/geometry";
// console.log("GET %s", url);
rest
.get(url)
.on("complete", function (result) {
if (done) return; // restler calls complete several times. Known bug.
done = true;
var centre = {
lat: result.centre_lat,
lon: result.centre_lon,
};
console.log(areaID, centre);
cb(null, centre);
});
}
function averageCentres (centres) {
// This is not geographically correct, but good enough for an estimate on already approximated data.
var count = centres.length;
var lat_sum = _.chain(centres).pluck('lat').reduce(function (a,b) {return a+b}, 0 ).value();
var lon_sum = _.chain(centres).pluck('lon').reduce(function (a,b) {return a+b}, 0 ).value();
return {
lat: lat_sum / count,
lon: lon_sum / count
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment