Last active
December 17, 2015 10:59
-
-
Save besi/5599117 to your computer and use it in GitHub Desktop.
Parse longitude and latitude from city names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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