2

Given a FeatureCollection that contains multiple Features (each consisting of polygons), I would like to use the Python API to loop through them all, and batch export them to Google Cloud Storage.

However, I can't figure out how to make all the code server-side, and hence my code runs extremely slowly (using the major no-no of getInfo inside a loop).

for i in range(0, 1000):
  region = ee.Feature(myFeatCol.get(i)).geometry()

  task = ee.batch.Export.image.toCloudStorage(
    image = naip.clip(region),
    description = 'uniqueName'+str(i),
    fileNamePrefix = 'uniqueName'+str(i),
    bucket = 'myGoogleBucketName',
    scale = 1,
    region = region.getInfo()['coordinates'])

  task.start()

The objective here is to end up with 1000 images in the bucket. It works, but is painstakingly slow! Several-minutes-per-image slow.

How can I create an efficient server-side loop to export an entire FeatureCollection?

0

1 Answer 1

2

Currently, there is no way to export a collection of images; an export task can only generate one image, and each export task requires a separate call to the server and, more importantly, will be scheduled and run separately.

(There is an export type that produces multiple images — Export.map — but it would be quite tedious and inconvenient to rearrange your data into fake map tiles, and it would end up with every image square and the same size, have a lot of extra images, incorrect metadata, and so on.)

2
  • Thanks for the quick response Kevin! I have tried creating a function and using .map to apply it to the whole FeatureCollection, but it fails to create unique names (as you expected)! Another idea I had was to preprocess the entire FeatureCollection with a single getInfo(), then reshape it into an ee.Dictionary, which I could then have GEE .map() over, but for some reason I still can't figure out the syntax to let GEE read it on the server side. "ee.Dictionary(myDict).get(feature.id())" returns the error "TypeError: Object of type 'ComputedObject' is not JSON serializable". Any ideas? Commented Oct 3, 2019 at 16:43
  • 1
    @curiouserand This is simply not possible. Export is a single API call, and you can't use a server-side map() to make more API calls. Even if it worked, it would still schedule separate export tasks and they would take the same amount of time.
    – Kevin Reid
    Commented Oct 3, 2019 at 19:45

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.