{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# H3 Python API" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from h3 import h3\n", "import folium\n", "\n", "def visualize_hexagons(hexagons, color=\"red\", folium_map=None):\n", " \"\"\"\n", " hexagons is a list of hexcluster. Each hexcluster is a list of hexagons. \n", " eg. [[hex1, hex2], [hex3, hex4]]\n", " \"\"\"\n", " polylines = []\n", " lat = []\n", " lng = []\n", " for hex in hexagons:\n", " polygons = h3.h3_set_to_multi_polygon([hex], geo_json=False)\n", " # flatten polygons into loops.\n", " outlines = [loop for polygon in polygons for loop in polygon]\n", " polyline = [outline + [outline[0]] for outline in outlines][0]\n", " lat.extend(map(lambda v:v[0],polyline))\n", " lng.extend(map(lambda v:v[1],polyline))\n", " polylines.append(polyline)\n", " \n", " if folium_map is None:\n", " m = folium.Map(location=[sum(lat)/len(lat), sum(lng)/len(lng)], zoom_start=13, tiles='cartodbpositron')\n", " else:\n", " m = folium_map\n", " for polyline in polylines:\n", " my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color=color)\n", " m.add_child(my_PolyLine)\n", " return m\n", " \n", "\n", "def visualize_polygon(polyline, color):\n", " polyline.append(polyline[0])\n", " lat = [p[0] for p in polyline]\n", " lng = [p[1] for p in polyline]\n", " m = folium.Map(location=[sum(lat)/len(lat), sum(lng)/len(lng)], zoom_start=13, tiles='cartodbpositron')\n", " my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color=color)\n", " m.add_child(my_PolyLine)\n", " return m" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "h3_address = h3.geo_to_h3(37.3615593, -122.0553238, 9) # lat, lng, hex resolution \n", "m = visualize_hexagons([h3_address])\n", "display(m)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "h3_address = h3.geo_to_h3(37.3615593, -122.0553238, 9) # lat, lng, hex resolution \n", "hex_center_coordinates = h3.h3_to_geo(h3_address) # array of [lat, lng] \n", "hex_boundary = h3.h3_to_geo_boundary(h3_address) # array of arrays of [lat, lng] \n", "m = visualize_hexagons(list(h3.k_ring_distances(h3_address, 4)[3]), color=\"purple\")\n", "m = visualize_hexagons(list(h3.k_ring_distances(h3_address, 4)[2]), color=\"blue\", folium_map=m)\n", "m = visualize_hexagons(list(h3.k_ring_distances(h3_address, 4)[1]), color=\"green\", folium_map=m)\n", "m = visualize_hexagons(list(h3.k_ring_distances(h3_address, 4)[0]), color = \"red\", folium_map=m)\n", "display(m)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "geoJson = {'type': 'Polygon',\n", " 'coordinates': [[[37.813318999983238, -122.4089866999972145], [ 37.7866302000007224, -122.3805436999997056 ], [37.7198061999978478, -122.3544736999993603], [ 37.7076131999975672, -122.5123436999983966 ], [37.7835871999971715, -122.5247187000021967], [37.8151571999998453, -122.4798767000009008]]] }\n", "\n", "polyline = geoJson['coordinates'][0]\n", "polyline.append(polyline[0])\n", "lat = [p[0] for p in polyline]\n", "lng = [p[1] for p in polyline]\n", "m = folium.Map(location=[sum(lat)/len(lat), sum(lng)/len(lng)], zoom_start=13, tiles='cartodbpositron')\n", "my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color=\"green\")\n", "m.add_child(my_PolyLine)\n", "\n", "hexagons = list(h3.polyfill(geoJson, 8))\n", "polylines = []\n", "lat = []\n", "lng = []\n", "for hex in hexagons:\n", " polygons = h3.h3_set_to_multi_polygon([hex], geo_json=False)\n", " # flatten polygons into loops.\n", " outlines = [loop for polygon in polygons for loop in polygon]\n", " polyline = [outline + [outline[0]] for outline in outlines][0]\n", " lat.extend(map(lambda v:v[0],polyline))\n", " lng.extend(map(lambda v:v[1],polyline))\n", " polylines.append(polyline)\n", "for polyline in polylines:\n", " my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color='red')\n", " m.add_child(my_PolyLine)\n", "display(m)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python 3", "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }