gps坐标系转换--Python实现

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import math


pi = 3.14159265358979324
a = 6378245.0
ee = 0.00669342162296594323
x_pi = 3.14159265358979324 * 3000.0 / 180.0


def outOfChina(lat, lng):
    if lng < 72.004 or lng > 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 * math.sqrt(abs(x))
    ret += (20.0 * math.sin(6.0 * x * pi) + 20.0 * math.sin(2.0 * x * pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(y * pi) + 40.0 * math.sin(y / 3.0 * pi)) * 2.0 / 3.0
    ret += (160.0 * math.sin(y / 12.0 * pi) + 320 * math.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 * math.sqrt(abs(x))
    ret += (20.0 * math.sin(6.0 * x * pi) + 20.0 * math.sin(2.0 * x * pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(x * pi) + 40.0 * math.sin(x / 3.0 * pi)) * 2.0 / 3.0
    ret += (150.0 * math.sin(x / 12.0 * pi) + 300.0 * math.sin(x / 30.0 * pi)) * 2.0 / 3.0
    return ret

#地球坐标转换为火星坐标,即WGS84(国际通用)转为GCJ02坐标系适用于腾讯地图、高德(阿里)地图或谷歌地图
def WGS84toGCJ02(wgLat, wgLon):
    latlng = [1.0, 1.0]
    if outOfChina(wgLat, wgLon) == True:
        latlng[0] = wgLat
        latlng[1] = wgLon
        return latlng

    dLat = transformLat(wgLon - 105.0, wgLat - 35.0)
    dLon = transformLon(wgLon - 105.0, wgLat - 35.0)
    radLat = wgLat / 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)
    latlng[0] = wgLat + dLat
    latlng[1] = wgLon + dLon
    return latlng


#地球坐标转换为百度坐标,即WGS84(国际通用)坐标系转为BD09坐标系适用于百度地图
def WGS84toBD09 (lat, lon):
    latlng = WGS84toGCJ02(lat, lon)
    x = latlng[1]
    y = latlng[0]

    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)
    latlng[0] = z * math.sin(theta) + 0.006     #0.006     #0.01205
    latlng[1] = z * math.cos(theta) + 0.0062    #0.0065    #0.00370
    return latlng

测试代码:

lat = 31.231905
lng = 121.469522
print("WGS84[%f,%f]" %(lng, lat))
latlng = WGS84toGCJ02(lat, lng)
print("GCJ02[%f,%f]" %(latlng[1], latlng[0]))
latlng = WGS84toBD09(lat, lng)
print("BD09 [%f,%f]" %(latlng[1], latlng[0]))

测试结果:

WGS84[121.469522,31.231905]
GCJ02[121.474059,31.229975]
BD09 [121.480298,31.235903]

实际位置:

WGS84坐标系121.469522,31.231905
GCJ02坐标系121.47405857079794,31.229974621114046
BD09坐标系121.48055338931275,31.235941661557085
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python中百度坐标系的坐标转换可以通过使用第三方库进行实现。一个常用的库是百度地图API的Python SDK,叫做BaiduMapAPI。 要使用BaiduMapAPI进行百度坐标系转换,首先需要安装该库。可以通过在命令行中运行以下指令来安装BaiduMapAPI: ``` pip install BaiduMapAPI ``` 安装完成后,在Python代码中引入BaiduMapAPI库,即可使用其中的转换功能。 下面是一个简单的示例代码,展示了如何使用BaiduMapAPI进行百度坐标系转换: ```python from BaiduMapAPI import convert_coords # 将GPS坐标系的经纬度转换为百度坐标系的经纬度 gps_coords = [(39.1234, 116.5678)] baidu_coords = convert_coords(gps_coords, from_coord='gps', to_coord='bd09') # 将百度坐标系的经纬度转换为火星坐标系的经纬度 baidu_coords = [(39.1234, 116.5678)] mars_coords = convert_coords(baidu_coords, from_coord='bd09', to_coord='gcj02') # 将百度坐标系的经纬度转换GPS坐标系的经纬度 baidu_coords = [(39.1234, 116.5678)] gps_coords = convert_coords(baidu_coords, from_coord='bd09', to_coord='gps') ``` 以上示例中,首先通过调用`convert_coords()`函数将GPS坐标系的经纬度转换为百度坐标系的经纬度,参数`from_coord`指定了转换前的坐标系,参数`to_coord`指定了转换后的坐标系。同样的方法可以用于其他坐标系转换。 需要注意的是,在使用BaiduMapAPI进行坐标系转换时,需要提前申请并配置好百度地图API的开发者密钥,以确保API请求正常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值