Last active
March 25, 2021 13:23
-
-
Save Kyrremann/995ed352b53b83a8f197281ceb4e1998 to your computer and use it in GitHub Desktop.
Simple Ruby script to get elevations for Norwegian cities
This file contains hidden or 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 'httparty' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'cgi' | |
require 'json' | |
cities = ['Alta', 'Arendal', 'Askim_(tettsted)', 'Bergen', 'Bodø', 'Brekstad', 'Brevik', 'Brumunddal', 'Bryne', 'Brønnøysund', 'Drammen', 'Drøbak', 'Egersund', 'Elverum', 'Fagernes', 'Farsund', 'Fauske', 'Finnsnes', 'Flekkefjord', 'Florø', 'Fosnavåg', 'Fredrikstad', 'Førde', 'Gjøvik', 'Grimstad', 'Halden', 'Hamar', 'Hammerfest', 'Harstad', 'Haugesund', 'Hokksund', 'Holmestrand', 'Honningsvåg', 'Horten', 'Hønefoss', 'Jessheim', 'Jørpeland', 'Kirkenes', 'Kolvereid', 'Kongsberg', 'Kongsvinger', 'Kopervik', 'Kragerø', 'Kristiansand', 'Kristiansund', 'Langesund', 'Larvik', 'Leknes', 'Levanger', 'Lillehammer', 'Lillesand', 'Lillestrøm', 'Lyngdal', 'Mandal', 'Mo_i_Rana', 'Moelv', 'Molde', 'Mosjøen', 'Moss', 'Mysen', 'Måløy', 'Namsos', 'Narvik', 'Notodden', 'Odda', 'Orkanger', 'Oslo', 'Otta', 'Porsgrunn', 'Raufoss', 'Risør', 'Rjukan', 'Røros', 'Sandefjord', 'Sandnes', 'Sandnessjøen', 'Sandvika', 'Sarpsborg', 'Sauda', 'Ski_(tettsted)', 'Skien', 'Skudeneshavn', 'Sortland', 'Stathelle', 'Stavanger', 'Stavern', 'Steinkjer', 'Stjørdalshalsen', 'Stokmarknes', 'Stord', 'Svelvik_(tettsted)', 'Svolvær', 'Tromsø', 'Trondheim', 'Tvedestrand', 'Tønsberg', 'Ulsteinvik', 'Vadsø', 'Vardø', 'Verdalsøra', 'Vinstra', 'Åkrehamn', 'Ålesund', 'Åndalsnes', 'Åsgårdstrand'] | |
# 69°58′36″ | |
# 60°47′N 10°41′Ø | |
def to_dd(coord) | |
coord.scan(/(\d+).(\d+).(\d+)?/).map{|x,y,z|x.to_f+y.to_f/60+z.to_f/3600}.first | |
end | |
elevations = {} | |
begin | |
cities.each do |city| | |
doc = Nokogiri::HTML(URI.open("https://no.wikipedia.org/wiki/#{CGI.escape(city)}")) | |
moh = doc.at('tr:contains("Høyde over havet")') | |
print "#{city}: " | |
if moh | |
elevation = moh.text.delete("^0-9,").to_f | |
elevations[city] = elevation | |
else | |
latitude = to_dd(doc.at('span[class="latitude"]').text) | |
longitude = to_dd(doc.at('span[class="longitude"]').text) | |
# response = HTTParty.get("https://api.opentopodata.org/v1/eudem25m?locations=#{latitude},#{longitude}") # https://www.opentopodata.org/#public-api # free | |
# elevation = response['results'].first['elevation'] # not properly tested | |
response = HTTParty.get("https://elevation-api.io/api/elevation?points=#{latitude},#{longitude}&resolution=30&key=API_TOKEN") # not free | |
elevation = response['elevations'].first['elevation'] | |
elevations[city] = elevation | |
end | |
p elevation | |
sleep(0.1) | |
end | |
rescue => e | |
p e | |
end | |
puts elevations.to_json | |
elevations.sort_by {|city,elevation| elevation }.reverse.map {|city,elevation| p "#{city}: #{elevation}" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output from local run