38 Interactive plotting with Bokeh in GemGIS
Contents
38 Interactive plotting with Bokeh in GemGIS#
The bokeh
package allows it to create interactive plots of the available geo data.
Set File Paths and download Tutorial Data#
If you downloaded the latest GemGIS
version from the Github repository, append the path so that the package can be imported successfully. Otherwise, it is recommended to install GemGIS
via pip install gemgis
and import GemGIS
using import gemgis as gg
. In addition, the file path to the folder where the data is being stored is set. The tutorial data is downloaded using Pooch (https://www.fatiando.org/pooch/latest/index.html) and stored in the specified folder. Use
pip install pooch
if Pooch is not installed on your system yet.
[1]:
import gemgis as gg
file_path ='../../../../gemgis_data/data/38_interactive_plotting_with_bokeh_in gemgis/'
WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`
C:\Users\ale93371\Anaconda3\envs\test_gempy\lib\site-packages\theano\configdefaults.py:560: UserWarning: DeprecationWarning: there is no c++ compiler.This is deprecated and with Theano 0.11 a c++ compiler will be mandatory
warnings.warn("DeprecationWarning: there is no c++ compiler."
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
[3]:
gg.download_gemgis_data.download_tutorial_data(filename="38_interactive_plotting_with_bokeh_in gemgis.zip", dirpath=file_path)
Downloading file '38_interactive_plotting_with_bokeh_in gemgis.zip' from 'https://rwth-aachen.sciebo.de/s/AfXRsZywYDbUF34/download?path=%2F38_interactive_plotting_with_bokeh_in gemgis.zip' to 'C:\Users\ale93371\Documents\gemgis_data\data\38_interactive_plotting_with_bokeh_in gemgis'.
Loading Libraries#
[2]:
import geopandas as gpd
import rasterio
WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`
C:\Users\ale93371\Anaconda3\envs\test_gempy\lib\site-packages\theano\configdefaults.py:560: UserWarning: DeprecationWarning: there is no c++ compiler.This is deprecated and with Theano 0.11 a c++ compiler will be mandatory
warnings.warn("DeprecationWarning: there is no c++ compiler."
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
Importing Bokeh#
Bokeh is being imported and the output of the plots is set to be in the notebooks.
[3]:
from bokeh.plotting import figure, show
from bokeh.io import output_notebook, show
output_notebook()
Plotting OSM Data#
The first data to be integrated is a Open Street Base Map. It can be downloaded by accessing the tile provider OSM
via get_provider(..)
.
NB: When using a tile provider, all other data has to be reprojected to ``’EPSG:3857’`` (https://wiki.openstreetmap.org/wiki/EPSG:3857)
[4]:
from bokeh.tile_providers import OSM, get_provider
tile_provider = get_provider(OSM)
tile_provider
[4]:
In order to display a plot, a figure needs to be created using figure(..)
and the provided arguments. The range of the map was limited to the rough extent of the Münsterland Basin in Northern Germany.
[5]:
p = figure(title="Test",
x_axis_label='X [m]',
y_axis_label='Y [m]',
match_aspect=True,
plot_width=800,
plot_height=600,
x_range=(7.5e5, 10.5e5),
y_range=(6.65e6, 6.90e6))
The OSM Map is now added to the figure using p.add_tile(..)
.
[6]:
p.add_tile(tile_provider)
[6]:
Showing the plot#
The plot is shown using show(p)
.
[7]:
show(p)
Plotting Raster Data#
Loading Data#
The data used for GemGIS is obtained from OpenDataNRW. It will be used under Datenlizenz Deutschland – Namensnennung – Version 2.0 (https://www.govdata.de/dl-de/by-2-0) with © Geowissenschaftliche Daten: Digitales Höhenmodell NRW 1m (2020).
[8]:
data = rasterio.open(file_path + 'DEM50_reproj.tif')
The CRS of the raster was reprojected using gg.raster.reproject_raster(..)
to match the CRS of the Open Street Map layer.
[9]:
data.crs
[9]:
CRS.from_epsg(3857)
A second figure was created.
[10]:
p = figure(title="Test",
x_axis_label='X [m]',
y_axis_label='Y [m]',
match_aspect=True,
plot_width=800,
plot_height=600,
x_range=(7.5e5, 10.5e5),
y_range=(6.65e6, 6.90e6))
The OSM Layer will be added as well.
[11]:
p.add_tile(tile_provider)
[11]:
Create GlypRenderer for Raster#
For a better representation of the raster, the nodata value of the raster will be replaced with the min value of the array/band.
[12]:
data.nodata
[12]:
9999.0
[13]:
data_cleaned = data.read(1)
data_cleaned[data_cleaned == data.nodata] = data.read(1).min()
An image will be added to the figure using the dimensions of the rasterio object.
[14]:
from bokeh.models.mappers import ContinuousColorMapper
import numpy as np
dem_renderer = p.image(image = [np.flipud(data_cleaned)],
x=data.bounds[0],
y=data.bounds[1],
dw=data.bounds[2]-data.bounds[0],
dh=data.bounds[3]-data.bounds[1],
legend_label="DEM",
palette = 'RdYlGn11'
)
Create Hover Tool for Bokeh Plot#
A HoverTool is created so that the height of the DEM will be returned at the respective cursor position when moving across the raster.
[15]:
from bokeh.models import HoverTool
dem_tool = HoverTool(tooltips=[('Height', '@image{0.00} m')],
renderers=[dem_renderer])
Showing the plot#
The plot can now be displayed. In addition, the HoverTool and and the option to hide the data is being added.
[16]:
p.add_tools(dem_tool)
p.legend.click_policy="hide"
show(p)