0

I have a CSV file that has 30 years of annual data that relates to my ROI. For the analysis I am doing, I want to turn this CSV file into an ImageCollection with each image being a single year having the the constant value from that year in each pixel of my roi. I could make a single image as follows:

var image = ee.Image.constant(5.5)//or whatever value
  .clip(geometry)
  .addBands(ee.Image.constant(2018).rename('year'))
  
Map.addLayer(image)

I could manually make each image and create an ImageCollection from them but I would like to automate the process or write a function so I can do this for various climate data files. Is there a way to do this? I currently use the JavaScript API.

1 Answer 1

0

You just need to make it into a function to apply to each feature in your table (FeatureCollection). The function should return an ee.Image, and when you map it to your table, make it into an ee.ImageCollection:

var imageCollection = ee.ImageCollection(
  table.map(function(feature){
    return ee.Image(
      ee.Number(
        feature.get("value")
        )
    ).rename("value")   
    .addBands(ee.Image(ee.Number(feature.get("year"))).rename("year")) // year as a band.
    .set({year:feature.get("year")})  // year as a property 
    .clip(geometry)
  })
)

I also added the option to set the "year" as a property, which might be useful instead of having it as an additional band.

Example using 10 random values from 2011 to 2020 (10 years):

https://code.earthengine.google.com/8f2fcfe18e1825ffbed9e2bdf3465a0e

1
  • Thanks! This worked great. The only change I needed to make for my specific table was to explicitly cast the value and year bands, i.e. .cast({'value': ee.PixelType('float', -31, 31)})
    – rebeccalm
    Commented Jun 12, 2023 at 20:19

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.