bokeh-使用datashader绘制shapefile地图

use*_*562 1 python-2.7 bokeh datashader

最初,我创建了英国邮政编码区域的交互式地图,其中单个区域根据其值(例如该邮政编码区域中的人口)用颜色表示,如下所示。

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)
Run Code Online (Sandbox Code Playgroud)

其中df是四列的数据框:邮政编码区域,x坐标,y坐标,值(即总体)。

上面的代码在网络浏览器上创建了一个交互式地图,这很棒,但是我注意到交互性在速度上不是很平稳。如果放大或移动地图,它会缓慢渲染。数据帧的大小只有1106行,所以我很困惑为什么它这么慢。

作为一种可能的解决方案,我遇到了datashader(https://datashader.readthedocs.io/en/latest/),但是我发现示例脚本相当复杂,并且大多数脚本都在Jupyter笔记本上带有holoview包,但我想要使用bokeh创建仪表板。

有人建议我将数据着色器合并到上述bokeh脚本中吗?我是否需要在datashader中使用其他函数来创建形状图,而不是使用bokeh的patch函数?

任何建议将不胜感激!!!

Jam*_*nar 6

没有数据文件,我无法直接回答您的问题,但是可以提供一些观察结果:

  1. Datashader不太可能为此目的提供价值,因为Datashader当前不支持渲染多边形。根据经验,Datashader旨在汇总您的数据,如果已经汇总了数据,则通常不会提供帮助。在这里,您的数据是按邮政编码进行聚合的,而datashader无法处理该邮政编码,但是如果您拥有每人的原始数据,将很高兴提供它。
  2. 如果您希望直接使用Bokeh而不是通过更高级别的HoloViews / GeoViews界面来工作,我建议您遵循马特·洛克林(Matt Rocklin)的有关加速大熊猫的工作;为了您的目的,他的方法应该非常快。
  3. 总而言之,无论您是否要创建仪表板,HoloViewsGeoViews应该是与Bokeh一起使用的便捷方法。例如,2017 JupyterCon教程显示了如何使用两个库制作简单的Bokeh仪表板。它不涵盖形状文件,但其他GeoViews示例中涵盖了这些文件。