Python如何使用bokeh包和geojson數(shù)據(jù)繪制地圖
最近要繪制倫敦區(qū)地圖,查閱了很多資料后最終選擇使用bokeh包以及倫敦區(qū)的geojson數(shù)據(jù)繪制。
bokeh是基于python的繪圖工具,可以繪制各種類型的圖表,支持geojson數(shù)據(jù)的讀取及繪制地圖。
安裝bokeh
$ pip install bokeh
軟件版本
python-3.7.7bokeh-2.0.0
數(shù)據(jù)來(lái)源
倫敦地圖數(shù)據(jù)來(lái)源于Highmaps地圖數(shù)據(jù)集。下載的是英國(guó)的地圖數(shù)據(jù)united-kindom.geo.json。需要對(duì)得到的數(shù)據(jù)進(jìn)行預(yù)處理才能得到只含倫敦地區(qū)的數(shù)據(jù)。這需要對(duì)geojson數(shù)據(jù)的格式有一定的了解。在對(duì)數(shù)據(jù)進(jìn)行處理之前,先看如何繪制英國(guó)地圖。
繪制英國(guó)地圖
from bokeh.plotting import curdoc, figure
from bokeh.models import GeoJSONDataSource
# 讀入英國(guó)地圖數(shù)據(jù)并傳給GeoJSONDataSource
with open("united-kindom.geo.json", encoding="utf8") as f:
geo_source = GeoJSONDataSource(geojson=f.read())
# 設(shè)置一張畫布
p = figure(width=500, height=500)
# 使用patches函數(shù)以及geo_source繪制地圖
p.patches(xs='xs', ys='ys', source=geo_source)
curdoc().add_root(p)
上述代碼可以繪制出英國(guó)地圖。將上述代碼保存為test.py,在終端運(yùn)行
$ bokeh serve --show test.py
這會(huì)自動(dòng)打開(kāi)瀏覽器,并顯示英國(guó)地圖。
運(yùn)行結(jié)果如圖:

獲取倫敦地區(qū)數(shù)據(jù)
獲取倫敦地區(qū)數(shù)據(jù)可以手動(dòng)從united-kingdom.geo.json文件中篩選出倫敦的數(shù)據(jù),也可以先用python先把數(shù)據(jù)過(guò)濾一遍,然后將數(shù)據(jù)傳給bokeh。這需要對(duì)geojson文件格式有一定的了解,在此不詳細(xì)介紹。
from bokeh.plotting import curdoc, figure
from bokeh.models import GeoJSONDataSource
import json
# 用json庫(kù)讀取數(shù)據(jù)
with open("united-kindom.geo.json", encoding="utf8") as f:
data = json.loads(f.read())
# 判斷是不是倫敦地區(qū)數(shù)據(jù)
def isInLondon(district):
if 'type' in district['properties'] and 'london borough' in district['properties']['type'].lower():
return True
if 'type-en' in district['properties'] and 'london borough' in district['properties']['type'].lower():
return True
if 'woe-name' in district['properties'] and 'city of london' in district['properties']['woe-name'].lower():
return True
return False
# 過(guò)濾數(shù)據(jù)
data['features'] = list(filter(isInLondon, data['features']))
#
geo_source = GeoJSONDataSource(geojson=json.dumps(data))
p = figure(width=500, height=500)
p.patches(xs='xs', ys='ys', source=geo_source)
curdoc().add_root(p)
運(yùn)行結(jié)果如圖:

美化
上面的倫敦地圖只是一個(gè)大概的輪廓,下面對(duì)地圖添加一系列功能。
添加各區(qū)輪廓線
p.patches(xs='xs', ys='ys', fill_alpha=0.7, # 畫輪廓線
line_color='white', # 線的顏色
line_width=0.5, # 線的寬度
source=geo_source)
現(xiàn)在地圖區(qū)域輪廓很清晰。
添加顏色
# 為每一個(gè)地區(qū)增加一個(gè)color屬性
for i in range(len(data['features'])):
data['features'][i]['properties']['color'] = ['blue', 'red', 'yellow', 'orange', 'gray', 'purple'][i % 6]
p.patches(xs='xs', ys='ys', fill_alpha=0.7,
line_color='white',
line_width=0.5,
color="color", # 增加顏色屬性,這里的"color"對(duì)應(yīng)每個(gè)地區(qū)的color屬性
source=geo_source)
現(xiàn)在地圖五顏六色。
增加圖注
import random
# 隨機(jī)產(chǎn)生數(shù)據(jù)用于展示
for i in range(len(data['features'])):
data['features'][i]['properties']['number'] = random.randint(0, 20_000)
p = figure(width=500, height=500,
tooltips="@name, number: @number" # 使用tooltips生成圖注,@+屬性名稱,這里的name是數(shù)據(jù)中原本有的,number是新近添加的。
)
現(xiàn)在鼠標(biāo)放到區(qū)域上時(shí),會(huì)顯示"區(qū)域名, number: 數(shù)字"。
去掉坐標(biāo)軸與背景線
p.axis.axis_label = None
p.axis.visible = False
p.grid.grid_line_color = None
最終代碼
from bokeh.plotting import curdoc, figure
from bokeh.models import GeoJSONDataSource
import json
import random
with open("united-kindom.geo.json", encoding="utf8") as f:
data = json.loads(f.read())
def isInLondon(district):
if 'type' in district['properties'] and 'london borough' in district['properties']['type'].lower():
return True
if 'type-en' in district['properties'] and 'london borough' in district['properties']['type'].lower():
return True
if 'woe-name' in district['properties'] and 'city of london' in district['properties']['woe-name'].lower():
return True
return False
data['features'] = list(filter(isInLondon, data['features']))
for i in range(len(data['features'])):
data['features'][i]['properties']['color'] = ['blue', 'red', 'yellow', 'orange', 'gray', 'purple'][i % 6]
data['features'][i]['properties']['number'] = random.randint(0, 20_000)
geo_source = GeoJSONDataSource(geojson=json.dumps(data))
p = figure(width=500, height=500,
tooltips="@name, number: @number")
p.patches(xs='xs', ys='ys', fill_alpha=0.7,
line_color='white',
line_width=0.5,
color="color",
source=geo_source)
p.axis.axis_label = None
p.axis.visible = False
p.grid.grid_line_color = None
curdoc().add_root(p)
倫敦地圖完成了

總結(jié)
最開(kāi)始想用pyecharts做的,但是pyecharts并沒(méi)有倫敦的地圖。折騰半天,最后只好自己找geojson數(shù)據(jù)來(lái)畫地圖。
找到了很多關(guān)于地圖的數(shù)據(jù)和工具,比如上文中提到的highmap數(shù)據(jù)集,以及DataV.altas,這個(gè)工具可以可視化地提取中國(guó)區(qū)域的地圖數(shù)據(jù),但感覺(jué)比起自己找數(shù)據(jù),畫中國(guó)地圖還是pyecharts來(lái)得實(shí)在。
數(shù)據(jù)最重要。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python開(kāi)發(fā)中module模塊用法實(shí)例分析
這篇文章主要介紹了python開(kāi)發(fā)中module模塊用法,以實(shí)例形式較為詳細(xì)的分析了Python中模塊的功能、定義及相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Python中DataFrame與內(nèi)置數(shù)據(jù)結(jié)構(gòu)相互轉(zhuǎn)換的實(shí)現(xiàn)
pandas?支持我們從?Excel、CSV、數(shù)據(jù)庫(kù)等不同數(shù)據(jù)源當(dāng)中讀取數(shù)據(jù),來(lái)構(gòu)建?DataFrame。但有時(shí)數(shù)據(jù)并不來(lái)自這些外部數(shù)據(jù)源,這就涉及到了?DataFrame?和?Python?內(nèi)置數(shù)據(jù)結(jié)構(gòu)之間的相互轉(zhuǎn)換,本文就來(lái)和大家詳細(xì)聊聊2023-02-02
Python中conda虛擬環(huán)境創(chuàng)建及使用小結(jié)
本文主要介紹了Python中conda虛擬環(huán)境創(chuàng)建及使用小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
Django+python服務(wù)器部署與環(huán)境部署教程詳解
這篇文章主要介紹了Django+python服務(wù)器部署與環(huán)境部署教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Python 使用PIL numpy 實(shí)現(xiàn)拼接圖片的示例
今天小編就為大家分享一篇Python 使用PIL numpy 實(shí)現(xiàn)拼接圖片的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Python標(biāo)準(zhǔn)庫(kù)之循環(huán)器(itertools)介紹
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)之循環(huán)器(itertools)介紹,本文講解了無(wú)窮循環(huán)器、函數(shù)式工具、組合工具、groupby()、其它工具等內(nèi)容,需要的朋友可以參考下2014-11-11

