应用结果
代码
#导入安装包
import os
import json
import datetime
import streamlit as st
import streamlit.components.v1 as components
import traceback
from PIL import Image
import aie
#读取当前目录的内容
current_work_dir = os.path.dirname(__file__)
#添加地图
collection_id = 'TJNU_3JECO_CHINA_DDEP_V01'
# 隐藏页面右上角的action bar按钮
hide_streamlit_action_button = """
<style>
#MainMenu {visibility: hidden;}
</style>
"""
#设定地图分布,页面标题设定名称
st.set_page_config(layout="wide", page_title="AIE AppHub")
st.markdown(hide_streamlit_action_button, unsafe_allow_html=True)
# 调整边距 设定边距
st.markdown(" <style> div[class^='css-1544g2n'] { padding-top: 1px; } </style> ", unsafe_allow_html=True)
st.markdown(" <style> div[class^='block-container'] { padding-top: 1px; } </style> ", unsafe_allow_html=True)
# aie的初始化应用分析
def aie_init():
'''
AIE开发者SDK初始化
请登录后访问https://engine-aiearth.aliyun.com/#/utility/auth-token 获取认证token,
并将token设置为系统环境变量(变量名为SDK_TOKEN),以便下述代码获取并初始化SDK。
'''
token = os.environ.get("SDK_TOKEN") # 请至应用空间设置-环境变量中添加SDK_TOKEN
aie.Authenticate(token=token)
aie.Initialize()
#这里我们需要让下面函数每一次进行运行,防止过渡运行。
@st.cache_resource
def get_default_aie_map():
aie_map = aie.Map(
center=[116.383331,39.916668],
height=800,
zoom=4)
return aie_map.to_html()
# 定义初始化的函数
def page_reset_callback():
st.session_state['region_province_select'] = '请选择'
st.session_state['time_picker'] = [datetime.date(2020, 1, 1), datetime.date(2020, 1, 31)]
st.session_state['render_map_html'] = get_default_aie_map()
#这里设定一个数据筛选函数,包含了地图矢量、底图,研究区,和图层
def aie_map_layer_assemble(year, month,parameter):
# 拼接AIE 数据查询SDK语句
aie_init()
feature_collection = aie.FeatureCollection('China_Province')
aie_map = aie.Map(
center=feature_collection.getCenter(),
height=800,
zoom=3)
region_vis_params = {
'color': '#00FF00'
}
aie_map.addLayer(
feature_collection,
region_vis_params,
'行政区划边界',
bounds=feature_collection.getBounds()
)
year_mon = year + month
image = aie.Image('ERA5_LAND_MONTHLY_' + year_mon ).clipToCollection(feature_collection)
#这里是复选框内容的选项
if parameter=="temperature_2m" or parameter=="dewpoint_temperature_2m" or parameter=="soil_temperature_level_1" or parameter=="soil_temperature_level_2" or parameter=="soil_temperature_level_3" or parameter=="soil_temperature_level_4":
para_image = image.select([parameter]).subtract(aie.Image.constant(273.15))
#这里我们选择后让其影像转化为摄氏度℃
#设定可视化参数
vis_temp = {
'min': -10,
'max': 30,
'palette': [
"#000080","#0000D9","#4000FF","#8000FF","#0080FF", "#00FF80",
"#00FF80","#80FF00","#DAFF00","#FFFF00","#FFF500","#FFDA00",
"#FFB000","#FFA400","#FF4F00","#FF2500","#FF0A00","#FF00FF",
]
}
#设定图层
aie_map.addLayer(
para_image,
vis_temp,
parameter +'(°C)',
bounds=para_image.getBounds()
)
### 绘制图例 设定图例,这里从ipyleaflet导入图例
from ipyleaflet import LegendControl
#这里图例展示和颜色
legend = LegendControl({
' < -10 ':'#000080',
'-10 - -5':'#