Created
September 3, 2014 21:11
-
-
Save ryanhanwu/960176e1f975ab1b9cba to your computer and use it in GitHub Desktop.
Lookup csv address and convert to latitude and longitude
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
var geocoder = require('geocoder'), | |
seqqueue = require('seq-queue'), | |
csv = require("fast-csv"), | |
fs = require('fs'), | |
stream = fs.createReadStream("pharmacy.csv");//CSV File with pharmacy data from database | |
var LookupAndPrint = function() { | |
this.queue = seqqueue.createQueue(1000); | |
}; | |
LookupAndPrint.prototype.cl = function(addr, data) { | |
this.queue.push( | |
function(task) { | |
setTimeout(function() { | |
geocoder.geocode(addr, function(err, res) { | |
if(err) console.dir(err); | |
var location = res.results[0].geometry.location; | |
//name, address, city, country,zipcode,phone,description, latitude, longitude,zoom_level,status,sortorder, created_time, update_time | |
var output = [data[2], (data[5] + data[6]).replace(' #',''), data[1], "TW", data[4], data[8], data[7], location.lat, location.lng, 6, 1, 0, "2014-08-25 22:54:45", "2014-08-25 22:54:45"]; | |
var op = output.join(','); | |
console.dir(op); | |
fs.appendFile('result.csv', op + '\n', function (err) { | |
}); | |
task.done(); | |
}); | |
}, 1000); | |
}, function() { | |
}, 0); | |
}; | |
var lap = new LookupAndPrint(); | |
var csvStream = csv().on("record", function(data) { | |
var address = [data[5] + data[6], "Taiwan", data[4]]; | |
// console.dir(address.join(',')); | |
lap.cl(address.join(','), data); | |
}).on("end", function() { | |
console.log("done"); | |
}); | |
stream.pipe(csvStream); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment