Skip to content

Commit

Permalink
Moved dms_to_float conversion function to utils lib
Browse files Browse the repository at this point in the history
  • Loading branch information
rdgfuentes committed Aug 16, 2024
1 parent 7b9b2a6 commit dcc7690
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
20 changes: 20 additions & 0 deletions packages/libs/utils/utils/convert.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
def to_float(value):
return float(value) if value is not None else None


def dms_to_float(dms_value: str):
"""
Convert Degrees, Minutes, Seconds coordinate to float value
:param dms_value: str Coordinate value sexpressed in Degrees, Minutes, Seconds
:return: float Coordinate value converted to float
"""
dms_value = dms_value.strip()
degrees, rest = dms_value.split("°")
minutes, rest = rest.split("'")
seconds, direction = rest.split('" ')
degrees = float(degrees)
minutes = float(minutes)
seconds = float(seconds)
float_value = degrees + minutes / 60 + seconds / 3600
if direction in ["S", "W"]:
float_value = -float_value
return float_value
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,7 @@

import apache_beam as beam
from shipdataprocess.standardize import standardize_str


# Function to convert DMS to decimal
def dms_to_decimal(dms_str):
dms_str = dms_str.strip()
degrees, rest = dms_str.split("°")
minutes, rest = rest.split("'")
seconds, direction = rest.split('" ')
degrees = float(degrees)
minutes = float(minutes)
seconds = float(seconds)
decimal = degrees + minutes / 60 + seconds / 3600
if direction in ["S", "W"]:
decimal = -decimal
return decimal
from utils.convert import dms_to_float


def extract_float(df_val):
Expand All @@ -39,8 +25,8 @@ def extract_int(df_val):
def map_pan_fields(msg):
return {
"shipname": standardize_str(msg.get("Nombre de la nave")),
"lat": dms_to_decimal(msg.get("Latitud")),
"lon": dms_to_decimal(msg.get("Longitud")),
"lat": dms_to_float(msg.get("Latitud")),
"lon": dms_to_float(msg.get("Longitud")),
"speed": extract_float(msg.get("Velocidad")),
"heading": extract_int(msg.get("Rumbo")),
"timestamp": dt.datetime.strptime(
Expand Down

0 comments on commit dcc7690

Please sign in to comment.