GPS经纬度坐标系转换【python版】

野生程序猿-杂烧4年前随意分享644

再补充个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


标签: gps经纬度

相关文章

如何判断一个经纬度是哪个省?

需求是这样的,有个卖gps防盗器的公司,需要分析出用户去年一年内经过了那些城市?假设有10万个用户,每个用户有100万个轨迹点。我们怎么分析呢?常规方法是一个点一个点的分析,那就是10w*100w=1...

GPS经纬度坐标系转换wgs84转百度【php版】

我们很多做gps设备的获取的经纬度展示轨迹要注意坐标系是需要转换的<?php class GPS {     private&nb...

经纬度的拓展应用

经纬度的拓展应用

前面讲的了如何分析一批gps数据一年的使用情况(所在经纬度是哪个城市);其实这个需求是前公司提给一个python数据分析的。需求蛮多,包括最大速度啊、平均速度啊、有没急刹车啊。我是负责配合数据生成报告...

GPS经纬度坐标系转换【js版】

万能的javascript版也来了,前端、后端nodejs都能用。<html> <body> <script>  /**   ...