I am using a simple ASCII raster file to test the rasterio.mask module

ncols 4
nrows 4
xllcorner 0
yllcorner 0
cellsize 0.5
nodata_value -9999
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

I generated the following script which should extract all the diagonals

import rasterio
import shapely
from rasterio import mask

def extract_by_mask(raster_dataset, geometry):
    #outputs a numpy masked array with values within polygon geometry
    geometry = [shapely.geometry.mapping(geometry)]
    m = mask.mask(raster = raster_dataset, shapes = geometry)
    return m
r = rasterio.open('raster.asc')
p = shapely.geometry.Polygon([[0,0],[0.3,0.1],[1.58,1.5],[2,2],[1.56,1.8],[0.1,0.4]])
m = extract_by_mask(r, p)
print m

It does extract the diagonals, but I'm not comfortable with the resulting mask array:

  data=[[[-9999, -9999, -9999, 4],
         [-9999, -9999, 7, -9999],
         [-9999, 10, -9999, -9999],
         [13, -9999, -9999, -9999]]],
  mask=[[[False, False, False, False],
         [False, False, False, False],
         [False, False, False, False],
         [False, False, False, False]]],
  fill_value=-9999), Affine(0.5, 0.0, 0.0,
       0.0, -0.5, 2.0))

It seems to consider that all the values are valid, such that when I take a mean it considers all the values including nodata:

>>> m[0].mean()

I have been playing around with some of the parameters in the mask method but no luck. I think the one I'm looking for in the docs is filled = False but when I do this I get an error:

Traceback (most recent call last):
  File "D:/Basis/common.py", line 114, in <module>
    m = extract_by_mask(r, p)
  File "D:/Basis/common.py", line 109, in extract_by_mask
    m = mask.mask(raster = raster_dataset, shapes = geometry, filled = False)
TypeError: mask() got an unexpected keyword argument 'filled'

It doesn't seem to recognize filled as a valid argument. I am using rasterio version 0.36.0 by anaconda.

I need that mask array to show True for the diagonal values so I can only consider those for calculations.

Could anyone help?

  • I'd start by updating rasterio. Current version looks to be 1.0.10 while you're on a pre-1.0 version meaning it was considered unstable (or at least that the APIs weren't finalized).
    – mikewatt
    Commented Nov 29, 2018 at 18:17

2 Answers 2


Explicitly passing nodata=9999 to rasterio.mask.mask may help output masked array with True at desired locations.


You could try rioxarray. An example of clipping/masking raster is here.

rds = rioxarray.open_rasterio('raster.asc')
geometries = [
        'type': 'Polygon',
        'coordinates': [[[0,0],[0.3,0.1],[1.58,1.5],[2,2],[1.56,1.8],[0.1,0.4]]]
geometry_crs = "???"  # not sure what projection the geometries are in
rds_clipped = rds.rio.clip(geometries, geometry_crs, drop=False)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.