2014-11-17 05:35:29 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2015-01-05 05:27:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var _ = require('underscore');
|
2014-11-17 05:35:29 +00:00
|
|
|
var geocoder = require('node-geocoder');
|
2015-01-05 05:27:05 +00:00
|
|
|
var geolib = require('geolib');
|
2014-11-17 05:35:29 +00:00
|
|
|
var jf = require('jsonfile');
|
|
|
|
|
2015-01-13 06:59:09 +00:00
|
|
|
|
2014-11-17 05:57:35 +00:00
|
|
|
function queryPosition(gc, address, cache, sequence, callback) {
|
2014-11-17 05:35:29 +00:00
|
|
|
if (_.has(cache, address)) {
|
|
|
|
console.log('Cache lookup success for:\n\t%s', address);
|
|
|
|
callback(cache[address]);
|
2014-11-17 05:57:35 +00:00
|
|
|
return sequence;
|
2014-11-17 05:35:29 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 05:57:35 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, sequence * 200);
|
|
|
|
|
|
|
|
return sequence + 1;
|
2014-11-17 05:35:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 05:27:05 +00:00
|
|
|
function buildAccess(reviewData, stationData, accessibility) {
|
|
|
|
_.each(reviewData, function(reviewItem) {
|
|
|
|
var distMin = Number.MAX_VALUE;
|
2015-01-05 06:46:12 +00:00
|
|
|
var station = '';
|
2015-01-05 05:27:05 +00:00
|
|
|
|
|
|
|
console.log('Computing access for: \n\t%s', reviewItem.name);
|
2015-01-05 06:46:12 +00:00
|
|
|
_.each(stationData, function(stationItem, stationName) {
|
2015-01-05 05:27:05 +00:00
|
|
|
var distance = geolib.getDistance(reviewItem.geo, stationItem.geo);
|
2015-01-05 06:46:12 +00:00
|
|
|
if (distance < distMin) {
|
|
|
|
station = stationName;
|
|
|
|
distMin = distance;
|
|
|
|
}
|
2015-01-05 05:27:05 +00:00
|
|
|
});
|
|
|
|
|
2015-01-05 06:46:12 +00:00
|
|
|
reviewItem.distanceToStn = distMin;
|
|
|
|
reviewItem.closestStn = station;
|
2015-01-05 05:27:05 +00:00
|
|
|
});
|
|
|
|
}
|
2014-11-17 05:35:29 +00:00
|
|
|
|
|
|
|
function main() {
|
2015-01-05 05:27:05 +00:00
|
|
|
var gc = geocoder.getGeocoder('google', 'http', {});
|
|
|
|
var sequence = 0;
|
|
|
|
|
|
|
|
var stationData = jf.readFileSync('stations.json');
|
|
|
|
var cacheData = jf.readFileSync('cache/geo.json', {throws: false}) || {};
|
|
|
|
var reviewData = jf.readFileSync('data.json');
|
|
|
|
var reviewCount = reviewData.length;
|
|
|
|
var reviewDataDest = [];
|
|
|
|
|
|
|
|
_.each(reviewData, function(reviewItem) {
|
|
|
|
sequence = queryPosition(gc, reviewItem.address, cacheData, sequence, function(geo) {
|
2014-11-17 05:35:29 +00:00
|
|
|
if (geo) {
|
2015-01-05 05:27:05 +00:00
|
|
|
var destItem = _.clone(reviewItem);
|
2014-11-17 05:35:29 +00:00
|
|
|
destItem.geo = geo;
|
2015-01-05 05:27:05 +00:00
|
|
|
reviewDataDest.push(destItem);
|
2014-11-17 05:35:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 05:27:05 +00:00
|
|
|
if (--reviewCount === 0) {
|
|
|
|
buildAccess(reviewDataDest, stationData);
|
|
|
|
|
|
|
|
jf.writeFileSync('data.json', reviewDataDest);
|
2014-11-17 05:35:29 +00:00
|
|
|
jf.writeFileSync('cache/geo.json', cacheData);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
main();
|
|
|
|
}
|