Nicholas Coertze
Nicholas Coertze

Reputation: 1

Extract pixels values from the least cloudy image at each sample points location

I am working in a Jupyter Notebook I want this code to extract pixels values from the least cloudy image (in the image collection s2_collection_clipped) at each point ( in the feature collection sample_FC (4800 point)) location which it overlaps with. This code does this successfully for the first 3 features in the collection but I believe the .getInfo function (a synchronous function makes this method very very slow) and I haven't been able to run my whole dataset)

I believe I need a asynchronous alternative to .getInfo to run my whole dataset. please can anyone help me with this or suggest a alternative route.

Kind regards Nicholas


# Initialize an empty dictionary to store image band values
image_band_dict = {}


# Loop through each point in the feature collection
for feature in sample_FC.getInfo()['features'][:3]:
    # Get the point's properties
    properties = feature['properties']
    point_id = properties['Random_ID']  # Using 'Random_ID' as the unique identifier for each point
    lat = properties['Latitude']
    long = properties['Longitude']
    class_value = properties['LULC__IAP1']
    class_code = properties['LULC__IAP1_Num']

    # Find the least cloudy image the point overlaps with
    least_cloudy_image = s2_collection_clipped.filterBounds(ee.Geometry.Point(long, lat)).sort('CLOUDY_PIXEL_PERCENTAGE').first()

    # Rescale image bands / 10,000
    least_cloudy_image = least_cloudy_image.divide(10000)

    # Extract pixel values from the least cloudy image at the point's location
    pixel_values = least_cloudy_image.sample(ee.FeatureCollection([feature]))

    # Extract band values for the point
    values = pixel_values.first().toDictionary().getInfo()

    # Add information to image_band_dict
    image_band_dict[point_id] = {
        'bands': values,
        'Latitude': lat,
        'Longitude': long,
        'RandomID' : properties['Random_ID'],
        'class_value' : properties['LULC__IAP1'],
        'class_code' : properties['LULC__IAP1_Num']# Appending point properties
    }


print("Processing complete.")

Upvotes: 0

Views: 18

Answers (0)

Related Questions