1

I have an image collection of 4 images and when I map a function to this it only picks up on two images.

I've created a simple version of the problem to hopefully make this more solvable.

# data to Filter IC
geom_meta_list = ee.List([7040632150, 7040707710, 7040632220]) 
WWF = ee.FeatureCollection("WWF/HydroSHEDS/v1/Basins/hybas_4").filter(ee.Filter.inList('HYBAS_ID',geom_meta_list))
geometry = WWF.geometry()

date_start = '2014-08-01'
date_end = '2014-08-30'


#Load IC
ls = TOA = ee.ImageCollection("LANDSAT/LC08/C01/T1").filterDate(date_start,date_end).filterBounds(geometry)

#check how many images
# ls.size().getInfo() # returns 4

#simple function
def test_func(image):
    print('one')

    
#ERRORS are here:  
ls.map(test_func) # one one (as opposed to one one one one)
# ls.map(lambda x:test_func(x)) # tried to see if this would make any difference but no luck 


#proves there are 4 seperate images
list_images = ls.toList(4)
for i in range(0,4):
    image = ee.Image(list_images.get(i))
    image_ID = image.get('LANDSAT_PRODUCT_ID')
    print(image_ID.getInfo())

When it comes to changing IC's to bands it responds with EEException: ImageCollection.toBands: Error in map(ID=LC08_038035_20171229): Image.select: Invalid band number (1) specified to select. Input only contains 0 bands.

Suggesting its missing these images from the IC after the map function occurred. I have some very similar code that runs with no issues. Is it something to do with how it uses scenes causing this issue?

I'm using the Python API on Jupyter.

1 Answer 1

1

Mapping over a collection happens on the server; you cannot use (or see the results of) a print inside a mapped function. The server is applying your function to all the images, but your function just isn't doing anything or returning anything.

See also: https://developers.google.com/earth-engine/guides/client_server#looping https://developers.google.com/earth-engine/guides/ic_mapping

1
  • Thank you, that was a problem with my example I used in an attempt to understand the problem rather than the problem itself but it helped! I was getting the exception at the end not because of the date calls inside it (no print/getInfo statements) but because these were used to filter another image collection. In some cases these resulted in trying to carry out toBands() on this empty image collection and then addBands to the original image which was causing the issues.
    – svollowork
    Jun 17, 2021 at 9:08

Your Answer

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

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