0

I have this code and my aim to calculate the sin of my raster in the power of 0.8.

import os
os.chdir('D:/NOA/Soil_Erosion/test_Project/Workspace/Input_Data_LS_Factor')
import rasterio
import math 

data = rasterio.open('Slope_degrees_clipped.tif')

band = data.read(1) # array of float32 with size (3297,2537)

w = band.shape[0]
print(w)

h = band.shape[1]
print(h)
dtypes =data.dtypes[0] 


band_calc = math.sin(band)**0.8 # the formula I would like to calculate

However, the following error pops up: only size-1 arrays can be converted to Python scalars / Rasterio

May you know how I should fix this?

P.S. I tried to vectorize it (np.vectorize()) but it does not work as it needs a real number. When I use the np.ndarray.flatten(band) the same error occurs.

1 Answers1

0

I found the solution on Geographic Information Systems:

import os
os.chdir('D:/NOA/Soil_Erosion/test_Project/Workspace/Input_Data_LS_Factor')

import rasterio
import math

data = rasterio.open('Slope_degrees_clipped.tif')

from rasterio.plot import show

show(data)

band = data.read(1) # array of float32 with size (3297,2537)

w = band.shape[0]
print(w)

h = band.shape[1]
print(h)

dtypes =data.dtypes[0]

Calculate the sine of the raster in the power of 0.8

import numpy as np

band_calc2 = np.sin(band)**0.8 # the formula I would like to calculate

"""

another way to do it

band_calc = [ [] for i in range(len(band)) ]

for i,row in enumerate(band):

for element in row:

    band_calc[i].append(math.sin(element*math.pi/180)**0.8)

"""