1

Adding script to add geo information to data

This commit is contained in:
Alex Yatskov 2014-11-17 14:35:29 +09:00
parent 534deca939
commit dbfd9caa91
2 changed files with 55 additions and 1 deletions

View File

@ -22,6 +22,7 @@
"gulp-nodemon": "^1.0.4", "gulp-nodemon": "^1.0.4",
"gulp-replace": "^0.4.0", "gulp-replace": "^0.4.0",
"gulp-uglify": "^1.0.1", "gulp-uglify": "^1.0.1",
"main-bower-files": "^2.0.0" "main-bower-files": "^2.0.0",
"jsonfile": "~2.0.0"
} }
} }

53
scrape/geocode.js Executable file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env node
var geocoder = require('node-geocoder');
var jf = require('jsonfile');
var _ = require('underscore');
function queryPosition(gc, address, cache, callback) {
if (_.has(cache, address)) {
console.log('Cache lookup success for:\n\t%s', address);
callback(cache[address]);
return;
}
gc.geocode(address, function(err, res) {
if (err) {
console.log('Geocode lookup fail for: \n\t%s', address);
callback(null);
}
else {
console.log('Geocode lookup success for: \n\t%s', address);
callback(cache[address] = res[0]);
}
});
}
function main() {
var gc = geocoder.getGeocoder('google', 'http', {});
var srcData = jf.readFileSync('data.json');
var srcCount = srcData.length;
var cacheData = jf.readFileSync('cache/geo.json', {throws: false}) || {};
var destData = [];
_.each(srcData, function(srcItem) {
queryPosition(gc, srcItem.address, cacheData, function(geo) {
if (geo) {
var destItem = _.clone(srcItem);
destItem.geo = geo;
destData.push(destItem);
}
if (--srcCount === 0) {
jf.writeFileSync('data.json', destData);
jf.writeFileSync('cache/geo.json', cacheData);
}
});
});
}
if (require.main === module) {
main();
}