Skip to content

Add Functionality to convert gdf and point data to xarray #1035

Open

Description

@ValentinGebhart

Is your feature request related to a problem? Please describe.
There are regular requests about writing intensity maps / exceedance intensity maps / return period maps to xarray raster data (and then netcdf files), e.g., #854, #857, #1013. The functionality is essentially already there, see util.plot.geo_im_from_array()

Describe the solution you'd like
We could separate the rasterize functionality from util.plot.geo_im_from_array() to a new function, if the interpolation used there (scipy.interpolate.griddata) is what we think makes sense. We could also make rasterizing optional, i.e. one could simply transform the point data to a 1d xarray if one wants.

def xarray_from_gdf(gdf, rasterize=True):

or more generally

def xarray_from_points(values, lon, lat, rasterize=True):

Activity

  1. peanutfun commented on Apr 7, 2025

    @peanutfun
    Member

    one could simply transform the point data to a 1d xarray if one wants.

    This appears rather trivial to me. Why would we need that?

    We could separate the rasterize functionality from util.plot.geo_im_from_array() to a new function, if the interpolation used there (scipy.interpolate.griddata) is what we think makes sense.

    The geo_im_from_array() tries to guess if the grid is regular. If it is, it reshapes the data into a suitable 2D array. If not, it interpolates onto a fixed resolution regular grid. I think this is what we want and what the user should be able to control:

    def to_grid(
        gdf: geopandas.GeoDataFrame,
        interpolate: Optional[bool] = None,
        resolution: int = 250
    ) -> xr.DataArray:
        regular, _, _ = grid_is_regular(gdf.geometry)
    
        if not regular:   # We must interpolate
            if interpolate is None:  # User does not care
                logger.warning("Interpolating non-regular grid")
                interpolate = True
            elif interpolate is False:   # User does not want interpolation
                raise RuntimeError("Cannot reshape non-regular grid")
    
        if interpolate:
            data, coords = interpolate(gdf, resolution)
        else:
            data, coords = reshape(gdf)
    
        return to_xarray(data, coords)
  2. self-assigned this
    on Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions