Created
October 20, 2012 11:49
-
-
Save tommorris/3923100 to your computer and use it in GitHub Desktop.
simplified version of reverse geocode parse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
require "open-uri" | |
class Post | |
def self.location_label(latitude, longitude) | |
data = open("http://nominatim.openstreetmap.org/reverse?format=json&lat=#{latitude}&lon=#{longitude}&zoom=18&addressdetails=1").readlines.join("\n") | |
result = JSON.parse(data) | |
result_arr = [] | |
# london mode | |
if result["address"].has_key?("county") && result["address"]["county"] == "Greater London" | |
if result["address"].has_key?("place") | |
result_arr << result["address"]["place"] | |
else | |
if result["address"].has_key?("pedestrian") | |
result_arr << result["address"]["pedestrian"] | |
elsif result["address"].has_key?("footway") | |
result_arr << result["address"]["footway"] | |
end | |
result_arr << result["address"]["suburb"] if result["address"].has_key?("suburb") | |
end | |
# state_district should be 'London' :) | |
result_arr << result["address"]["state_district"] if result["address"].has_key?("state_district") | |
# strip end off postcodes | |
if result["address"].has_key?("postcode") | |
postcode = result["address"]["postcode"] | |
if postcode.split(" ").size != 1 | |
result_arr << postcode.split(" ").first | |
else | |
result_arr << postcode | |
end | |
end | |
# not london. | |
else | |
if result["address"].has_key?("road") | |
result_arr << result["address"]["road"] | |
end | |
if result["address"].has_key?("suburb") | |
result_arr << result["address"]["suburb"] | |
end | |
if result["address"].has_key?("town") | |
result_arr << result["address"]["town"] | |
else | |
if result["address"].has_key?("city") | |
result_arr << result["address"]["city"] | |
end | |
end | |
end | |
return result_arr.join(", ") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostTest | |
test "posts generate useful location labels" do | |
assert_equal "St Pancras, London, NW1", Post.location_label(51.5294418333333,-0.126272633333333) | |
assert_equal "St Pancras, London, NW1", Post.location_label(51.5295385,-0.1264996) | |
assert_equal "Bishops Square, London, E1", Post.location_label(51.5192044,-0.077819) | |
assert_equal "Westminster, London, WC2N", Post.location_label(51.509092275,-0.123459925) | |
assert_equal "Gloucester Road, Round Hill, Brighton", Post.location_label(50.8267737,-0.1367264) | |
assert_equal "Trinity Street, Bohemia, Hastings", Post.location_label(50.8550399,0.5767676) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As @danstowell has produced a Python version, I should probably license this code. It's under the MIT License.
Copyright © 2012 Tom Morris.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.