TransWikia.com

Editing list of strings using Google Earth Engine?

Geographic Information Systems Asked by Inacio Bueno on February 26, 2021

I’m trying to edit string elements in a list object. I need to get a list of IDs of a image collection, and then replace the orbit row to another (in this case, 073 to 074). I’m new to GEE and got stucked creating the .map() function. Does anyone know how can I solve this?

var landsat = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
    .filterDate('2008-01-01', '2011-12-31')
    .filter(ee.Filter.calendarRange(6,7,'month'))
    .filter(ee.Filter.eq('WRS_PATH', 222))
    .filter(ee.Filter.eq('WRS_ROW', 73))
    .filterMetadata('CLOUD_COVER', 'equals', 0)

var names = ee.List(landsat.aggregate_array('system:index'));
print(names)

var replace = function(i){
  var size = (i.size().subtract(1)) // get list size
  var imgs = ee.List.sequence(0, 11) // creates list sequence to iterate
  var replace = imgs.map(function(j) {
    var str = ee.String(i.get(j)) // convert each element to a string
    var str2 = str.replace('222073','222074')
    var newlist = []
    return newlist.add(str2)
  })
} 
var newnames = names.map(replace)

What I have:

  1. LT05_222073_20080604
  2. LT05_222073_20080706
  3. LT05_222073_20080722
  4. LT05_222073_20100626
  5. LT05_222073_20110715

What I need:

  1. LT05_222074_20080604
  2. LT05_222074_20080706
  3. LT05_222074_20080722
  4. LT05_222074_20100626
  5. LT05_222074_20110715

One Answer

When you map over names, which is a list of the ids, the argument of the function would be each element of that list, thus the name. So you'd only need to rename that name

var landsat = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
    .filterDate('2008-01-01', '2011-12-31')
    .filter(ee.Filter.calendarRange(6,7,'month'))
    .filter(ee.Filter.eq('WRS_PATH', 222))
    .filter(ee.Filter.eq('WRS_ROW', 73))
    .filterMetadata('CLOUD_COVER', 'equals', 0)

var names = ee.List(landsat.aggregate_array('system:index'));
print(names)

var replace = function(name){
  return ee.String(name).replace('073', '074')
} 
var newnames = names.map(replace)
print(newnames)

link

Answered by Rodrigo E. Principe on February 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP