0

My program needs a numpy array for processing, so I convert GEE image into numpy array using this code:

# Define an image.
img1 = ee.Image( 'LANDSAT/LC08/C01/T1_SR/LC08_038029_20180810') \
  .select(['B2','B3','B4', 'B5', 'B6'])
new=img1.toArray().toArray(1)
# Define an area of interest.
aoi = ee.Geometry.Polygon(
  [[[-110.8, 44.7],
    [-110.8, 44.6],
    [-110.6, 44.6],
    [-110.6, 44.7]]], None, False)

# Get 2-d pixel array for AOI - returns feature with 2-D pixel array as property per band.
band_arrs = img1.sampleRectangle(region=aoi)

# Get individual band arrays.
band_arr_b2 = band_arrs.get('B2')
band_arr_b3 = band_arrs.get('B3')
band_arr_b4 = band_arrs.get('B4')


# Transfer the arrays from server to client and cast as np array.
np_arr_b2 = np.array(band_arr_b2.getInfo())
np_arr_b3 = np.array(band_arr_b3.getInfo())
np_arr_b4 = np.array(band_arr_b4.getInfo())
# Expand the dimensions of the images so they can be concatenated into 3-D.
np_arr_b2 = np.expand_dims(np_arr_b2, 2)
np_arr_b3 = np.expand_dims(np_arr_b3, 2)
np_arr_b4 = np.expand_dims(np_arr_b4, 2)
# Stack the individual bands to make a 3-D array.
rgb_img = np.concatenate((  np_arr_b4,np_arr_b3,np_arr_b2), 2)

Now can I go back from numpy array to GEE image?

4
  • I don't think this is possible, at least in any efficient way. You probably need to upload it as a GeoTIFF. May 13, 2020 at 8:50
  • you mean first i have to export it into drive as geotif May 13, 2020 at 9:06
  • I mean, you have to turn your numpy array to a GeoTIFF, uploaded it to Google Cloud Storage, and from there, upload it as an Earth Engine asset. I recall there are some command line tools for helping out with the two upload steps though. May 13, 2020 at 9:15
  • Yes, it's very fiddly. You can of course do the upload manually through the Code Editor too. May 13, 2020 at 9:16

1 Answer 1

0

Here I am uploading code to convert GEE image to array and then again convert that array to GEE image, Instead of Image you can select band to do process with the converted band array and then convert it into the image as you said your requirement.

  • As we have image collection and converted it into an array

    var array = image.toarray();
    
  • sort array by the first band, keeping other bands

  • After converting into an array you can do your required process with the array, as I have done below.

    var axes = { image:0, band:1 }
    var sort = array.arraySlice(axes.band, 0, 1); 
    
  • select bands from index 0 to 1` (NDVI)

    var sorted = array.arraySort(sort);
    
  • take the last image only (MAX value)

  • for the max value sorted

    var length = sorted.arrayLength(axes.image);
    var valuesMax = sorted.arraySlice(axes.image, length.subtract(1), length);
    
  • Now convert that sliced array back to image.

    var max = valuesMax.arrayProject([axes.band]).arrayFlatten([['max', 'time']])
    

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.