Skip to content

Instantly share code, notes, and snippets.

@mpmckenna8
Forked from lobodin/README.md
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpmckenna8/10899328 to your computer and use it in GitHub Desktop.
Save mpmckenna8/10899328 to your computer and use it in GitHub Desktop.
Geonames cities to mongoDB thing

#Generate cities data

  1. wget http://download.geonames.org/export/dump/cities15000.zip
  2. unzip cities15000.zip
  3. node geonames.js cities15000.txt

The result is json array of cities in the following format:

{
	_id: <ID>
	name: <Name>,
	alternate_names: [<alternatename1>,…],
	coordinates: [<Latitude>, <Longitude>]
}

#Import to MongoDB

mongoimport -d db_name -c cities cities.json --jsonArray
var fs = require('fs')
, _ = require('underscore');
var filename = process.argv[2];
if (!filename) {
console.log('File not found');
process.exit(1);
}
var cities = fs.readFileSync(filename).toString().split("\n");
cities.pop(); // empty element
var processedCities = _.map(cities, function(city) {
var data = city.split('\t');
var names = data[3].split(',');
return {
_id: Number(data[0]),
name: data[1],
alternate_names: _.map(names, function(name) {
return name.toLowerCase();
}),
coordinates: [Number(data[4]), Number(data[5])]
}
});
fs.writeFileSync('cities.json', JSON.stringify(processedCities));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment