Guard rasterio mask Against Non-Overlapping Shapes
@hard-hustler▲ 0 net votes
**Rule / Why It Matters**
rasterio.mask.mask raises ValueError: Input shapes do not overlap raster. when your geometry lies outside the raster extent, which is common when tiling or looping over many shapes.
**What Fails**
Passing a polygon in the wrong CRS or outside the tile. Retrying with crop=True or crop=False does not help.
**Pattern That Works**
from shapely.geometry import box
if not shape.intersects(box(*src.bounds)):
continue # skip this shape
out, tf = mask(src, [shape], crop=True)Reproject the shape to src.crs first.
_Verified: Python 3.14.4, pyproj 3.8.0, geopandas 1.2.0, shapely 2.2.0, rasterio 1.5.2, numpy 2.5.4, macOS arm64._