Skip to content

Instantly share code, notes, and snippets.

@lobodin
Last active March 30, 2023 06:23
Show Gist options
  • Save lobodin/5204793 to your computer and use it in GitHub Desktop.
Save lobodin/5204793 to your computer and use it in GitHub Desktop.
Import GeoNames.org cities data to mongodb.

#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));
@tobsn
Copy link

tobsn commented Sep 1, 2015

wouldn't you want to include the type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment