I try to reclassify a DEM file. I never had problem with this code, but with this file (actually with this folder consisting of .adf files) I get the error: 'operands could not be broadcast together with shapes (3,14452099) (1,3178,9156)'
I do not understand why…
My code:
import rasterio
import numpy as np
with rasterio.open("C:.../dem_au") as src:
# Read as numpy array
array = src.read()
profile = src.profile
array = array.copy()
# Reclassify
array[np.where(array <= 0)] = 0
array[np.where(array > 0) & (array <= 300)] = 1
array[np.where(array > 300) & (array <= 600)] = 2
array[np.where(array > 600) & (array <= 900)] = 3
array[np.where(array > 900) & (array <= 1200)] = 4
array[np.where(array > 1200) & (array <= 1500)] = 5
array[np.where(array > 1500) & (array <= 1800)] = 6
array[np.where(array > 1800) & (array <= 2100)] = 7
array[np.where(array > 2100) & (array <= 2400)] = 8
array[np.where(array > 2400)] = 9
with rasterio.open("C:.../DEM_reclass.tif", 'w', **profile) as dst:
# Write to disk
dst.write(array)
Best Answer
You miss parentheses. Enclose all conditions in parentheses.
np.where(array > 0) & (array <= 300)
->np.where( (array > 0) & (array <= 300) )