I am currently working with a dataset from GEE (USGS/GFSAD1000_V0) with the purpose of transposing it onto a set of polygons that I already have determined before all of this. I am now trying to calculate the percentage of pixels of different values (0-9) within a certain polygon and print that out.
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)
I have tried this before but as you can see it only would focus on the 9 value, whilst doing it manually like this isn't an option for a world wide dataset