Skip to content

Instantly share code, notes, and snippets.

@erochest
Forked from waynegraham/add_item_snippet.rb
Created September 26, 2012 18:18
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 erochest/3789641 to your computer and use it in GitHub Desktop.
Save erochest/3789641 to your computer and use it in GitHub Desktop.
Geocoding in Omeka
add_item_page.form_with(:method => 'POST') do |item_form|
item_form['public'] = 1
item_form['Elements[50][0][text]'] = line[:address]
# This should really be KML, but it will recognize WKT too.
item_form['Elements[38][0][geo]'] = line[:point]
item_form['Elements[38][0][zoom]'] = 10
item_form['Elements[38][0][mapon]'] = 1
item_form['Elements[38][0][center_lon]'] = line[:lon]
item_form['Elements[38][0][center_lat]'] = line[:lat]
item_form['Elements[38][0][text]'] = "POINT(#{line[:address]})" + '////osm
'
end.submit
address city state point lat lon
1600 Pennsylvania Ave Washington DC POINT(-8572649.720054641 4705902.156014869) 4705902.156014869 -8572649.720054641
931 Thomas Jefferson Parkway Charlottesville VA POINT(-8733720.184442518 4580189.258447956) 4580189.258447956 -8733720.184442518
Eiffel Tower POINT(255395.18699487977 6250848.2584100235) 6250848.2584100235 255395.18699487977
require 'csv'
require 'geocoder'
LOCATIONS = './locations.csv'
def degrees_to_meters(lon, lat)
half_circumference = 20037508.34
x = lon * half_circumference / 180
y = Math.log(Math.tan((90 + lat) * Math::PI / 360)) / (Math::PI / 180)
y = y * half_circumference / 180
return [x, y]
end
puts 'address,city,state,point,lat,lon'
CSV.foreach(LOCATIONS, :headers => true, :header_converters => :symbol) do |line|
address_string = "#{line[:address]}, #{line[:city]}, #{line[:state]}"
result = Geocoder.search(address_string).first
lat = result.latitude
lon = result.longitude
#point = "POINT(#{lon} #{lat})"
projected = degrees_to_meters(lon, lat)
point = "POINT(#{projected[0]} #{projected[1]})"
puts "#{address_string}, #{point}, #{projected[1]}, #{projected[0]}"
end
We can make this file beautiful and searchable if this error is corrected: It looks like row 4 should actually have 3 columns, instead of 1. in line 3.
Address,City,State
"1600 Pennsylvania Ave",Washington,DC
"931 Thomas Jefferson Parkway",Charlottesville,VA
"Eiffel Tower"
require 'rubygems'
require 'mechanize'
require 'csv'
agent = Mechanize.new {|a|
a.user_agent_alias = 'Mac Safari'
}
agent.get('http://localhost:8888/omeka/admin/') do |page|
omeka_page = page.form_with(:action => '/omeka/admin/users/login') do |form|
form.username = 'username'
form.password = 'password'
end.submit
CSV.foreach('./geocoded.csv', :headers => true, :header_converters => :symbol) do |line|
# add logic to fill out form
# click on items
item_page = agent.click(omeka_page.link_with(:text => %r/Items/))
# click on add items
add_item_page = agent.click(item_page.link_with(:text => %r/Add an Item/))
puts "Adding #{line[:address]}..."
puts line[:point]
puts line[:lon]
puts line[:lat]
# Add the item to the form
add_item_page.form_with(:method => 'POST') do |item_form|
item_form['public'] = 1
item_form['Elements[50][0][text]'] = line[:address]
item_form['Elements[38][0][wkt]'] = line[:point]
item_form['Elements[38][0][zoom]'] = 10
item_form['Elements[38][0][mapon]'] = 1
item_form['Elements[38][0][center_lon]'] = line[:lon]
item_form['Elements[38][0][center_lat]'] = line[:lat]
item_form['Elements[38][0][text]'] = "#{line[:address]}" + '////osm
'
end.submit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment