GEE do not import data into array

Lacococha :

I am having problems importing data from Google Earth Engine to a local array using Python API.

A simplified version of my code:

import ee
ee.Initialize()

#Load a collection
TERRA = ee.ImageCollection("MODIS/006/MOD09A1").select(['sur_refl_b02', 'sur_refl_b07',"StateQA"])
TERRA = TERRA.filterDate('2003-01-01', '2019-12-31')

#Extract an image
TERRA_list = TERRA.toList(TERRA.size())
Terra_img = ee.Image(TERRA_list.get(1))

#Load as array
Terra_img = Terra.get('sur_refl_b02')
np_arr_b2 = np.array(Terra_img.getInfo())

But np_arr_b2 seems to be empty

Does anybody know what am I doing wrong?

Thanks!

Lukasz Tracewski :

You are not far from the goal, at least to a certain extent. There's a limit to how many pixels can be transferred over such a request, namely 262144. Your image, when taken over the whole globe (like you are doing), has 3732480000 - over 10000x too many. Still, you can sample a small area and put in the numpy:

import ee
import numpy as np
import matplotlib.pyplot as plt

ee.Initialize()

#Load a collection
TERRA = ee.ImageCollection("MODIS/006/MOD09A1").select(['sur_refl_b02', 'sur_refl_b07',"StateQA"])
TERRA = TERRA.filterDate('2003-01-01', '2019-12-31')

#Extract an image
TERRA_list = TERRA.toList(TERRA.size())
Terra_img = ee.Image(TERRA_list.get(1))
img = Terra_img.select('sur_refl_b02')

sample = img.sampleRectangle()
numpy_array = np.array(sample.get('sur_refl_b02').getInfo())

It's an area over Wroclaw, Poland, and looks like this when passed to matplotlib via imshow:

satellite image

What if you really need the whole image? That's where Export.image.toDrive comes into play. Here's how you'd download the image to the Google Drive:

bbox = img.getInfo()['properties']['system:footprint']['coordinates']
task = ee.batch.Export.image.toDrive(img, 
    scale=10000,
    description='MOD09A1',
    fileFormat='GeoTIFF',
    region=bbox)
task.start()

After the task is completed (which you can monitor also from Python), you can download your image from Drive and access it like any other GeoTIFF (see this GIS Stack Exchange post).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=293450&siteId=1