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

asked Jun 12, 2021 at 23:30

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()))
answered Jun 15, 2021 at 10:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.