Python:更新bokeh中的shapefile

2024-10-06 11:25:35 发布

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

我正在尝试使用bokeh更新shapefile的可视化。我有两种形状:点和面片。现在更新只适用于点,我不明白为什么。你知道吗

def getPointCoords(row, geom, coord_type):
    """Calculates coordinates ('x' or 'y') of a Point geometry"""
    if coord_type == 'x':
        return row[geom].x
    elif coord_type == 'y':
        return row[geom].y

我有两种形状文件:点和面片。你知道吗

schools = gpd.read_file('point1.shp'})
schools['x'] = schools.apply(getPointCoords, geom='geometry', coord_type='x', axis=1)
schools['y'] = schools.apply(getPointCoords, geom='geometry', coord_type='y', axis=1)
data1=dict(
    x=list(schools['x'].values),
    y=list(schools['y'].values),
)

hf = gpd.read_file('point2.shp')
hf = hf.to_crs({'init': 'epsg:4326'})
hf['x'] = hf.apply(getPointCoords, geom='geometry', coord_type='x', axis=1)
hf['y'] = hf.apply(getPointCoords, geom='geometry', coord_type='y', axis=1)
data2=dict(
    x=list(hf['x'].values),
    y=list(hf['y'].values),
)

df1 = pd.DataFrame(data=data1, )
df2 = pd.DataFrame(data=data2, )
source = ColumnDataSource(df1 )

patch1 = gpd.read_file('patch1.shp')
patch1 = patch1.to_crs({'init': 'epsg:4326'})
geo_patch1 = GeoJSONDataSource(geojson=patch1.to_json())


patch2 = gpd.read_file('patch2.shp')
patch2 = patch2.to_crs({'init': 'epsg:4326'})
geo_patch2 = GeoJSONDataSource(geojson=patch2.to_json())

然后我创造了这个图形

source1 = geo_patch1
p = figure(plot_width=800, tooltips=[("(Long, Lat)", "($x, $y)")])
p.hover.point_policy = "follow_mouse"
p.patches('xs', 'ys', source = source1, fill_color='white', fill_alpha=0.5, 
          line_color="black", line_width=1)
p.circle('x', 'y', size=1, source = source, color="black")
### selection for points
select  =  Select(title="Buildings",  options=['Schools', 'Hospitals' ])

## Selection for patches
dptList = ['None']
dptList.extend(list(patch1['Name']))
select1 =  Select(title="Departments",  options = dptList)

## update
def update_plot(attrname, old, new):
    if select.value == 'Schools':
        newSource = data1  # changed this to the dict
    if select.value == 'Hospitals':
        newSource = data2  # changed this to the dict
    if select1.value != 'None':
        newSource_geo = geo_patch1
    source.data  =  newSource
    source1 =  newSource_geo

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

Tags: tosourcetypeselectlistgeogeometrygeom