0

I got a list of partial PRODUCT_IDS, like:

var partials = ['S2B_MSIL2A_20210807T075609', 'S2B_MSIL2A_20210827T075609']

How do I filter a collection with that list?

There are 2 functions:

var s2_sr_col = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
    .filterBounds(g)
    .filter(ee.Filter.inList('PRODUCT_ID', IMG_LIST))

-- this one only filters by full PRODUCT_ID strings

and:

var s2_sr_col = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
        .filterBounds(g)
        .filter(ee.Filter.stringContains('PRODUCT_ID', partial_id))

--- this only filters by one partial id

How do I merge the both together so it would filter by list of partial names?

When I do something like this:

var s2_sr_coln = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterBounds(g)
var final_col = s2_sr_coln.filter(ee.Filter.stringContains('PRODUCT_ID', IMG_LIST[0]))
for(var i=1; i < IMG_LIST.length; i++){
  final_col = final_col.merge(s2_sr_coln.filter(ee.Filter.stringContains('PRODUCT_ID', IMG_LIST[i])));
}

it seems to work, but... the problem is that it changes system:index on every merge. And I need original indexes.

Output indexes are something like this (it adds something before them on every merge):

0: 1_1_1_1_1_1_1_1_1_1_1_1_20210807T075609_20210807T081506_T36MTB
1: 1_1_1_1_1_1_1_1_1_1_1_2_20210827T075609_20210827T081448_T36MTB
2: 1_1_1_1_1_1_1_1_1_1_2_20210921T075621_20210921T080228_T36MTB
3: 1_1_1_1_1_1_1_1_1_1_2_20210921T075621_20210921T082107_T36MTB
4: 1_1_1_1_1_1_1_1_1_2_20211220T080341_20211220T080337_T36MTB
5: 1_1_1_1_1_1_1_1_2_20220129T080201_20220129T081736_T36MTB
6: 1_1_1_1_1_1_1_2_20220129T080201_20220129T081736_T36MTB
7: 1_1_1_1_1_1_2_20220208T080101_20220208T081919_T36MTB
8: 1_1_1_1_1_2_20220409T075611_20220409T082104_T36MTB
9: 1_1_1_1_2_20220519T075621_20220519T082029_T36MTB
10: 1_1_1_2_20220618T075621_20220618T081516_T36MTB
11: 1_1_2_20220628T075621_20220628T081518_T36MTB
12: 1_2_20220708T075621_20220708T081518_T36MTB
13: 2_20220728T075621_20220728T081514_T36MTB

Should be:

0: 20210807T075609_20210807T081506_T36MTB
1: 20210827T075609_20210827T081448_T36MTB
2: 20210921T075621_20210921T080228_T36MTB
3: 20210921T075621_20210921T082107_T36MTB
4: 20211220T080341_20211220T080337_T36MTB
5: 20220129T080201_20220129T081736_T36MTB
6: 20220129T080201_20220129T081736_T36MTB
7: 20220208T080101_20220208T081919_T36MTB
8: 20220409T075611_20220409T082104_T36MTB
9: 20220519T075621_20220519T082029_T36MTB
10: 20220618T075621_20220618T081516_T36MTB
11: 20220628T075621_20220628T081518_T36MTB
12: 20220708T075621_20220708T081518_T36MTB
13: 20220728T075621_20220728T081514_T36MTB

1 Answer 1

1

You should't use for loops with Earth Engine, and you definitely shouldn't merge() a bunch of times. Instead, build a collection of collections and flatten() the result.

var s2 = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED').filterBounds(geometry)
var partials = ['S2B_MSIL2A_20210807T075609', 'S2B_MSIL2A_20210827T075609']
var result = partials.map(function(str) {
  return s2.filter(ee.Filter.stringContains('PRODUCT_ID', str))
})
result = ee.FeatureCollection(result).flatten()
result = ee.ImageCollection(result)

Beware,

  1. stringContains and stringStartsWith are both really expensive, because it can't use the index.
  2. The filter might return more than one result.
1
  • This actually works, thank You very much! PS. I tried something similar, but got an error with types, Your last 2 lines fixed it. Commented May 25 at 8:53

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.