3

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
  • 10,794
  • 3
  • 34
  • 53
Lacococha
  • 128
  • 8

2 Answers2

1

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).

Lukasz Tracewski
  • 10,794
  • 3
  • 34
  • 53
0

It seems like you want to download data from earth engine to then use them with numpy. You are doing two things wrong here:

  1. You are treating Google Earth Engine as a download service. This is not the purpose of Earth Engine. If you want to download big amounts of data (like in your case a year of Terra Surface Reflectance) you should download them directly from the service providers. The only thing you should download from Earth Engine are the end-results of your analysis which you conducted within Earth Engine.

  2. .getInfo does not get you the satellite data, it will only get you the metadata of your ImageCollection in the form of a JSON Object. If you want to the actual raster data you would have to export it (which, as said in 1, you shouldn't do for this amount of data).

JonasV
  • 792
  • 5
  • 16