目录
主函数
转换函数
仅仅是简单运行,后续优化点标记在了代码注释中。
主函数
# mian.py
# WGS84坐标系转换成火星坐标系
from trans import transform
import pandas as pd
# 示例
# WGS84_Lat = 39.990205
# WGS84_Long = 116.327847
# print (transform(WGS84_Lat, WGS84_Long))
# 读取原始数据
# #可以通过sheet_name来指定读取的表单 df=pd.read_excel('lemon.xlsx',sheet_name='student')
ori_data = pd.read_excel('data.xlsx')
# 获取数据的行数与列数
(num_row,num_col) = ori_data.shape
ori_longs = ori_data['经度'].values
ori_lats =ori_data['纬度'].values
# 新数据的初始值
name_newExcel = 'newdata.xlsx' #可以使用正则,增加命名规律
# 优化:运行之前检测有没有已经存在的newdata的文件,如果已经存在,则先删除再新增;或者设置新的名字
new_data = ori_data
new_data.drop('经度',axis=1) #删除列
new_data.drop('纬度',axis=1) #删除列
Mars_Lat =[0 for x in range(num_row)]
Mars_Long = [0 for x in range(num_row)]
#处理数据
new_long = 0
new_lat = 0
for i in range(num_row):
WGS84_Lat=ori_lats[i]
WGS84_Long=ori_longs[i]
# 优化:增加 无效数据处理过程, 可以转换为0;此过程可往前放
# 有效数据转换
(new_long,new_lat = transform(WGS84_Lat, WGS84_Long)
Mars_Lat[i]=new_lat
Mars_Long[i]=new_long
# 往新的excel数据里面,新增列
new_data['经度']=Mars_Long
new_data['纬度']=Mars_Lat
#注意:只在最后的列的地方增加列,没有按原来位置插入列
# 输出新的excel文件
pd.DataFrame(new_data).to_excel(name_newExcel)
转换函数
其中的转换函数是引用自他人的代码:
# transform.py
from __future__ import division
from math import pi, sqrt, sin, cos
# a python binding of https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936
# Krasovsky 1940
#
# a = 6378245.0, 1/f = 298.3
# b = a * (1 - f)
# ee = (a^2 - b^2) / a^2
a = 6378245.0 # 长半轴
ee = 0.00669342162296594323 # 扁率
# World Geodetic System ==> Mars Geodetic System
def transform(wgLat, wgLon):
"""
transform(latitude,longitude) , WGS84
return (latitude,longitude) , GCJ02
"""
if (outOfChina(wgLat, wgLon)):
mgLat = wgLat
mgLon = wgLon
return
dLat = transformLat(wgLon - 105.0, wgLat - 35.0)
dLon = transformLon(wgLon - 105.0, wgLat - 35.0)
radLat = wgLat / 180.0 * pi
magic = sin(radLat)
magic = 1 - ee * magic * magic
sqrtMagic = sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi)
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi)
mgLat = wgLat + dLat
mgLon = wgLon + dLon
return mgLon, mgLat
def outOfChina(lat, lon):
if (lon < 72.004 or lon > 137.8347):
return True
if (lat < 0.8293 or lat > 55.8271):
return True
return False
def transformLat(x, y):
ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0
ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0
ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0
return ret
def transformLon(x, y):
ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x))
ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0
ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0
ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0
return ret
# if __name__ == "__main__":
# WGS84_Lat = 39.990205
# WGS84_Long = 116.327847
# print (transform(WGS84_Lat, WGS84_Long))
?
|