easyclimate_map.core.clip

Geospatial Clipping Utilities

This module provides functions for clipping and converting geospatial data for visualization in matplotlib with cartopy projections. The tools are specifically designed for working with GeoDataFrame objects and converting them to matplotlib-compatible path objects for clipping operations.

Functions

get_geometry_path(→ easyclimate_map.core.datanode.DataNode)

Convert geometries from a GeoDataFrame into matplotlib clipping paths.

clip_rectangle_geometry(...)

Clip a GeoDataFrame using a rectangular bounding box.

Module Contents

easyclimate_map.core.clip.get_geometry_path(gdf: geopandas.geodataframe.GeoDataFrame, ax: matplotlib.axes.Axes) easyclimate_map.core.datanode.DataNode

Convert geometries from a GeoDataFrame into matplotlib clipping paths.

This function extracts geometries from a GeoDataFrame, projects them to the coordinate system of the given matplotlib axis, and converts them into matplotlib Path objects suitable for clipping or other operations.

Parameters

gdfgeopandas.GeoDataFrame

The GeoDataFrame containing geometries to be converted. Geometries should be in geographic coordinates (longitude/latitude in degrees) using the EPSG:4326 (PlateCarree) coordinate reference system.

axmatplotlib.axes.Axes

The matplotlib axis with a cartopy projection. The axis must have a cartopy.crs projection set, which will be used to transform the geometries from geographic coordinates to the map’s coordinate system.

Returns

easyclimate-map.DataNode

A DataNode object containing two items:

  • clip_path: matplotlib.path.Path

    A compound Path object containing all projected geometries.

  • transform_clip_path: matplotlib.transforms.TransformedPath

    The same Path object transformed to data coordinates of the axis.

Notes

  1. The function assumes input geometries are in geographic coordinates (EPSG:4326 / PlateCarree).

  2. The axis must use a cartopy projection. Standard matplotlib axes without cartopy will not work.

  3. The resulting Path objects can be used for clipping matplotlib plots or other operations that require vector paths.

Examples

>>> import matplotlib.pyplot as plt
>>> import cartopy.crs as ccrs
>>> import geopandas as gpd
>>>
>>> # Create a map with cartopy projection
>>> fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})
>>> # Load GeoDataFrame (assumed to be in geographic coordinates)
>>> gdf = gpd.read_file('path/to/shapefile.shp')
>>> # Convert geometries to clipping paths
>>> paths = get_geometry_path(gdf, ax)
>>> # Use the paths for clipping
>>> im = ax.imshow(data, transform=ccrs.PlateCarree())
>>> im.set_clip_path(paths['transform_clip_path'])

See Also

cartopy.mpl.path.shapely_to_path : Converts shapely geometries to matplotlib paths matplotlib.path.Path.make_compound_path : Combines multiple paths into one matplotlib.transforms.TransformedPath : Applies transformations to paths

easyclimate_map.core.clip.clip_rectangle_geometry(gdf: geopandas.geodataframe.GeoDataFrame, *args) geopandas.geodataframe.GeoDataFrame

Clip a GeoDataFrame using a rectangular bounding box.

Supports two parameter styles: 1. Four separate arguments: clip_rectangle_geometry(gdf, lon1, lon2, lat1, lat2) 2. A single list/tuple: clip_rectangle_geometry(gdf, [lon1, lon2, lat1, lat2])

Parameters

gdfgeopandas.GeoDataFrame

The GeoDataFrame to clip

*argsUnion[float, List[float], Tuple[float]]

Either four separate float arguments (lon1, lon2, lat1, lat2) or a single list/tuple containing four floats

Returns

geopandas.GeoDataFrame

The clipped GeoDataFrame

Examples

>>> # Method 1: Four separate arguments
>>> clipped = clip_rectangle_geometry(gdf, 110, 130, 30, 40)
>>>
>>> # Method 2: Single list argument
>>> clipped = clip_rectangle_geometry(gdf, [110, 130, 30, 40])