计算多边形内不同波段的百分比

问题描述 投票:1回答:1

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

def Landcover_Crops_nr(polygons):
    dataset = ee.Image("USGS/GFSAD1000_V0").clip(polygons)
    resolution = 30.87
    type_crop = dataset.select("landcover")
    threshold = type_crop.lt(9)
    mask = threshold.mask(threshold)

我之前已经尝试过,但是您可以看到它只专注于9值,而像这样,对于全球数据集,则不能手动选择它

python dataset polygon percentage google-earth-engine
1个回答
0
投票

尝试使用numpy库。例如

import numpy as np

# generate a dataset of size 10x10 with values (0-9)
dataset = np.random.randint(10, size=(10, 10))

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

# calculate sum of values
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]

© www.soinside.com 2019 - 2024. All rights reserved.