Xarray extensions for map plotting

I thought I’d share here two projects I’m working on. They are xarray extensions that offer tiled map plotting capabilities. By using (Leaflet or deck.gl) tiles in the frond-end and (xarray) chunks in the back-end, they are especially good at visualizing big data, possibly stored in the cloud.

  • xarray-leaflet: 2D map plotting based on ipyleaflet (and thus LeafletJS). Supports custom projections. Data can be transformed through a pipeline, possibly using Dask’s lazy evaluation system.
  • xtrude: 3D terrain visualization based on pydeck (and thus deck.gl), from an elevation array. A surface texture can also be overlaid on the 3D mesh.

Both projects are in an early stage of development (especially xtrude), but could be of great value to the Pangeo community.
xarray-leaflet was presented at JupyterCon 2020 (video here).

8 Likes

Fantastic, thanks for sharing! These would integrate very nicely with an interactive viewer for cloud-based data.

BTW Ryan, when I look at LLC4320 Bot’s vorticy plot, I think it might render well in ipyleaflet’s velocity layer.

1 Like

PR welcome! :stuck_out_tongue_winking_eye:

1 Like

I like the look of xarray-leaflet!

Bear in mind I’m a data scientist and something of a geospatial (vis) numpty. I’m after the best (pronounced “easiest”) route to achieve an interactive (base)map and xarray based raster data. I get the basics of CRS and projections, but I’m sure not many of the deeper details, especially when it comes to the various formats for specifying projections and which function of which package accepts, or does, what!

I see ipyleaflet has a few built-in projections in dicts in the projections attribute. I’m surprised it has so few built in? I have data in xarray in CRS EPSG:32643 (N. India). If I try to specify that CRS when creating an ipyleaflet basemap, I just get a grey image. I think I understand this is because the projection isn’t supported. I tried creating a basemap (m) using the default ipyleaflet CRS and then add my raster image with da.leaflet.plot(m). I get the basemap show up, but no raster image. I think I understand this is because the xarray_leaflet extension doesn’t do reprojections and just assumes the coordinates already match? So my raster could be appearing on the Moon or somewhere.

So I conclude I either need to tell ipyleafet about EPSG:32643, or reproject my xarray data to one of the CRS included in ipyleaflet’s projections? Getting a basemap in my target CRS sounds like the most computationally efficient approach.

I tried defining a custom projection using information from the epsg page and some copypaste:

proj43 = {
    'name': 'EPSG:32643',
    'custom': True,
    'proj4def': '+proj=utm +zone=43 +datum=WGS84 +units=m +no_defs',
    'bounds': [
        [166021.44, 0.00],
        [534994.66, 9329005.18]
    ],
    'resolutions': [
        8192.0,
        4096.0,
        2048.0,
        1024.0,
        512.0,
        256.0
    ]
}

but that resulted in a grey image as well.

I was previously using contextily, but that’s not interactive. So this worked:

predicted.where(predicted.label == id).label.plot(ax=ax)
ctx.add_basemap(ax, crs='EPSG:32643', source=ctx.providers.OpenStreetMap.France)

where predicted is my xarray to plot.

I just want to create a basemap in the right CRS and have my dask-backed xarray rasters magically overplotted. It’s not much to ask! :wink: I don’t mind defining a projection, but I don’t really know what I’m doing (which is likely clear from the above) so not sure whether I defined something wrong in proj43 or simply the specified basemap isn’t available in that CRS. Naively, it’s the same (?) basemap (at least provider) as works for EPSG:32643 in contextily.

Although I may be slightly hijacking/resurrecting your post about xarray-leaflet, @davidbrochart (which does look very cool and useful, BTW!) this does seem appropriate given the title of “xarray extensions for map plotting”.

If someone could give me a pointer, I’d be immensely grateful.

xarray-leaflet should support custom projections, here is an example: xarray_leaflet/custom_projection.ipynb at master · davidbrochart/xarray_leaflet · GitHub
You will need a basemap in that projection, for sure. xarray-leaflet will reproject your raster in that projection, provided it’s originally in WGS84 (“lat/lon”).
But there might be issues with specific projections, especially if you’re trying to set your own. @betolink might be able to help here, as he contributed to ipyleaflet’s built-in projections.
I can also help you if you can share a notebook.

2 Likes

There are a number of options for this these days, all with quirks and caveats but a few links to explore examples if folks are interested: