gridmarthe.grid.processing.gis

GIS utility for marthe grids

gridmarthe.grid.processing.gis.to_geodataframe(ds, epsg='EPSG:27572', fmt='long')[source]

Convert marthegrid.Dataset to a geodataframe

Parameters:
dsxarray.Dataset

The dataset to convert.

epsgstr, optional

The EPSG code for the coordinate reference system, by default ‘EPSG:27572’.

fmtstr, optional

The format of the output GeoDataFrame, either ‘long’ or ‘wide’, by default ‘long’.

Returns:
geopandas.GeoDataFrame

The converted GeoDataFrame.

gridmarthe.grid.processing.gis.clip_dataset(ds, gdf, crs=27572, engine='gdf')[source]

Clip a xarray Dataset with a gpd.GeoDataFrame

Needs rioxarray. If not installed, raise ModuleNotFoundError please install it or reinstall gridmarthe with optional dependancies: pip install gridmarthe[opt]

See: https://corteva.github.io/rioxarray/html/examples/clip_geom.html Todo: shapely version

Parameters:
dsxarray.Dataset

The dataset to clip.

gdfgeopandas.GeoDataFrame

The GeoDataFrame containing the geometry to clip with.

crsint, optional

The coordinate reference system to use for the clipping, by default 27572.

enginestr, optional

The engine to use for clipping, by default ‘gdf’.

Returns:
xarray.DataArray or xarray.Dataset

The clipped dataset.

gridmarthe.grid.processing.gis.subset_with_coords(da, dims=['x', 'y'], gdf=None, xmin=None, ymin=None, xmax=None, ymax=None)[source]

subset DataArray or Dataset on rectangular shape, with gpd.GeoDataFrame or bounds

Parameters:
daxarray.DataArray or xarray.Dataset

The data to subset.

dimslist of str, optional

The dimensions to use for subsetting, by default [‘x’, ‘y’].

gdfgeopandas.GeoDataFrame, optional

A GeoDataFrame containing the geometry to use for subsetting, by default None.

xmin, ymin, xmax, ymaxfloat, optional

The manual bounds to use for subsetting, by default None.

Returns:
xarray.DataArray or xarray.Dataset

The subsetted data.

gridmarthe.grid.processing.gis.reproj_grid(ds, from_epsg='EPSG:27572', to_epsg='EPSG:2154', engine='regrid', decimals=None)[source]

Transform the projection of a dataset using pyproj

Parameters:
dsxarray.Dataset

Dataset to transform

from_epsgstr

EPSG code of input data

to_epsgstr

targeted EPSG code

enginestr, optional

Engine to use, either ‘rioxarray’ or ‘regrid’ (default)

decimalsint, optional

Number of decimals to round the coordinates to (default is None, not used). E.g use decimals=0 to round at meters scale for EPSG 27572, 2154 (Lambert projections).

Returns:
xarray.Dataset

Dataset with new coordinates in the targeted projection

Warning

  • (!) NaN should not have been removed before (full x,y,dx,dy are needed)

  • This is only valid for regular grids.

  • If used with ‘rioxarray’ mode, it should be only used for visualization purposes. For rewriting a Marthe grid with another projection, further tests are required.

  • This function is still EXPERIMENTAL and should be used with caution.

gridmarthe.grid.processing.gis.to_raster(ds, varname=None, x_dim='x', y_dim='y', time=None, epsg=27572, filename_tpl='raster')[source]

Write a xr.DataArray to a raster file

Parameters:
dsxarray.Dataset, xarray.DataArray

The dataset/dataArray to write to a raster file

x_dimstr, optional

The name of the x dimension, by default ‘x’.

y_dimstr, optional

The name of the y dimension, by default ‘y’.

timestr, list

time or list of time from da.time

epsgint, optional

The EPSG code for the coordinate reference system, by default 27572.

filename_tplstr, 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.

Notes

  • Warning, only functionnal for regular grids

  • Requires rasterio/rioxarray packages

  • TODO: add support for irregular grids, using PostMARTHE QGIS plugin code.