【问题标题】:only size-1 arrays can be converted to Python scalars / Rasterio只有 size-1 的数组可以转换为 Python 标量 / Rasterio
【发布时间】:2021-05-03 16:14:06
【问题描述】:

我有这段代码,我的目标是计算我的栅格的 0.8 次方的 sin。

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

但是,会弹出以下错误: 只有 size-1 的数组可以转换为 Python 标量/光栅

你知道我应该如何解决这个问题吗?

附:我尝试对其进行矢量化(np.vectorize()),但它不起作用,因为它需要一个实数。 当我使用 np.ndarray.flatten(band) 时,会发生同样的错误。

【问题讨论】:

    标签: arrays trigonometry rasterio


    【解决方案1】:

    我在地理信息系统上找到了解决方案:

    导入操作系统
    os.chdir('D:/NOA/Soil_Erosion/test_Project/Workspace/Input_Data_LS_Factor')

    导入光栅
    导入数学

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

    从 rasterio.plot 导入显示

    显示(数据)

    band = data.read(1) # 大小为 (3297,2537) 的 float32 数组

    w = band.shape[0]
    打印(w)

    h = band.shape[1]
    打印(h)

    dtypes =data.dtypes[0]

    以 0.8 次方计算栅格的正弦

    将 numpy 导入为 np

    band_calc2 = np.sin(band)**0.8 # 我要计算的公式

    """

    另一种方法

    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)
    

    """

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2021-12-24
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      相关资源
      最近更新 更多