add more comments

This commit is contained in:
aaron
2024-03-12 14:12:54 +01:00
parent 132857445e
commit d86ace247d

View File

@@ -3,17 +3,24 @@ import json
import pyexcel_ods import pyexcel_ods
def dms_to_decimal(dms_str): def dms_to_decimal(dms_str):
"""
Turn a degree/minute/second format to decimal
"""
# the pattern matches 3 groups degrees, minutes and seconds
pattern = re.compile(r'(\d+)°(\d+)\'([\d.]+)\"([NSEW])') pattern = re.compile(r'(\d+)°(\d+)\'([\d.]+)\"([NSEW])')
match = pattern.match(dms_str) match = pattern.match(dms_str)
# check if there was a match
if match: if match:
degrees = float(match.group(1)) degrees = float(match.group(1))
minutes = float(match.group(2)) minutes = float(match.group(2))
seconds = float(match.group(3)) seconds = float(match.group(3))
direction = match.group(4) direction = match.group(4)
# round to 6 significant figures (google maps does this)
decimal_degrees = round(degrees + (minutes / 60) + (seconds / 3600), 6) decimal_degrees = round(degrees + (minutes / 60) + (seconds / 3600), 6)
# in decimal there are no cardinal diractions, only signed or unsigned
if direction in ['S', 'W']: if direction in ['S', 'W']:
decimal_degrees *= -1 decimal_degrees *= -1
@@ -22,16 +29,22 @@ def dms_to_decimal(dms_str):
raise ValueError("invalid coordinate format") raise ValueError("invalid coordinate format")
def split_coordinates(coord_str): def split_coordinates(coord_str):
"""
Split a dms coordinate into latitude and longitude
E.g: 33°56'35.02"S, 023°30'26.10"E
"""
parts = coord_str.split(', ') parts = coord_str.split(', ')
latitude = parts[0] latitude = parts[0]
longitude = parts[1] longitude = parts[1]
return latitude, longitude return latitude, longitude
def main(): def main():
# capture site_degree (int dd mm ss) # specific implementation: ironwood_site (dd mm ss)
sites = pyexcel_ods.get_data("../ironwood_data_cleaned.ods", sheet_name="Sheet1", start_row=1, start_column=1, column_limit=1) sites = pyexcel_ods.get_data("../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) # specific implementation: tree data (dd mm ss)
trees = pyexcel_ods.get_data("../data/ironwood_data_cleaned.ods", sheet_name="Sheet1", start_row=1, start_column=5, column_limit=5)
# iterate over the retrieved data and convert everything
for cell in trees["Sheet1"]: for cell in trees["Sheet1"]:
if cell: if cell:
latitude, longitude = split_coordinates(cell[0]) latitude, longitude = split_coordinates(cell[0])