Google Earth Engine Python API – Converting ee.Image to Numpy Array Efficiently

google-earth-enginegoogle-earth-engine-python-apimatplotlibnumpy

I am using Earth Engine's Python API to convert ee.Image() to Python plots. I am currently using the ee.sampleRectangle() function to convert ee.Image() to a NumPy array and then using matplotlib's imshow() to generate the plot and save them as PNG.

What I have found is that even for the smallest of regions it takes atleast 1 sec for the conversion to NumPy arrays. I have lakhs of images I want to convert. Is there a better way to avoid the bottleneck I am facing?

Best Answer

sampleRectangle() is not the ideal way to retrieve image pixels from Earth Engine, because they'll end up converted to JSON and back. Try using ee.Image.getDownloadURL() — this can give you “NumPy binary format” data directly. (I'm not familiar with NumPy to tell you how exactly to use that, but I assume it'll suit your needs.)

That said, if your problem is the speed independent of region size, you might find it useful to modify your program to request multiple images in parallel rather than one at a time. (Not too many or your requests will be rejected, but it's entirely normal to do that at all — for example, the Earth Engine Code Editor requests many individual image tiles to make up the interactive map.)

Related Question