代码转java不行,精度不一样,转换后经纬度有问题。
import json
import urllib
import math
import pandas as pd
x_pi = 3.14159265358979324 * 3000.0 / 180.0
pi = 3.1415926535897932384626 # π
a = 6378245.0 # 长半轴
ee = 0.00669342162296594323 # 偏心率平方
def gcj02_to_bd09(lon, lat):
"""
火星坐标系(GCJ-02)转百度坐标系(BD-09)
谷歌、高德——>百度
:param lon:火星坐标经度
:param lat:火星坐标纬度
:return:
"""
z = math.sqrt(lon * lon + lat * lat) + 0.00002 * math.sin(lat * x_pi)
theta = math.atan2(lat, lon) + 0.000003 * math.cos(lon * x_pi)
bd_lon = z * math.cos(theta) + 0.0065
bd_lat = z * math.sin(theta) + 0.006
return [bd_lon, bd_lat]
def bd09_to_gcj02(bd_lon, bd_lat):
"""
百度坐标系(BD-09)转火星坐标系(GCJ-02)
百度——>谷歌、高德
:param bd_lat:百度坐标纬度
:param bd_lon:百度坐标经度
:return:转换后的坐标列表形式
"""
x = bd_lon - 0.0065
y = bd_lat - 0.006
z = math.sqrt(x * x + y * y) - 0.00002 * math.sin(y * x_pi)
theta = math.atan2(y, x) - 0.000003 * math.cos(x * x_pi)
gg_lon = z * math.cos(theta)
gg_lat = z * math.sin(theta)
return [gg_lon, gg_lat]
def wgs84_to_gcj02(lon, lat):
"""
WGS84转GCJ02(火星坐标系)
:param lon:WGS84坐标系的经度
:param lat:WGS84坐标系的纬度
:return:
"""
if out_of_china(lon, lat): # 判断是否在国内
return [lon, lat]
dlat = _transformlat(lon - 105.0, lat - 35.0)
dlon = _transformlon(lon - 105.0, lat - 35.0)
radlat = lat / 180.0 * pi
magic = math.sin(radlat)
magic = 1 - ee * magic * magic
sqrtmagic = math.sqrt(magic)
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)
dlon = (dlon * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
mglat = lat + dlat
mglon = lon + dlon
return [mglon, mglat]
def gcj02_to_wgs84(lon, lat):
"""
GCJ02(火星坐标系)转GPS84
:param lon:火星坐标系的经度
:param lat:火星坐标系纬度
:return:
"""
if out_of_china(lon, lat):
return [lon, lat]
dlat = _transformlat(lon - 105.0, lat - 35.0)
dlon = _transformlon(lon - 105.0, lat - 35.0)
radlat = lat / 180.0 * pi
magic = math.sin(radlat)
magic = 1 - ee * magic * magic
sqrtmagic = math.sqrt(magic)
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)
dlon = (dlon * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
mglat = lat + dlat
mglon = lon + dlon
return [lon * 2 - mglon, lat * 2 - mglat]
def bd09_to_wgs84(bd_lon, bd_lat):
lon, lat = bd09_to_gcj02(bd_lon, bd_lat)
return gcj02_to_wgs84(lon, lat)
def wgs84_to_bd09(lon, lat):
lon, lat = wgs84_to_gcj02(lon, lat)
return gcj02_to_bd09(lon, lat)
def _transformlat(lon, lat):
ret = -100.0 + 2.0 * lon + 3.0 * lat + 0.2 * lat * lat + \
0.1 * lon * lat + 0.2 * math.sqrt(math.fabs(lon))
ret += (20.0 * math.sin(6.0 * lon * pi) + 20.0 *
math.sin(2.0 * lon * pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lat * pi) + 40.0 *
math.sin(lat / 3.0 * pi)) * 2.0 / 3.0
ret += (160.0 * math.sin(lat / 12.0 * pi) + 320 *
math.sin(lat * pi / 30.0)) * 2.0 / 3.0
return ret
def _transformlon(lon, lat):
ret = 300.0 + lon + 2.0 * lat + 0.1 * lon * lon + \
0.1 * lon * lat + 0.1 * math.sqrt(math.fabs(lon))
ret += (20.0 * math.sin(6.0 * lon * pi) + 20.0 *
math.sin(2.0 * lon * pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lon * pi) + 40.0 *
math.sin(lon / 3.0 * pi)) * 2.0 / 3.0
ret += (150.0 * math.sin(lon / 12.0 * pi) + 300.0 *
math.sin(lon / 30.0 * pi)) * 2.0 / 3.0
return ret
def out_of_china(lon, lat):
"""
判断是否在国内,不在国内不做偏移
:param lon:
:param lat:
:return:
"""
return not (lon > 73.66 and lon < 135.05 and lat > 3.86 and lat < 53.55)
#举例:批量转换excel中给出的的经纬度
if __name__ == '__main__':
#读取Excel文件
excel_file_path = 'F://home//file//test.xlsx' # 替换为实际的输入Excel文件路径
df = pd.read_excel(excel_file_path)
results = df.values
# 将转换后的WGS84坐标添加到DataFrame中
wgs84_lon_list = []
wgs84_lat_list = []
for result in results:
# 转换为WGS84坐标系的经纬度
wgs84_lon, wgs84_lat = gcj02_to_wgs84(result[4], result[5]) #修改为excel中(经度,纬度)的实际下标
wgs84_lon_list.append(wgs84_lon)
wgs84_lat_list.append(wgs84_lat)
df['WGS84_Lon'] = wgs84_lon_list
df['WGS84_Lat'] = wgs84_lat_list
# 导出DataFrame到Excel文件
output_excel_file_path = 'F://home//file//outputTest.xlsx' # 替换为实际的输出Excel文件路径
df.to_excel(output_excel_file_path, index=False)