Skip to content

Instantly share code, notes, and snippets.

@elomar
Created May 18, 2010 02:55
Show Gist options
  • Save elomar/404552 to your computer and use it in GitHub Desktop.
Save elomar/404552 to your computer and use it in GitHub Desktop.
# encoding: UTF-8
require 'sinatra'
require 'sinatra/mongo'
require 'csv'
require 'geokit'
set :mongo, 'mongo://localhost:27017/fulbright2010'
set :haml, {:format => :html5}
GOOGLE_APPLICATION_ID = "ABQIAAAAAZX3aTjRo2xfx1lrVs0boRS82gkS-Z98sZltoqpfSzIi6nwd6xQTdeXxh6sEhSJJ7xiEPfAeocsvE"
Geokit::Geocoders::google = GOOGLE_APPLICATION_ID
$LOAD_PATH << File.expand_path('google_maps/lib')
# pegar pasta google_maps em http://github.com/bhedana/google_maps
require File.expand_path('google_maps/lib/unbacked_dom_id')
require File.expand_path('google_maps/init')
class Object
def blank?
respond_to?(:empty?) ? empty? : !self
end
end
get '/' do
@students = mongo['students'].find
@map = GoogleMap::Map.new
@map.controls = [:large, :type]
@map.bounds = [GoogleMap::Point.new(50.007739, -127.617187), GoogleMap::Point.new(25.482951,-78.222656)]
@students.each do |student|
html = student.values_at('First Name', 'E-mail', 'City', 'Field of Study').join('<br>')
@map.markers << GoogleMap::Marker.new(map: @map, lat: student['lat'], lng: student['lng'], html: html)
end
haml :index
end
get '/insert' do
coll = mongo['students']
coll.drop
CSV.foreach 'students.csv', :headers => true do |student|
addr = Geokit::Geocoders::GoogleGeocoder.geocode "#{student['Program College']} near #{student['College State']}"
fields = student.to_hash
fields.delete nil
offset = coll.find({'Program College' => student['Program College']}).count / 2.0
puts student.inspect
puts offset.inspect
fields['City'] = fields['City'] + ' / ' + fields['State']
coll.insert fields.merge(lat: addr.lat + offset, lng: addr.lng + offset)
end
end
__END__
@@ index
!!!
%head
%title Fulbright 2010/2011
%meta{'http-equiv' => 'Content-Type', 'value' => 'text/html; charset=UTF-8'}
%body
%h1 Brasileiros no Fulbright 2010/2011
= @map.to_html
%div{style: "width: 1200px; height: 500px;"}
= @map.div
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment