如何用Bokeh绘制Shapely多边形?

问题描述 投票:0回答:1

我想绘制形状多边形,用Bokeh存储在地图上的GeoDataFrame中。 1.选择什么类型的雕文来绘制多边形? 2.如何将数据传递给字形?

我试图通过以下方式进行:

from bokeh.models import GMapOptions, PanTool, WheelZoomTool, 
    WheelPanTool, BoxSelectTool, BoxEditTool
from bokeh.plotting import gmap

p = gmap(api_key, map_options, title= f'offer {str(sales_id)} ')

map_options = GMapOptions(lat = lats_s, lng = lons_s, 
                          map_type="roadmap", zoom=12)


api_key = 'my_api_key'

x, y = some_shapely_polygon.exterior.coords.xy

x = x.tolist()
y = y.tolist()

source = ColumnDataSource(data=dict(
x=x, y=y,))

p.patches('x', 'y', source=source, 
          fill_alpha=0.8, line_color="black", line_width=0.3)
show(p)

我收到一个错误:“Javascript错误:数组长度无效”

当我通过Circles传递儿子其他数据时,一切运作良好,我无法绘制Ploygons。

谢谢!

bokeh geopandas shapely
1个回答
0
投票

这些例子对我有用。将map.shp替换为您的文件名。适用于Bokeh v1.0.4。运行:python map.py

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
import geopandas as gp
import shapely

sf = gp.read_file('map.shp')    
x, y = [], []
[(x.append(list(polygon.exterior.coords.xy[0])), y.append(list(polygon.exterior.coords.xy[1]))) for polygon in sf['geometry'] if type(polygon.boundary) == shapely.geometry.linestring.LineString ]
p = figure(title = "A map from Shapefile", plot_width = 800)
p.patches('x', 'y', source = ColumnDataSource(dict(x = x, y = y)), line_color = "white", line_width = 0.5)
show(p)

要么

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
import geopandas as gp

def getPolyCoords(row, geom, coord_type):
    if coord_type == 'x':
        return list(row[geom].exterior.coords.xy[0])
    elif coord_type == 'y':
        return list(row[geom].exterior.coords.xy[1])

gdf = gp.GeoDataFrame.from_file('map.shp')
gdf['x'] = gdf.apply(getPolyCoords, geom = 'geometry', coord_type = 'x', axis = 1)
gdf['y'] = gdf.apply(getPolyCoords, geom = 'geometry', coord_type = 'y', axis = 1)
p_df = gdf.drop('geometry', axis = 1).copy()
p = figure(title = "A map from Shapefile", plot_width = 800)
p.patches('x', 'y', source = ColumnDataSource(p_df), line_color = "white", line_width = 0.5)
show(p)
© www.soinside.com 2019 - 2024. All rights reserved.