-
-
Save aaronpdennis/88dae9821df8559d479a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rasterio, fiona | |
from rasterio import features | |
uidfield = #whatever field contains unique values for extraction | |
infile = #input shapefile | |
inraster = #input raster | |
#read raster | |
with rasterio.open(inraster, 'r') as src: | |
rasArr = src.read_band(band_no) | |
oshape = src.shape | |
otrans = src.transform | |
#read and rasterize vector features | |
with fiona.open(infile, 'r') as shp: | |
fields = list(set(feat['properties'][uidfield] for feat in shp)) | |
sampleRaster = features.rasterize( | |
((feat['geometry'], feat['properties'][uidfield]) for feat in shp), | |
out_shape=oshape, | |
transform=otrans) | |
#list of unique values | |
values = list(np.mean(rasArr[np.where(sampleRaster == i)]) for i in fields)#calculate mean input raster values for each rasterized feature id | |
print values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment