Skip to content

Instantly share code, notes, and snippets.

@ole
Created January 23, 2014 18:23
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ole/8583982 to your computer and use it in GitHub Desktop.
Save ole/8583982 to your computer and use it in GitHub Desktop.
Geocodes addresses on the command line and print the geo coordinates (latitude and longitude). See http://oleb.net/blog/2014/01/geocoding-automator-service/ for more info on how I am using this script.
#!/usr/bin/env ruby
# Determines the coordinates (latitude and longitude) of the places or
# addresses passed as arguments (either on the command line or via stdin)
# and prints the results.
# This script requires the Ruby Geocoder gem by Alex Reisner
# (http://www.rubygeocoder.com).
# To install the gem, run this command from a Terminal session:
#
# sudo gem install geocoder
#
# If you are using a Ruby version manager such as rbenv or RVM, make sure
# to install the Geocoder gem in the operating system's Ruby installation
# if you use this script inside an OS X Automator action.
# For example, in rbenv, call rbenv global system before installing.
require 'geocoder'
if ARGV.count > 0
input = ARGV
else
input = ARGF
input.set_encoding("utf-8") # required to handle non-ASCII characters in Automator action
end
output = ""
input.each do |address|
trimmed_address = address.strip
if trimmed_address.empty?
output += "\n"
next
end
geocode_result = Geocoder.search(trimmed_address)
if geocode_result.first
coordinates = geocode_result.first.coordinates.join(", ")
else
coordinates = "Not found"
end
output += "#{coordinates}\n"
end
puts output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment