I want to show markers on a map, but only the datapoints that exist in the selected area. The dataset is stored in Clickhouse and consists of 10 million records. I only want the query to return records based on the bouding box of the map. The following code works, I can see that the data is read from the database.
import folium
import streamlit as st
import pandas as pd
import clickhouse_connect
from streamlit_folium import folium_static
from streamlit_folium import st_folium
import os
# ClickHouse config
clickhouse_host = os.getenv('CLICKHOUSE_HOST')
clickhouse_user = os.getenv('CLICKHOUSE_USER')
clickhouse_pass = os.getenv('CLICKHOUSE_PASSWORD')
client = clickhouse_connect.get_client(host=clickhouse_host, port=8123, user=clickhouse_user, password=clickhouse_pass, database='db1')
def get_data_in_bbox(bbox):
parameters = (bbox[0], bbox[2], bbox[1], bbox[3])
result = client.query('SELECT decimallatitude, decimallongitude FROM gbif_enriched_mv WHERE decimallatitude BETWEEN %s AND %s AND decimallongitude BETWEEN %s AND %s LIMIT 100', parameters=parameters)
return result.result_rows
m = folium.Map()
map_a = st_folium(m,
width=800,
height=400
)
# Get bounding box of selected area
bounds = map_a["bounds"]
south = bounds["_southWest"]["lat"]
west = bounds["_southWest"]["lng"]
north = bounds["_northEast"]["lat"]
east = bounds["_northEast"]["lng"]
bbox = [south, west, north, east]
# Retrieve datapoints in bouding box using function get_data_in_bbox
datapoints = get_data_in_bbox(bbox)
print(datapoints)
# Add to existing map, this part doesn't work
for data_point in datapoints:
folium.Marker(
location=data_point
).add_to(m)
I can pass the data points to a second map using a featuregroup, that works:
I want to be able to zoom in and out and see the corresponding data points in that area, using 1 map.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.