Skip to content

Instantly share code, notes, and snippets.

@stephenkeep
Forked from lobodin/README.md
Last active August 29, 2015 14:16
Show Gist options
  • Save stephenkeep/335051194b257ad5db8a to your computer and use it in GitHub Desktop.
Save stephenkeep/335051194b257ad5db8a to your computer and use it in GitHub Desktop.

#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