GPS经纬度坐标系转换【python版】
再补充个python版的gps经纬度坐标系,在脚本处理数据的时候python,nodejs要比php性能好很多。尤其是他们更擅长多线程
# -*- coding: UTF-8 -*- ''' WGS-84:是国际标准,GPS坐标(Google Earth使用、或者GPS模块) GCJ-02:中国坐标偏移标准,Google Map、高德、腾讯使用 BD-09:百度坐标偏移标准,Baidu Map使用 ''' import math import os import demjson class gps: x_pi = 0 PI = 3.14159265358979324 china_points={} def __init__(self): # self.x_pi = 3.14159265358979324 * 3000.0 / 180.0 #取出china的点,加进china_points current_path = os.path.dirname(os.path.abspath(__file__)) ''' china_path=current_path + os.path.sep + 'china_0.1_all.json' with open(china_path, "r") as file: # 只需要将之前的”w"改为“a"即可,代表追加内容 china_all_str = file.read() file.close() china_all_arr=demjson.decode(china_all_str) for v in china_all_arr: this_arr=v.split(',') this_lng = this_arr[0] this_lat = this_arr[1] this_lng=int(float(this_lng)*10) this_lat = int(float(this_lat) * 10) this_key=str(this_lng)+'_'+str(this_lat) self.china_points[this_key]=1 with open(current_path + os.path.sep + 'china_points.json','w') as file: #只需要将之前的”w"改为“a"即可,代表追加内容 file.write(demjson.encode(self.china_points)) file.close() ''' china_points_path = current_path + os.path.sep + 'china_points.json' with open(china_points_path, "r") as file: # 只需要将之前的”w"改为“a"即可,代表追加内容 china_points_str = file.read() file.close() self.china_points = demjson.decode(china_points_str) #WGS-84 to GCJ-02 (Google Map、高德、腾讯使用) def gcj_encrypt(self,wgsLon,wgsLat): if self.outOfChina(wgsLon,wgsLat)==0: return [wgsLon,wgsLat] d = self.delta(wgsLon,wgsLat) return [wgsLon + d[0],wgsLat + d[1]] #GCJ-02 to BD-09 (百度使用) def bd_encrypt(self,gcjLon,gcjLat): x = gcjLon y = gcjLat z = math.sqrt(x * x + y * y) + 0.00002 * math.sin(y * self.x_pi) theta = math.atan2(y, x) + 0.000003 * math.cos(x * self.x_pi) bdLon = z * math.cos(theta) + 0.0065 bdLat = z * math.sin(theta) + 0.006 return [bdLon,bdLat] def in_china(self,this_lng,this_lat): this_lng = int(float(this_lng) * 10) this_lat = int(float(this_lat) * 10) result=0 this_key = str(this_lng) + '_' + str(this_lat) lng_t=[0,1,-1] lat_t=[0,1,-1] for lng_x in lng_t: for lat_x in lat_t: new_lng = this_lng + lng_x new_lat = this_lat + lat_x new_key = str(new_lng) + '_' + str(new_lat) #print(new_key) try: if self.china_points[new_key]==1: result=1 except: # key不存在 pass if result==1: break if result == 1: break return result def outOfChina(self,wgsLon,wgsLat): return 1 #WGS-84 to BD-09 def wgs84_bd(self,wgsLon,wgsLat): r=self.gcj_encrypt(wgsLon,wgsLat) gcjLat=r[0] gcjLon=r[1] result=self.bd_encrypt(gcjLat, gcjLon) return result def delta(self,lon,lat): # Krasovsky 1940 # # a = 6378245.0, 1/f = 298.3 # b = a * (1 - f) # ee = (a^2 - b^2) / a^2 a = 6378245.0# a: 卫星椭球坐标投影到平面地图坐标系的投影因子。 ee = 0.00669342162296594323# ee: 椭球的偏心率。 dLat = self.transformLat(lon - 105.0, lat - 35.0) dLon = self.transformLon(lon - 105.0, lat - 35.0) radLat = lat / 180.0 * self.PI magic = math.sin(radLat) magic = 1 - ee * magic * magic sqrtMagic = math.sqrt(magic) dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * self.PI) dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * self.PI) return [dLon,dLat] def transformLat(self,x, y): ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x)) ret += (20.0 * math.sin(6.0 * x * self.PI) + 20.0 * math.sin(2.0 * x * self.PI)) * 2.0 / 3.0 ret += (20.0 * math.sin(y * self.PI) + 40.0 * math.sin(y / 3.0 * self.PI)) * 2.0 / 3.0 ret += (160.0 * math.sin(y / 12.0 * self.PI) + 320 * math.sin(y * self.PI / 30.0)) * 2.0 / 3.0 return ret def transformLon(self,x, y): ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x)) ret += (20.0 * math.sin(6.0 * x * self.PI) + 20.0 * math.sin(2.0 * x * self.PI)) * 2.0 / 3.0 ret += (20.0 * math.sin(x * self.PI) + 40.0 * math.sin(x / 3.0 * self.PI)) * 2.0 / 3.0 ret += (150.0 * math.sin(x / 12.0 * self.PI) + 300.0 * math.sin(x / 30.0 * self.PI)) * 2.0 / 3.0 return ret