# Geometry masks *Geometry masks* are Shapely polygons that the chain rasterizes at tile level before calling the core resampling function. All source-array masks (raster mask + geometry masks) are merged into a single mask that is passed to `array_grid_resampling`. This feature is only available in the chain layer; the core function does not see polygons directly. **What you'll learn** - The role of `array_src_geometry_pair` (valid + invalid polygons) - How to use `array_src_geometry_origin` to shift polygon coordinates - How geometry masks combine with the source raster mask ## Setting things up We reuse the source raster, grid file, grid mask and source raster mask from previous notebooks. ## Defining the geometries `array_src_geometry_pair` is a length-2 sequence of Shapely polygons: - index 0 -- the **valid** geometry: pixels inside are kept valid, - index 1 -- the **invalid** geometry: pixels inside are forced invalid. Either entry may be `None`. Coordinates are expressed in the source raster frame; `array_src_geometry_origin` shifts them if needed. ```python from shapely.geometry import Polygon geometry_valid = Polygon([(40, 10), (300, 30), (270, 200), (40, 200)]) invalid_polygon = create_star_polygon(100, 100, 50) ``` ![geometry_input.png](grid_resampling_chain_005_geometry_masks_files/geometry_input.png) The figure above shows the green valid polygon and the red star-shaped invalid polygon, together with the grid nodes and the source raster mask. ## Running the chain with geometry masks ```python with rasterio.open(GRID_IN_F64, "r") as grid_in_ds, \ rasterio.open(RASTER_IN, "r") as array_src_ds, \ rasterio.open(grid_mask_in_path, "r") as grid_mask_in_ds, \ rasterio.open(raster_mask_in_path_u8, "r") as raster_mask_in_ds, \ rasterio.open(output_raster_path, "w", **raster_out_open_args) as array_out_ds, \ rasterio.open(output_mask_path, "w", **mask_out_open_args) as mask_out_ds: basic_grid_resampling_chain( grid_ds = grid_in_ds, grid_row_coords_band = 1, grid_col_coords_band = 2, grid_resolution = grid_resolution, array_src_ds = array_src_ds, array_src_bands = 1, array_out_ds = array_out_ds, interp = "cubic", nodata_out = 0, mask_out_ds = mask_out_ds, grid_mask_in_ds = grid_mask_in_ds, grid_mask_in_unmasked_value = grid_mask_in_valid_value, grid_mask_in_band = 1, array_src_mask_ds = raster_mask_in_ds, array_src_mask_band = 1, array_src_mask_validity_pair = (array_src_mask_validity_valid, array_src_mask_validity_invalid), array_src_geometry_origin = (0., 0.), array_src_geometry_pair = [geometry_valid, invalid_polygon], ) ``` ![geometry_mask_output.png](grid_resampling_chain_005_geometry_masks_files/geometry_mask_output.png)