I tried to implement a map that update the map drawing and the mark position after selecting a row of data with latitude and longtitude by trying to follow the examples from Streamlit Folium Update – more dynamic maps alng with Avoid Page Reload When Interacting With Folium Map, dynamic map vs. rerender and How to use st.dataframe, on_select, callable
So far, I could extract the lattitude and longtitude from th dataframe shown as follows:
Here is the code related the map and dataframe:
import pandas as pd
import streamlit as st
import folium
from streamlit_folium import st_folium
from streamlit_option_menu import option_menu
# function to convert lat long into floating point var to display on 3
df1 = pd.read_excel("/app/data/data.xlsx",sheet_name = 'GCxGC TOFMS',usecols='A,F:H',header=0) # read a CSV file inside the 'data" folder next to 'app.py'
df2 = pd.read_excel("/app/data/data.xlsx",sheet_name = 'Diagnostic Ratio',usecols='A,D:F',header=0) # read a CSV file inside the 'data" folder next to 'app.py'
df3 = pd.read_excel("/app/data/data.xlsx",sheet_name = 'XRF',usecols='A,D:F',header=0) # read a CSV file inside the 'data" folder next to 'app.py'
newDF = df1.copy()
Latitude = 27.049801
Longtitude = 49.5663726
ZoomStartmMag = 16
CENTER_START = [Latitude, Longtitude]
PopupText = "Arab Extra Light: Al-Reesh oil field (Saudi Arabia)"
TooTipText = "Arab Extra Light: Al-Reesh oil field (Saudi Arabia)"
ChoiceNum = 0
def fix_latlong(x):
"""Work on individual latitude value X."""
if x.endswith('S'):
stripstring = x.strip('S')
return -float(stripstring);
elif x.endswith('W'):
stripstring = x.strip('W')
return -float(stripstring);
elif x.endswith('N'):
stripstring = x.strip('N')
return float(stripstring);
elif x.endswith('E'):
stripstring = x.strip('E')
return float(stripstring);
# Placeholder for the selected row
def initialize_session_state():
if "selected_row" not in st.session_state:
st.session_state.selected_row = None
if "df" not in st.session_state:
st.session_state.selected_row = None
if "filtered_df" not in st.session_state:
st.session_state.filtered_df = None
if "location" not in st.session_state:
st.session_state.location = CENTER_START
if "zoom_start" not in st.session_state:
st.session_state.zoom_start = ZoomStartmMag
if "markers" not in st.session_state:
st.session_state.markers = []
if "popup" not in st.session_state:
st.session_state.popup = PopupText
if "tooltip" not in st.session_state:
st.session_state.tooltip = TooTipText
def initialize_map(center, zoom):
m = folium.Map(location=center, zoom_start=zoom, scrollWheelZoom=False)
return m
st.set_page_config(
page_title="Showing Map by streamlit-folium & st_folium",
page_icon="🔎",
layout="wide",
initial_sidebar_state="auto",
menu_items=None,
)
# Sidebar menu
sidebar, left, right = st.columns([3, 6,9])
with sidebar:
...
with left:
# call to render Folium map in Streamlit
initialize_session_state()
m = initialize_map(st.session_state.location,st.session_state.zoom_start)
st.session_state.markers = [folium.Marker(location=st.session_state.location, popup=st.session_state.popup,
tooltip=st.session_state.tooltip,icon=folium.Icon(color='green', icon='crosshairs', prefix='fa'))]
fg = folium.FeatureGroup(name="Markers")
for marker in st.session_state["markers"]:
fg.add_child(marker)
map_data = st_folium(m,
center=st.session_state.location,
zoom=st.session_state.zoom_start,
feature_group_to_add=fg,
returned_objects=["all_drawings"],
use_container_width=True)
with right:
LatitudeFloat,LongtitudeFloat = Latitude,Longtitude
PlaceValue, SourceValue = "Arab Extra Light","Al-Reesh oil field (Saudi Arabia)"
newDF = df1.copy()
...
event = st.dataframe(
newDF,
on_select="rerun", # on_select=callback,
selection_mode='single-row',
hide_index = True)
NumRow = newDF[newDF.columns[0]].count()
st.write(NumRow)
people = event.selection.rows # Attribute - the way to assign the row index
st.session_state.selected_row = people
st.session_state.df = newDF
filtered_df = newDF.iloc[people]
st.session_state.filtered_df = filtered_df;
PlaceHolding = filtered_df["Place"];
SourceHolding = filtered_df["Source"];
LatHoding = filtered_df["Lattitude"];
LongHolding = filtered_df["Longtitude"];
if (PlaceHolding.values is not None) and (SourceHolding.values is not None):
PlaceString = PlaceHolding.values # PlaceString = PlaceHolding.values[0]
SourceString = SourceHolding.values # SourceString = SourceHolding.values[0]
for PlaceValue in PlaceString:
st.write(PlaceValue)
for SourceValue in SourceString:
st.write(SourceValue)
if len(PlaceValue) >=1 and len(PlaceValue) >=1:
PopupText1 = PlaceValue + ": " + PlaceValue
TooTipText1 = PopupText1
st.session_state.popup = PopupText1
st.session_state.tooltip = TooTipText1
if (LatHoding.values is not None) and (LongHolding.values is not None) :
LatString = LatHoding.values # LatString = LatHoding.values[0] # LatString
LongString = LongHolding.values # LongString = LongHolding.values[0] #LongString =
for LatValue in LatString:
st.write(LatValue)
LatitudeFloat = fix_latlong(LatValue)
st.write(LatitudeFloat)
for LongValue in LongString:
st.write(LongValue)
LongtitudeFloat = fix_latlong(LongValue)
st.write(LongtitudeFloat)
if (len(str(LatitudeFloat)) >=1) and (len(str(LongtitudeFloat))>=1):
CENTER_START1 = [LatitudeFloat, LongtitudeFloat]
st.session_state.location = CENTER_START1
if (len(str(LatitudeFloat)) >=1) and (len(str(LongtitudeFloat)) >=1) and (len(PlaceValue) >=1) and (len(SourceValue)>=1):
st.session_state["markers"] = [folium.Marker(location=CENTER_START1, popup=st.session_state.popup,
tooltip=st.session_state.tooltip, icon=folium.Icon(color='green', icon='crosshairs', prefix='fa'))]
Is there anyway to change the map according to change attitude and longtitude after making a row selection?