Mapping Geo Data¶
Bokeh has started adding support for working with Geographical data. There are a number of powerful features already available, but we still have more to add. Please tell us your use cases through the Discourse or on github so that we can continue to build out these features to meet your needs.
Tile Provider Maps¶
Bokeh plots can also consume XYZ tile services which use the Web Mercator projection.
The module bokeh.tile_providers contains several pre-configured tile sources with
appropriate attribution which can be added to a plot using the
from bokeh.plotting import figure, show, output_file from bokeh.tile_providers import get_provider, Vendors output_file("tile.html") tile_provider = get_provider(Vendors.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)
Notice also that passing
figure generate axes with latitude and longitute labels, instead of raw Web
Bokeh can also plot glyphs over a Google Map using the
function. You must pass this function Google API Key in order for it to work, as
well as any
GMapOptions to configure the Google Map
Google has its own terms of service for using Google Maps API and any use of Bokeh with Google Maps must be within Google’s Terms of Service
Also note that Google Maps exert explicit control over aspect ratios at all
times, which imposes some limitations on
Range1dranges are supported. Attempting to use other range types will result in an error.
BoxZoomToolis incompatible with
GMapPlot. Adding a
BoxZoomToolwill have no effect.
GeoJSON is a popular open standard for representing geographical features with JSON. It describes points, lines and polygons (called Patches in Bokeh) as a collection of features. Each feature can also have a set of properties.
GeoJSONDataSource can be used almost seamlessly in place of Bokeh’s
ColumnDataSource. For example:
from bokeh.io import output_file, show from bokeh.models import GeoJSONDataSource from bokeh.plotting import figure from bokeh.sampledata.sample_geojson import geojson import json 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)
It is important to note that behind the scenes, Bokeh converts the GeoJSON coordinates into columns called x and y or xs and ys) (depending on whether the features are Points, Lines, MultiLines, Polygons or MultiPolygons). Properties with clashing names will be overridden when the GeoJSON is converted, and should be avoided.