本文示例檔案已上傳至我的
Github
倉庫https://github.com/CNFeffery/DataScienceStudyNotes
1 簡介
國慶期間,抽空給大家分享在geopandas
中疊加各種線上瓦片底圖的方法,來製作出更多樣式的地圖作品。話不多說,我們直接進入正題。

2 在geopandas中疊加線上地圖
我們需要配合contextily
這個第三方庫來輔助geopandas
疊加線上地圖,在geopandas
已經被正確安裝的情況下,使用pip install contextily
或conda install contextily
安裝contextily
。
從下面這個小例子出發:
import geopandas as gpd
import contextily as ctx
import matplotlib.pyplot as plt
cq = gpd.read_file('重慶市.geojson').to_crs('EPSG:3857')
fig, ax = plt.subplots(figsize=(10, 10))
ax = cq.plot(ax=ax, alpha=0.1, edgecolor='k')
ax.axis('off')
ctx.add_basemap(ax,
source='https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png',
zoom=8)
fig.savefig('圖2 疊加線上地圖示例.png', pad_inches=0, bbox_inches='tight', dpi=300)

下面我們來劃重點,在圖2所示的例子中,我們前面正常讀入向量資料後一定要先變換投影為web墨卡託即EPSG:3857,接著正常繪圖,在最後一步時將ax
物件傳入ctx.add_basemap
中,並新增了引數source
代表對應線上瓦片地圖的url,引數zoom
來控制地圖縮放精度級別。
在稍事等待之後(如果沒有“特殊”的上網技巧,可能要多等一段時間),我們的底圖便自動獲取拼接完畢,之後直接匯出影像檔案即可,是不是非常的方便~
在掌握了geopandas
+contextily
相互配合疊加線上底圖之後,下面給大家推薦一些有意思的底圖url供大家日常選擇使用:
https://a.tile.thunderforest.com/mobile-atlas/{z}/{x}/{y}.png?apikey=41f4f936f1d148f69cbd100812875c88

http://{s}.tile.stamen.com/terrain/{z}/{x}/{y}.jpg

http://{s}.tiles.maps.sputnik.ru/{z}/{x}/{y}.png

https://c.tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=41f4f936f1d148f69cbd100812875c88

http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png

http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png

https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png

http://{s}.tile.stamen.com/watercolor/{z}/{x}/{y}.jpg

https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png

https://stamen-tiles-a.a.ssl.fastly.net/terrain-background/{z}/{x}/{y}.png
即stamen地形圖的無地名標註版本

https://d.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png
即carto淺色地圖的無地圖示註版本

以上就是本文的全部內容,歡迎在評論區與我進行討論~