import re import json import pyexcel_ods 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])') match = pattern.match(dms_str) # check if there was a match if match: degrees = float(match.group(1)) minutes = float(match.group(2)) seconds = float(match.group(3)) direction = match.group(4) # round to 6 significant figures (google maps does this) 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']: decimal_degrees *= -1 return decimal_degrees else: raise ValueError("invalid coordinate format") 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(', ') latitude = parts[0] longitude = parts[1] return latitude, longitude def main(): # specific implementation: ironwood_site (dd mm ss) sites = pyexcel_ods.get_data("../data/ironwood_data_cleaned.ods", sheet_name="Sheet1", start_row=1, start_column=1, column_limit=1) # 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"]: 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()