Python——将高德坐标(GCJ02)转换为GPS(WGS84)坐标
1 # 官方API: http://lbs.amap.com/api/webservice/guide/api/convert 2 # 坐标体系说明:http://lbs.amap.com/faq/top/coordinate/3 3 # GCJ02->WGS84 Java版本:http://www.cnblogs.com/xinghuangroup/p/5787306.html 4 # 验证坐标转换正确性的地址:http://www.gpsspg.com/maps.htm 5 # 以下内容为原创,转载请注明出处。 6 import xlrd 7 import xlwt 8 import math 9 from xlutils.copy import copy 10 workbook = xlrd.open_workbook("E:/090000440305.xls") 11 sheet = workbook.sheet_by_index(0) 12 locations = sheet.col_values(6) 13 def GCJ2WGS(location): 14 # location格式如下:locations[1] = "113.923745,22.530824" 15 lon = float(location[0:location.find(",")]) 16 lat = float(location[location.find(",") + 1:len(location)]) 17 a = 6378245.0 # 克拉索夫斯基椭球参数长半轴a 18 ee = 0.00669342162296594323 #克拉索夫斯基椭球参数第一偏心率平方 19 PI = 3.14159265358979324 # 圆周率 20 # 以下为转换公式 21 x = lon - 105.0 22 y = lat - 35.0 23 # 经度 24 dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x)); 25 dLon += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0; 26 dLon += (20.0 * math.sin(x * PI) + 40.0 * math.sin(x / 3.0 * PI)) * 2.0 / 3.0; 27 dLon += (150.0 * math.sin(x / 12.0 * PI) + 300.0 * math.sin(x / 30.0 * PI)) * 2.0 / 3.0; 28 #纬度 29 dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x)); 30 dLat += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0; 31 dLat += (20.0 * math.sin(y * PI) + 40.0 * math.sin(y / 3.0 * PI)) * 2.0 / 3.0; 32 dLat += (160.0 * math.sin(y / 12.0 * PI) + 320 * math.sin(y * PI / 30.0)) * 2.0 / 3.0; 33 radLat = lat / 180.0 * PI 34 magic = math.sin(radLat) 35 magic = 1 - ee * magic * magic 36 sqrtMagic = math.sqrt(magic) 37 dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI); 38 dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * PI); 39 wgsLon = lon - dLon 40 wgsLat = lat - dLat 41 return wgsLon,wgsLat 42 wgsWorkbook = copy(workbook) # CMD下:pip install xlutils,该库可以通过复制一个工作簿实现对已有内容的excel文件的写入。 43 wgsSheet = wgsWorkbook.get_sheet(0) # 接上。方式为:复制原工作簿,获取工作表,在新表下写入,保存时名称可以与源文件一致。 44 wgsSheet.write(0,sheet.ncols,"wgsLocation") 45 for i in range(1,sheet.nrows): 46 wgsSheet.write(i,sheet.ncols,str(GCJ2WGS(locations[i])).replace("(","").replace(")","")) # 在新的一列写入转换后的坐标 47 wgsWorkbook.save("E:/090000440305.xls") 48 print("Done!")