SHUXIN JI
SHUXIN JI

Reputation: 1

how to get mean of ImageCollection where a coordinates Corresponding to more than one image?

I'm trying to use geemap to collet a series of points ndvi and ndvi through a time series sentinel2 imagecollection, the problem is function works well, but some points in the output file could show twice or more sence there are more than one image on that day and that point. So I want to know whether there's way to solve that before the csv exportaion?

Here's the list of points: enter image description here

import ee
ee.Initialize()

import numpy as np
import pandas as pd

# compute NDVI from NIR and red band in sentinel -2 image
# For other satellite image, please change the band information accordingly

def getNDVI(image):
    
    # Normalized difference vegetation index (NDVI)
    ndvi = image.normalizedDifference(['B8','B4']).rename("NDVI")
    image = image.addBands(ndvi)

    return(image)


# compute EVI from NIR and red band in sentinel -2 image
# For other satellite image, please change the band information accordingly

def getEVI(image):
    # Compute the EVI using an expression.
    EVI = image.expression(
        '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
            'NIR': image.select('B8').divide(10000),
            'RED': image.select('B4').divide(10000),
            'BLUE': image.select('B2').divide(10000)
        }).rename("EVI")

    image = image.addBands(EVI)

    return(image)
    
# manage the date formating as per your requirements
# Mine is in format of YYYYMMdd
def addDate(image):
    img_date = ee.Date(image.date())
    img_date = ee.Number.parse(img_date.format('YYYYMMdd'))
    return image.addBands(ee.Image(img_date).rename('date').toInt())
    
Sentinel_data = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") \
    .filterDate("2021-08-01","2021-08-03") \
    .map(getNDVI).map(getEVI).map(addDate)
    
plot_df = pd.read_csv("~/Desktop/sv.csv")

features=[]
for index, row in plot_df.iterrows():
#     print(dict(row))
#     construct the geometry from dataframe
    poi_geometry = ee.Geometry.Point([row['longitude'], row['latitude']])
#     print(poi_geometry)
#     construct the attributes (properties) for each point 
    poi_properties = dict(row)
#     construct feature combining geometry and properties
    poi_feature = ee.Feature(poi_geometry, poi_properties)
#     print(poi_feature)
    features.append(poi_feature)

# final Feature collection assembly
ee_fc = ee.FeatureCollection(features) 
# ee_fc.getInfo()

def rasterExtraction(image):
    feature = image.sampleRegions(
        collection = ee_fc, # feature collection here
        scale = 10 # Cell size of raster
    )
    return feature
    
results = Sentinel_data.filterBounds(ee_fc).select('NDVI', 'EVI').map(addDate).map(rasterExtraction).flatten()

sample_result = results.first().getInfo()
sample_result

# extract the properties column from feature collection
# column order may not be as our sample data order
columns = list(sample_result['properties'].keys())
print(columns)


# Order data column as per sample data
# You can modify this for better optimization
column_df = list(plot_df.columns)
column_df.extend(['NDVI', 'EVI', 'date'])
print(column_df)


url_csv = results.getDownloadURL('csv')
# click the link below, this will download CSV directly to your local device
# You can use this url and download with python request module, I will leave that to you
url_csv

Upvotes: 0

Views: 16

Answers (0)

Related Questions