如何用shapefile更新Bokeh上的选择

2024-09-29 18:42:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用bokeh选择特定区域中的点,并使用如下所述的函数selectBoundary。对建筑的选择,即select有效,而对城市的选择,即select1无效,我不知道为什么

import geopandas as gpd
from geopandas.tools import sjoin

s = schools.copy()  ## schools point (geopandas dataframe)
h = hf.copy()       ## hospitals points (geopandas dataframe)
dpt = cities.copy() ## cities (geopandas dataframe)


def selectBoundary(dptSelected):
    if dptSelected == 'City1':
        dptTmp = dpt[dpt.index==0]
        s = sjoin(schools, dpt)
        h = sjoin(hf, dpt)
    else:
        s = schools.copy()
        h = hf.copy()
    return s,h

def update_plot(attrname, old, new):
    s,h = selectBoundary(select1.value)
    if select.value == 'Schools':
        point0=dict(
            x=list(s['x'].values),
            y=list(s['y'].values),
        )
        newSource = point0  
    if select.value == 'Hospitals':
        point1=dict(
            x=list(h['x'].values),
            y=list(h['y'].values),
        )
        newSource = point1  
    source_point.data  =  newSource

做情节

point0=dict(
    x=list(s['x'].values),
    y=list(s['y'].values),
)

point1=dict(
    x=list(h['x'].values),
    y=list(h['y'].values),
)
source_point = ColumnDataSource(data = point0)

p = figure(plot_width=800, 
           tooltips=[("(Long, Lat)", "($x, $y)")])

p.hover.point_policy = "follow_mouse"

p.circle('x', 'y', size=1, 
         source = source_point, 
         color="black")

## Selection
select  =  Select(title="Buildings",  options=['Schools', 'Hospitals' ])
## Selection

select1 =  Select(title="Departments",  options = ['None', 'City1'])


## Controls
controls = widgetbox(select, select1)
select.on_change('value', update_plot)
select1.on_change('value', update_plot)
layout = column(row(controls), p)
curdoc().add_root(layout)

Tags: sourceplotvalueselectdictlistpointgeopandas

热门问题