Created
February 4, 2015 19:48
-
-
Save dnomadb/7dea9d53653653877b82 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
with rasterio.open(inraster, 'r') as src: | |
rasArr = src.read_band(band_no) | |
oshape = src.shape | |
otrans = src.transform | |
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) | |
print values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment