我想从 Google Earth Engine 下载 GeoTiff 格式的火灾数据。我正在努力执行下载按钮。它给我一个错误“无效的二进制数据格式:
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)
我使用了https://developers.google.com/earth-engine/apidocs/ee-image-getdownloadurl中存在的逻辑。
我的代码如下所示:
# Function to export the image to a GeoTIFF file
def export_image(image, date):
try:
# Export the image directly to the deployment environment
url = image.getDownloadURL({
'name': f'image{date}',
'scale': 20,
'crs': 'EPSG:4674',
'region': roi.geometry().getInfo(),
'format': 'GEO_TIFF'
})
st.sidebar.success(f"Image exported for {date} successfully. Download [here]({url}).")
except Exception as e:
st.sidebar.error(f"Error exporting the image: {str(e)}")
...
# Button to trigger the data download
if st.sidebar.button("Download Data"):
# Check if the ROI is defined
if 'roi' in locals() and roi is not None:
# Export the selected collection to GeoTIFF files
for date in selected_dates:
# Filter the collection for the selected date
selected_collection_date = selected_collection.filter(ee.Filter.eq('data', date))
# Export the first image in the filtered collection
export_image(selected_collection_date.first(), date)
else:
st.warning("Please select an area of interest before downloading.")