Type With a streamlit application, I try to add a georeferenced raster image in the scattermap graph. a fixed image appears on the top left of the map, the image seems not well georeferenced, and does not move with the map when I change the zoom and center of the map.
My code :
import streamlit as st
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
import imageio
import numpy as np
from PIL import Image
# Titre de l'application
st.title("Scattermap avec Image Raster en Fond")
# Charger les données pour le scattermap
df = pd.read_csv(r"df2.csv")
# Lire l'image PNG et la convertir en tableau numpy
image = imageio.imread(r"raster_img.png")
image_array = np.array(image)
# image_array = image_array[::10, ::10] # Décimer l'image pour la rendre plus légère
# Convertir le tableau numpy en image PIL
pil_image = Image.fromarray(image_array)
img = px.imshow(image_array)
# Créer un scattermap avec Plotly Express
fig = px.scatter_map(
data_frame=df,
lat="x",
lon="y",
zoom=8,
opacity=0.5,
map_style="carto-positron",
title="ceci est un titre"
)
# Ajouter l'image à la mise en page de la carte
fig.add_layout_image(
dict(
pil_image,
xref="x", # Référence de l'axe x
yref="y", # Référence de l'axe y
x=-25, # Coordonnée x de l'image
y=18, # Coordonnée y de l'image
xanchor="left", # Ancrage de l'image sur l'axe x
yanchor="top", # Ancrage de l'image sur l'axe y
sizex=22, # Taille en x de l'image
sizey=12, # Taille en y de l'image
sizing="stretch",
opacity=0.5,
layer="above" # Placer l'image derrière la carte
)
)
st.plotly_chart(fig, theme=None, on_select="rerun", selection_mode="lasso")