Skip to content

Instantly share code, notes, and snippets.

@ajfriend
Last active October 20, 2024 01:56
Show Gist options
  • Save ajfriend/3035688c293d0feb07019550af1d42c2 to your computer and use it in GitHub Desktop.
Save ajfriend/3035688c293d0feb07019550af1d42c2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "45aaa1c0-d9e2-4909-93aa-99169cbaf892",
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-20T01:53:00.823847Z",
"iopub.status.busy": "2024-10-20T01:53:00.823696Z",
"iopub.status.idle": "2024-10-20T01:53:01.024219Z",
"shell.execute_reply": "2024-10-20T01:53:01.023960Z",
"shell.execute_reply.started": "2024-10-20T01:53:00.823830Z"
}
},
"outputs": [],
"source": [
"import h3\n",
"import pandas as pd\n",
"import geopandas as gpd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "129c460e-5e2f-48f9-83b7-bace1e64e157",
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-20T01:53:01.024587Z",
"iopub.status.busy": "2024-10-20T01:53:01.024486Z",
"iopub.status.idle": "2024-10-20T01:53:01.026489Z",
"shell.execute_reply": "2024-10-20T01:53:01.026271Z",
"shell.execute_reply.started": "2024-10-20T01:53:01.024580Z"
}
},
"outputs": [],
"source": [
"lat, lng = 37.7749, -122.4194\n",
"h = h3.latlng_to_cell(lat, lng, 9)\n",
"def foo(h, K=2):\n",
" return [\n",
" (c, K-k)\n",
" for k in range(K)\n",
" for c in h3.grid_ring(h, k)\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "27f43897-1fe4-45cf-9fc0-8240952bd480",
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-20T01:53:01.026878Z",
"iopub.status.busy": "2024-10-20T01:53:01.026807Z",
"iopub.status.idle": "2024-10-20T01:53:01.040580Z",
"shell.execute_reply": "2024-10-20T01:53:01.040405Z",
"shell.execute_reply.started": "2024-10-20T01:53:01.026871Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>cell</th>\n",
" <th>k</th>\n",
" <th>geometry</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>89283082803ffff</td>\n",
" <td>10</td>\n",
" <td>POLYGON ((-122.41701 37.77201, -122.41594 37.7...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>89283082813ffff</td>\n",
" <td>9</td>\n",
" <td>POLYGON ((-122.41682 37.76882, -122.41575 37.7...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>8928308281bffff</td>\n",
" <td>9</td>\n",
" <td>POLYGON ((-122.41361 37.77387, -122.41594 37.7...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>8928308280bffff</td>\n",
" <td>9</td>\n",
" <td>POLYGON ((-122.41361 37.77387, -122.41254 37.7...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>8928308280fffff</td>\n",
" <td>9</td>\n",
" <td>POLYGON ((-122.41972 37.77821, -122.42079 37.7...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>266</th>\n",
" <td>89283082dd3ffff</td>\n",
" <td>1</td>\n",
" <td>POLYGON ((-122.43345 37.75314, -122.43578 37.7...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>267</th>\n",
" <td>89283082ca7ffff</td>\n",
" <td>1</td>\n",
" <td>POLYGON ((-122.42968 37.74863, -122.4286 37.75...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>268</th>\n",
" <td>89283082ca3ffff</td>\n",
" <td>1</td>\n",
" <td>POLYGON ((-122.4286 37.75031, -122.42968 37.74...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>269</th>\n",
" <td>89283082cbbffff</td>\n",
" <td>1</td>\n",
" <td>POLYGON ((-122.4225 37.74598, -122.42143 37.74...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>270</th>\n",
" <td>89283082c8fffff</td>\n",
" <td>1</td>\n",
" <td>POLYGON ((-122.42143 37.74766, -122.4225 37.74...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>271 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" cell k geometry\n",
"0 89283082803ffff 10 POLYGON ((-122.41701 37.77201, -122.41594 37.7...\n",
"1 89283082813ffff 9 POLYGON ((-122.41682 37.76882, -122.41575 37.7...\n",
"2 8928308281bffff 9 POLYGON ((-122.41361 37.77387, -122.41594 37.7...\n",
"3 8928308280bffff 9 POLYGON ((-122.41361 37.77387, -122.41254 37.7...\n",
"4 8928308280fffff 9 POLYGON ((-122.41972 37.77821, -122.42079 37.7...\n",
".. ... .. ...\n",
"266 89283082dd3ffff 1 POLYGON ((-122.43345 37.75314, -122.43578 37.7...\n",
"267 89283082ca7ffff 1 POLYGON ((-122.42968 37.74863, -122.4286 37.75...\n",
"268 89283082ca3ffff 1 POLYGON ((-122.4286 37.75031, -122.42968 37.74...\n",
"269 89283082cbbffff 1 POLYGON ((-122.4225 37.74598, -122.42143 37.74...\n",
"270 89283082c8fffff 1 POLYGON ((-122.42143 37.74766, -122.4225 37.74...\n",
"\n",
"[271 rows x 3 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = foo(h, 10)\n",
"df = pd.DataFrame(data, columns = ['cell', 'k'])\n",
"df['geometry'] = df.cell.apply(lambda x: h3.cells_to_h3shape([x]))\n",
"df = gpd.GeoDataFrame(df, crs='EPSG:4326')\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f21ca4e8-c34b-48cc-8469-98d2317947a6",
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-20T01:53:01.040993Z",
"iopub.status.busy": "2024-10-20T01:53:01.040904Z",
"iopub.status.idle": "2024-10-20T01:53:01.230743Z",
"shell.execute_reply": "2024-10-20T01:53:01.230464Z",
"shell.execute_reply.started": "2024-10-20T01:53:01.040987Z"
}
},
"outputs": [],
"source": [
"import lonboard\n",
"from lonboard import PolygonLayer, Map\n",
"from lonboard.colormap import apply_continuous_cmap\n",
"from palettable.colorbrewer.sequential import YlOrRd_9"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "936639b4-260c-4bd8-ad53-d19523e0c23d",
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-20T01:53:01.231121Z",
"iopub.status.busy": "2024-10-20T01:53:01.231022Z",
"iopub.status.idle": "2024-10-20T01:53:01.252522Z",
"shell.execute_reply": "2024-10-20T01:53:01.252285Z",
"shell.execute_reply.started": "2024-10-20T01:53:01.231114Z"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4faf85486c74487fa1ff1e5b0ed973f8",
"version_major": 2,
"version_minor": 1
},
"text/plain": [
"Map(custom_attribution='', layers=(PolygonLayer(get_fill_color=arro3.core.ChunkedArray<FixedSizeList(Field { n…"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layer = PolygonLayer.from_geopandas(\n",
" df,\n",
" get_fill_color = apply_continuous_cmap(df['k']/max(df['k']), cmap=YlOrRd_9),\n",
")\n",
"Map(layer, _height=800, show_tooltip=True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "66d2f311-785c-47ac-898e-1042399a935a",
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-20T01:53:01.253719Z",
"iopub.status.busy": "2024-10-20T01:53:01.253640Z",
"iopub.status.idle": "2024-10-20T01:53:01.267977Z",
"shell.execute_reply": "2024-10-20T01:53:01.267764Z",
"shell.execute_reply.started": "2024-10-20T01:53:01.253713Z"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "962d4e6cec5e4f159dae20d73cdbad4c",
"version_major": 2,
"version_minor": 1
},
"text/plain": [
"Map(custom_attribution='', layers=(PolygonLayer(extruded=True, get_elevation=arro3.core.ChunkedArray<Float32>\n",
"…"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layer = PolygonLayer.from_geopandas(\n",
" df,\n",
" extruded = True,\n",
" get_elevation = df['k']*200,\n",
" get_fill_color = apply_continuous_cmap(df['k']/max(df['k']), cmap=YlOrRd_9),\n",
")\n",
"m = Map(layer, _height=800)\n",
"\n",
"# optional, but you can move the map around with your mouse and capture the view state to preserve\n",
"# a specific view for the future.\n",
"m.view_state = {\n",
" 'longitude': -122.41820874498809,\n",
" 'latitude': 37.773543063566244,\n",
" 'zoom': 12,\n",
" 'pitch': 56.75037883302402,\n",
" 'bearing': -4.4830827067669174,\n",
"}\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "499e5f9e-bd83-48e8-850d-ef74aad9dd51",
"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.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@ajfriend
Copy link
Author

Screenshot 2024-10-19 at 6 55 06 PM Screenshot 2024-10-19 at 6 55 11 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment