1

I'm trying to create an array from an EE image, following the accepted answer in this post. However, instead of grabbing a specific image, I start with an EE imageCollection, then reduce it to an image (taking the median across cloud free pixels). When grabbing a specific image, I get the expected shape of the np array; however, I'm not able to get the expected shape when starting with an imageCollection.

(This is a similar issue as described in this post, although I'm not sure of the solution in this context. In addition, this post gets close to what I'm looking for -- but returns a 1d array, not a 2d array).

#### Set up
import ee
import numpy as np
import geetools
from geetools import ui, cloud_mask

ee.Authenticate()
ee.Initialize()

#### Create AOI
aoi = ee.Geometry.Polygon(
  [[[-110.8, 44.7],
    [-110.8, 44.6],
    [-110.6, 44.6],
    [-110.6, 44.7]]], None, False)

#### Create np array, starting from image
# This works!

img = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_038029_20180810') 

band_arrs = img.sampleRectangle(region=aoi)
band_arr_b1 = band_arrs.get('B1')
np_arr_b1 = np.array(band_arr_b1.getInfo())
np_arr_b1.shape

# Returns --> (373, 531)


#### Create np array, starting from imageCollection
# This doesn't seem to work

mask_l8SR_all = cloud_mask.landsatSR()

img = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')\
  .filterDate('2017-01-01', '2020-12-31')\
  .map(mask_l8SR_all)\
  .median()\
  .multiply(0.0001)

band_arrs = img.sampleRectangle(region=aoi)
band_arr_b1 = band_arrs.get('B1')
np_arr_b1 = np.array(band_arr_b1.getInfo())
np_arr_b1.shape

# Returns --> (1, 1)

Google Colab link to the above code

1 Answer 1

0

SampleRectangle samples in the projection of the image; composites don't have a projection. Assign one with setDefaultProjection.

collection = (ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
  .filterDate('2017-01-01', '2020-12-31')
  .map(mask_l8SR_all))
img = (collection.median()
  .multiply(0.0001)
  .setDefaultProjection(collection.first().projection()))

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.