add python script to convert gps degrees to decimal
This commit is contained in:
45
tools/convert_gps.py
Normal file
45
tools/convert_gps.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import re
|
||||
import json
|
||||
import pyexcel_ods
|
||||
|
||||
def dms_to_decimal(dms_str):
|
||||
pattern = re.compile(r'(\d+)°(\d+)\'([\d.]+)\"([NSEW])')
|
||||
match = pattern.match(dms_str)
|
||||
|
||||
if match:
|
||||
degrees = float(match.group(1))
|
||||
minutes = float(match.group(2))
|
||||
seconds = float(match.group(3))
|
||||
direction = match.group(4)
|
||||
|
||||
decimal_degrees = round(degrees + (minutes / 60) + (seconds / 3600), 6)
|
||||
|
||||
if direction in ['S', 'W']:
|
||||
decimal_degrees *= -1
|
||||
|
||||
return decimal_degrees
|
||||
else:
|
||||
raise ValueError("invalid coordinate format")
|
||||
|
||||
def split_coordinates(coord_str):
|
||||
parts = coord_str.split(', ')
|
||||
latitude = parts[0]
|
||||
longitude = parts[1]
|
||||
return latitude, longitude
|
||||
|
||||
def main():
|
||||
# capture site_degree (int dd mm ss)
|
||||
sites = pyexcel_ods.get_data("../ironwood_data_cleaned.ods", sheet_name="Sheet1", start_row=1, start_column=1, column_limit=1)
|
||||
trees = pyexcel_ods.get_data("../ironwood_data_cleaned.ods", sheet_name="Sheet1", start_row=1, start_column=5, column_limit=5)
|
||||
|
||||
for cell in trees["Sheet1"]:
|
||||
if cell:
|
||||
latitude, longitude = split_coordinates(cell[0])
|
||||
latitude_decimal = dms_to_decimal(latitude)
|
||||
longitude_decimal = dms_to_decimal(longitude)
|
||||
print(f"{latitude_decimal},{longitude_decimal}")
|
||||
else:
|
||||
print(" ")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user