简体   繁体   English

从文本文件中的数据将度-分钟转换为弧度

[英]Converting degrees-minutes to radians from data in a text file

For example in my text file, Data.txt : 例如在我的文本文件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

These are the sides, bearing and the length/distance respectively. 这些分别是侧面,方位和长度/距离。 How can I convert this to radians? 如何将其转换为弧度? (after that I will get the latitude and departures, but I will do that later if only I could get this one first) Then I write a txt file with the results, the fourth and fifth are the latitude and departure. (此后,我将获得纬度和纬度,但是如果我只能先获得该纬度和纬度,则稍后再做)。然后,我将结果写入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())

I'm not sure I understand what your data presents, so this is just a rough hint at the string conversion part: 我不确定我是否理解您的数据显示什么,因此这只是字符串转换部分的粗略提示:

# s = "05-30-00"
(degrees, minutes, seconds) = \
   map(float, re.match("(\d+)-(\d+)-(\d+)", s).groups())

Once you've got degrees , minutes , and seconds , you "flatten" them into a single value: 获得degreesminutesseconds ,就可以将它们“展平”为一个值:

degrees = degrees + minutes / 60.0 + seconds / 3600.0

finally, 最后,

rads = math.radians(degrees)

will finish that conversion, as you hinted. 如您所提示,将完成该转换。

Depending on what you're doing with it, you might want to flip the sign, based on the compass direction, like: 根据您的操作方式,您可能需要根据指南针的方向翻转标志,例如:

if compass in ('W', 'S'):
  degrees = -degrees

For more details on the conversions, lookup " Geographic coordinate conversion ". 有关转换的更多详细信息,请查找“ 地理坐标转换 ”。

Yes Jay Kominek is right. 是的,Jay Kominek是对的。

Just add to his answer. 只需添加他的答案即可。

  1. Created function according Jay Kominek answer. 根据Jay Kominek的回答创建了函数。
  2. Read input file by CSV modules because file is well structured. 通过CSV模块读取输入文件,因为文件结构良好。
  3. Calculate output and write Row in to output file. 计算输出并将行写入输出文件。

input : 输入

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

Demo : 演示

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)

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM