Google Earth Engine – Elevation Figure for Geometry Using Python API

google-earth-enginegoogle-earth-engine-python-api

I'm creating an elevation figure with Google Earth Engine for a geometry, but I'm getting an error on the image. At the time of plotting the figure is black and is not getting the elevation data of the geometry, and the legend is correct because I delimited it, but I would like this identification to be automatic.
Below I leave the image and the link.

The link to url: https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/6ff93197575e2983cab7d9022d883ca9-7689bd0b33bac148cb8089cfbac74943:getPixels

The image:
https://i.stack.imgur.com/p7syA.png


    # loading packages
    import ee
    import matplotlib.pyplot as plt
    from PIL import Image
    from io import BytesIO
    import matplotlib as mpl
    import requests
    ee.Initialize()
    
    # area
    geojson_data = {'type': 'Polygon',
     'coordinates': (((-54.33788537979125, -25.05490926196943),
       (-54.3358039855957, -25.05990486897294),
       (-54.33934450149536, -25.062159628776328),
       (-54.33292865753174, -25.064258850604656),
       (-54.32595491409302, -25.06381179711849),
       (-54.32370185852051, -25.062781624178893),
       (-54.323294162750244, -25.06105169164402),
       (-54.32368040084839, -25.058835787325044),
       (-54.32531118392944, -25.054695437669956),
       (-54.33788537979125, -25.05490926196943)),)}
    
    roi = ee.Geometry(geojson_data)
    
    # loading data
    elevation = ee.Image('NASA/NASADEM_HGT/001').select('elevation').clip(roi)
    
    #geometry to fc to processing
    fc = ee.FeatureCollection(roi)
    
    url = elevation.getThumbURL({'min': 240, 'max': 260, 'palette': ['00A600', '63C600', 'E6E600', 'E9BD3A', 'ECB176', 'EFC2B3', 'F2F2F2'],'dimensions': 1024,
                                  'region': fc.geometry(),
                                  'crs':'EPSG:4326',
                                  })
    images = []
    # Get image content from the URL.
    response = requests.get(url)
    image = Image.open(BytesIO(response.content))
    images.append(image)
    
    # plot
    fig  = plt.figure(figsize=(10,15))
    plt.imshow(images[0])
    plt.axis('off')
    
    fig.suptitle('Elevation ', fontsize=20)
    norm = mpl.colors.Normalize(vmin=220, vmax=260)
    plt.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap="RdYlGn"), orientation='vertical')
    fig.tight_layout()

Best Answer

You didn't provide a complete, running script. If you'd done that, it certainly would have make it easier to help you. Now, just glancing at your script, I see that you're actually not using the elevation at all, you're just rendering a static image with the value of 1:

elevation1 = ee.Image(ee.Image(1))

The way you create the (unused) elevation is also incorrect. NASA/NASADEM_HGT/001 is an image, not an image collection. It would look like this:

elevation = ee.Image('NASA/NASADEM_HGT/001').select('elevation').clip(roi)

To dynamically determine min/max:

min_max = elevation.reduceRegion(
    reducer=ee.Reducer.minMax(), 
    geometry=roi, 
    scale=10, # Tweak scale/bestEffort/maxPixels as you want
    bestEffort=True, 
    maxPixels=1e4    
).getInfo()
elevation_min = min_max['elevation_min']
elevation_max = min_max['elevation_max']