I am trying to extract an image patch from Earth Engine imagery. The location of the patch coincides with the edge of a single image, so I first mosaic two adjacent images to form a bigger image that includes the patch, and then use sampleRectangle to extract the image patch. However, this returns the error "EEException: Image.sampleRectangle: Fully masked pixels / pixels outside of the image footprint when sampling band 'R' with no default value set."

Any idea what I should do?

Here is my full code:

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

patch = ee.Geometry.BBox(-113.01284266119998,33.7460453388,-113.01014333879999,33.74874466120001)

mosaic_img = ee.ImageCollection('USDA/NAIP/DOQQ') \
        .filterDate('2007-05-01', '2007-07-01') \

band_arrs = mosaic_img.sampleRectangle(region=patch)

band_arr_R = band_arrs.get('R')
band_arr_G = band_arrs.get('G')
band_arr_B = band_arrs.get('B')

np_arr_R = np.array(band_arr_R.getInfo())
np_arr_G = np.array(band_arr_G.getInfo())
np_arr_B = np.array(band_arr_B.getInfo())

np_arr_R = np.expand_dims(np_arr_R, 2)
np_arr_G = np.expand_dims(np_arr_G, 2)
np_arr_B = np.expand_dims(np_arr_B, 2)

rgb_img = np.concatenate((np_arr_R, np_arr_G, np_arr_B), 2).astype('uint8')

1 Answer 1


sampleRectangle samples using the image's projection. In the case of a mosaic, there is no projection so it uses 1 degree/pixel WGS84 by default. In that projection, there's less than 1 whole pixel in the rectangle, so it is treated as empty, and it's complaining that there is no defaultValue set because that's what it's trying to use.

Just force the mosaic to have a default projection:

var collection = ee.ImageCollection('USDA/NAIP/DOQQ')
        .filterDate('2007-05-01', '2007-07-01')
var mosaic = collection.mosaic().setDefaultProjection(collection.first().projection())
  • This worked. Very helpful. Thank you!
    – Mohamad
    Commented Jun 9, 2021 at 16:13

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.