Skip to content

Instantly share code, notes, and snippets.

@manchoz
Created May 18, 2012 15:18
Show Gist options
  • Save manchoz/2725821 to your computer and use it in GitHub Desktop.
Save manchoz/2725821 to your computer and use it in GitHub Desktop.
Address geocoding in Bash with Google Maps API
#!/bin/bash
# Quick and dirty geocoding with Google Maps API
MAPSAPIURL="http://maps.googleapis.com/maps/api/geocode/json"
[ -f latlng.txt ] && rm latlng.txt
[ -f results.json ] && rm results.json
while read line; do
# Get address from column 3 and 4 of a CSV file provided as argument and prepare the string address. YMMV.
address=`cut -d";" -f3-4 <<<$line | tr ';' '+' | tr ' ' '+'`
curl -G -s --data sensor=true --data-urlencode address=$address "$MAPSAPIURL" -o results.json
# Parse json with jshon (http://kmkeen.com/jshon/)
jshon -e results -a -e geometry -e location -e "lat" -u -p -e "lng" -u < results.json | paste -d, - - >> latlng.txt
sleep 1
done < $1
@marksparrish
Copy link

I love this script - I wanted to have an line id passed to to the results file.

#!/bin/bash
# Quick and dirty geocoding with Google Maps API
 
MAPSAPIURL="http://maps.googleapis.com/maps/api/geocode/json"
 
[ -f latlng.txt ] && rm latlng.txt
[ -f results.json ] && rm results.json
 
while read line; do
    # Get an line id from column 1
    id=`cut -d"," -f1 <<<$line`
    # Get address from column 5-8 of a CSV file provided as argument and prepare the string address. YMMV.
    address=`cut -d"," -f5-8 <<<$line | tr ',' '+' | tr ' ' '+'`
    curl -G -s --data sensor=true --data-urlencode address=$address "$MAPSAPIURL" -o results.json
    # Parse json with jshon (http://kmkeen.com/jshon/)
    { echo $id; jshon -e results -a -e geometry -e location -e "lat" -u -p -e "lng" -u < results.json; } | paste -d, - - - >> latlng.txt
    sleep 1
done < $1

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