日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python各類經緯度轉換的實例代碼

系統 1786 0

python各類經緯度轉換,具體代碼如下所示:

            
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服務解析地址獲取位置坐標
    :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):
  """
  火星坐標系(GCJ-02)轉百度坐標系(BD-09)
  谷歌、高德――>百度
  :param lng:火星坐標經度
  :param lat:火星坐標緯度
  :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):
  """
  百度坐標系(BD-09)轉火星坐標系(GCJ-02)
  百度――>谷歌、高德
  :param bd_lat:百度坐標緯度
  :param bd_lon:百度坐標經度
  :return:轉換后的坐標列表形式
  """
  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轉GCJ02(火星坐標系)
  :param lng:WGS84坐標系的經度
  :param lat:WGS84坐標系的緯度
  :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 [mglng, mglat]
def gcj02_to_wgs84(lng, lat):
  """
  GCJ02(火星坐標系)轉GPS84
  :param lng:火星坐標系的經度
  :param lat:火星坐標系緯度
  :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):
  """
  判斷是否在國內,不在國內不做偏移
  :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


          

知識點擴展:Python設置matplotlib.plot的坐標軸刻度間隔及刻度范圍

一、用默認設置繪制折線圖

            
import matplotlib.pyplot as plt
x_values=list(range(11))
#x軸的數字是0到10這11個整數
y_values=[x**2 for x in x_values]
#y軸的數字是x軸數字的平方
plt.plot(x_values,y_values,c='green')
#用plot函數繪制折線圖,線條顏色設置為綠色
plt.title('Squares',fontsize=24)
#設置圖表標題和標題字號
plt.tick_params(axis='both',which='major',labelsize=14)
#設置刻度的字號
plt.xlabel('Numbers',fontsize=14)
#設置x軸標簽及其字號
plt.ylabel('Squares',fontsize=14)
#設置y軸標簽及其字號
plt.show()
#顯示圖表
          

這樣制作出的圖表如下圖所示:

python各類經緯度轉換的實例代碼_第1張圖片

我們希望x軸的刻度是0,1,2,3,4……,y軸的刻度是0,10,20,30……,并且希望兩個坐標軸的范圍都能再大一點,所以我們需要手動設置。

二、手動設置坐標軸刻度間隔以及刻度范圍

            
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
#從pyplot導入MultipleLocator類,這個類用于設置刻度間隔
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軸的刻度間隔設置為1,并存在變量里
y_major_locator=MultipleLocator(10)
#把y軸的刻度間隔設置為10,并存在變量里
ax=plt.gca()
#ax為兩條坐標軸的實例
ax.xaxis.set_major_locator(x_major_locator)
#把x軸的主刻度設置為1的倍數
ax.yaxis.set_major_locator(y_major_locator)
#把y軸的主刻度設置為10的倍數
plt.xlim(-0.5,11)
#把x軸的刻度范圍設置為-0.5到11,因為0.5不滿一個刻度間隔,所以數字不會顯示出來,但是能看到一點空白
plt.ylim(-5,110)
#把y軸的刻度范圍設置為-5到110,同理,-5不會標出來,但是能看到一點空白
plt.show()
          

繪制的結果如圖所示:

python各類經緯度轉換的實例代碼_第2張圖片

總結

以上所述是小編給大家介紹的Python設置matplotlib.plot的坐標軸刻度間隔及刻度范圍,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 措勤县| 石门县| 西昌市| 同仁县| 曲阳县| 贵德县| 甘孜县| 茌平县| 松溪县| 泰顺县| 宁南县| 焦作市| 唐海县| 玉树县| 独山县| 始兴县| 射阳县| 兴业县| 永善县| 阜城县| 武山县| 大丰市| 缙云县| 聂荣县| 临颍县| 拉孜县| 乌海市| 扎囊县| 武夷山市| 沂水县| 从化市| 宁晋县| 扬中市| 东乡| 中牟县| 茶陵县| 陆良县| 成武县| 休宁县| 仙游县| 华阴市|