Skip to content

Instantly share code, notes, and snippets.

@invisiblefunnel
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save invisiblefunnel/f5735b1df744b1748b5f to your computer and use it in GitHub Desktop.
Save invisiblefunnel/f5735b1df744b1748b5f to your computer and use it in GitHub Desktop.
require 'csv'
require 'json'
require 'faraday'
require 'faraday_middleware'
class Geocoder < Struct.new(:query)
def self.coords(query)
result = connection.get('/maps/api/geocode/json', address: query).body
latlng = result["results"][0]["geometry"]["location"]
[latlng['lng'], latlng['lat']]
end
def self.connection
@connection ||= Faraday.new(url: 'http://maps.googleapis.com') do |conn|
conn.headers['Content-Type'] = 'application/json'
conn.params['language'] = 'en'
conn.params['sensor'] = false
conn.response :json
conn.adapter Faraday.default_adapter
end
end
end
feature_collection = {
'type' => 'FeatureCollection',
'features' => []
}
CSV.foreach('marijuana_facilities.csv', headers: true) do |row|
begin
coords = Geocoder.coords(row[1])
feature = {
'type' => 'Feature',
'properties' => {
'business_name' => row[0],
'license_type' => row[2]
},
'geometry' => {
'type' => 'Point',
'coordinates' => coords
}
}
feature_collection['features'] << feature
puts feature
sleep 0.5
rescue => e
puts "Failure: #{row}"
puts "ERROR: #{e}"
end
end
File.write('marijuana_facilities.geojson', JSON.pretty_generate(feature_collection))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment