我在散景中使用GeoJSONDataSource的更新功能有问题。 对于以下代码,我采用两个脚本 - 运行良好 - 并将它们添加到一个脚本中。
但是当我bokeh serve app
时,我收到以下错误消息,我不明白为什么:304 GET /app
这是我的代码:
from bokeh.io import curdoc
from bokeh.layouts import widgetbox, layout
from bokeh.models import HoverTool, GeoJSONDataSource, Div, LinearColorMapper, LogColorMapper
from bokeh.plotting import figure
from bokeh.models.widgets.inputs import DatePicker, MultiSelect, Select
import datetime as dt
import pickle
import os
import geopandas as gpd
lin_color_mapper = LinearColorMapper(palette=['#FFFFFF', '#FAFEFF', '#F6FCFF'])
log_color_mapper = LogColorMapper(palette=['#FFFFFF', '#FAFEFF', '#F6FCFF'])
这是散景的一个例子。
desc = Div(text=open(os.path.join(os.path.dirname(__file__), "templates/index.html")).read())
这是与DWH的连接
# get data from DWH
table = ...
下一步我得到准备好的多边形
# get pickle with geometry data
home_dir = os.path.dirname(os.path.realpath(__file__))
file_dir = os.path.join(home_dir, 'static', 'geometry.pickle')
with open(file_dir, 'rb') as file:
geometry = pickle.load(file)
将它们与我的数据合并
table = geometry.merge(table, how='inner', right_on='delivery_region', left_on='geometry')
只有一些Slider的东西(我为此尝试切了很多)
# Create Input Sliders
min_date = DatePicker(title='Start',
min_date=dt.date(2017, 1, 1),
max_date=dt.date.today(),
value=dt.date(2018, 1, 1))
max_date = DatePicker(title='End',
min_date=dt.date(2017, 1, 1),
max_date=dt.date.today(),
value=dt.date.today())
使用来自网络的geojson smaple添加GeoJSONDataSource
# adding the source with some sample date from the web (must be geojson)
geo_source = GeoJSONDataSource(geojson='{'
'"type": "Feature",'
'"geometry": {'
'"type": "Point",'
'"coordinates": [125.6, 10.1]'
'},'
'"properties": {'
'"name": "Dinagat Islands"'
'}'
'}')
创建悬停工具
hover = HoverTool(tooltips=[
("Region", "@region"),
("Value", "@value")])
建立情节
p = figure(plot_width=600, plot_height=800, x_axis_location=None,
y_axis_location=None, tools=[hover])
p.grid.grid_line_color = None
p.patches('xs', 'ys', fill_color={'field': 'value', 'transform': lin_color_mapper}, line_color='white', line_width=0.1, source=geo_source)
定义一个函数来为滑块选择正确的数据
def select_table():
selected = table[(table['date'] >= min_date.value) & (table['date'] <= max_date.value)]
return selected
定义我的更新函数以获取所需的数据。这里,GeoJSONDataSource已更新
def update():
df = select_table()
gdf = gpd.GeoDataFrame()
group = df.groupby('region')
value = group.agg({'date': 'count', 'geometry': 'first'}).reset_index(drop=False)
gdf['region'] = value['region']
gdf['value'] = value['date']
gdf['geometry'] = value['geometry']
gdf.crs = {'init': 'epsg:2263'}
gdf = gdf.to_crs({'init': 'epsg:2263'})
gjson = gdf.to_json()
geo_source.geojson = gjson
在这里,我为滑块创建了更新
controls = [min_date, max_date]
for control in controls:
control.on_change('value', lambda attr, old, new: update())
inputs = widgetbox(*controls)
仅从散景的滑块示例布局内容
l = layout([
[desc],
[inputs, p], ])
update()
curdoc().add_root(l)
curdoc().title = 'Choropleth Map Test'