如何用散景绘制Shapely多边形?

时间:2019-04-12 15:02:13

标签: bokeh geopandas shapely

我想绘制散景的多边形,该多边形存储在具有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。

谢谢!

1 个答案:

答案 0 :(得分: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)