1

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

Aegis
  • 9
  • 5

1 Answers1

-1

Try using the numpy library. For example

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]

To adapt this approach in your method, try using

arr = np.array(type_crop)

instead

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

assuming your type_crop is the 2d array.

Nafiz Ahmed
  • 567
  • 3
  • 10
  • for me the variable dataset is already defined using the dataset in question and clipping it to the polygons I have. How do I adapt your suggestion in that case? – Aegis May 27 '20 at 08:17
  • I have adjusted the names and edited the answer. I think your ````type_crop```` returns the 2d array which you should use. – Nafiz Ahmed May 27 '20 at 08:34
  • I assume arr stands for array? My python version doesn't seem to recognise it. – Aegis May 27 '20 at 09:37
  • It is just a variable name for holding the 2d array – Nafiz Ahmed May 27 '20 at 11:00
  • Hmm, strange. When I put it in I get an error message of an undefined variable whilst it is defined. I updated my original post to reflect what happened so far. – Aegis May 28 '20 at 10:53