0

I am trying to create a csv file with all the pixel values of covariates within my polygons. I created a for loop that reads the year when the polygon was created and automatically extracts ndvi values for all pixels.

# read and load farm plots and select year
import geopandas as gpd
farm_geojson_shp = gpd.read_file('Test_farm.geojson')
years = farm_geojson_shp['T_Year'].unique().tolist()
ee_object = geemap.geopandas_to_ee(farm_geojson_shp)

# read covariates based on yearly values
for year in years:
    farm_geojson_year = farm_geojson_shp[farm_geojson_shp['T_Year'] == year]
    ee_object_year = geemap.geopandas_to_ee(farm_geojson_year)
    ndviPerc_10 = read_ndviPerc_10(year, continent)
    stacked_image = landsat_sr_median.addBands(ndviPerc_10)
    stacked_image_2 = stacked_image.addBands(dem_3)
    stacked_image_4 = stacked_image_2.addBands(dsm_4)
    stacked_image_6 = stacked_image_4.addBands(dtm_elevation_3)
    stacked_image_7 = stacked_image_6.addBands(fapar_3)
 
    # get the values for all pixels in each polygon in the training
    table_points = stacked_image_7.sampleRegions(collection = ee_object_year,
    # keep this list of properties from the polygons
    properties = ['farm_id', 'T_Year'],
    # set the farm to get Landsat pixels in the polygons
    scale = 30, geometries = True)

    # Break point coordinates up into properties (table columns) explicitly.
    def func_zci (feature):
        coordinates = feature.geometry().transform('epsg:4326').coordinates()
        return feature.set('lon', coordinates.get(0), 'lat', coordinates.get(1))

    collection_with_latlon = table_points.map(func_zci)

    test = ee_object_year.merge(collection_with_latlon)

    # use an equals filter to specify how the collections match
    toyFilter = ee.Filter.equals(leftField = 'farm_id',rightField = 'farm_id')

    # define the join
    innerJoin = ee.Join.inner()

    # apply the join
    Joined = innerJoin.apply(ee_object, collection_with_latlon,  toyFilter)

    def cleanJoin(feature):
        return ee.Feature(feature.get('primary')).copyProperties(feature.get('secondary'));

    Joined = Joined.map(cleanJoin)

The wanted output contains a merge of the different years. Unfortunately, the output featurecollection only gives me one year instead of all the years merged in one featurecollection. What am I doing wrong?

1 Answer 1

1

You're using a local GeoJSON file in your script, so it's not executable for others. You also have quite a lot going on in this script that it makes it harder to decipher what exactly you're trying to do. You should try to boil scripts in your questions down to the pure essence of what you're asking.

Maybe you want to do something like this?

years = range(2010, 2020)

# The collection you want to populate
collection = ee.FeatureCollection([])

for year in years:
    # Generate a feature collection for the year in some manner
    year_collection = ee.FeatureCollection([ee.Feature(None, {'year': year})])
    
    # Merge your collection
    collection = collection.merge(year_collection)
    
# Here you've got features from all years    
print(collection.aggregate_array('year').getInfo())

Though I think I like this approach better, where collection isn't being reassigned:

years = range(2010, 2020)

def collection_for_year(year):
    return ee.FeatureCollection([ee.Feature(None, {'year': year})])

collection = ee.FeatureCollection([
    collection_for_year(year) 
    for year in years
]).flatten()

# Here you've got features from all years    
print(collection.aggregate_array('year').getInfo())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.