How to solve an error in downloading Google Earth Engine image in Streamlit?

Ask the community or our support engineers for answers to questions.

Hello everyone, I would like to download the fire data on GeoTiff format from Google Earth Engine. I am struggling to perform a download button. It gives me an error with " Invalid binary data format: <class ‘ee.image.Image’>". How can I make it work?Thank you! I would attach the error message and the code in the following:

import datetime

import ee

import streamlit as st

import geemap.foliumap as geemap

import os

import folium

Map = geemap.Map(locate_control=True)

    col1, col2, col3, col4, col5= st.columns([1, 1, 1, 2, 2])
    with col1:
        longitude = st.number_input("Longitude", 102, 110, 105)
    with col2:
        latitude = st.number_input("Latitude", 10, 16, 12)
    with col3:
        zoom = st.number_input("Zoom", 0, 20, 7)

    Map.setCenter(longitude, latitude, zoom)

    with col4:
        start = st.date_input("Start Date for Fire Forest: YYYY/MM/DD", datetime.date(2021, 1, 1))
    with col5:
        end = st.date_input("End Date for Fire Forest: YYYY/MM/DD", datetime.date(2021, 1, 3))

    start_date = start.strftime("%Y-%m-%d")
    end_date = end.strftime("%Y-%m-%d")

    countries=ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017")
    country = countries.filter(ee.Filter.eq('country_na', 'Cambodia'));
    esa = ee.ImageCollection("FIRMS").select('T21').filterBounds(country).filterDate(start_date,end_date).mosaic().clip(country)
    esa_vis = {"min": 325,"max": 400,"palette": ['red', 'orange', 'yellow'],}
    Map.addLayer(country,{}, name ="Cambodia Global Boundary")
    Map.addLayer(esa, esa_vis, 'fire')

    #To test export image
    out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
    filename = os.path.join(out_dir, 'fire.tiff')
    # image = esa.clip(country).unmask()
    date=(start_date,end_date)
    geemap.ee_export_image(esa, filename=filename, scale=30, region=country, file_per_band=False)
    st.download_button('Download fire Output (.tiff)',
                    esa,
                    file_name = "fire.tiff",
                    mime= "image/GeoTIFF")

    #Add legend
    labels = ['Fire Detection']
    colors = ['#FF0000']
    Map.add_legend(
                title="Legend",
                labels=labels,
                colors=colors)

    #Press on map and show lat and lng
    Map.add_child(folium.LatLngPopup())

    Map.to_streamlit(height=550)

Hi @yputhealy,

Instead of using st.download_button you could just create a Streamlit button and set it to download the data when clicked. I’m not sure if st.download_button supports all file formats, so that may be causing the error you’re running into.

I shared a code snippet in response to your earlier post about this same issue that seems to download this type of file. Again, I think you could just add a Streamlit button to that script and that would probably give you the same functionality you’re looking for.

Thank You for your convenient time in trying to solve this problem. Even, I try to add only st.download_button the problem is still there. I hope I have another solution.

Hi @yputhealy, my recommendation was to add an st.button and use the example snippet that I shared. It doesn’t look like you’ve tried that.

Thank You for quick response! As I review on that code snippet, it is used for a Geoserver data. In my case, it is from Google Earth Engine dataset. Please correct me if I was misunderstanding.

Thanks for clarifying. One way you could use the st.download_button would be to convert the file to a .png or .jpg or some other supported filetype.

Otherwise, if you can get a URL for the file, you could do something like this.

geemap seems to recommend the following for downloading the file:

out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
filename = os.path.join(out_dir, 'landsat.tif')
type or paste code here

You could probably combine the above snippet with some type of button (not the st.download_button) to download the file.

Thanks for your recommendation. Actually, I cannot make it, however I see an example with the download button from Google Earth Engine model through https://skd862-fier-mekong-demo-vlobm9.streamlitapp.com . The model is so complicated and I recently cannot figure it out on how I can replicate the download button in my simple geemap. If you could spend sometimes on the app, could you please guide me on that.