最初,我創建了一個英國郵政編碼區域的交互式地圖,其中個別區域是基於其值(例如該郵政編碼區域中的人口)表示的顏色,如下所示。散景 - 使用數據查詢器繪製shapefile地圖
from bokeh.plotting import figure
from bokeh.palettes import Viridis256 as palette
from bokeh.models import LinearColorMapper
from bokeh.models import ColumnDataSource
import geopandas as gpd
shp = 'file_path_to_the_downloaded_shapefile'
#read shape file into dataframe using geopandas
df = gpd.read_file(shp)
def expandMultiPolygons(row, geometry):
if row[geometry].type = 'MultiPolygon':
row[geometry] = [p for p in row[geometry]]
return row
#Some rows were in MultiPolygons instead of Polygons.
#Expand MultiPolygons to multi rows of Polygons
df = df.apply(expandMultiPolygons, geometry='geometry', axis=1)
df = df.set_index('Area')['geometry'].apply(pd.Series).stack().reset_index()
#Visualize the polygons. To visualize different colors for different post areas, I added another column called 'value' which has some random integer value.
p = figure()
color_mapper = LinearColorMapper(palette=palette)
source = ColumnDataSource(df)
p.patches('x', 'y', source=source,\
fill_color={'field': 'value', 'transform': color_mapper},\
fill_alpha=1.0, line_color="black", line_width=0.05)
其中df是四列的數據框:郵政編碼區域,x座標,y座標,值(即總體)。
上面的代碼在網頁瀏覽器上創建了一個很好的交互式地圖,但我注意到交互速度並不是很平滑。如果我放大或移動地圖,它會呈現緩慢。數據幀的大小隻有1106行,所以我很困惑它爲什麼這麼慢。
作爲可能的解決方案之一,我遇到了datashader(https://datashader.readthedocs.io/en/latest/),但我發現這個示例腳本非常複雜,其中大部分都是在Jupyter筆記本上使用holoview包,但我想使用散景創建儀表板。
有沒有人建議我將datashader合併到上面的散景腳本中?在數據採集器中是否需要使用不同的功能來創建形狀映射,而不是使用散景的補丁功能?
任何建議將不勝感激!
數據幀的大小似乎不成問題給我。你是如何創建你的補丁?你使用shapefile還是geojson?我認爲繪製非常複雜的多邊形可能會降低速度。你可以嘗試簡化你的多邊形。 – mc51
我從http://www.opendoorlogistics.com/downloads/以shapefile格式使用英國郵政編碼邊界多邊形。英國郵政編碼區域多邊形是否太複雜?我需要使用區域進行可視化目的,也許還有低分辨率英國郵政編碼形狀文件呢? – user4279562