Created
May 17, 2015 18:51
-
-
Save ptbrowne/60d7338502de1d16ac46 to your computer and use it in GitHub Desktop.
building at lat lng
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// npm install bluebird request xml-parser lodash | |
// usage: node buildingAt.js 43.699100505623726 7.264087200164794 | |
var Promise = require('bluebird'); | |
var request = Promise.promisify(require('request')); | |
var parse = require('xml-parser'); | |
var _ = require('lodash'); | |
var isBuildingNode = function (node) { | |
return _.any(node.children, function (t) { | |
return t.name == 'tag' && t.attributes['k'] == 'building' && t.attributes['v'] == 'yes'; | |
}); | |
}; | |
var nodeToObj = function (n) { | |
var props = _(n.children) | |
.filter(function (t) { return t.name == 'tag'; }) | |
.map(function (t) { return [t.attributes['k'], t.attributes['v']]; }) | |
.object() | |
.value(); | |
return _.extend({}, n.attributes, props); | |
}; | |
var getBuildingsAt = function (lat, lng) { | |
return request({ | |
url: 'http://overpass.osm.rambler.ru/cgi/interpreter', | |
qs: { | |
data: '<coord-query lat="'+ lat +'" lon="'+ lng +'"/><print mode="body"/>' | |
} | |
}).spread(function (response, body) { | |
var data = parse(body); | |
var buildingNodes = _(data.root.children) | |
.filter(isBuildingNode) | |
.map(nodeToObj) | |
.value(); | |
return buildingNodes | |
}) | |
}; | |
getBuildingsAt(process.argv[2], process.argv[3]) | |
.then(function (buildings) { | |
console.log(buildings); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment