Turning ripples into waves

Exploring what's new in Cartopy

I was exploring what's new in the latest release of cartopy, one of the new features I found out is that cartopy can plot from both Web Map Service (WMS) and Web Map Tile Service (WMTS).

This post is just to show a few examples. As usual let's start with a function to create a cartopy map.

In [3]:
import as ccrs
import matplotlib.pyplot as plt
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

def make_map(projection=ccrs.PlateCarree(), extent=[-42, 0, -32, 0.5]):
    subplot_kw = dict(projection=projection)
    fig, ax = plt.subplots(subplot_kw=subplot_kw)
    gl = ax.gridlines(draw_labels=True)
    gl.xlabels_top = gl.ylabels_right = False
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER
    return fig, ax

Both WMS and WMTS features depend on OWSlib. We need to pass an WebMapService object to cartopy ax.add_wms.

In [4]:
from owslib.wms import WebMapService
url = ""
wms = WebMapService(url)

fig, ax = make_map(projection=ccrs.PlateCarree())
ax.add_wms(wms, layer)
_ = ax.set_title(layer)

Same thing for WMTS, but here we have options to pass the WebMapService or the url+layer we wish to plot.

In [5]:
url = ''

layers = ['BlueMarble_NextGeneration', 'VIIRS_CityLights_2012',
          'Reference_Features', 'Sea_Surface_Temp_Blended', 'MODIS_Terra_Aerosol',
          'Coastlines', 'BlueMarble_ShadedRelief', 'BlueMarble_ShadedRelief_Bathymetry']

for layer in layers:
    fig, ax = make_map()
    ax.add_wmts(url, layer)

Thanks to this I was able to create several figures for an Intro Oceanography lecture. All Open data, Open software and Open figures ;-).

PS: These figures are created with cartopy's SlippyImageArtist that means if you are using matplotlib's interactive, you can pan and zoom while the data are downloaded and updated automatically for you.

In [6]:

This post was written as an IPython notebook. It is available for download or as a static html.

Creative Commons License
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at