Retrieve Coordinates of Rectangle drawn on interactive Map

I would like to be able draw a rectangle on an interactive map and use the coordinates of the corners of the rect in subsequent computations.

These are the (im)possible options I currently have on the radar, but sadly none of them really does what I’m looking for:

  1. ipyleaflet has exactly the functionality I’m looking for. One can draw rectangles and with the DrawControl and on_draw callback functions, you are able to retrieve the coordinates of the drawn rect. However, as far as I can see streamlit does unfortunately not work with ipyleaflet. ipyleaflet seems to work great in jupyter notebooks
  2. AFAIK the streamlit mapbox st.map does not provide the functionality of drawing a rect at all. Or does it and I just couldn’t find it?
  3. While the geemap streamlit app by Qiusheng Wu is really awesome, I feel like downloading and uploading the geojson of the rectangle is a dirty workaround and UX killer I honestly would like to avoid in 2022.

Does anyone know how to achieve the described behavior?

(More context: I built this little app which computes a 3d-printable STL file based on a geojson rectangle, which works great with ipyleaflet in a jupyter notebook. However, I would love to port this to streamlit and share it via streamlit-cloud.)

streamlit-folium recently added the bi-directional functionality. I am yet to integrate it into my apps. You can give it a try.

2 Likes

Great, thanks a lot, that seems to be what I was looking for! :+1: I’ll keep you posted once I have something to share.

For everyone interested in how to potentially implement this, I’ve just published a streamlit app based on streamlit-folium, which offers exactly what I was looking for: