Map display and interaction
This recipe enables you to display geographic data on a map and collect user geo input through interactive map drawing. You can load location data from Unity Catalog tables or use the drawing tools to capture points, polygons, and geofences from users.
Code snippet
Display geo data from a table
app.py
import streamlit as st
from databricks import sql
from databricks.sdk.core import Config
from databricks.sdk import WorkspaceClient
import pandas as pd
cfg = Config()
w = WorkspaceClient()
# List available SQL warehouses
warehouses = w.warehouses.list()
warehouse_paths = {wh.name: wh.odbc_params.path for wh in warehouses}
# Connect to SQL warehouse
def get_connection(http_path):
return sql.connect(
server_hostname=cfg.host,
http_path=http_path,
credentials_provider=lambda: cfg.authenticate,
)
# Read table
def read_table(table_name, conn):
with conn.cursor() as cursor:
cursor.execute(f"SELECT * FROM {table_name}")
return cursor.fetchall_arrow().to_pandas()
# Get data and display on map
warehouse_name = "your_warehouse_name"
table_name = "samples.accuweather.forecast_daily_calendar_metric"
http_path = warehouse_paths[warehouse_name]
conn = get_connection(http_path)
df = read_table(table_name, conn)
# Display map with latitude/longitude columns
st.map(df, latitude="latitude", longitude="longitude")
Collect user geo input
app.py
import streamlit as st
from streamlit_folium import st_folium
import folium
from folium.plugins import Draw
# Create a map centered on a location
m = folium.Map(location=[37.7749, -122.4194], zoom_start=13)
# Enable drawing tools (set True for the tools you want to enable)
draw = Draw(
draw_options={
"marker": True, # For collecting points
"polygon": True, # For collecting geofences/polygons
"polyline": True, # For collecting polylines
"rectangle": True, # For collecting rectangles
"circle": True, # For collecting circles
"circlemarker": False,
},
edit_options={"edit": True},
)
draw.add_to(m)
output = st_folium(m, width=700, height=500)
# Access the drawn geometry
if output["last_active_drawing"] and "geometry" in output["last_active_drawing"]:
geometry = output["last_active_drawing"]["geometry"]
st.json(geometry)
Resources
- SQL warehouse (optional, only for reading table data)
- Unity Catalog table (optional, only for reading table data)
Permissions
Your app service principal needs the following permissions:
CAN USEon the SQL warehouse (only required if reading data from tables)SELECTon the Unity Catalog table (only required if reading data from tables)
See Unity Catalog privileges and securable objects for more information.
Dependencies
- Streamlit -
streamlit - Streamlit Folium -
streamlit-folium - Databricks SDK -
databricks-sdk(for table data) - Databricks SQL Connector -
databricks-sql-connector(for table data)
requirements.txt
streamlit
streamlit-folium
databricks-sdk
databricks-sql-connector