Skip to content

Commit

Permalink
reprs for H3Shape objects should work better with geopandas
Browse files Browse the repository at this point in the history
  • Loading branch information
ajfriend committed Nov 12, 2023
1 parent d52679a commit d60a5af
Showing 1 changed file with 111 additions and 5 deletions.
116 changes: 111 additions & 5 deletions poly/plotting.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "aa3a6a1e-b947-4e92-a5e4-c3dac6621885",
"metadata": {},
"source": [
"# notes\n",
"\n",
"## projections\n",
"\n",
"- 4326 # wgs84 (for H3 compatibility)\n",
" - H3 **does not use** wgs84/epsg4326, but it **does use** the \"authalic radius\" from that standard\n",
"- 3857 # web mercator (for plotting)\n",
"- 2263 # new york (just what's used in the example below)\n",
"\n",
"## geoviews\n",
"\n",
"- tile options: https://github.com/holoviz/geoviews/blob/main/geoviews/tile_sources.py\n",
" - also this, but i don't see a good way to programatically get all the options: https://geoviews.org/gallery/matplotlib/tile_sources.html \n",
"- active tools: https://stackoverflow.com/questions/57767169/set-box-zoom-or-pan-or-wheel-zoom-as-default-in-using-holoviews-or-hvplot\n",
"- bokeh pallets: https://docs.bokeh.org/en/latest/docs/reference/palettes.html\n",
"- Bokeh \"tap\" tool seems to do something on this website, but i can't get the same functionality (isolating a single geometry) locally: https://geoviews.org/gallery/bokeh/new_york_boroughs.html\n",
"\n",
"## folium\n",
"\n",
"- seems to be most popular, but they make you manually determine the center and zoom level!\n",
" - too much work. geoviews does it automatically\n",
" - https://geopandas.org/en/stable/gallery/polygon_plotting_with_folium.html"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -20,11 +49,7 @@
"id": "9e971ca2-24a4-43ba-8a90-83f6091b462b",
"metadata": {},
"outputs": [],
"source": [
"2263 # new york\n",
"4326 # wgs84\n",
"3857 # web mercator"
]
"source": []
},
{
"cell_type": "code",
Expand Down Expand Up @@ -147,12 +172,93 @@
"bar(df, 3857)"
]
},
{
"cell_type": "markdown",
"id": "c6a0ea52-0721-496e-bb1f-50faebd38493",
"metadata": {},
"source": [
"# converting to cells\n",
"\n",
"- example of a common bug users will run into"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c701a9b6-a26d-44b7-beb1-9981d9ea22d3",
"metadata": {},
"outputs": [],
"source": [
"df = geopandas.read_file(geodatasets.get_path('nybb'))\n",
"df.crs"
]
},
{
"cell_type": "markdown",
"id": "ae6bec98-1a7f-4223-8990-04a3169c2911",
"metadata": {},
"source": [
"The following code works, but runs for a while and then breaks:\n",
"\n",
"```python\n",
"df.geometry.apply(lambda x: h3.geo_to_cells(x, 9))\n",
"```\n",
"\n",
"The bug is that we first need to convert to the WGS84 authalic radius system:\n",
"`df = df.to_crs(epsg=4326)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c432273e-d03f-4a29-8788-b869f1b9cf9b",
"metadata": {},
"outputs": [],
"source": [
"#df.geometry.apply(lambda x: h3.geo_to_cells(x, 9))\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "831de887-a746-430b-a586-5e7069581d95",
"metadata": {},
"outputs": [],
"source": [
"df84 = df.to_crs(epsg=4326)\n",
"df84"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65f2c481-2b80-4acd-932d-5c873e58cccd",
"metadata": {},
"outputs": [],
"source": [
"# TODO: this needs to show H3MultiPoly\n",
"out = df84.geometry.apply(lambda x: h3.geo_to_cells(x, 9)).apply(lambda x: h3.cells_to_h3shape(x))\n",
"out"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adacc9ea-3370-4060-82ff-8a10321e7dc1",
"metadata": {},
"outputs": [],
"source": [
"for a in out:\n",
" print(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aac772f7-255f-4a61-954f-ac04240f163d",
"metadata": {},
"outputs": [],
"source": []
}
],
Expand Down

0 comments on commit d60a5af

Please sign in to comment.