diff --git a/notebooks/usage.ipynb b/notebooks/usage.ipynb index 85a9d6e..a519f99 100644 --- a/notebooks/usage.ipynb +++ b/notebooks/usage.ipynb @@ -13,7 +13,7 @@ "metadata": {}, "outputs": [], "source": [ - "from h3 import h3\n", + "import h3\n", "import folium\n", "\n", "def visualize_hexagons(hexagons, color=\"red\", folium_map=None):\n", @@ -25,10 +25,10 @@ " lat = []\n", " lng = []\n", " for hex in hexagons:\n", - " polygons = h3.h3_set_to_multi_polygon([hex], geo_json=False)\n", + " polygons = h3.cells_to_polygons([hex])\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", + " outlines = [polygon.outer for polygon in polygons]\n", + " polyline = [list(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", @@ -50,7 +50,8 @@ " 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" + " return m\n", + "\n" ] }, { @@ -61,10 +62,83 @@ { "data": { "text/html": [ - "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ - "" + "" ] }, "metadata": {}, @@ -72,7 +146,7 @@ } ], "source": [ - "h3_address = h3.geo_to_h3(37.3615593, -122.0553238, 9) # lat, lng, hex resolution \n", + "h3_address = h3.latlng_to_cell(37.3615593, -122.0553238, 9) # lat, lng, hex resolution \n", "m = visualize_hexagons([h3_address])\n", "display(m)" ] @@ -85,10 +159,437 @@ { "data": { "text/html": [ - "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ - "" + "" ] }, "metadata": {}, @@ -96,13 +597,14 @@ } ], "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", + "h3_address = h3.latlng_to_cell(37.3615593, -122.0553238, 9) # lat, lng, hex resolution \n", + "polygons = h3.cells_to_polygons([h3_address])\n", + "#hex_center_coordinates = h3.h3_to_geo(h3_address) # array of [lat, lng] \n", + "hex_boundary = [p.outer for p in polygons] # array of arrays of [lat, lng] \n", + "m = visualize_hexagons(list(h3.grid_ring(h3_address, 4)), color=\"purple\")\n", + "m = visualize_hexagons(list(h3.grid_ring(h3_address, 3)), color=\"blue\", folium_map=m)\n", + "m = visualize_hexagons(list(h3.grid_ring(h3_address, 2)), color=\"green\", folium_map=m)\n", + "m = visualize_hexagons(list(h3.grid_ring(h3_address, 1)), color = \"red\", folium_map=m)\n", "display(m)" ] }, @@ -114,10 +616,1163 @@ { "data": { "text/html": [ - "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ - "" + "" ] }, "metadata": {}, @@ -136,21 +1791,23 @@ "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", + "hexagons = list(h3.polygon_to_cells(h3.Polygon(polyline), 8))\n", + " \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", + " polygons = h3.cells_to_polygons([hex])\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", + " outlines = [polygon.outer for polygon in polygons]\n", + " polyline = [list(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", + " my_PolyLine=folium.PolyLine(locations=polyline,weight=8, color='red')\n", + " m.add_child(my_PolyLine)\n", "display(m)" ] }, @@ -165,7 +1822,7 @@ "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -179,7 +1836,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.7" + "version": "3.8.13" } }, "nbformat": 4,