Module: MeteofranceApi::Helpers

Defined in:
lib/meteofrance_api/helpers.rb

Instance Method Summary collapse

Instance Method Details

#alert_code_to_str(code, lang = :fr) ⇒ Object

Convert the phenomenom code in readable text (Hepler).

Args:
    code: ID of the phenomenom in int. Value expected between 1 and 9.
    lang: Optional; If language is equal :fr (default value) results will
        be in French. All other value will give results in English.

Returns:
    Phenomenom in text. French or English according to the lang parameter.


32
33
34
35
36
37
38
39
# File 'lib/meteofrance_api/helpers.rb', line 32

def alert_code_to_str(
  code,
  lang = :fr
)
  alert_types = MeteofranceApi::ALERT_TYPES[lang] || MeteofranceApi::ALERT_TYPES[:en]

  alert_types[code]
end

#color_code_to_str(code, lang = :fr) ⇒ Object

Color status in text. French or English according to the lang parameter.



13
14
15
16
17
18
19
20
# File 'lib/meteofrance_api/helpers.rb', line 13

def color_code_to_str(
  code,
  lang = :fr
)
  colors = MeteofranceApi::ALERT_COLORS[lang] || MeteofranceApi::ALERT_COLORS[:en]

  colors[code]
end

#haversine(coord1, coord2) ⇒ Object

Compute distance in meters between to GPS coordinates using Harvesine formula.

source: https://janakiev.com/blog/gps-points-distance-python/

Args:
    coord1: Tuple with latitude and longitude in degrees for first point
    coord2: Tuple with latitude and longitude in degrees for second point

Returns:
    Distance in meters between the two points


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/meteofrance_api/helpers.rb', line 79

def haversine(coord1, coord2)
  to_radians = ->(v) { v * (Math::PI / 180) }
  radius = 6372800  # Earth radius in meters

  lat1, lon1 = coord1
  lat2, lon2 = coord2

  phi1, phi2 = to_radians.call(lat1), to_radians.call(lat2)
  dphi = to_radians.call(lat2 - lat1)
  dlambda = to_radians.call(lon2 - lon1)

  a = (
      Math.sin(dphi / 2) ** 2
      + Math.cos(phi1) * Math.cos(phi2) * Math.sin(dlambda / 2) ** 2
  )

  return 2 * radius * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
end

#is_coastal_department?(department_number) ⇒ Boolean

Identify when a second bulletin is availabe for coastal risks (Helper).

Args:
    department_number: Department number on 2 characters

Returns:
    True if the department have an additional coastal bulletin. False otherwise.

Returns:

  • (Boolean)


50
51
52
# File 'lib/meteofrance_api/helpers.rb', line 50

def is_coastal_department?(department_number)
  MeteofranceApi::COASTAL_DEPARTMENTS.include?(department_number)
end

#is_department?(department_number) ⇒ Boolean

Identify if there is a weather alert bulletin for this department (Helper).

Weather alert buletins are available only for metropolitan France and Andorre.

Args:
    department_number: Department number on 2 characters.

Returns:
    True if a department is metropolitan France or Andorre.

Returns:

  • (Boolean)


65
66
67
# File 'lib/meteofrance_api/helpers.rb', line 65

def is_department?(department_number)
  MeteofranceApi::VALID_DEPARTMENTS.include?(department_number)
end

#sort_places_versus_distance_from_coordinates(places, gps_coord) ⇒ Object

Order list of places according to the distance to a reference coordinates.

Note: this helper is compensating the bad results of the API. Results in the API
are generally sorted, but lot of cases identified where the order is inconsistent
(example: Montréal)

Args:
    list_places: List of Place instances to be ordered
    gps_coord: Tuple with latitude and longitude in degrees for the reference point

Returns:
    List of Place instances ordered by distance to the reference point (nearest
        first)


113
114
115
116
117
118
# File 'lib/meteofrance_api/helpers.rb', line 113

def sort_places_versus_distance_from_coordinates(
    places,
    gps_coord
)
  places.sort_by {|place| haversine(place.latitude.to_i, place.longitude.to_i, gps_coord)}
end