Python: обновить шейп-файл в боке - PullRequest
0 голосов
/ 20 мая 2019

Я пытаюсь обновить визуализацию шейп-файлов, используя bokeh.У меня есть два вида фигур: точки и пятна.Теперь обновление работает только для точек, и я не понимаю, почему.

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)
...