GIS interoperability and export (shapefile, raster)

This notebook shows how to export gridmarthe features to common GIS format.

Script API

# import modules
import numpy as np
import gridmarthe as gm

Shapefile, step 1: conversion

gridmarthe use the geopandas API to convert grids to a geodataframe object.

path_to_data = './data'
ds = gm.load_marthe_grid(f'{path_to_data}/chasim_hallue.out', 'CHARGE', fpastp=f'{path_to_data}/hallue.pastp').isel(time=-1)
gdf = gm.to_geodataframe(ds)
gdf.head()
charge time x y dx dy geometry
zone
1 9999.0 2012-07-01 596.75 2568.75 0.5 0.5 POLYGON ((596.5 2568.5, 597 2568.5, 597 2569, ...
2 9999.0 2012-07-01 597.25 2568.75 0.5 0.5 POLYGON ((597 2568.5, 597.5 2568.5, 597.5 2569...
3 9999.0 2012-07-01 597.75 2568.75 0.5 0.5 POLYGON ((597.5 2568.5, 598 2568.5, 598 2569, ...
4 9999.0 2012-07-01 598.25 2568.75 0.5 0.5 POLYGON ((598 2568.5, 598.5 2568.5, 598.5 2569...
5 9999.0 2012-07-01 598.75 2568.75 0.5 0.5 POLYGON ((598.5 2568.5, 599 2568.5, 599 2569, ...

Shapefile, step 2: export

# gdf.to_file('chasim.shp')

Export to raster

For one time step, user can export a regular grid as a TIFF file with function glm.to_raster For all time steps at once, use ds.mart.to_raster(filename_tpl='chasim_hallue'), the filename template argument must not contain any extension.

CAUTION: this is only for regular grids. For irregular/nested grids, the QGIS plugin PostMARTHE is preferable to save chasim as .tiff It will save the results in raster file with the smallest resolution available in all nested grids.

help(gm.to_raster)  # function doc to write a raster file
Help on function to_raster in module gridmarthe.grid.processing.gis:

to_raster(ds, varname=None, x_dim='x', y_dim='y', time=None, epsg=27572, filename_tpl='raster')
    Write a xr.DataArray to a raster file

    Notes
    -----
    - Warning, only functionnal for regular grids
    - Requires rasterio/rioxarray packages
    - TODO: add support for irregular grids, using PostMARTHE QGIS plugin code.

    Parameters
    ----------
    ds : xarray.Dataset, xarray.DataArray
        The dataset/dataArray to write to a raster file
    x_dim : str, optional
        The name of the x dimension, by default 'x'.
    y_dim : str, optional
        The name of the y dimension, by default 'y'.
    time : str, list
        time or list of time from `da.time`
    epsg : int, optional
        The EPSG code for the coordinate reference system, by default 27572.
    filename_tpl : str, optional
        The output file template for the raster file, by default 'raster'.
        Final name will be '{filename_tpl}_{time}.tiff'

    Returns
    -------
    None
        The function writes the raster file and returns None.

CLI for gis transformation

A command line script is also provided to transform a marthe grid file to shapefile or geopackage.

!martshp -h
usage: martshp [-h] [--output OUTPUT] [--varname VARNAME]
               [--xyfactor XYFACTOR] [--gpkg] [--mask] [--version]
               [--wide_fmt]
               [grid timesteps ...]

Convert a Marthe GridFile to shapefile format.

positional arguments:
  grid timesteps        Paths to marthe grid [and timesteps if result] files
                        are expected.If input is already a netcdf file, this
                        script allow the conversion to shapefile/gpkg.

options:
  -h, --help            show this help message and exit
  --output OUTPUT, -o OUTPUT
                        Output filename. Default is input.nc
  --varname VARNAME, -n VARNAME
                        Variable (field) to read, default is None: i.e
                        variable will be parsed from file and ONLY the first
                        variable will be read. Pass 'all' to get all
                        variables.
  --xyfactor XYFACTOR, -x XYFACTOR
                        Transformation factor for coordinates. Optional,
                        default is 1 (no transformation).
  --gpkg, -g            Use GPKG format instead of shapefile
  --mask, -m            Only get a mask of active domain
  --version, -v         Show version and exit
  --wide_fmt, -w        Use wide format (columns) for time

gridmarthe  Copyright (C) 2025  BRGM.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, see GNU General Public Lisence v3 for copying.