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:
(masked_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()
-7497.125
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?