Skip to content

Add DataArray.metpy.to_geotiff for GIS raster export#4086

Open
martinguthrie93 wants to merge 1 commit into
Unidata:mainfrom
martinguthrie93:geotiff-export
Open

Add DataArray.metpy.to_geotiff for GIS raster export#4086
martinguthrie93 wants to merge 1 commit into
Unidata:mainfrom
martinguthrie93:geotiff-export

Conversation

@martinguthrie93

Copy link
Copy Markdown

Description of Changes

Adds a .metpy.to_geotiff() method to the DataArray accessor, giving MetPy a
first-class path to export a gridded field as a georeferenced GeoTIFF that
GIS software (QGIS, ArcGIS, GDAL-based tooling) can read directly.

Today MetPy can read, compute on, and plot gridded data in its native projected
coordinates, but there is no way to write that data back out as a standard GIS
raster. This closes the raster-interoperability gap (item 1 of #4085).

What it does

to_geotiff() is a thin, well-scoped bridge to rioxarray:

  • pulls the MetPy CRS (as assigned by .parse_cf / .assign_crs) and communicates
    it to rioxarray via the existing .metpy.pyproj_crs;
  • identifies the x/y spatial dimension coordinates via .metpy.coordinates('x', 'y');
  • drops pint units and the non-serializable metpy_crs object coordinate before
    writing;
  • forwards **kwargs to rioxarray's to_raster (e.g. compress, dtype, tiled).
import xarray as xr
from metpy.cbook import get_test_data

ds = xr.open_dataset(get_test_data('narr_example.nc', False)).metpy.parse_cf()
ds['Temperature'].isel(isobaric=0).metpy.to_geotiff('temperature.tif')
# -> a Lambert-Conformal GeoTIFF openable in QGIS/ArcGIS/GDAL

Error handling

  • Raises a clear ImportError (with install hint) when the optional rioxarray
    dependency is missing.
  • Raises a clear ValueError when x/y dimension coordinates cannot be identified.

Dependencies

Adds rioxarray>=0.13.0 to the optional extras group. It is not a hard
dependency; the method degrades gracefully when it is absent.

Tests

  • test_to_geotiff — round-trips a parsed field to GeoTIFF and asserts the CRS and
    spatial dimensions are preserved on read-back.
  • test_to_geotiff_no_rioxarray — asserts the helpful ImportError when rioxarray
    is unavailable.
  • test_to_geotiff_no_spatial_coords — asserts the helpful ValueError when x/y
    coordinates are absent.

All new tests pass locally; flake8 is clean on the changed files.

Notes / possible follow-ups (out of scope for this PR)

  • A matching Dataset.metpy.to_geotiff() (multi-band export).
  • Reprojection/regridding utilities and vector-geometry export (items 2 and 3 of
    the linked issue).

MetPy can compute on gridded fields in their native projected coordinates
but has no way to hand those fields to the wider GIS ecosystem as a
georeferenced raster. Add a `.metpy.to_geotiff()` method on the DataArray
accessor that bridges MetPy's CRS (from `parse_cf`/`assign_crs`) and x/y
spatial dimensions to rioxarray, producing a GeoTIFF readable by GIS
software such as QGIS, ArcGIS, and GDAL-based tooling.

rioxarray is added as an optional ("extras") dependency; the method raises
a clear ImportError when it is absent and a clear ValueError when x/y
dimension coordinates cannot be identified. Adds tests covering the
georeferenced round-trip, the missing-rioxarray path, and the
missing-spatial-coordinate path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@martinguthrie93 martinguthrie93 requested a review from a team as a code owner July 3, 2026 09:13
@martinguthrie93 martinguthrie93 requested review from dopplershift and removed request for a team July 3, 2026 09:13
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


HoHo seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants