Use Nominatim to reverse geocode diary location information and display

a friendly human readable location instead of some coordinates.
This commit is contained in:
Tom Hughes 2010-02-27 12:41:56 +00:00
parent 1df4234c72
commit 0b913efd05
7 changed files with 40 additions and 9 deletions

View file

@ -1,4 +1,6 @@
module ApplicationHelper module ApplicationHelper
require 'rexml/document'
def htmlize(text) def htmlize(text)
return linkify(sanitize(simple_format(text))) return linkify(sanitize(simple_format(text)))
end end
@ -34,6 +36,15 @@ module ApplicationHelper
return js return js
end end
def describe_location(lat, lon, zoom, language)
zoom = zoom || 14
language = language || request.user_preferred_languages.join(',')
url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
response = REXML::Document.new(Net::HTTP.get(URI.parse(url)))
return response.get_text("reversegeocode/result").to_s
end
private private
def javascript_strings_for_key(key) def javascript_strings_for_key(key)

View file

@ -30,5 +30,9 @@ private
expire_action(:controller => 'diary_entry', :action => 'rss', :language => nil, :display_name => nil) expire_action(:controller => 'diary_entry', :action => 'rss', :language => nil, :display_name => nil)
expire_action(:controller => 'diary_entry', :action => 'rss', :language => entry.language_code, :display_name => nil) expire_action(:controller => 'diary_entry', :action => 'rss', :language => entry.language_code, :display_name => nil)
expire_action(:controller => 'diary_entry', :action => 'rss', :language => nil, :display_name => entry.user.display_name) expire_action(:controller => 'diary_entry', :action => 'rss', :language => nil, :display_name => entry.user.display_name)
if record.is_a?(DiaryEntry)
expire_fragment(:controller => 'diary_entry', :action => 'view', :display_name => entry.user.display_name, :id => entry.id, :part => "location")
end
end end
end end

View file

@ -1,7 +1,8 @@
<b><%= link_to h(diary_entry.title), :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id %></b><br /> <b><%= link_to h(diary_entry.title), :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id %></b><br />
<%= htmlize(diary_entry.body) %> <%= htmlize(diary_entry.body) %>
<% if diary_entry.latitude and diary_entry.longitude %> <% if diary_entry.latitude and diary_entry.longitude %>
<%= t 'map.coordinates' %> <div class="geo" style="display: inline"><span class="latitude"><%= diary_entry.latitude %></span>; <span class="longitude"><%= diary_entry.longitude %></span></div> (<%=link_to (t 'map.view'), :controller => 'site', :action => 'index', :lat => diary_entry.latitude, :lon => diary_entry.longitude, :zoom => 14 %> / <%=link_to (t 'map.edit'), :controller => 'site', :action => 'edit', :lat => diary_entry.latitude, :lon => diary_entry.longitude, :zoom => 14 %>)<br/> <%= render :partial => "location", :object => diary_entry %>
<br />
<% end %> <% end %>
<%= t 'diary_entry.diary_entry.posted_by', :link_user => (link_to h(diary_entry.user.display_name), :controller => 'user', :action => 'view', :display_name => diary_entry.user.display_name), :created => l(diary_entry.created_at), :language_link => (link_to h(diary_entry.language.name), :controller => 'diary_entry', :action => 'list', :language => diary_entry.language_code) %> <%= t 'diary_entry.diary_entry.posted_by', :link_user => (link_to h(diary_entry.user.display_name), :controller => 'user', :action => 'view', :display_name => diary_entry.user.display_name), :created => l(diary_entry.created_at), :language_link => (link_to h(diary_entry.language.name), :controller => 'diary_entry', :action => 'list', :language => diary_entry.language_code) %>
<% if params[:action] == 'list' %> <% if params[:action] == 'list' %>

View file

@ -0,0 +1,11 @@
<% cache(:controller => 'diary_entry', :action => 'view', :display_name => location.user.display_name, :id => location.id, :part => "location") do %>
<%= t 'diary_entry.location.location' %>
<abbr class="geo" title="<%= number_with_precision(location.latitude, :precision => 4) %>; <%= number_with_precision(location.longitude, :precision => 4) %>">
<%= describe_location location.latitude, location.longitude, 14, location.language_code %>
</abbr>
(<%=link_to t('diary_entry.location.view'), :controller => 'site', :action => 'index', :lat => location.latitude, :lon => location.longitude, :zoom => 14 %>
/
<%=link_to t('diary_entry.location.edit'), :controller => 'site', :action => 'edit', :lat => location.latitude, :lon => location.longitude, :zoom => 14 %>)
<% end %>

View file

@ -76,10 +76,6 @@ en:
with_id: "{{id}}" with_id: "{{id}}"
with_version: "{{id}}, v{{version}}" with_version: "{{id}}, v{{version}}"
with_name: "{{name}} ({{id}})" with_name: "{{name}} ({{id}})"
map:
view: View
edit: Edit
coordinates: "Coordinates:"
browse: browse:
changeset: changeset:
title: "Changeset" title: "Changeset"
@ -341,6 +337,10 @@ en:
comment_from: "Comment from {{link_user}} at {{comment_created_at}}" comment_from: "Comment from {{link_user}} at {{comment_created_at}}"
hide_link: Hide this comment hide_link: Hide this comment
confirm: Confirm confirm: Confirm
location:
location: "Location:"
view: "View"
edit: "Edit"
feed: feed:
user: user:
title: "OpenStreetMap diary entries for {{user}}" title: "OpenStreetMap diary entries for {{user}}"

View file

@ -700,3 +700,7 @@ input[type="submit"] {
top: 15px; top: 15px;
left: 15px left: 15px
} }
abbr.geo {
border-bottom: none;
}

View file

@ -3,6 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper'
class DiaryEntryControllerTest < ActionController::TestCase class DiaryEntryControllerTest < ActionController::TestCase
fixtures :users, :diary_entries, :diary_comments fixtures :users, :diary_entries, :diary_comments
include ActionView::Helpers::NumberHelper
def test_showing_new_diary_entry def test_showing_new_diary_entry
get :new get :new
assert_response :redirect assert_response :redirect
@ -110,8 +112,7 @@ class DiaryEntryControllerTest < ActionController::TestCase
# This next line won't work if the text has been run through the htmlize function # This next line won't work if the text has been run through the htmlize function
# due to formatting that could be introduced # due to formatting that could be introduced
assert_select "p", :text => /#{new_body}/, :count => 1 assert_select "p", :text => /#{new_body}/, :count => 1
assert_select "span.latitude", :text => new_latitude, :count => 1 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
assert_select "span.longitude", :text => new_longitude, :count => 1
# As we're not logged in, check that you cannot edit # As we're not logged in, check that you cannot edit
#print @response.body #print @response.body
assert_select "a[href='/user/#{users(:normal_user).display_name}/diary/#{diary_entries(:normal_user_entry_1).id}/edit']", :text => "Edit this entry", :count => 1 assert_select "a[href='/user/#{users(:normal_user).display_name}/diary/#{diary_entries(:normal_user_entry_1).id}/edit']", :text => "Edit this entry", :count => 1
@ -134,8 +135,7 @@ class DiaryEntryControllerTest < ActionController::TestCase
# This next line won't work if the text has been run through the htmlize function # This next line won't work if the text has been run through the htmlize function
# due to formatting that could be introduced # due to formatting that could be introduced
assert_select "p", :text => /#{new_body}/, :count => 1 assert_select "p", :text => /#{new_body}/, :count => 1
assert_select "span.latitude", :text => new_latitude, :count => 1 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
assert_select "span.longitude", :text => new_longitude, :count => 1
# As we're not logged in, check that you cannot edit # As we're not logged in, check that you cannot edit
assert_select "a[href='/user/#{users(:normal_user).display_name}/diary/#{diary_entries(:normal_user_entry_1).id}/edit']", :text => "Edit this entry", :count => 0 assert_select "a[href='/user/#{users(:normal_user).display_name}/diary/#{diary_entries(:normal_user_entry_1).id}/edit']", :text => "Edit this entry", :count => 0
end end