python各類經(jīng)緯度轉(zhuǎn)換的實(shí)例代碼
python各類經(jīng)緯度轉(zhuǎn)換,具體代碼如下所示:
import math
import urllib
import json
x_pi = 3.14159265358979324 * 3000.0 / 180.0
pi = 3.1415926535897932384626 # π
a = 6378245.0 # 長半軸
ee = 0.00669342162296594323 # 扁率
class Geocoding:
def __init__(self, api_key):
self.api_key = api_key
def geocode(self, address):
"""
利用高德geocoding服務(wù)解析地址獲取位置坐標(biāo)
:param address:需要解析的地址
:return:
"""
geocoding = {'s': 'rsv3',
'key': self.api_key,
'city': '全國',
'address': address}
geocoding = urllib.urlencode(geocoding)
ret = urllib.urlopen("%s?%s" % ("http://restapi.amap.com/v3/geocode/geo", geocoding))
if ret.getcode() == 200:
res = ret.read()
json_obj = json.loads(res)
if json_obj['status'] == '1' and int(json_obj['count']) >= 1:
geocodes = json_obj['geocodes'][0]
lng = float(geocodes.get('location').split(',')[0])
lat = float(geocodes.get('location').split(',')[1])
return [lng, lat]
else:
return None
else:
return None
def gcj02_to_bd09(lng, lat):
"""
火星坐標(biāo)系(GCJ-02)轉(zhuǎn)百度坐標(biāo)系(BD-09)
谷歌、高德——>百度
:param lng:火星坐標(biāo)經(jīng)度
:param lat:火星坐標(biāo)緯度
:return:
"""
z = math.sqrt(lng * lng + lat * lat) + 0.00002 * math.sin(lat * x_pi)
theta = math.atan2(lat, lng) + 0.000003 * math.cos(lng * x_pi)
bd_lng = z * math.cos(theta) + 0.0065
bd_lat = z * math.sin(theta) + 0.006
return [bd_lng, bd_lat]
def bd09_to_gcj02(bd_lon, bd_lat):
"""
百度坐標(biāo)系(BD-09)轉(zhuǎn)火星坐標(biāo)系(GCJ-02)
百度——>谷歌、高德
:param bd_lat:百度坐標(biāo)緯度
:param bd_lon:百度坐標(biāo)經(jīng)度
:return:轉(zhuǎn)換后的坐標(biāo)列表形式
"""
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_lng = z * math.cos(theta)
gg_lat = z * math.sin(theta)
return [gg_lng, gg_lat]
def wgs84_to_gcj02(lng, lat):
"""
WGS84轉(zhuǎn)GCJ02(火星坐標(biāo)系)
:param lng:WGS84坐標(biāo)系的經(jīng)度
:param lat:WGS84坐標(biāo)系的緯度
:return:
"""
if out_of_china(lng, lat): # 判斷是否在國內(nèi)
return lng, lat
dlat = _transformlat(lng - 105.0, lat - 35.0)
dlng = _transformlng(lng - 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)
dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
mglat = lat + dlat
mglng = lng + dlng
return [mglng, mglat]
def gcj02_to_wgs84(lng, lat):
"""
GCJ02(火星坐標(biāo)系)轉(zhuǎn)GPS84
:param lng:火星坐標(biāo)系的經(jīng)度
:param lat:火星坐標(biāo)系緯度
:return:
"""
if out_of_china(lng, lat):
return lng, lat
dlat = _transformlat(lng - 105.0, lat - 35.0)
dlng = _transformlng(lng - 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)
dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
mglat = lat + dlat
mglng = lng + dlng
return [lng * 2 - mglng, 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(lng, lat):
ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + \
0.1 * lng * lat + 0.2 * math.sqrt(math.fabs(lng))
ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 *
math.sin(2.0 * lng * 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 _transformlng(lng, lat):
ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + \
0.1 * lng * lat + 0.1 * math.sqrt(math.fabs(lng))
ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 *
math.sin(2.0 * lng * pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lng * pi) + 40.0 *
math.sin(lng / 3.0 * pi)) * 2.0 / 3.0
ret += (150.0 * math.sin(lng / 12.0 * pi) + 300.0 *
math.sin(lng / 30.0 * pi)) * 2.0 / 3.0
return ret
def out_of_china(lng, lat):
"""
判斷是否在國內(nèi),不在國內(nèi)不做偏移
:param lng:
:param lat:
:return:
"""
return not (73.66 < lng < 135.05 and lat > 3.86 and lat < 53.55)
def baidu_to_google(lng, lat):
result5 = bd09_to_wgs84(float(lng), float(lat))
return result5
def google_to_baidu(lng, lat):
result5 = wgs84_to_bd09(float(lng), float(lat))
return result5
知識(shí)點(diǎn)擴(kuò)展:Python設(shè)置matplotlib.plot的坐標(biāo)軸刻度間隔及刻度范圍
一、用默認(rèn)設(shè)置繪制折線圖
import matplotlib.pyplot as plt
x_values=list(range(11))
#x軸的數(shù)字是0到10這11個(gè)整數(shù)
y_values=[x**2 for x in x_values]
#y軸的數(shù)字是x軸數(shù)字的平方
plt.plot(x_values,y_values,c='green')
#用plot函數(shù)繪制折線圖,線條顏色設(shè)置為綠色
plt.title('Squares',fontsize=24)
#設(shè)置圖表標(biāo)題和標(biāo)題字號(hào)
plt.tick_params(axis='both',which='major',labelsize=14)
#設(shè)置刻度的字號(hào)
plt.xlabel('Numbers',fontsize=14)
#設(shè)置x軸標(biāo)簽及其字號(hào)
plt.ylabel('Squares',fontsize=14)
#設(shè)置y軸標(biāo)簽及其字號(hào)
plt.show()
#顯示圖表
這樣制作出的圖表如下圖所示:

我們希望x軸的刻度是0,1,2,3,4……,y軸的刻度是0,10,20,30……,并且希望兩個(gè)坐標(biāo)軸的范圍都能再大一點(diǎn),所以我們需要手動(dòng)設(shè)置。
二、手動(dòng)設(shè)置坐標(biāo)軸刻度間隔以及刻度范圍
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
#從pyplot導(dǎo)入MultipleLocator類,這個(gè)類用于設(shè)置刻度間隔
x_values=list(range(11))
y_values=[x**2 for x in x_values]
plt.plot(x_values,y_values,c='green')
plt.title('Squares',fontsize=24)
plt.tick_params(axis='both',which='major',labelsize=14)
plt.xlabel('Numbers',fontsize=14)
plt.ylabel('Squares',fontsize=14)
x_major_locator=MultipleLocator(1)
#把x軸的刻度間隔設(shè)置為1,并存在變量里
y_major_locator=MultipleLocator(10)
#把y軸的刻度間隔設(shè)置為10,并存在變量里
ax=plt.gca()
#ax為兩條坐標(biāo)軸的實(shí)例
ax.xaxis.set_major_locator(x_major_locator)
#把x軸的主刻度設(shè)置為1的倍數(shù)
ax.yaxis.set_major_locator(y_major_locator)
#把y軸的主刻度設(shè)置為10的倍數(shù)
plt.xlim(-0.5,11)
#把x軸的刻度范圍設(shè)置為-0.5到11,因?yàn)?.5不滿一個(gè)刻度間隔,所以數(shù)字不會(huì)顯示出來,但是能看到一點(diǎn)空白
plt.ylim(-5,110)
#把y軸的刻度范圍設(shè)置為-5到110,同理,-5不會(huì)標(biāo)出來,但是能看到一點(diǎn)空白
plt.show()
繪制的結(jié)果如圖所示:

總結(jié)
以上所述是小編給大家介紹的Python設(shè)置matplotlib.plot的坐標(biāo)軸刻度間隔及刻度范圍,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
使用IPython下的Net-SNMP來管理類UNIX系統(tǒng)的教程
這篇文章主要介紹了使用IPython下的Net-SNMP來管理類UNIX系統(tǒng)的教程,本文來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04
linux環(huán)境下安裝pyramid和新建項(xiàng)目的步驟
這篇文章簡單介紹了linux環(huán)境下安裝pyramid和新建項(xiàng)目的步驟,大家參考使用2013-11-11
Python數(shù)據(jù)分析pandas之布爾索引使用詳解
這篇文章主要為大家介紹了Python數(shù)據(jù)分析pandas之布爾索引使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Pyqt5 關(guān)于流式布局和滾動(dòng)條的綜合使用示例代碼
這篇文章主要介紹了Pyqt5 關(guān)于流式布局和滾動(dòng)條的綜合使用示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Pytorch中的圖像增廣transforms類和預(yù)處理方法
這篇文章主要介紹了Pytorch中的圖像增廣和預(yù)處理方法(transforms類),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
OpenCV模板匹配matchTemplate的實(shí)現(xiàn)
這篇文章主要介紹了OpenCV模板匹配matchTemplate的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

