TransWikia.com

Error trying to cast an ee.Image to a numpy array

Geographic Information Systems Asked by panchester on December 9, 2020

I want to cast my ee.Image, to do so I followed this link implementation, but I got this error:

Traceback (most recent call last):
  File "/home/felipe/.local/lib/python3.6/site-packages/ee/data.py", line 338, in _execute_cloud_call
    return call.execute(num_retries=num_retries)
  File "/home/felipe/.local/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper 
    return wrapped(*args, **kwargs)
  File "/home/felipe/.local/lib/python3.6/site-packages/googleapiclient/http.py", line 898, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/value:compute?prettyPrint=false&alt=json returned "Image.sampleRectangle: Fully masked pixels / pixels outside of the image footprint when sampling band 'soilDiff' with no default value set. Note that calling sampleRectangle() on an image after ee.Image.clip() may result in a sampling bounding box outside the geometry passed to clip().">

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "exportImgs_v2.py", line 205, in <module>
    array = imageToArray(imgAlerta, rect)
  File "exportImgs_v2.py", line 41, in imageToArray
    print(band_arrs.getInfo())
  File "/home/felipe/.local/lib/python3.6/site-packages/ee/computedobject.py", line 95, in getInfo
    return data.computeValue(self)
  File "/home/felipe/.local/lib/python3.6/site-packages/ee/data.py", line 703, in computeValue
    prettyPrint=False))['result']
  File "/home/felipe/.local/lib/python3.6/site-packages/ee/data.py", line 340, in _execute_cloud_call
    raise _translate_cloud_exception(e)
ee.ee_exception.EEException: Image.sampleRectangle: Fully masked pixels / pixels outside of the image footprint when sampling band 'soilDiff' with no default value set. Note that calling sampleRectangle() on an image after ee.Image.clip() may result in a sampling bounding box outside the geometry passed to clip().

my exact code is the one below:

def imageToArray(img, geomet):
    band_arrs = ee.Image(img).sampleRectangle(ee.Geometry(geomet))
    print(band_arrs.getInfo())
    band_arr_b4 = band_arrs.get('eviDiff')
    band_arr_b5 = band_arrs.get('soilDiff')
    band_arr_b6 = band_arrs.get('mask')

    # Transfer the arrays from server to client and cast as np array.
    np_arr_b4 = np.array(band_arr_b4.getInfo())
    np_arr_b5 = np.array(band_arr_b5.getInfo())
    np_arr_b6 = np.array(band_arr_b6.getInfo())

    print(np_arr_b4.shape)
    print(np_arr_b5.shape)
    print(np_arr_b6.shape)

    np_arr_b4 = np.expand_dims(np_arr_b4, 2)
    np_arr_b5 = np.expand_dims(np_arr_b5, 2)
    np_arr_b6 = np.expand_dims(np_arr_b6, 2)

    rgb_img = np.concatenate((np_arr_b6, np_arr_b5, np_arr_b4), 2)
    print(rgb_img.shape)

    return rgb_img

Does anyone know how can I fix this?

One Answer

Is your AOI/geometry in projected or geographic coordinates? I was running into this same error trying to use sampleRectangle() on Sentinel-2 image composites, and when I reprojected the image to the same coordinate system of my AOI (which was in UTM coordinates), this error went away. I think the issue is that sampleRectangle() expects the region argument to have a geometry that is in the same coordinate system as the image. Unlike reduceRegion(), there is no option to specify the crs or crs_transform when getting your rectangle, so it seems like you might need to reproject the image (easier) or the AOI geometry (just a bit harder).

Answered by David Diaz on December 9, 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