Google Earth Engine – Offset FeatureCollection Limit

google-earth-engine

I am trying to loop over my Feature Collection in steps of 500. My FeatureCollection contains over 30,000 features and I want to offset the features obtained by 500.

What I want (pseudocode):

for i in fc_step_of_500: 
fc[i:500+i]

Currently I am able to limit the collection like this

fc.limit(500)

But I want to offset on every iteration.

I have looked into iterate method, which essentially loops over every feature. But that is not what I want in this case.

Best Answer

Collections aren't designed for random access; any method you use to get there will severely limit the scalability of your code. That said, you can get to positional chunks by converting to a list, since toList has an offset argument.

collection.toList(count, offset)

A more scalable solution, if you don't mind them being non-contiguous, is to assign a random number to each feature and then iteratively filter to a subset that's approximately 500 items:

collection.randomColumn().filter('random < 0.15')

However, if you can explain why you want batches in the first place, there's almost certainly a better answer.