Projections#
import geoviews as gv
import geoviews.feature as gf
from geoviews import opts
from cartopy import crs
gv.extension('bokeh', 'matplotlib')
The GeoViews package provides a library of HoloViews Element types which make it very easy to plot data on various geographic projections and other utilities to plot in geographic coordinate systems.
Elements are very simple wrappers around the data along with some declaration about its dimensions. The only thing that distinguishes a GeoViews element from a HoloViews one is the addition of a crs
parameter. The crs
parameter defines a cartopy coordinate reference system declaring the coordinate system of the data. This allows GeoViews to automatically project the data to the displayed projection
. The crs
therefore serves a dual purpose:
The
Element.crs
defines which coordinate system is defined in.The plot
projection
defined as a plot option defines what coordinate system to display the data in.
By default all elements assume a PlateCarree
projection (also sometimes known as the equirectangular projection), and therefore expects longitudes and latitudes.
To start with let’s declare two Points
objects, one using the default PlateCarree
crs
, the other using the GOOGLE_MERCATOR
coordinate system. Just looking at the difference in the coordinates for NYC or Beijing and London we can immediately tell they are very different units but by declaring the crs
we can make it possible to automatically translate between them:
nyc, beijing = (-74.0, 40.7, 'NYC'), (116.4, 39.9, 'Beijing')
london = (14471.53, 6712008., 'London')
cities_lonlat = gv.Points([nyc, beijing], vdims='City')
cities_mercator = gv.Points([london], crs=crs.GOOGLE_MERCATOR, vdims='City')
(gv.tile_sources.OSM * cities_lonlat * cities_mercator).opts(
opts.Points(global_extent=True, width=500, height=475, size=12, color='black'))
As we can see we overlaid the above plot on a WMTS tile source, letting us interactively zoom in and out of the plot. This functionality is only available when using a Web Mercator projection
, which is what the bokeh backend uses by default. When using matplotlib on the other hand the plot will automatically use the crs
declared on the plotted element (in this case PlateCarree
):
features = gv.Overlay([gf.ocean, gf.land, gf.rivers, gf.lakes, gf.borders, gf.coastline])
gv.output(features, backend='matplotlib', fig='svg', size=300)
When using bokeh a custom plot projection
may also be used. The same behavior as matplotlib can be enabled with the infer_projection
parameter or an explicit projection can be set:
(features * cities_lonlat * cities_mercator).options(
opts.Points(projection=crs.Mollweide(), width=800, height=400, size=12, color='black'))