refactor: CoordinateUtils.get_coordinates_from_mic_e uses guarded clauses
- get_coordinates_from_mic_e: replace two inline `if direction == ...`
rebinds with a reusable apply_hemisphere_sign/3 helper; the final
range check becomes maybe_return_coords/2 with a guarded clause that
yields {lat, lon} or {nil, nil}.
- has_position_data?: the boolean short-circuit collapses from an
explicit if/else to a single `or` expression.
All 37 CoordinateUtils tests (4 properties) still pass.
This commit is contained in:
parent
d07758a3f4
commit
c90a76df04
1 changed files with 24 additions and 10 deletions
|
|
@ -33,13 +33,31 @@ defmodule AprsmeWeb.Live.Shared.CoordinateUtils do
|
|||
"""
|
||||
@spec get_coordinates_from_mic_e(MicE.t()) :: {number() | nil, number() | nil}
|
||||
def get_coordinates_from_mic_e(mic_e) do
|
||||
lat = mic_e.lat_degrees + mic_e.lat_minutes / 60.0 + mic_e.lat_fractional / 6000.0
|
||||
lat = if mic_e.lat_direction == :south, do: -lat, else: lat
|
||||
lon = mic_e.lon_degrees + mic_e.lon_minutes / 60.0 + mic_e.lon_fractional / 6000.0
|
||||
lon = if mic_e.lon_direction == :west, do: -lon, else: lon
|
||||
if lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180, do: {lat, lon}, else: {nil, nil}
|
||||
lat =
|
||||
apply_hemisphere_sign(
|
||||
mic_e.lat_degrees + mic_e.lat_minutes / 60.0 + mic_e.lat_fractional / 6000.0,
|
||||
mic_e.lat_direction,
|
||||
:south
|
||||
)
|
||||
|
||||
lon =
|
||||
apply_hemisphere_sign(
|
||||
mic_e.lon_degrees + mic_e.lon_minutes / 60.0 + mic_e.lon_fractional / 6000.0,
|
||||
mic_e.lon_direction,
|
||||
:west
|
||||
)
|
||||
|
||||
maybe_return_coords(lat, lon)
|
||||
end
|
||||
|
||||
# Negate the value when the hemisphere indicator matches the negative axis.
|
||||
defp apply_hemisphere_sign(value, direction, negative) when direction == negative, do: -value
|
||||
defp apply_hemisphere_sign(value, _direction, _negative), do: value
|
||||
|
||||
defp maybe_return_coords(lat, lon) when lat >= -90 and lat <= 90 and lon >= -180 and lon <= 180, do: {lat, lon}
|
||||
|
||||
defp maybe_return_coords(_lat, _lon), do: {nil, nil}
|
||||
|
||||
@doc """
|
||||
Check if packet has position data in any format.
|
||||
"""
|
||||
|
|
@ -48,11 +66,7 @@ defmodule AprsmeWeb.Live.Shared.CoordinateUtils do
|
|||
lat = Map.get(packet, :lat) || Map.get(packet, "lat")
|
||||
lon = Map.get(packet, :lon) || Map.get(packet, "lon")
|
||||
|
||||
if has_direct_coordinates?(lat, lon) do
|
||||
true
|
||||
else
|
||||
has_position_in_data_extended?(packet)
|
||||
end
|
||||
has_direct_coordinates?(lat, lon) or has_position_in_data_extended?(packet)
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue