例如,在我的文本文件中,Data.txt:
AB N05-30-00E 495.85
BC N46-02-00E 850.62
CD S67-38-00E 855.45
DE S12-25-00E 1020.87
EF S83-44-00W 1117.26
FA N55-09-00W 660.08
这些是侧面,轴承和长度/距离。如何将其转换为弧度? (之后我会得到纬度和偏离,但如果我能先得到这个,我会稍后再做)然后我写一个带有结果的txt文件,第四个和第五个是纬度和离开。
import math
fileread = open("Data.txt","r")
data = fileread.read
#missing code, convert the string to degrees decimal?
print "%.3f" % math.cos(math.radians())
答案 0 :(得分:3)
我不确定我理解你的数据是什么,所以这只是字符串转换部分的粗略暗示:
# s = "05-30-00"
(degrees, minutes, seconds) = \
map(float, re.match("(\d+)-(\d+)-(\d+)", s).groups())
获得degrees
,minutes
和seconds
之后,您将其“展平”为单个值:
degrees = degrees + minutes / 60.0 + seconds / 3600.0
最后,
rads = math.radians(degrees)
正如你所暗示的,将完成转换。
根据您正在使用的内容,您可能希望根据指南针方向翻转标志,例如:
if compass in ('W', 'S'):
degrees = -degrees
有关转化的详细信息,请查找“Geographic coordinate conversion”。
答案 1 :(得分:1)
是的Jay Kominek是对的。
加上他的回答。
CSV
模块读取输入文件,因为文件结构良好。 <强>输入强>:
AB N05-30-00E 495.85
BC N46-02-00E 850.62
CD S67-38-00E 855.45
DE S12-25-00E 1020.87
EF S83-44-00W 1117.26
FA N55-09-00W 660.08
<强>演示强>:
import csv
import math
import re
def convertRedis(format_input):
"""
Coordinate format conversion
degrees minutes seconds:
decimal degrees = degrees + minutes}/60 + seconds/3600.
"""
degrees, minutes, seconds = map(float, re.match("\w(\d+)-(\d+)-(\d+)\w", format_input).groups())
degrees = degrees + minutes/60 + seconds/3600
return math.radians(degrees)
with open("Technical Description.txt") as fp:
root_r = csv.reader(fp)
root_r = csv.reader(fp, delimiter='\t')
with open("output.txt", "wb+") as fp2:
root_w = csv.writer(fp2, delimiter='\t')
for row in root_r:
rads = convertRedis(row[1])
new_row = list(row)
distance = float(row[2])
if "S" in row[1]:
d_cos = "%.2f"%(distance*math.cos(rads) * -1, )
else:
d_cos = "%.2f"%(distance*math.cos(rads), )
if "W" in row[1]:
d_sin = "%.2f"%(distance*math.sin(rads) * -1, )
else:
d_sin = "%.2f"%(distance*math.sin(rads), )
new_row.extend([d_sin, d_cos])
root_w.writerow(new_row)
输出:
AB N05-30-00E 495.85 47.53 493.57
BC N46-02-00E 850.62 612.23 590.53
CD S67-38-00E 855.45 791.09 -325.53
DE S12-25-00E 1020.87 219.51 -996.99
EF S83-44-00W 1117.26 -1110.58 -121.96
FA N55-09-00W 660.08 -541.70 377.19