Map display with geo spatial index H3
This recipe enables you to display geographic data on a map with H3
Code snippet
Display geo data from a table
app.py
import streamlit as st
import streamlit_hexviz as shv
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
shv.h3_map(df, lat="latitude", lon="longitude")
# Display map in case table contains H3 index
h3_col= 'your_h3_column_name'
shv.h3_choropleth(df, h3_col=h3_col)
Resources
- SQL warehouse (optional, only for reading table data)
- Unity Catalog table (optional, only for reading table data)
- H3 SQL reference (optional, only for reading table data)
- H3 (optional, only for reference to the concept H3)
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 Hexviz -
streamlit-hexviz - Databricks SDK -
databricks-sdk(for table data) - Databricks SQL Connector -
databricks-sql-connector(for table data)
requirements.txt
streamlit
streamlit-hexviz
databricks-sdk
databricks-sql-connector