def mask_chip(feature):
'''
Apply polygon mask to bounding-box chips. Chips must be named
'feature_id.tif' and exist in the current working directory.
'''
fn = feature[:-4] + '.geojson'
chip = gdal.Open(feature)
# Create ogr vector file for gdal_rasterize
vectordata = {'type': 'FeatureCollection', 'features': [feature]}
with open(fn, 'wb') as f:
geojson.dump(vectordata, f)
# Mask raster
cmd = 'gdal_rasterize -q -i -b 1 -b 2 -b 3 -burn 0 -burn 0 -burn 0 {} {}'.format(fn, feature)
subprocess.call(cmd, shell=True)
# Remove ogr vector file
os.remove(fn)
评论列表
文章目录