我目前正在使用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值,而像这样,对于全球数据集,则不能手动选择它
尝试使用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]