Last active
March 24, 2025 15:39
-
-
Save do-me/96f4037aa5f1a81b388aed103d5b4f29 to your computer and use it in GitHub Desktop.
Simple pydeck deck.gl Scatterplot layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pydeck as pdk | |
import pandas as pd | |
import numpy as np | |
### sample data | |
import duckdb | |
import geopandas | |
df = duckdb.sql(f"SELECT * FROM 'geonames_23_03_2025.parquet' WHERE \"1\" = 'London' \ | |
AND \"8\" = 'GB' ").df() | |
gdf = geopandas.GeoDataFrame( | |
df, | |
geometry=geopandas.points_from_xy(x=df["5"], y=df["4"]) | |
) | |
gdf | |
### | |
# load some gdf | |
gdf["coordinates"] = gdf.apply(lambda x: [x.geometry.x, x.geometry.y], axis=1) | |
# Define a layer to display on a map | |
layer = pdk.Layer( | |
"ScatterplotLayer", | |
# coordinates is an array | |
gdf[["name","coordinates"]], # super important! only pass what's needed. If geometry column from geopandas is passed, error! | |
pickable=True, | |
opacity=0.8, | |
stroked=True, | |
filled=True, | |
radius_scale=6, | |
radius_min_pixels=1, | |
radius_max_pixels=100, | |
line_width_min_pixels=1, | |
get_position="coordinates", | |
get_radius="1", | |
get_fill_color=[255, 140, 0], | |
get_line_color=[255, 140, 0], | |
) | |
# Set the viewport location | |
view_state = pdk.ViewState(latitude=np.mean(gdf.geometry.y), longitude=np.mean(gdf.geometry.x), zoom=12, bearing=0, pitch=0) | |
# Render | |
r = pdk.Deck(layers=[layer], initial_view_state=view_state,height=2000, tooltip={"text": "{name}"}) | |
r.to_html("scatterplot_layer.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment