最近在处理数据的过程中,偶然遇到了经纬度转换的问题,如下所示,获取的GPS数据是以度为单位的,但是突然想将其转化为以度分秒为单位的数据,遂写点代码作为记录
###############################################################
# 将以度为单位的经纬度(N*2)转化为以度分秒为单位的经纬度(N*3)
# input:latlon.xlsx output:GPS.xlsx
###############################################################
import openpyxl # 处理Excel的包
f = openpyxl.load_workbook("D:\Data\latlon.xlsx") # 打开源文件
out_f = openpyxl.Workbook() # 创建输出Excel
table1 = out_f.active
table1.title = 'success' # 创建输出的sheet名称
# 写入表头
title = ['LONGITUDE', 'LATITUDE', 'LABEL']
for i in range(3):
table1.cell(row=1, column=i + 1, value=title[i])
sheet1 = f['Sheet1'] # 存储数据表头
ncols = sheet1.max_row
s = 2
for i in range(1, ncols + 1):
N = sheet1.cell(row=i, column=1).value # 获取原坐标
E = sheet1.cell(row=i, column=2).value
tempN = str(int(N)) + "°" + str(int((N - int(N)) * 60)) + "'" + str(
(((N - int(N)) * 60) - int((N - int(N)) * 60)) * 60) + "''" + ' ' + 'N' # 坐标转换
tempE = str(int(E)) + "°" + str(int((E - int(E)) * 60)) + "'" + str(
(((E - int(E)) * 60) - int((E - int(E)) * 60)) * 60) + "''" + ' ' + 'E'
label = i
table1.cell(row=s, column=2, value=tempN)
table1.cell(row=s, column=1, value=tempE)
table1.cell(row=s, column=3, value=label)
s += 1
out_f.save("D:\Data\GPS.xlsx") # 保存文件