【问题标题】:calculating percentage of different bands within polygons计算多边形内不同波段的百分比
【发布时间】:2020-05-27 07:30:09
【问题描述】:

我目前正在使用来自 GEE (USGS/GFSAD1000_V0) 的数据集,目的是将其转置到我在所有这些之前已经确定的一组多边形上。我现在正在尝试计算某个多边形内不同值(0-9)的像素百分比并将其打印出来。

import ee
import numpy as np

ee.Initialize()

polygon_version = '(SOURCE)'
land_polygons = ee.FeatureCollection(f'users/(SOURCE)/{polygon_version}_poly_land')

def Landcover_Crops_nr(polygons):
    dataset = ee.Image("USGS/GFSAD1000_V0").clip(polygons)
    type_crop = dataset.select("landcover")
    arr = np.array(type_crop)

    rawres = type_crop.getInfo()["features"]
    res = {
        x["properties"]["id"]: {
            "id": x["properties"]["id"],
            "area": float(x["properties"]["area"]),
            "center_lat": x["properties"]["center_lat"],
            "crop_area": x["properties"]["sum"],
        }
        for x in rawres
    }

    return res
values, frequencies= np.unique(arr, return_counts=True)
sum = np.sum(frequencies)
percentages = [x/sum*100 for x in frequencies]
dfgen = Landcover_Crops_nr(land_polygons)
dfgen.to_csv(f'{polygon_version}_Crops.csv', index=False)
print (dfgen)

我以前尝试过,但正如您所见,它只会关注 9 值,而像这样手动执行对于全球数据集来说不是一个选项

【问题讨论】:

  • 您的数据集是二维值数组 (0-9) 吗?
  • 是的,数据集是一个 0 到 9 之间值的二维数组

标签: python dataset polygon percentage google-earth-engine


【解决方案1】:

尝试使用 numpy 库。 例如

import numpy as np

# generate an array of size 10x10 with values (0-9)
arr = np.random.randint(10, size=(10, 10))

# get unique values with frequencies
values, frequencies= np.unique(arr, return_counts=True) 

# calculate sum of frequencies
sum = np.sum(frequencies)

# calculate percentages
percentages = [x/sum*100 for x in frequencies]

# Example

# print(values)
# [0, 1, ...,9 ]

# print(percentages)
# [25.0, 15.0, ..., 2.5]

要在您的方法中采用这种方法,请尝试使用

arr = np.array(type_crop)

改为

arr = np.random.randint(10, size=(10, 10))

假设您的 type_crop 是二维数组。

【讨论】:

  • 对我来说,变量数据集已经使用相关数据集定义并将其剪切到我拥有的多边形中。在这种情况下,我该如何调整您的建议?
  • 我已经调整了名称并编辑了答案。我认为您的 type_crop 返回您应该使用的二维数组。
  • 我假设 arr 代表数组?我的 python 版本似乎无法识别它。
  • 只是一个保存二维数组的变量名
  • 嗯,奇怪。当我把它放进去时,我得到一个未定义变量的错误消息,而它被定义了。我更新了我原来的帖子以反映到目前为止发生的事情。
猜你喜欢
  • 2021-08-25
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多