# - - - p a r s e L a t
def parseLat ( s ):
"""Validate and convert an external latitude.
[ s is a nonempty string ->
if s is a valid external latitude ->
return that latitude in radians
else -> raise SyntaxError ]
"""
A latitude has two pieces: an angle, and a suffix letter
which must be either “n” or
“s”, case-insensitive.
First we'll peel off the suffix and check it using
Section 8.11, “NS_PAT: Latitude suffix”.
#-- 1 --
# [ last := last character of s
# rawAngle := s up to the last character ]
last = s[-1]
rawAngle = s[:-1]
#-- 2 --
# [ if last matches NS_PAT ->
# nsFlag := last, lowercased
# else -> raise SyntaxError ]
m = NS_PAT.match ( last )
if m is None:
raise SyntaxError, ( "Latitude '%s' does not end with 'n' "
"or 's'." % s )
else:
nsFlag = last.lower()
Validating the angle part is done by Section 9.14, “parseAngle(): Convert an external
angle string”.
#-- 3 --
# [ if rawAngle is a valid angle ->
# absAngle := that angle in radians
# else -> raise SyntaxError ]
absAngle = parseAngle ( rawAngle )
North latitude is positive, and south latitude is negative.
#-- 4 --
if nsFlag == 's': angle = - absAngle
else: angle = absAngle
#-- 5 --
return angle