Class: Overhelper
- Inherits:
-
Object
- Object
- Overhelper
- Defined in:
- lib/overhelper.rb
Constant Summary collapse
- EARTH_RADIUS_IN_M =
6371*1000
Class Method Summary collapse
- .convert_to_ways(json_string) ⇒ Object
- .distance_in_m(lat1, lon1, lat2, lon2) ⇒ Object
- .list_of_locations_from_overpass_into_array(list, data = []) ⇒ Object
- .nodes_to_dict(data) ⇒ Object
Class Method Details
.convert_to_ways(json_string) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/overhelper.rb', line 50 def self.convert_to_ways(json_string) data = JSON.parse(json_string) node_library = nodes_to_dict(data) ways = [] for element in data["elements"] if element["type"] == "way" nodes_of_way = element["nodes"] prev_node = nil for node in nodes_of_way if prev_node != nil way = {} way[:lat1] = (node_library[prev_node])[:lat] way[:lon1] = (node_library[prev_node])[:lon] way[:lat2] = (node_library[node])[:lat] way[:lon2] = (node_library[node])[:lon] ways << way end prev_node = node end end end return ways end |
.distance_in_m(lat1, lon1, lat2, lon2) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/overhelper.rb', line 14 def self.distance_in_m(lat1, lon1, lat2, lon2) =begin * Computes distance between two points on Earth using Haversine formula * Earth is assumed to be sphere, errors from assuming spherical geometry might be up to 0.55% crossing the equator * source: https://en.wikipedia.org/wiki/Haversine_formula and http://www.movable-type.co.uk/scripts/latlong.html =end lat1 = lat1 * Math::PI / 180 lon1 = lon1 * Math::PI / 180 lat2 = lat2 * Math::PI / 180 lon2 = lon2 * Math::PI / 180 dLat = (lat2 - lat1).abs; dLon = (lon2 - lon1).abs; #a: square of half the chord length between the points a = Math.sin(dLat / 2) * Math.sin(dLat / 2); a += Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat2) * Math.cos(lat1); angularDistanceInRadians = Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); delta_in_m = 2 * EARTH_RADIUS_IN_M * angularDistanceInRadians; return delta_in_m end |
.list_of_locations_from_overpass_into_array(list, data = []) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/overhelper.rb', line 3 def self.list_of_locations_from_overpass_into_array(list, data=[]) list.split(/[\n]+/m).each{|element| element = Hash[[:lat, :lon].zip(element.split(/[\s]+/m))] element[:lat] = element[:lat].to_f element[:lon] = element[:lon].to_f data << element } return data end |
.nodes_to_dict(data) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/overhelper.rb', line 37 def self.nodes_to_dict(data) node_library = {} for element in data["elements"] if element["type"] == "node" saved = {} saved[:lat] = element["lat"].to_f saved[:lon] = element["lon"].to_f node_library[element["id"]] = saved end end return node_library end |