Python - 将GPS位置批量转换为经纬度小数 [英] Python - Batch convert GPS positions to Lat Lon decimals

查看:549
本文介绍了Python - 将GPS位置批量转换为经纬度小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个遗留数据库与一些位置数据。这些字段只是带有字符串的文本字段,如 0°25'30S,91°7'W 。有什么方法可以将它们转换为两个浮点数为小数点纬度小数经度



编辑:所以一个例子是: 0°25'30S,91°7'W - > $ b

code> 0.425 , 91.116667 其中原始单个字段位置产生两个浮点数。


解决方案

这种方法可以处理秒和分钟不存在,我认为处理指南针方向正确:

 # -  *  -  coding:latin-1  -  *  -  

def conversion(旧的):
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'30S,91°7'W'''。split(',')
print conversion(lat),conversion(lon)
#Output:
0.425 91.1166666667


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 these to two floating point numbers for Decimal Latitude and Decimal Longitude?

EDIT:

So an example would be: 0°25'30"S, 91°7'W -> 0.425, 91.116667 where the original single field position yields two floats.

Any help much appreciated.

解决方案

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

这篇关于Python - 将GPS位置批量转换为经纬度小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆