我有一个.dat文件,其中包含数千条记录,每条记录都包含单独的Degree Minute Seconds字段。我试图将其转换为十进制度数,这是大多数地图/ GIS软件的标准。
转换的数学计算非常简单,但我没有得到预期的输出。我假设这是因为数据以string
的形式存在,我投放到int
和float
类型进行计算。
这里的计算如下:
latitude = (degs+(mins/60)+(secs/3600))
脚本:
import csv
georgia = r'C:\Users\rhewitt\Desktop\CellTowers\DOF\13-GA.Dat'
output = r'C:\Users\rhewitt\Desktop\CellTowers\CSV\georgia.csv'
header = r'C:\Users\rhewitt\Desktop\CellTowers\header.csv'
dat = open(georgia, 'r')
f = open(output, 'wb')
wr = csv.writer(f, quoting=csv.QUOTE_ALL)
# Create header
header = ["ObstacleNo", "ver_status", "Country", "State", "City", "latdeg", "latmin", "latsec", "N_S", "latitude", "londeg", "lonmin", "lonsec", "E_W", "longitude", "Type", "QTY", "AGL_HT", "AMSL_HT", "Lighting", "horz_acc", "vert_acc", "marking", "FAA_no", "Action", "Julian_dt"]
wr.writerow(header)
i=-1
for line in dat:
i+=1
# The top five rows of the .dat file contains header information (skip them)
if i < 5:
continue
# .dat files have fixed-width columns, so I can pull out information like this:
else:
ORS = line[:9]
v = line[9:11]
country = line[11:14]
state = line[14:17]
city = line[17:35]
latDeg = line[35:37]
latMin = line[37:40]
latSec = line[40:46]
latHem = line[46:47]
lonDeg = line[47:51]
lonMin = line[51:54]
lonSec = line[54:60]
lonHem = line[60:61]
obType = line[61:75]
uknown = line[75:76]
agl = line[76:82]
amsl = line[82:88]
lt = line[88:90]
ah = line[90:92]
ccv = line[92:94]
MIN = line[94:96]
study = line[96:111]
action = line[111:113]
jDate = line[113:]
# Convert Degrees Minutes Seconds to Decimal Degrees for easier mapping
if latHem == "N":
latitude = (int(latDeg)+(int(latMin)/60)+(float(latSec)/3600))
else:
latitude = -1*(int(latDeg)+(int(latMin)/60)+(float(latSec)/3600))
if lonHem == "E":
longitude = (int(lonDeg)+(int(lonMin)/60)+(float(lonSec)/3600))
else:
longitude = -1*(int(lonDeg)+(int(lonMin)/60)+(float(lonSec)/3600))
csvLine = [ORS, v, country, state, city, latDeg, latMin, latSec, latHem, latitude, lonDeg, lonMin, lonSec, lonHem, longitude, obType, uknown, agl, amsl, lt, ah, ccv, MIN, study, action, jDate.rstrip("\n")]
wr.writerow(csvLine)
f.close()
dat.close()
以下是我脚本的示例输出:
(34 + (59/60)+(10.00/3600)) = 34.0027777778
,但正确的值实际为34.9861111111