<s> import re <EOL> from itertools import islice <EOL> from geopy import util , units , format <EOL> class Point ( object ) : <EOL> """<STR_LIT>""" <EOL> UTIL_PATTERNS = dict ( <EOL> FLOAT = r'<STR_LIT>' , <EOL> DEGREE = format . DEGREE , <EOL> PRIME = format . PRIME , <EOL> DOUBLE_PRIME = format . DOUBLE_PRIME , <EOL> SEP = r'<STR_LIT>' <EOL> ) <EOL> POINT_PATTERN = re . compile ( r"""<STR_LIT>""" % UTIL_PATTERNS , re . X ) <EOL> def __new__ ( cls , latitude = None , longitude = None , altitude = None ) : <EOL> single_arg = longitude is None and altitude is None <EOL> if single_arg and not isinstance ( latitude , util . NUMBER_TYPES ) : <EOL> arg = latitude <EOL> if arg is None : <EOL> pass <EOL> elif isinstance ( arg , Point ) : <EOL> return cls . from_point ( arg ) <EOL> elif isinstance ( arg , basestring ) : <EOL> return cls . from_string ( arg ) <EOL> else : <EOL> try : <EOL> seq = iter ( arg ) <EOL> except TypeError : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % ( arg , ) <EOL> ) <EOL> else : <EOL> return cls . from_sequence ( seq ) <EOL> latitude = float ( latitude or <NUM_LIT:0> ) <EOL> if abs ( latitude ) > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" % latitude ) <EOL> longitude = float ( longitude or <NUM_LIT:0> ) <EOL> if abs ( longitude ) > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" % longitude ) <EOL> altitude = float ( altitude or <NUM_LIT:0> ) <EOL> self = super ( Point , cls ) . __new__ ( cls ) <EOL> self . latitude = latitude <EOL> self . longitude = longitude <EOL> self . altitude = altitude <EOL> return self <EOL> def __getitem__ ( self , index ) : <EOL> return ( self . latitude , self . longitude , self . altitude ) [ index ] <EOL> def __setitem__ ( self , index , value ) : <EOL> point = [ self . latitude , self . longitude , self . altitude ] <EOL> point [ index ] = value <EOL> self . latitude , self . longitude , self . altitude = point <EOL> def __iter__ ( self ) : <EOL> return iter ( ( self . latitude , self . longitude , self . altitude ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . latitude , self . longitude , self . altitude <EOL> ) <EOL> def format ( self , altitude = None , deg_char = '<STR_LIT>' , min_char = '<STR_LIT:m>' , sec_char = '<STR_LIT:s>' ) : <EOL> latitude = "<STR_LIT>" % ( <EOL> format . angle ( abs ( self . latitude ) , deg_char , min_char , sec_char ) , <EOL> self . latitude >= <NUM_LIT:0> and '<STR_LIT:N>' or '<STR_LIT:S>' <EOL> ) <EOL> longitude = "<STR_LIT>" % ( <EOL> format . angle ( abs ( self . longitude ) , deg_char , min_char , sec_char ) , <EOL> self . longitude >= <NUM_LIT:0> and '<STR_LIT:E>' or '<STR_LIT>' <EOL> ) <EOL> coordinates = [ latitude , longitude ] <EOL> if altitude is None : <EOL> altitude = bool ( self . altitude ) <EOL> if altitude : <EOL> if not isinstance ( altitude , basestring ) : <EOL> altitude = '<STR_LIT>' <EOL> coordinates . append ( self . format_altitude ( altitude ) ) <EOL> return "<STR_LIT:U+002CU+0020>" . join ( coordinates ) <EOL> def format_decimal ( self , altitude = None ) : <EOL> latitude = "<STR_LIT:%s>" % self . latitude <EOL> longitude = "<STR_LIT:%s>" % self . longitude <EOL> coordinates = [ latitude , longitude ] <EOL> if altitude is None : <EOL> altitude = bool ( self . altitude ) <EOL> if altitude : <EOL> if not isinstance ( altitude , basestring ) : <EOL> altitude = '<STR_LIT>' <EOL> coordinates . append ( self . format_altitude ( altitude ) ) <EOL> return "<STR_LIT:U+002CU+0020>" . join ( coordinates ) <EOL> def format_altitude ( self , unit = '<STR_LIT>' ) : <EOL> return format . distance ( self . altitude , unit ) <EOL> def __str__ ( self ) : <EOL> return self . format ( ) <EOL> def __unicode__ ( self ) : <EOL> return self . format ( <EOL> None , format . DEGREE , format . PRIME , format . DOUBLE_PRIME <EOL> ) <EOL> def __eq__ ( self , other ) : <EOL> return tuple ( self ) == tuple ( other ) <EOL> def __ne__ ( self , other ) : <EOL> return tuple ( self ) != tuple ( other ) <EOL> @ classmethod <EOL> def parse_degrees ( cls , degrees , arcminutes , arcseconds , direction = None ) : <EOL> negative = degrees < <NUM_LIT:0> or degrees . startswith ( '<STR_LIT:->' ) <EOL> degrees = float ( degrees or <NUM_LIT:0> ) <EOL> arcminutes = float ( arcminutes or <NUM_LIT:0> ) <EOL> arcseconds = float ( arcseconds or <NUM_LIT:0> ) <EOL> if arcminutes or arcseconds : <EOL> more = units . degrees ( arcminutes = arcminutes , arcseconds = arcseconds ) <EOL> if negative : <EOL> degrees -= more <EOL> else : <EOL> degrees += more <EOL> if direction in [ None , '<STR_LIT:N>' , '<STR_LIT:E>' ] : <EOL> return degrees <EOL> elif direction in [ '<STR_LIT:S>' , '<STR_LIT>' ] : <EOL> return - degrees <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def parse_altitude ( cls , distance , unit ) : <EOL> if distance is not None : <EOL> distance = float ( distance ) <EOL> CONVERTERS = { <EOL> '<STR_LIT>' : lambda d : d , <EOL> '<STR_LIT:m>' : lambda d : units . kilometers ( meters = d ) , <EOL> '<STR_LIT>' : lambda d : units . kilometers ( miles = d ) , <EOL> '<STR_LIT>' : lambda d : units . kilometers ( feet = d ) , <EOL> '<STR_LIT>' : lambda d : units . kilometers ( nautical = d ) , <EOL> '<STR_LIT>' : lambda d : units . kilometers ( nautical = d ) <EOL> } <EOL> return CONVERTERS [ unit ] ( distance ) <EOL> else : <EOL> return distance <EOL> @ classmethod <EOL> def from_string ( cls , string ) : <EOL> """<STR_LIT>""" <EOL> match = re . match ( cls . POINT_PATTERN , string ) <EOL> if match : <EOL> latitude = cls . parse_degrees ( <EOL> match . group ( '<STR_LIT>' ) , <EOL> match . group ( '<STR_LIT>' ) , <EOL> match . group ( '<STR_LIT>' ) , <EOL> match . group ( '<STR_LIT>' ) <EOL> ) <EOL> longitude = cls . parse_degrees ( <EOL> match . group ( '<STR_LIT>' ) , <EOL> match . group ( '<STR_LIT>' ) , <EOL> match . group ( '<STR_LIT>' ) , <EOL> match . group ( '<STR_LIT>' ) , <EOL> ) <EOL> altitude = cls . parse_altitude ( <EOL> match . group ( '<STR_LIT>' ) , <EOL> match . group ( '<STR_LIT>' ) <EOL> ) <EOL> return cls ( latitude , longitude , altitude ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> @ classmethod <EOL> def from_sequence ( cls , seq ) : <EOL> """<STR_LIT>""" <EOL> args = tuple ( islice ( seq , <NUM_LIT:4> ) ) <EOL> return cls ( * args ) <EOL> @ classmethod <EOL> def from_point ( cls , point ) : <EOL> """<STR_LIT>""" <EOL> return cls ( point . latitude , point . longitude , point . altitude ) </s>
