天天看點

(資料科學學習手劄96)在geopandas中疊加線上地圖

本文示例檔案已上傳至我的

Github

倉庫https://github.com/CNFeffery/DataScienceStudyNotes

1 簡介

  國慶期間,抽空給大家分享在

geopandas

中疊加各種線上瓦片底圖的方法,來制作出更多樣式的地圖作品。話不多說,我們直接進入正題。

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖1

2 在geopandas中疊加線上地圖

  我們需要配合

contextily

這個第三方庫來輔助

geopandas

疊加線上地圖,在

geopandas

已經被正确安裝的情況下,使用

pip install contextily

conda install contextily

安裝

contextily

  從下面這個小例子出發(使用“特殊”上網技巧的朋友請注意,需要将

requests

庫降級到

2.24.0

才可以在使用“特殊”上網技巧的同時,正常使用疊加線上底圖的功能):

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)
           
(資料科學學習手劄96)在geopandas中疊加線上地圖

圖2 疊加線上地圖示例

  下面我們來劃重點,在圖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

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖3

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

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖4

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

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖5

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

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖6

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

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖7

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

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖8

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

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖9

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

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖10

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

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖11

  • https://stamen-tiles-a.a.ssl.fastly.net/terrain-background/{z}/{x}/{y}.png

  即stamen地形圖的無地名标注版本

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖12

  • https://d.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png

  即carto淺色地圖的無地圖示注版本

(資料科學學習手劄96)在geopandas中疊加線上地圖

圖13

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

繼續閱讀