Reputation: 1317
So I understand that this isn't the traditional way to share code but seeing how there isn't anything on the stack about how to accomplish such a task - I figured I'd share.
To further explain, I looked everywhere for an easy to use Node.JS module to process geonames data into a mongo database. The select few projects I found turned out to be a bust. Perhaps people aren't sharing because it really is such a simple task. With that said, I still think it could be valuable to share what I have.
The following answer will show how to process geonames data from http://geonames.org into a usable object that can then be saved into a database or just used out right.
Upvotes: 1
Views: 2260
Reputation: 1317
To further extend the above answer. I have included an example that uses the geonames Node.JS module from Github. ( https://github.com/bugs181/geonames )
Disclaimer: I am the author of the aforementioned github repo.
This example can also be found on the Github repo.
var geonames = require('./geonames.js');
// Intialize geonames module.
geonames.init();
// 34.0500, -118.2500 - Los Angeles, California - More specifically Bunker Hill, California
// 34.057°N 118.238°W - San Francisco, California - More specifically Opera Plaza, California
geonames.city(34.0500, -118.2500, function(err, result) {
if (err) {
console.log("There was an error resolving coordinates.");
console.log(err);
return;
}
console.log("Result: " + JSON.stringify(result, true, 3));
});
Output:
[geonames] Resolve city from coordinates. [34.05, -118.25]
Result: {
"city": "Bunker Hill",
"state": "CA"
}
You may use strings for the latitude and longitude as well but you must convert them for the Mongo database. Use the Number()
method to do this.
For example:
var lat = "34.0500";
var lon = "-118.2500";
geonames.city(Number( lat ), Number( lon ), function(err, result) {
Upvotes: 0
Reputation: 1317
var lineReader = require('line-reader');
lineReader.eachLine('./geonames-data/US/US.txt', function(line, last) {
var city = line.split('\t').structure();
if (city.is) { // if this is a city.
// Delete un-needed object properties so we don't add useless info to DB.
delete city.is;
delete city.type;
// Do something with the city object.
console.log(JSON.stringify(city, true, 3) + '\n');
}
});
Array.prototype.structure = function() {
// from geonames readme: - P.PPL populated place a city, town, village, or other agglomeration of buildings where people live and work
return {
_id: Number(this[0]),
name: this[1],
state: this[10],
country: this[8],
coordinates: [this[4], this[5]],
timezone: this[17],
is: ((this[6] == "P") ? true : false), // if object type is city.
type: ((this[6] == "P") ? "city" : "other") // todo: add a parse function to parse other geonames db types
}
}
Upvotes: 2