Simon's posterous

« Back to blog

Import a bunch of geo location data into GeoCouch

I found this site http://earth-info.nga.mil/gns/html/cntry_files.html which has the long/lat for a bunch of cities around the world. I wanted to dump this into GeoCouch quickly, without firing up a serious code editor. The following bash script does just that for Saint Lucia and I thought I'd record it here for posterity.

I've not tried to import *all* the cities in the world, yet... ;)

# Populating geo couch with all the cities in the world

# Create the database
curl -X PUT http://localhost:5985/places

curl -X PUT -d '{"spatial":{"points":"function(doc) {\n    if (doc.loc) {\n        emit({\n            type: \"Point\",\n            coordinates: [doc.loc[0], doc.loc[1]]\n        }, [doc._id, doc.loc]);\n    }};"}}' http://localhost:5985/places/_design/main

# Get the data

wget http://earth-info.nga.mil/gns/html/geonames_dd_dms_date_20100706.zip

unzip geonames_dd_dms_date_20100706.zip

# Want to use \n as a line separator

ORIGIFS=$IFS

# set $IFS to end-of-line

IFS=`echo -en "\n\b"`

# Import the data

for i in `tail -7383059 geonames_dd_dms_date_20100706.txt |awk -F '\t' {'print "{\"loc\":[" $4 ", " $5 "], \"_id\": \"" $23 "\"}"'}`; do echo $i; curl -X POST -d $i http://localhost:5985/places/; done;

# set $IFS back

IFS=$ORIGIFS

curl -X GET 'http://localhost:5985/places/_design/main/_spatial/points?bbox=0,0,180,90'

Comments (1)

Jul 13, 2010
Simon Metson said...
Also, couldn't work out how to use a pre tag in the above...

Leave a comment...