Skip to content

Instantly share code, notes, and snippets.

@besi
Last active December 17, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save besi/5599117 to your computer and use it in GitHub Desktop.
Save besi/5599117 to your computer and use it in GitHub Desktop.
Parse longitude and latitude from city names
#!/usr/bin/env ruby
# encoding: utf-8
#
# Installation:
#
# gem install geocoder
#
# Usage: ruby Geocoder.rb cities.csv
#
# The input CSV file must have the serach term in the first column
#
# Example:
#
# GeoSearchString,Title
# "New York","Manhatten"
# "Infinite Loop 1, San Francisco","Apple Headquarter"
#
require 'geocoder'
require 'csv'
require 'json'
require 'yaml'
abort 'Usage ruby Geocoder.rb cities.csv' unless ARGV[0]
def main(file)
puts "Loading file #{file}"
csv = CSV.parse(File.read(file), :headers => true)
results = []
CSV.foreach(File.read(file), :headers => true) do |row|
search = row['GeoSearchString'] rescue continue
title = row['Title'] rescue ''
geo = Geocoder.search(search).first
if geo
results << {search: search, title: title, lon: geo.longitude, lat: geo.latitude}
end
end
puts JSON.pretty_generate(results)
end
main ARGV[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment