测绘地理数据

Bokeh已经开始增加对地理数据的支持。已经有很多强大的功能可用,但是我们还有更多的功能需要添加。请告诉我们您的用例 Discourse 或在 GitHub 这样我们就可以继续扩展这些功能来满足您的需求。

平铺提供程序映射

Bokeh绘图还可以使用使用Web Mercator投影的XYZ平铺服务。模块 bokeh.tile_providers 包含多个具有适当属性的预配置图块源,可以使用 add_tile() 方法。

from bokeh.plotting import figure, output_file, show
from bokeh.tile_providers import CARTODBPOSITRON, get_provider

output_file("tile.html")

tile_provider = get_provider(CARTODBPOSITRON)

# range bounds supplied in web mercator coordinates
p = figure(x_range=(-2000000, 6000000), y_range=(-1000000, 7000000),
           x_axis_type="mercator", y_axis_type="mercator")
p.add_tile(tile_provider)

show(p)

也请注意 x_axis_type="mercator"y_axis_type="mercator"figure 生成带有纬度和经度标签的轴,而不是原始的Web墨卡托坐标。

谷歌地图

Bokeh还可以使用 gmap() 功能。你必须通过 Google API Key 为了使它工作,以及任何 GMapOptions 配置Google地图参考底图。googleapi密钥将存储在Bokeh文档JSON中。

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, GMapOptions
from bokeh.plotting import gmap

output_file("gmap.html")

map_options = GMapOptions(lat=30.2861, lng=-97.7394, map_type="roadmap", zoom=11)

# For GMaps to function, Google requires you obtain and enable an API key:
#
#     https://developers.google.com/maps/documentation/javascript/get-api-key
#
# Replace the value below with your personal API key:
p = gmap("GOOGLE_API_KEY", map_options, title="Austin")

source = ColumnDataSource(
    data=dict(lat=[ 30.29,  30.20,  30.29],
              lon=[-97.70, -97.74, -97.78])
)

p.circle(x="lon", y="lat", size=15, fill_color="blue", fill_alpha=0.8, source=source)

show(p)

注解

谷歌对使用谷歌地图API有自己的服务条款,任何在谷歌地图上使用Bokeh都必须在谷歌的服务条款内

还请注意,googlemaps始终对纵横比进行显式控制,这对 GMapPlot

  • 只有 Range1d 支持范围。尝试使用其他范围类型将导致错误。

  • 用法 BoxZoomTool 与不兼容 GMapPlot . 添加 BoxZoomTool 不会有任何效果。

GeoJSON数据

GeoJSON 是一个流行的用JSON表示地理特征的开放标准。它将点、线和多边形(在Bokeh中称为面片)描述为特征的集合。每个功能也可以有一组属性。

博克的 GeoJSONDataSource 几乎可以无缝地代替Bokeh的 ColumnDataSource . 例如:

import json

from bokeh.io import output_file, show
from bokeh.models import GeoJSONDataSource
from bokeh.plotting import figure
from bokeh.sampledata.sample_geojson import geojson

output_file("geojson.html")

data = json.loads(geojson)
for i in range(len(data['features'])):
    data['features'][i]['properties']['Color'] = ['blue', 'red'][i%2]

geo_source = GeoJSONDataSource(geojson=json.dumps(data))

TOOLTIPS = [
    ('Organisation', '@OrganisationName')
]

p = figure(background_fill_color="lightgrey", tooltips=TOOLTIPS)
p.circle(x='x', y='y', size=15, color='Color', alpha=0.7, source=geo_source)

show(p)

警告

在幕后,Bokeh将GeoJSON坐标转换为名为 xyxsys (取决于要素是点、线、多线、多边形还是多多边形)。 转换GeoJSON时,名称冲突的属性将被重写,应该避免 .