Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diehl/3d84755881029166868ae2c1a4d0d204 to your computer and use it in GitHub Desktop.
Save diehl/3d84755881029166868ae2c1a4d0d204 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "0fed8932",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import panel as pn\n",
"import pandas as pd\n",
"import pydeck as pdk\n",
"import geopandas as gpd\n",
"import ipywidgets as ipw\n",
"from shapely.geometry import Point\n",
"pn.extension('deckgl')\n",
"pn.extension('ipywidgets')"
]
},
{
"cell_type": "markdown",
"id": "5c7aca4c",
"metadata": {},
"source": [
"### Load the conditioned dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cdea06d1",
"metadata": {},
"outputs": [],
"source": [
"dataset_df = gpd.read_file('../datasets/derived_data/blm_surface_management_polygons/new_mexico_data/blm_nm_surface_management_data.geojson', driver='GeoJSON')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5392365",
"metadata": {},
"outputs": [],
"source": [
"dataset_df"
]
},
{
"cell_type": "markdown",
"id": "3e70b032",
"metadata": {},
"source": [
"### Build the visualization"
]
},
{
"cell_type": "markdown",
"id": "e4cdd12e",
"metadata": {},
"source": [
"#### Initial view state"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b5a50ae",
"metadata": {},
"outputs": [],
"source": [
"INITIAL_VIEW_STATE = pdk.ViewState(\n",
" latitude=34.25,\n",
" longitude=-106,\n",
" zoom=6,\n",
" min_zoom=3,\n",
" max_zoom=16,\n",
" pitch=0,\n",
" bearing=0\n",
")"
]
},
{
"cell_type": "markdown",
"id": "7bbce963",
"metadata": {},
"source": [
"#### Disabling the double click zoom to eliminate the click state delay in deck.gl"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d9d5b87",
"metadata": {},
"outputs": [],
"source": [
"view = pdk.View(type=\"MapView\", controller={\"doubleClickZoom\": False})"
]
},
{
"cell_type": "markdown",
"id": "a4e6d191",
"metadata": {},
"source": [
"#### Background vector tiles with the land ownership data for New Mexico"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a194d82d",
"metadata": {},
"outputs": [],
"source": [
"vtiles = pdk.Deck(\n",
" initial_view_state=INITIAL_VIEW_STATE,\n",
" views=[view],\n",
" map_provider='mapbox',\n",
" map_style='mapbox://styles/diehl/cl2pasplc002414p0wjnyih26')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce84d43c",
"metadata": {},
"outputs": [],
"source": [
"map = pn.pane.DeckGL(vtiles, sizing_mode='stretch_both', throttle={'click':0})"
]
},
{
"cell_type": "markdown",
"id": "67a3e338",
"metadata": {},
"source": [
"### Polygon label inspection experiment"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03804416",
"metadata": {},
"outputs": [],
"source": [
"c1 = ipw.Text(description=\"Latitude\", disabled=False, style = {'description_width': 'initial'})\n",
"c2 = ipw.Text(description=\"Longitude\", disabled=False, style = {'description_width': 'initial'})\n",
"c3 = ipw.Text(description=\"Land Steward\", disabled=False, style = {'description_width': 'initial'})\n",
"\n",
"def lng(target, event):\n",
" target.value = f\"{event.obj.click_state['lngLat'][0]:.5f}\"\n",
" \n",
"def lat(target, event):\n",
" target.value = f\"{event.obj.click_state['lngLat'][1]:.5f}\"\n",
"\n",
"def owner(target, event):\n",
" coords = event.obj.hover_state['lngLat']\n",
" pt = Point(coords)\n",
" results = dataset_df.sindex.query(pt,predicate='intersects')\n",
" if len(results) == 1:\n",
" i = results[0]\n",
" target.value = dataset_df.iloc[i]['own']\n",
" else:\n",
" target.value = 'No Label Available'\n",
" \n",
"map.link(c1, callbacks={'click_state': lat})\n",
"map.link(c2, callbacks={'click_state': lng})\n",
"map.link(c3, callbacks={'click_state': owner})\n",
"\n",
"pn.Column(pn.Row(c1,c2,c3),map,sizing_mode='stretch_both').show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cce0d3f0",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment