Python-Cartopy调用自带线上地图作为底图
生活随笔
收集整理的這篇文章主要介紹了
Python-Cartopy调用自带线上地图作为底图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Cartopy自帶的底圖是有Google、MapQuest、Stamen、Mapbox、Quadtree等多家圖層服務,提供影像、矢量和地形圖,可作為繪圖時的底圖。可以在img_tiles.py文件中查看具體的實現形式。如:
GoogleWTS:
class GoogleWTS(metaclass=ABCMeta):"""Implement web tile retrieval using the Google WTS coordinate system.A "tile" in this class refers to the coordinates (x, y, z).The tiles can be saved to a cache directory using the cache parameter, sothey are downloaded only once. If it is set to True, the default pathstored in the cartopy.config dictionary is used. If it is set to a custompath, this path is used instead of the default one. If it is set to False(the default behavior), the tiles are downloaded each time."""MapQuestOSM:
class MapQuestOSM(GoogleWTS):# https://developer.mapquest.com/web/products/open/map for terms of use# https://devblog.mapquest.com/2016/06/15/# modernization-of-mapquest-results-in-changes-to-open-tile-access/# this now requires a sign up to a plandef _image_url(self, tile):x, y, z = tileurl = f'https://otile1.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg'mqdevurl = ('https://devblog.mapquest.com/2016/06/15/''modernization-of-mapquest-results-in-changes''-to-open-tile-access/')warnings.warn(f'{url} will require a log in and and will likely'f' fail. see {mqdevurl} for more details.')return urlStamen:
class Stamen(GoogleWTS):"""Retrieves tiles from maps.stamen.com. Styles include``terrain-background``, ``terrain``, ``toner`` and ``watercolor``.For a full reference on the styles available please seehttp://maps.stamen.com. Of particular note are the sub-stylesthat are made available (e.g. ``terrain`` and ``terrain-background``).To determine the name of the particular [sub-]style you want,follow the link on http://maps.stamen.com to your desired style andobserve the style name in the URL. Your style name will be in theform of: ``http://maps.stamen.com/{STYLE_NAME}/#9/37/-122``.Except otherwise noted, the Stamen map tile sets are copyright StamenDesign, under a Creative Commons Attribution (CC BY 3.0) license.Please see the attribution notice at http://maps.stamen.com on how toattribute this imagery.等等,也可以通過修改img_tiles.py文件自定義需調用的線上底圖。此處我們看一下怎么調用Cartopy自帶的圖層,代碼如下:
#調用線上地圖作為底圖 import numpy as np import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.io.img_tiles as cimgt from cartopy.mpl.ticker import LongitudeFormatter,LatitudeFormatterfig = plt.figure(figsize=[7,8]) ax = fig.add_subplot(111,projection=ccrs.PlateCarree()) ### 調用cartopy自帶地圖# backmap=cimgt.OSM() # backmap=cimgt.Stamen() # backmap=cimgt.Stamen('terrain-background') backmap=cimgt.QuadtreeTiles() ax.add_image(backmap, 6) #數字為縮放比例,數值越大,地圖越精細###設置底圖范圍 extent =[118, 124, 26, 32] ax.set_extent(extent,crs=ccrs.PlateCarree()) ###設置經緯網格線 gl = ax.gridlines(crs=ccrs.PlateCarree(),draw_labels=False,linewidth=0.5, color='k', alpha=0.3, linestyle='--') ###設置坐標間隔 ax.set_xticks(np.arange(extent[0], extent[1]+2, 2),crs=ccrs.PlateCarree()) ax.set_yticks(np.arange(extent[2], extent[3]+2, 2),crs=ccrs.PlateCarree()) ax.xaxis.set_major_formatter(LongitudeFormatter()) ax.yaxis.set_major_formatter(LatitudeFormatter()) ax.tick_params(axis='both', labelsize=12, direction='out')#設置坐標刻度字體大小和方向# plt.title('OSM', fontsize=15) # plt.title('Stamen', fontsize=15) # plt.title('StamenTerrain', fontsize=15) plt.title('QuadtreeTiles', fontsize=15)# plt.savefig('./OSM.png') # plt.savefig('./Stamen.png') # plt.savefig('./StamenTerrain.png') plt.savefig('./QuadtreeTiles.png')輸出為:
?大家可以挨個試試。Mapbox等有些圖層另需要API賬號,此處沒有給出,等我需要用了,弄懂了再分享哇!
總結
以上是生活随笔為你收集整理的Python-Cartopy调用自带线上地图作为底图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python-turtle绘画旅程第二站
- 下一篇: python算法教程百度云_如何用免费G