google-earth-engine – How to Name Automatically an Image to Export to Drive in GEE

google-earth-enginegoogle-earth-engine-javascript-api

I am trying to export to drive an image in GEE. I have created a string made from other variables and I would like to use it as name of the file.

If I print the variable as a string, I get the name I would like to have for my file. But if I use it to name the file in the export to drive the name results something like:

ee.Image({ "type": "Invocation", "arguments": { "collection": { "type": "Invocation", "arguments": { "collection": { "type": "Invocation",

A script reproducing the error is the following:

https://code.earthengine.google.com/18768850e984659e9a3f409b02ad5835

var geometry = ee.Geometry.Point([-105.483203125, 41.85704089594469]);

var date1=ee.Date('2018-01-01')

var date2= ee.Date('2023-01-01')

var BUFF= 10000 

var AOI=geometry.buffer(BUFF)

var nameSite='NameSite'

var name = ee.String(nameSite).cat('_').cat(date1.get('year')).cat('_').cat(date2.get('year')).cat('_').cat(ee.String(ee.Number(BUFF/1000))).cat('km')

print(name)

var img = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
                  .filterDate('2020-01-01', '2020-01-30').filterBounds(AOI).first()


Export.image.toDrive({
  image: img,
  description: img,
  scale: 30,
  crs:  'EPSG:4326',
  maxPixels: 1e10, 
  region: AOI,
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true
  }
});

What I am doing wrong here?

Best Answer

I have a solution for you:

After a quick browser search I came across: Converting ee:Date object to client string in Google Earth Engine

I would recommend to use the .getInfo() Method to return a new variable like OutputName with the information of your string. Here is your modified code:

var geometry = ee.Geometry.Point([-105.483203125, 41.85704089594469]);

var date1=ee.Date('2018-01-01')

var date2= ee.Date('2023-01-01')

var BUFF= 10000 //10 km

var AOI=geometry.buffer(BUFF)

var nameSite='NameSite'

var ImageName = ee.String(nameSite).cat('_').cat(date1.get('year')).cat('_').cat(date2.get('year')).cat('_').cat(ee.String(ee.Number(BUFF/1000))).cat('km');
var OutputName = ImageName.getInfo();

print(OutputName)


var img = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
                  .filterDate('2020-01-01', '2020-01-30').filterBounds(AOI).first()


Export.image.toDrive({
  image: img,
  description: OutputName,
  fileNamePrefix: OutputName,
  scale: 30,
  crs:  'EPSG:4326',
  maxPixels: 1e10, 
  region: AOI,
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true
  }
});

When I run the Code in the GEE it correctly assigns automatically the desired name from the variable ImageName (I renamed your variables). I think this is what you wanted.

Related Question