TransWikia.com

GEE 'sampleRectangle()' returning 1x1 array

Geographic Information Systems Asked by gdaldegan on December 22, 2020

I’m facing an issue when trying to use sampleRectangle() function in GEE, it is returning 1×1 arrays and I can’t seem to find a workaround. See below a Python code in which I’m using an approach posted by Justin Braaten. I suspect there’s something wrong with the geometry object I’m passing to the function, but at the same time I’ve tried several ways to check how this argument is behaving and couldn’t no spot any major issue.

Can anyone give me a hand trying to understand what is happening?

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

ee.Initialize()


point = ee.Geometry.Point([-55.8571, -9.7864])

box_l8sr = ee.Geometry(point.buffer(50).bounds())
box_l8sr2 = ee.Geometry.Polygon(box_l8sr.coordinates())
# print(box_l8sr2)

# Define an image.
# l8sr_y = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_038029_20180810')
oli_sr_coll = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')

## Function to mask out clouds and cloud-shadows present in Landsat images
def maskL8sr(image):
  ## Bits 3 and 5 are cloud shadow and cloud, respectively.
    cloudShadowBitMask = (1 << 3)
    cloudsBitMask = (1 << 5)
  ## Get the pixel QA band.
    qa = image.select('pixel_qa')
  ## Both flags should be set to zero, indicating clear conditions.
    mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
    mask = qa.bitwiseAnd(cloudsBitMask).eq(0)
    return image.updateMask(mask)

l8sr_y = oli_sr_coll.filterDate('2019-01-01', '2019-12-31').map(maskL8sr).mean()

l8sr_bands = l8sr_y.select(['B2', 'B3', 'B4']).sampleRectangle(box_l8sr2)
print(type(l8sr_bands))
# Get individual band arrays.
band_arr_b4 = l8sr_bands.get('B4')
band_arr_b3 = l8sr_bands.get('B3')
band_arr_b2 = l8sr_bands.get('B2')


# Transfer the arrays from server to client and cast as np array.
np_arr_b4 = np.array(band_arr_b4.getInfo())
np_arr_b3 = np.array(band_arr_b3.getInfo())
np_arr_b2 = np.array(band_arr_b2.getInfo())
print(np_arr_b4.shape)
print(np_arr_b3.shape)
print(np_arr_b2.shape)

# Expand the dimensions of the images so they can be concatenated into 3-D.
np_arr_b4 = np.expand_dims(np_arr_b4, 2)
np_arr_b3 = np.expand_dims(np_arr_b3, 2)
np_arr_b2 = np.expand_dims(np_arr_b2, 2)
# # print(np_arr_b4.shape)
# # print(np_arr_b5.shape)
# # print(np_arr_b6.shape)

# # Stack the individual bands to make a 3-D array.
rgb_img = np.concatenate((np_arr_b2, np_arr_b3, np_arr_b4), 2)
# print(rgb_img.shape)

# # Scale the data to [0, 255] to show as an RGB image.
rgb_img_test = (255*((rgb_img - 100)/3500)).astype('uint8')
# plt.imshow(rgb_img)
plt.show()

# # # create L8OLI plot
# fig, ax = plt.subplots()
# ax.set(title = "Satellite Image")
# ax.set_axis_off()
# plt.plot(42, 42, 'ko')
# img = ax.imshow(rgb_img_test, interpolation='nearest')

One Answer

sampleRectangle uses the input band's projection to determine the sampling resolution, and when you create a composite using .mean(), the result's projection is the default projection of WGS 84 with one degree scale, and your sampling area is much smaller than one degree.

To fix this, override the default projection with the projection of one of the images; change

l8sr_y = oli_sr_coll.filterDate('2019-01-01', '2019-12-31').map(maskL8sr).mean()

to

range_coll = oli_sr_coll.filterDate('2019-01-01', '2019-12-31')
l8sr_y = (range_coll
    .map(maskL8sr)
    .mean()
    .setDefaultProjection(range_coll.first().projection()))

This will use the projection of the first image in the filtered collection, which is reasonable assuming that they all have the same projection. In a case where the images do not have the same projection, you would have to consider which projection you actually want your sampled pixels to be in.


While I was working on understanding the question, I wrote this EE Code Editor version of key parts of your code. It doesn't particularly illustrate the actual problem, but I'm leaving it here in case it's of interest.

Correct answer by Kevin Reid on December 22, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP