def convert_dms2dec(direction, degrees_str, minutes_str, seconds_str): """ Convert DMS format into a N or E decimal coordinate """ degrees = _float_or_value(degrees_str) minutes = _float_or_value(minutes_str) seconds = _float_or_value(seconds_str) ## lua version is: # direction = mw.ustring.gsub(direction, '^[ ]*(.-)[ ]*$', '%1') ## a plain translation would be: # direction_match = re.match(r'^[ ]*(.)[ ]*$', direction) # if direction_match and direction_match.group(1) in "NE": ## but it can also be done with: direction = direction.strip() if direction in "NE": factor = 1 else: factor = -1 if seconds_str: precision = 5 + max(math_mod._precision(seconds_str), 0) elif minutes_str: precision = 3 + max(math_mod._precision(minutes_str), 0) else: precision = max(math_mod._precision(degrees_str), 0) decimal = factor * (degrees + (minutes + seconds / 60) / 60) # not float since this whole thing is string based. return ("%%.%df" % (precision,)) % (decimal,)
def convert_dms2dec(direction, degrees_str, minutes_str, seconds_str): """ Convert DMS format into a N or E decimal coordinate """ degrees = _float_or_value(degrees_str) minutes = _float_or_value(minutes_str) seconds = _float_or_value(seconds_str) ## lua version is: # direction = mw.ustring.gsub(direction, '^[ ]*(.-)[ ]*$', '%1') ## a plain translation would be: # direction_match = re.match(r'^[ ]*(.)[ ]*$', direction) # if direction_match and direction_match.group(1) in "NE": ## but it can also be done with: direction = direction.strip() if direction in "NE": factor = 1 else: factor = -1 if seconds_str: precision = 5 + max(math_mod._precision(seconds_str), 0) elif minutes_str: precision = 3 + max(math_mod._precision(minutes_str), 0) else: precision = max(math_mod._precision(degrees_str), 0) decimal = factor * (degrees + (minutes + seconds / 60) / 60) # not float since this whole thing is string based. return ("%%.%df" % (precision, )) % (decimal, )
def parseDMS(lat_d, lat_m, lat_s, lat_f, long_d, long_m, long_s, long_f, format_=None): """ Transforms degrees, minutes, seconds format latitude and longitude into the a structure to be used in displaying coordinates """ coordinateSpec = {} lat_f = lat_f.upper() long_f = long_f.upper() # Check if specified backward if lat_f == 'E' or lat_f == 'W': t_d = lat_d t_m = lat_m t_s = lat_s t_f = lat_f lat_d = long_d lat_m = long_m lat_s = long_s lat_f = long_f long_d = t_d long_m = t_m long_s = t_s long_f = t_f errors = validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, 'parseDMS', True) if not long_d: raise Exception('Missing longitude') if lat_m is None and lat_s is None and long_m is None and long_s is None and not len(errors): if math_mod._precision(lat_d) > 0 or math_mod._precision(long_d) > 0: if lat_f.upper() == 'S': lat_d = '-' + lat_d if long_f.upper() == 'W': long_d = '-' + long_d return parseDec(lat_d, long_d, format_) coordinateSpec["dms-lat"] = lat_d + "°" + optionalArg( lat_m, "′") + optionalArg(lat_s, "″") + lat_f coordinateSpec["dms-long"] = long_d + "°" + optionalArg( long_m, "′") + optionalArg(long_s, "″") + long_f # {{coord/dms2dec|{{{4}}}|{{{1}}}|0{{{2}}}|0{{{3}}}}} coordinateSpec["dec-lat"] = convert_dms2dec( lat_f, lat_d, lat_m, lat_s) # {{coord/dms2dec|{{{8}}}|{{{5}}}|0{{{6}}}|0{{{7}}}}} coordinateSpec["dec-long"] = convert_dms2dec( long_f, long_d, long_m, long_s) if format_: coordinateSpec['default'] = format_ else: coordinateSpec['default'] = "dms" return coordinateSpec, errors
def coordinates_determineMode(value1, value2): """ Helper function to determine whether to use D, DM, or DMS format depending on the precision of the decimal input. """ precision = max(math_mod._precision(value1), math_mod._precision(value2)) if precision <= 0: return 'd' elif precision <= 2: return 'dm' else: return 'dms'
def coordinates_determineMode(value1, value2): """ Helper function to determine whether to use D, DM, or DMS format depending on the precision of the decimal input. """ precision = max(math_mod._precision( value1), math_mod._precision(value2)) if precision <= 0: return 'd' elif precision <= 2: return 'dm' else: return 'dms'
def parseDMS(lat_d, lat_m, lat_s, lat_f, long_d, long_m, long_s, long_f, format_=None): """ Transforms degrees, minutes, seconds format latitude and longitude into the a structure to be used in displaying coordinates """ coordinateSpec = {} lat_f = lat_f.upper() long_f = long_f.upper() # Check if specified backward if lat_f == 'E' or lat_f == 'W': t_d = lat_d t_m = lat_m t_s = lat_s t_f = lat_f lat_d = long_d lat_m = long_m lat_s = long_s lat_f = long_f long_d = t_d long_m = t_m long_s = t_s long_f = t_f errors = validate(lat_d, lat_m, lat_s, long_d, long_m, long_s, 'parseDMS', True) if not long_d: raise Exception('Missing longitude') if lat_m is None and lat_s is None and long_m is None and long_s is None and not len( errors): if math_mod._precision(lat_d) > 0 or math_mod._precision(long_d) > 0: if lat_f.upper() == 'S': lat_d = '-' + lat_d if long_f.upper() == 'W': long_d = '-' + long_d return parseDec(lat_d, long_d, format_) coordinateSpec["dms-lat"] = lat_d + "°" + optionalArg( lat_m, "′") + optionalArg(lat_s, "″") + lat_f coordinateSpec["dms-long"] = long_d + "°" + optionalArg( long_m, "′") + optionalArg(long_s, "″") + long_f # {{coord/dms2dec|{{{4}}}|{{{1}}}|0{{{2}}}|0{{{3}}}}} coordinateSpec["dec-lat"] = convert_dms2dec(lat_f, lat_d, lat_m, lat_s) # {{coord/dms2dec|{{{8}}}|{{{5}}}|0{{{6}}}|0{{{7}}}}} coordinateSpec["dec-long"] = convert_dms2dec(long_f, long_d, long_m, long_s) if format_: coordinateSpec['default'] = format_ else: coordinateSpec['default'] = "dms" return coordinateSpec, errors