Skip to content

Instantly share code, notes, and snippets.

@kenielf
Last active January 18, 2024 19:18
Show Gist options
  • Save kenielf/918272222942a9226fee9e7f76be24cc to your computer and use it in GitHub Desktop.
Save kenielf/918272222942a9226fee9e7f76be24cc to your computer and use it in GitHub Desktop.
Convert DMS (Degree Minutes Seconds) coordinates to decimal
#!/usr/bin/env python3
# By Chrystian M. F.
from typing import List, Tuple
def get_coordinates() -> Tuple[List[int], List[int]]:
# Get Latitude
latitude_str: List[str] = (
input("Latitude (Degrees Minutes Seconds)\n > ").strip().lower().split(" ")
)
latitudes: List[int] = [int(x) for x in latitude_str]
# Get Latitude
longitude_str: List[str] = (
input("Longitude (Degrees Minutes Seconds)\n > ").strip().lower().split(" ")
)
longitudes: List[int] = [int(x) for x in longitude_str]
return (latitudes, longitudes)
def convert(degree, minutes, seconds) -> float:
return degree + (minutes / 60) + (seconds / 3600)
def main():
# Get user input
dms_lat, dms_lon = get_coordinates()
#print(dms_lat, dms_lon)
# Convert it
dec_lat = convert(dms_lat[0], dms_lat[1], dms_lat[2])
dec_lon = convert(dms_lon[0], dms_lon[1], dms_lon[2])
# Return result
print(f"Result: Latitude = {dec_lat:8f}, Longitude = {dec_lon:8f}")
if __name__ == "__main__":
main()
#!/usr/bin/env sh
# Mass Converter, by Chrystian M. F.
# NOTE: This has a really specific use case, I left it here for keepsake.
if [ -z "${1}" ]; then
exit 1
fi
FILE=${1}
# Converter Loop
while IFS= read -r line; do
split="$(echo "${line}" | sed -re 's/, /\n/g' | tr -d '°' | tr -d "'" | tr -d '"' | tr -d 'SNWE')"
result="$(echo "${split}" | ./converter.py | grep "Result:" | sed -re 's/ > //g')"
echo "${result}" | tee -a result.txt
done < "${FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment