From d60a5aff54538aebb424169f5d3507dfdee5b4f3 Mon Sep 17 00:00:00 2001 From: ajfriend Date: Sat, 11 Nov 2023 17:44:07 -0800 Subject: [PATCH] reprs for H3Shape objects should work better with geopandas --- poly/plotting.ipynb | 116 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 5 deletions(-) diff --git a/poly/plotting.ipynb b/poly/plotting.ipynb index 574875f9..d9e81789 100644 --- a/poly/plotting.ipynb +++ b/poly/plotting.ipynb @@ -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, @@ -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", @@ -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": [] } ],