{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd, folium, branca\n", "from folium.plugins import Search" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0.7.0+258.geb90e06'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "folium.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's get some JSON data from the web - both a point layer and a polygon GeoJson dataset with some population data." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "states = gpd.read_file(r\"https://rawcdn.githack.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json\", driver='GeoJSON')\n", "\n", "cities = gpd.read_file(r\"https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_populated_places_simple.geojson\", driver='GeoJSON')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And take a look at what our data looks like:" ] }, { "cell_type": "code", "execution_count": 5, "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", "
density
count52.000000
mean402.504404
std1395.100812
min1.264000
25%53.440000
50%100.335000
75%234.050000
max10065.000000
\n", "
" ], "text/plain": [ "
\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", "
density
count52.000000
mean402.504404
std1395.100812
min1.264000
25%53.440000
50%100.335000
75%234.050000
max10065.000000
\n", "
" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "states.describe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Look how far the minimum and maximum values for the density are from the top and bottom quartile breakpoints! We have some outliers in our data that are well outside the meat of most of the distribution. Let's look into this to find the culprits within the sample." ] }, { "cell_type": "code", "execution_count": 6, "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", " \n", "
namedensity
8District of Columbia10065.000
30New Jersey1189.000
51Puerto Rico1082.000
39Rhode Island1006.000
21Massachusetts840.200
31New Mexico17.160
34North Dakota9.916
26Montana6.858
50Wyoming5.851
1Alaska1.264
\n", "
" ], "text/plain": [ "
\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", " \n", "
namedensity
8District of Columbia10065.000
30New Jersey1189.000
51Puerto Rico1082.000
39Rhode Island1006.000
21Massachusetts840.200
31New Mexico17.160
34North Dakota9.916
26Montana6.858
50Wyoming5.851
1Alaska1.264
\n", "
" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "states_sorted = states.sort_values(by='density', ascending=False)\n", "\n", "states_sorted.head(5).append(states_sorted.tail(5))[['name','density']]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looks like Washington D.C. and Alaska were the culprits on each end of the range. Washington was more dense than the next most dense state, New Jersey, than the least dense state, Alaska was from Wyoming, however. Washington D.C. has a has a relatively small land area for the amount of people that live there, so it makes sense that it's pretty dense. And Alaska has a lot of land area, but not much of it is habitable for humans.\n", "

\n", "However, we're looking at all of the states in the US to look at things on a more regional level. That high figure at the top of our range for Washington D.C. will really hinder the ability for us to differentiate between the other states, so let's account for that in the min and max values for our color scale, by getting the quantile values close to the end of the range. Anything higher or lower than those values will just fall into the 'highest' and 'lowest' bins for coloring." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Min: 8.54\n\nMax: 1040.2\n\nMean: 402.5\n" ] } ], "source": [ "min, max = states['density'].quantile([0.05,0.95]).apply(lambda x: round(x, 2))\n", "\n", "mean = round(states['density'].mean(),2)\n", "\n", "\n", "print(f\"Min: {min}\", f\"Max: {max}\", f\"Mean: {mean}\", sep=\"\\n\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This looks better. Our min and max values for the colorscale are much closer to the mean value now. Let's run with these values, and make a colorscale. I'm just going to use a sequential light-to-dark color palette from the [ColorBrewer](http://colorbrewer2.org/#type=sequential&scheme=Purples&n=5)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "8.541040.2" ], "text/plain": [ "8.541040.2" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "colormap = branca.colormap.LinearColormap(\n", " colors=['#f2f0f7','#cbc9e2','#9e9ac8','#756bb1','#54278f'],\n", " index=states['density'].quantile([0.2,0.4,0.6,0.8]),\n", " vmin=min,\n", " vmax=max\n", ")\n", "\n", "colormap.caption=\"Population Density in the United States\"\n", "\n", "colormap\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's narrow down these cities to United states cities, by using GeoPandas' spatial join functionality between two GeoDataFrame objects, using the Point 'within' Polygon functionality." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "us_cities = gpd.sjoin(cities, states, how='inner', op='within')\n", "pop_ranked_cities = us_cities.sort_values(by='pop_max', ascending=False)[['nameascii','pop_max', 'geometry'\n", " ]].iloc[:20]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ok, now we have a new GeoDataFrame with our top 20 populated cities. Let's see the top 5." ] }, { "cell_type": "code", "execution_count": 10, "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", "
nameasciipop_maxgeometry
1224New York19040000POINT (-73.98196278740681 40.75192492259464)
1222Los Angeles12500000POINT (-118.1819263699404 33.99192410876543)
1186Chicago8990000POINT (-87.75200083270931 41.83193651927843)
1184Miami5585000POINT (-80.22605193945003 25.78955655502153)
1076Philadelphia5492000POINT (-75.17194183200792 40.00191902252647)
\n", "
" ], "text/plain": [ "
\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", "
nameasciipop_maxgeometry
1224New York19040000POINT (-73.98196278740681 40.75192492259464)
1222Los Angeles12500000POINT (-118.1819263699404 33.99192410876543)
1186Chicago8990000POINT (-87.75200083270931 41.83193651927843)
1184Miami5585000POINT (-80.22605193945003 25.78955655502153)
1076Philadelphia5492000POINT (-75.17194183200792 40.00191902252647)
\n", "
" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pop_ranked_cities.head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alright, let's build a map!" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "
" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Instantiate our folium Map.\n", "m = folium.Map(location=[38,-97], zoom_start=4)\n", "\n", "# Create some folium GeoJson objects from our GeoDataFrames.\n", "stategeo = folium.GeoJson(states,\n", " name='US States',\n", " style_function=lambda x: {'fillColor': colormap(x['properties']['density']), 'color': 'black',\n", " 'weight':2, 'fillOpacity':0.5},\n", " tooltip=folium.GeoJsonTooltip(fields=['name', 'density'], \n", " aliases=['State', 'Density'], \n", " localize=True)\n", " ).add_to(m)\n", "\n", "citygeo = folium.GeoJson(pop_ranked_cities,\n", " name='US Cities',\n", " tooltip=folium.GeoJsonTooltip(fields=['nameascii','pop_max'],\n", " aliases=['','Population Max'],\n", " localize=True)\n", " ).add_to(m)\n", "\n", "# Add some Search boxes to the map that reference the GeoDataFrames with some different parameters passed to the \n", "# arguments.\n", "statesearch = Search(layer=stategeo, \n", " geom_type='Polygon', \n", " placeholder=\"Search for a US State\", \n", " collapsed=False, \n", " search_label='name',\n", " weight=3\n", " ).add_to(m)\n", "\n", "citysearch = Search(layer=citygeo, \n", " geom_type='Point', \n", " placeholder=\"Search for a US City\", \n", " collapsed=True, \n", " search_label='nameascii'\n", " ).add_to(m)\n", "\n", "# Add a LayerControl.\n", "folium.LayerControl().add_to(m)\n", "\n", "# And the Color Map legend.\n", "colormap.add_to(m)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }