Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.