python4oceanographers

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 cartopy.crs 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)
    ax.set_extent(extent)
    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 = "http://nowcoast.noaa.gov/wms/com.esri.wms.Esrimap/analyses"
wms = WebMapService(url)

layer = 'NCEP_RAS_ANAL_RTG_SST'
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 = 'http://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi'

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)
    ax.set_title(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]:
HTML(html)
Out[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 https://ocefpaf.github.io/.

Comments