8 Geography

Datashader is a general-purpose tool for rasterizing (and re-rasterizing) data of many different types. To make it easier to apply this general functionality to the particular domain of geoscience, Datashader provides a few geospatial-specific utilities as well:

This notebook explains each of these topics in turn. See also GeoViews, which is designed to work with Datashader to provide a large range of additional geospatial functionality.

Project points

You can use GeoViews or the underlying pyproj/proj.4 libraries to perform arbitrary projections to and from a large number of different coordinate reference systems. However, for the common case of wanting to view data with latitude and longitude coordinates on top of a Web Mercator tile source such as Google Maps or OpenStreetMap, Datashader provides a simple self-contained utility lnglat_to_meters(longitude, latitude) to project your data once, before visualization. For instance, if you have a dataframe with some latitude and longitude points stretching from San Diego, California to Bangor, Maine:

In [1]:
import numpy as np, pandas as pd
from datashader.utils import lnglat_to_meters

San_Diego = 32.715, -117.1625
Bangor = 44.8, -68.8
n = 20

df = pd.DataFrame(dict(longitude = np.linspace(San_Diego[1], Bangor[1], n),
                       latitude  = np.linspace(San_Diego[0], Bangor[0], n)))

Then you can create new columns (or overwrite old ones) with the projected points in meters from the origin (Web Mercator coordinates):

In [2]:
df.loc[:, 'x'], df.loc[:, 'y'] = lnglat_to_meters(df.longitude,df.latitude)
df.tail()
Out[2]:
longitude latitude x y
15 -78.981579 42.255789 -8.792189e+06 5.199373e+06
16 -76.436184 42.891842 -8.508837e+06 5.295524e+06
17 -73.890789 43.527895 -8.225485e+06 5.392671e+06
18 -71.345395 44.163947 -7.942133e+06 5.490849e+06
19 -68.800000 44.800000 -7.658781e+06 5.590090e+06

The new x and y coordinates aren't very useful for humans to read, but they can now be overlaid directly onto web map sources, which are labeled with latitude and longitude appropriately by Bokeh but are actually in Web Mercator coordinates internally:

In [3]:
import holoviews as hv, geoviews.tile_sources as gts
from holoviews.operation.datashader import datashade, spread
hv.extension('bokeh')

gts.EsriImagery() * spread(datashade(hv.Points(df, ['x', 'y']), cmap="white"), px=3)