Mapping and comparing urban extents

Mapping and comparing urban extents#

import os

import ipywidgets as widgets
from IPython.display import display

map_files = os.listdir("MAPS")

# Define the dropdown options
options = [x.split("__")[0] for x in map_files]

# Create the dropdown widget
dropdown = widgets.Dropdown(options=options, description="Select a city:")
# Create an empty image widget
image_widget = widgets.Image()


# Function to print the selected value
def update_image(option):
    sel_file = f"MAPS/{option.new}__DB_vs_DoU.png"
    print(sel_file)
    try:
        with open(sel_file, "rb") as f:
            image_data = f.read()
            image_widget.value = image_data
    except:
        image_widget.value = b""  # Clear image if file not found
        print(f"Error: Image file not found at {sel_file}")


# Observe the change in dropdown value
dropdown.observe(update_image, names="value")

# Display the dropdown
display(dropdown)
display(image_widget)
sel_file = "C:/WBG/Work/Code/GOSTurban/notebooks/Implementations/MENA_Benchmarking/MAPS/AGO_Luanda__DB_vs_DoU.png"
with open(sel_file, "rb") as f:
    image_data = f.read()
    image_widget.value = image_data