使用Bokeh在地图图块的顶部添加Choropleth地图

时间:2018-06-21 22:07:13

标签: python bokeh

我为学区的学区提供了自定义shapefile,可以在地图图块的顶部绘制该文件,例如Cartodbpositron使用Folium。但是,我想添加小部件,以便在选择其他小部件选项时更新地图渲染。为此,我正在使用Bokeh。但是,在Bokeh上,地图绘制是在空的画布上完成的,而不是在地图图块上完成的,因此我无法在Bokeh中将shapefile边界覆盖在地图图块的顶部。

很抱歉,如果这个问题不是完整的代码示例,但是这个问题不一定是编程的问题,而是包功能之一。

谢谢。

1 个答案:

答案 0 :(得分:2)

问题是Bokeh地图图块需要Web墨卡托坐标。我的自定义shapefile具有经度/纬度坐标对,这一事实使它与Bokeh地图图块渲染不兼容。

我使用以下方法将多边形坐标从纬/经对转换为Web墨卡托坐标:

def latlontomercator_math(row):
    x_lon = row['x']
    y_lat = row['y']

    # longitudes are a one step transformation
    x_mer = list(map(lambda x: x * 20037508.34 / 180, x_lon))

    # latitudes are a two step transformation
    y_mer_aux = list(map(lambda y: math.log(math.tan((90 + y) * math.pi / 360))
                                    / (math.pi / 180), y_lat))
    y_mer = list(map(lambda y: y * 20037508.34 / 180, y_mer_aux))

    return(x_mer, y_mer)

data[['x_mer', 'y_mer']] = data.apply(latlontomercator_math, axis=1).apply(pd.Series)

该函数被编写为使用Pandas数据框逐行应用。

相关问题