Skip to content

Instantly share code, notes, and snippets.

@diehl
Created August 23, 2023 03:20
Show Gist options
  • Save diehl/4a3af23416c2f5e6e65b3ed6bf00833f to your computer and use it in GitHub Desktop.
Save diehl/4a3af23416c2f5e6e65b3ed6bf00833f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "d57e2eb1",
"metadata": {},
"source": [
"### PyDeck demonstration of the ability to scale text relative to a physical dimension in a TextLayer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e67cc1e",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import pydeck as pdk\n",
"from pydeck.types import String\n",
"\n",
"TEXT_LAYER_DATA = \"https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/bart-stations.json\" # noqa\n",
"df = pd.read_json(TEXT_LAYER_DATA)\n",
"\n",
"# Define a layer to display on a map\n",
"layer = pdk.Layer(\n",
" \"TextLayer\",\n",
" df,\n",
" pickable=True,\n",
" get_position=\"coordinates\",\n",
" get_text=\"name\",\n",
" get_size=160,\n",
" get_color=[0, 0, 0],\n",
" get_angle=0,\n",
" # Note that string constants in pydeck are explicitly passed as strings\n",
" # This distinguishes them from columns in a data set\n",
" size_units = String(\"meters\") # <--- The key addition to switch to meters as the units. \n",
")\n",
"\n",
"# Set the viewport location\n",
"view_state = pdk.ViewState(latitude=37.7749295, longitude=-122.4194155, zoom=10, bearing=0, pitch=0)\n",
"\n",
"# Render\n",
"r = pdk.Deck(\n",
" layers=[layer],\n",
" initial_view_state=view_state,\n",
" tooltip={\"text\": \"{name}\\n{address}\"},\n",
" map_style=pdk.map_styles.ROAD,\n",
")\n",
"r.to_html()"
]
},
{
"cell_type": "markdown",
"id": "bd51cca4-396f-4f4f-a369-2325ae8d443d",
"metadata": {},
"source": [
"### Replication of this demonstration in Panel using a DeckGL JSON specification"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8eb811dc",
"metadata": {},
"outputs": [],
"source": [
"import panel as pn\n",
"pn.extension('deckgl')\n",
"\n",
"MAPBOX_KEY = \"pk.eyJ1IjoicGFuZWxvcmciLCJhIjoiY2s1enA3ejhyMWhmZjNobjM1NXhtbWRrMyJ9.B_frQsAVepGIe-HiOJeqvQ\"\n",
"\n",
"json_spec = {\n",
" \"initialViewState\": {\n",
" \"bearing\": 0,\n",
" \"latitude\": 37.7749,\n",
" \"longitude\": -122.4194,\n",
" \"pitch\": 0,\n",
" \"zoom\": 10\n",
" },\n",
" \"layers\": [{\n",
" \"@@type\": \"TextLayer\",\n",
" \"data\": \"https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/bart-stations.json\",\n",
" \"getText\": \"@@=name\",\n",
" \"getPosition\": \"@@=coordinates\",\n",
" \"getSize\": 160,\n",
" \"getColor\": [0,0,0],\n",
" \"getAngle\": 0,\n",
" \"sizeUnits\": \"meters\",\n",
" \"id\": \"8a553b25-ef3a-489c-bbe2-e102d18a3211\",\n",
" }],\n",
" \"mapProvider\": \"carto\",\n",
" \"mapStyle\": \"https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json\",\n",
" \"views\": [\n",
" {\"@@type\": \"MapView\", \"controller\": True}\n",
" ]\n",
"}\n",
"\n",
"deck_gl = pn.pane.DeckGL(json_spec, mapbox_api_key=MAPBOX_KEY, sizing_mode='stretch_both')\n",
"deck_gl.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment