import os import leafmap import solara import ipywidgets as widgets from leafmap import leafmap import leafmap.colormaps as cm zoom = solara.reactive(2) center = solara.reactive((20, 0)) maps = { 'Most limiting soil quality rating factor': 'https://s3.eu-west-1.amazonaws.com/data.gaezdev.aws.fao.org/LR/soi1/SQ0_mze_v9aH.tif', "Nutrient retention capacity": 'https://s3.eu-west-1.amazonaws.com/data.gaezdev.aws.fao.org/LR/soi1/SQ2_mze_v9aH.tif', "Rooting conditions" : 'https://s3.eu-west-1.amazonaws.com/data.gaezdev.aws.fao.org/LR/soi1/SQ3_mze_v9aH.tif', 'Soil and terrain suitability, rain-fed': 'https://s3.eu-west-1.amazonaws.com/data.gaezdev.aws.fao.org/LR/soi2/siHr_sst_mze.tif' } maps_list= list(maps) maps_values= list(maps.values()) legend_dict1 = { "NA": "#FFFFFF", # Branco "0.0 - 0.1": "#8B0000", # Vermelho escuro "0.1 - 0.2": "#FF4500", # Laranja avermelhado "0.2 - 0.3": "#FFA500", # Laranja "0.3 - 0.4": "#FFD700", # Amarelo dourado "0.4 - 0.5": "#FFFF00", # Amarelo "0.5 - 0.6": "#ADFF2F", # Verde amarelado "0.6 - 0.7": "#7FFF00", # Verde limão "0.7 - 0.8": "#32CD32", # Verde médio "0.8 - 0.9": "#008000", # Verde "0.9 - 1.0": "#006400", # Verde escuro "Permafrost": "#B0C4DE", # Cinza claro azulado "Not evaluated": "#A9A9A9", # Cinza escuro "Water": "#0000FF", # Azul } hex_colors_dict = { 0: "#FFFFFF", # NA 1: "#8B0000", # 0.0 - 0.1 2: "#FF4500", # 0.1 - 0.2 3: "#FFA500", # 0.2 - 0.3 4: "#FFD700", # 0.3 - 0.4 5: "#FFFF00", # 0.4 - 0.5 6: "#ADFF2F", # 0.5 - 0.6 7: "#7FFF00", # 0.6 - 0.7 8: "#32CD32", # 0.7 - 0.8 9: "#008000", # 0.8 - 0.9 10: "#006400", # 0.9 - 1.0 11: "#B0C4DE", # Permafrost 12: "#A9A9A9", # Not evaluated 13: "#0000FF", # Water } def add_widgets(m): setattr(m, "zoom_to_layer", True) style = {"description_width": "initial"} padding = "0px 0px 0px 5px" image = widgets.Dropdown( value=None, options=[('Most limiting soil quality rating factor', maps_values[0]), ('Nutrient retention capacity', maps_values[1]), ('Rooting conditions', maps_values[2]), ('Soil and terrain suitability, rain-fed', maps_values[3]), ], description="Image:", style=style, layout=widgets.Layout(width="270px", padding=padding), ) output = widgets.Output() def change_image(change): if change.new: if change.new not in m.get_layer_names(): #mosaic = f"{url}/datasets/{dataset.value}/{image.value}.json" layer_list = m.get_layer_names() for layer in layer_list: m.remove_layer(m.find_layer(layer)) #municipiosBR= 'https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2023/Brasil/BR_Municipios_2023.zip' #estadosBR = 'https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2023/Brasil/BR_UF_2023.zip' m.add_basemap("HYBRID") #m.add_shp(estadosBR, layer_name='EstadosBR') #m.add_shp(municipiosBR, layer_name='MunicipiosBR') #m.remove_legend() if image.value == maps_values[0]: m.add_cog_layer(image.value, name= maps_list[0], colormap = hex_colors_dict, nodata =0) m.add_legend(legend_title="Legend", legend_dict=legend_dict1) elif image.value == maps_values[1]: m.add_cog_layer(image.value, name= maps_list[1], colormap = hex_colors_dict, nodata =0) m.add_legend(legend_title="Legend", legend_dict=legend_dict1) elif image.value == maps_values[2]: m.add_cog_layer(image.value, name= maps_list[2], colormap = hex_colors_dict, nodata=0) m.add_legend(legend_title="Legend", legend_dict=legend_dict1) elif image.value == maps_values[3]: colors_dict = { 0: "#FFFFFF10", # Branco 1: "#006400", # Verde escuro 2: "#008000", # Verde 3: "#ADFF2F", # Verde claro 4: "#FFFF00", # Amarelo 5: "#DAA520", # Dourado escuro 6: "#FF8C00", # Laranja 7: "#A9A9A9", # Cinza escuro 8: "#D3D3D3", # Cinza claro 9: "#0000FF", # Azul 10: "#FF0000" # Vermelho } legend_dict = { "NA": "#FFFFFF", # Branco "SI > 75 : Very high": "#006400", # Verde escuro "SI > 63 : High": "#008000", # Verde "SI > 50 : Good": "#ADFF2F", # Verde claro "SI > 35 : Medium": "#FFFF00", # Amarelo "SI > 20 : Moderate": "#DAA520", # Dourado escuro "SI > 10 : Marginal": "#FF8C00", # Laranja "SI > 0 : Very marginal": "#A9A9A9", # Cinza escuro "Not suitable": "#D3D3D3", # Cinza claro "No cultivation": "#0000FF", # Azul "Water": "#FF0000" # Vermelho } m.add_cog_layer(image.value, name= maps_list[3], colormap = colors_dict,nodata=0) m.add_legend(legend_title="Legend", legend_dict=legend_dict) output.outputs = () #output.append_stdout(f"Image date: {image_date}\n") image.observe(change_image, names="value") box = widgets.VBox( [ image, output] ) m.add_widget(box, position="topright", add_header=False) class Map(leafmap.Map): def __init__(self, **kwargs): super().__init__(**kwargs) add_widgets(self) self.add_layer_manager(opened= True) @solara.component def Page(): with solara.Column(style={"min-width": "500px"}): Map.element( zoom=zoom.value, on_zoom=zoom.set, center=center.value, on_center=center.set, height="600px", toolbar_ctrl=False, data_ctrl=True, )