{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Geopatra" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import random\n", "import geopatra\n", "import geopandas" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))\n", "cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))\n", "cities['value'] = [int(random.randint(10, 1000)) for i in range(len(cities))]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pop_estcontinentnameiso_a3gdp_md_estgeometry
0920938OceaniaFijiFJI8374.0MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
153950935AfricaTanzaniaTZA150600.0POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2603253AfricaW. SaharaESH906.5POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
335623680North AmericaCanadaCAN1674000.0MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4326625791North AmericaUnited States of AmericaUSA18560000.0MULTIPOLYGON (((-122.84000 49.00000, -120.0000...
\n", "
" ], "text/plain": [ " pop_est continent name iso_a3 gdp_md_est \\\n", "0 920938 Oceania Fiji FJI 8374.0 \n", "1 53950935 Africa Tanzania TZA 150600.0 \n", "2 603253 Africa W. Sahara ESH 906.5 \n", "3 35623680 North America Canada CAN 1674000.0 \n", "4 326625791 North America United States of America USA 18560000.0 \n", "\n", " geometry \n", "0 MULTIPOLYGON (((180.00000 -16.06713, 180.00000... \n", "1 POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... \n", "2 POLYGON ((-8.66559 27.65643, -8.66512 27.58948... \n", "3 MULTIPOLYGON (((-122.84000 49.00000, -122.9742... \n", "4 MULTIPOLYGON (((-122.84000 49.00000, -120.0000... " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "world.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namegeometryvalue
0Vatican CityPOINT (12.45339 41.90328)934
1San MarinoPOINT (12.44177 43.93610)202
2VaduzPOINT (9.51667 47.13372)33
3LuxembourgPOINT (6.13000 49.61166)466
4PalikirPOINT (158.14997 6.91664)649
\n", "
" ], "text/plain": [ " name geometry value\n", "0 Vatican City POINT (12.45339 41.90328) 934\n", "1 San Marino POINT (12.44177 43.93610) 202\n", "2 Vaduz POINT (9.51667 47.13372) 33\n", "3 Luxembourg POINT (6.13000 49.61166) 466\n", "4 Palikir POINT (158.14997 6.91664) 649" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cities.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Interactive maps with folium\n", "\n", "Map geodataframes with Folium \n", "\n", "## Supported Maps\n", "\n", " - Geojson plots (Plot the geometries in geodataframes)\n", " - Choropleth maps (Colors and Jazz)\n", " - Markercluster (Count, Sum, Average)\n", " - Heatmaps (Oooh 🔥)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geojson maps" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cities.folium.plot()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Directly interact with the function\n", "m = geopatra.folium.geojson(cities, zoom=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Circle Maps" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cities.folium.circle(\n", " tooltip=[\"name\"],\n", " radius=10, \n", " fill=True, \n", " fill_color='red', \n", " zoom=100\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Directly interact with the function\n", "m = geopatra.folium.circle(cities, zoom=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Choropleth maps" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "world.folium.choropleth(color_by= 'pop_est',\n", " tooltip=['name'],\n", " color= 'green',\n", " zoom= 1,\n", " style = {'color': 'black',\n", " 'weight': 1,\n", " 'dashArray': '10, 5',\n", " 'fillOpacity': 0.5,\n", " })" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Directly interact with the function\n", "m = geopatra.folium.choropleth(world, color_by= 'pop_est',)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Markercluster maps" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cities.folium.markercluster(zoom=1)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# Directly interact with the function\n", "m = geopatra.folium.markercluster(cities, zoom=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Weighted Markercluster maps\n", "\n", "\n", "TODO: add color change and tooltip " ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cities.folium.markercluster(zoom=1, metric='sum', weight='value')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Heat maps" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cities.folium.heatmap(style={'min_opacity': 0.3}, zoom=5)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# Directly interact with the function\n", "m = geopatra.folium.heatmap(cities, zoom=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interactive maps with kepler.gl\n", "\n", "Build an interface for geodataframes to work natively with Kepler.gl \n", "https://docs.kepler.gl/docs/keplergl-jupyter\n", "\n", "## Supported Maps\n", " - Geojson plots & Choropleth\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geojson maps" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import warnings\n", "from IPython.display import IFrame\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User Guide: https://github.com/keplergl/kepler.gl/blob/master/docs/keplergl-jupyter/user-guide.md\n", "Map saved to ./_static/_maps/kepler_map1.html!\n" ] } ], "source": [ "kmap1 = cities.kepler.plot()\n", "kmap1.save_to_html(file_name='./_static/_maps/kepler_map1.html')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "IFrame('./_static/_maps/kepler_map1.html', width=900, height=500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple Polygons" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User Guide: https://github.com/keplergl/kepler.gl/blob/master/docs/keplergl-jupyter/user-guide.md\n", "Map saved to ./_static/_maps/kepler_map2.html!\n" ] } ], "source": [ "kmap2 = world.kepler.plot(name=\"world\",\n", " tooltip=['continent'],\n", " color=[255,0,0],\n", " opacity=0.2,\n", " stroke_color=[0,0,0],\n", " stroke_thickness=1\n", ")\n", "kmap2.save_to_html(file_name='./_static/_maps/kepler_map2.html')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "IFrame('./_static/_maps/kepler_map2.html', width=900, height=500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Choropleth maps" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User Guide: https://github.com/keplergl/kepler.gl/blob/master/docs/keplergl-jupyter/user-guide.md\n", "Map saved to ./_static/_maps/kepler_map3.html!\n" ] } ], "source": [ "kmap_3 = world.kepler.plot(name = 'world',\n", " tooltip=['continent', 'pop_est'],\n", " stroke_color=[0,0,0],\n", " stroke_thickness=1,\n", " color_field = 'pop_est',\n", " color_scheme = 'Blues',\n", " color_scheme_steps = 5\n", ")\n", "kmap_3.save_to_html(file_name='./_static/_maps/kepler_map3.html')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "IFrame('./_static/_maps/kepler_map3.html', width=900, height=500)" ] } ], "metadata": { "kernelspec": { "display_name": "geopatra", "language": "python", "name": "geopatra" }, "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }