Created
September 11, 2018 19:43
-
-
Save mappingvermont/fc303378a13e66d0f3261450ac7c35f5 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 numpy as np | |
import rasterio | |
# open loss and grab metadata about the tif, | |
# like it's location / projection / cellsize | |
with rasterio.open('loss.tif') as loss_src: | |
kwargs = loss_src.meta | |
# also grab the windows (stripes? tiles?) so we can iterate | |
# over the entire tif without running out of memory | |
windows = loss_src.block_windows(1) | |
# open gain | |
with rasterio.open('gain.tif') as gain_src: | |
# open extent | |
with rasterio.open('extent.tif') as extent_src: | |
# update those kwargs for the dataset we'll write out | |
kwargs.update( | |
driver='GTiff', | |
count=1, | |
compress='lzw', | |
nodata=0 | |
) | |
with rasterio.open('output.tif', 'w', **kwargs) as dst: | |
for idx, window in windows: | |
loss_data = loss_src.read(1, window=window) | |
gain_data = gain_src.read(1, window=window) | |
extent_data = extent_src.read(1, window=window) | |
# create an empty array | |
dst_data = np.zeros((window.height, window.width), dtype='uint8') | |
# where loss & gain, set output to 100, otherwise keep dst_data value | |
dst_data[np.where((loss_data > 0) & (gain_data > 0))] = 100 | |
# etc | |
dst_data[np.where((loss_data > 10) & (gain_data > 0) & (extent_data > 50))] = 99 | |
dst_data[np.where((loss_data < 10) & (gain_data == 0) & (extent_data < 50))] = 1 | |
dst_data[np.where((loss_data > 10) & (gain_data > 0) & (extent_data >= 90))] = 2 | |
dst.write_band(1, dst_data, window=window) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment