Converte la stringa di input nell'output previsto. Può gestire minuti e secondi in cui non è presente.
Attualmente non conta per Nord/Sud, Est/Ovest. Se mi dirai come vorresti che vengano gestiti, aggiornerò la risposta.
# -*- coding: latin-1 -*-
import re
PATTERN = re.compile(r"""(?P<lat_deg>\d+)° # Latitude Degrees
(?:(?P<lat_min>\d+)')? # Latitude Minutes (Optional)
(?:(?P<lat_sec>\d+)")? # Latitude Seconds (Optional)
(?P<north_south>[NS]) # North or South
,[ ]
(?P<lon_deg>\d+)° # Longitude Degrees
(?:(?P<lon_min>\d+)')? # Longitude Minutes (Optional)
(?:(?P<lon_sec>\d+)")? # Longitude Seconds (Optional)
(?P<east_west>[EW]) # East or West
""", re.VERBOSE)
LAT_FIELDS = ("lat_deg", "lat_min", "lat_sec")
LON_FIELDS = ("lon_deg", "lon_min", "lon_sec")
def parse_dms_string(s, out_type=float):
"""
Convert a string of the following form to a tuple of out_type latitude, longitude.
Example input:
0°25'30"S, 91°7'W
"""
values = PATTERN.match(s).groupdict()
return tuple(sum(out_type(values[field] or 0)/out_type(60 ** idx) for idx, field in enumerate(field_names)) for field_names in (LAT_FIELDS, LON_FIELDS))
INPUT = """0°25'30"S, 91°7'W"""
print parse_dms_string(INPUT) # Prints: (0.42500000000000004, 91.11666666666666)
fonte
2012-06-01 16:01:19
Quale sarebbe l'uscita corrispondente per la coordinata fornito. Inoltre, la latitudine ha 3 campi numerici, la longitudine 2 .. è quella tipica di come questi sono specificati (e costante nei tuoi dati)? Cosa hai provato fino ad ora? – Levon
Quindi voglio generare su float: '0 ° 25'30" S, 91 ° 7'W' -> '0.425',' 91.116667'. Sembra che i dati possano o meno avere associati minuti. nessuno poi può assumere 0. –
Longitudine 180 W = -180 180 E = 180 Latitudine 90 N = 90 90 S = -90 Esempio bisogna dare -0,425, -91,116667. Controllare questo cercando 0 ° Ad esempio, 25'30 "S, 91 ° 7'W su Google Maps. –