Python - Batch convert GPS positions to Lat Lon decimals

后端 未结 3 999
故里飘歌
故里飘歌 2020-12-20 17:00

Hi I have a legacy db with some positional data. The fields are just text fields with strings like this 0°25\'30\"S, 91°7\'W. Is there some way I can convert th

3条回答
  •  有刺的猬
    2020-12-20 17:24

    This approach can deal with seconds and minutes being absent, and I think handles the compass directions correctly:

    # -*- coding: latin-1 -*-
    
    def conversion(old):
        direction = {'N':1, 'S':-1, 'E': 1, 'W':-1}
        new = old.replace(u'°',' ').replace('\'',' ').replace('"',' ')
        new = new.split()
        new_dir = new.pop()
        new.extend([0,0,0])
        return (int(new[0])+int(new[1])/60.0+int(new[2])/3600.0) * direction[new_dir]
    
    lat, lon = u'''0°25'30"S, 91°7'W'''.split(', ')
    print conversion(lat), conversion(lon)
    #Output:
    0.425 91.1166666667
    

提交回复
热议问题