{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Folium to Visualize Beijing Station Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reference: https://projects.raspberrypi.org/en/projects/mapping-the-weather/9\n", "\n", "Using data from [KDD CUP of Fresh Air](https://biendata.com/competition/kdd_2018/data/)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np \n", "import pandas as pd\n", "import folium" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "bj_aq = pd.read_csv(\"../data/bj_201803_renamed_aq.csv\")" ] }, { "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", "
station_idLongitudeLatitudetype
0dongsi_aq116.41739.929城区
1tiantan_aq116.40739.886城区
2guanyuan_aq116.33939.929城区
3wanshouxigong_aq116.35239.878城区
4aotizhongxin_aq116.39739.982城区
\n", "
" ], "text/plain": [ " station_id Longitude Latitude type\n", "0 dongsi_aq 116.417 39.929 城区\n", "1 tiantan_aq 116.407 39.886 城区\n", "2 guanyuan_aq 116.339 39.929 城区\n", "3 wanshouxigong_aq 116.352 39.878 城区\n", "4 aotizhongxin_aq 116.397 39.982 城区" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stations = pd.read_csv(\"../data/Beijing_AirQuality_Stations.csv\")\n", "stations.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Map of Beijing" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map_hooray = folium.Map(\n", " location=[39.929, 116.417],\n", " tiles = \"Stamen Terrain\",\n", " zoom_start = 10) # Uses lat then lon. The bigger the zoom number, the closer in you get\n", "map_hooray # Calls the map to display " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mark Stations using folium.Marker" ] }, { "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", " \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", "
station_idtimePM25_ConcentrationPM10_ConcentrationNO2_ConcentrationCO_ConcentrationO3_ConcentrationSO2_ConcentrationLongitudeLatitudetype
0aotizhongxin_aq2017-01-01 14:00:00453.0467.0156.07.23.09.0116.39739.982城区
1badaling_aq2017-01-01 14:00:0087.0115.084.01.52.049.0115.98840.365对照点及区域点
2beibuxinqu_aq2017-01-01 14:00:00479.0487.0166.07.44.09.0116.17440.090城区
3daxing_aq2017-01-01 14:00:00352.0488.0125.05.85.08.0116.40439.718郊区
4dingling_aq2017-01-01 14:00:00339.0372.0137.05.96.018.0116.22040.292对照点及区域点
\n", "
" ], "text/plain": [ " station_id time PM25_Concentration \\\n", "0 aotizhongxin_aq 2017-01-01 14:00:00 453.0 \n", "1 badaling_aq 2017-01-01 14:00:00 87.0 \n", "2 beibuxinqu_aq 2017-01-01 14:00:00 479.0 \n", "3 daxing_aq 2017-01-01 14:00:00 352.0 \n", "4 dingling_aq 2017-01-01 14:00:00 339.0 \n", "\n", " PM10_Concentration NO2_Concentration CO_Concentration O3_Concentration \\\n", "0 467.0 156.0 7.2 3.0 \n", "1 115.0 84.0 1.5 2.0 \n", "2 487.0 166.0 7.4 4.0 \n", "3 488.0 125.0 5.8 5.0 \n", "4 372.0 137.0 5.9 6.0 \n", "\n", " SO2_Concentration Longitude Latitude type \n", "0 9.0 116.397 39.982 城区 \n", "1 49.0 115.988 40.365 对照点及区域点 \n", "2 9.0 116.174 40.090 城区 \n", "3 8.0 116.404 39.718 郊区 \n", "4 18.0 116.220 40.292 对照点及区域点 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.merge(bj_aq[bj_aq[\"time\"]==\"2017-01-01 14:00:00\"], stations, on=\"station_id\")\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map_hooray = folium.Map(\n", " location=[39.929, 116.8],\n", " tiles = \"Stamen Terrain\",\n", " zoom_start = 9) \n", "\n", "for _, row in data.iterrows():\n", " folium.Marker([\n", " row[\"Latitude\"],row[\"Longitude\"]],\n", " popup=row[\"station_id\"]).add_to(map_hooray)\n", "\n", "map_hooray" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using Colors to Reprecent PM2.5 Concentration with folium.CircleMarker" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def colourgrad(minimum, maximum, value):\n", " minimum, maximum = float(minimum), float(maximum)\n", " ratio = (value-minimum) / (maximum - minimum)\n", " g = int(max(0, 128*(1 - ratio)))\n", " r = int(max(0, 128*(1 - ratio) + 127))\n", " b = 0\n", " hexcolour = '#%02x%02x%02x' % (r,g,b)\n", " return hexcolour" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.merge(bj_aq[bj_aq[\"time\"]==\"2017-01-01 15:00:00\"], stations, on=\"station_id\")\n", "map_hooray = folium.Map(\n", " location=[39.929, 116.8],\n", " tiles = \"Stamen Terrain\",\n", " zoom_start = 9) \n", "\n", "for _, row in data.iterrows():\n", " color = colourgrad(0, 500, min(row[\"PM25_Concentration\"], 500))\n", " folium.CircleMarker([\n", " row[\"Latitude\"],row[\"Longitude\"]],\n", " color=color, radius=9, fill_opacity=1, fill=True, fill_color=color,\n", " popup=row[\"station_id\"] + \":\" + str(row[\"PM25_Concentration\"])).add_to(map_hooray)\n", "\n", "# CWD = os.getcwd()\n", "# map_ws.save('osm.html')\n", "# webbrowser.open_new('file://'+CWD+'/'+'osm.html')\n", "map_hooray" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A Few Hours later..." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.merge(bj_aq[bj_aq[\"time\"]==\"2017-01-01 18:00:00\"], stations, on=\"station_id\")\n", "map_hooray = folium.Map(\n", " location=[39.929, 116.8],\n", " tiles = \"Stamen Terrain\",\n", " zoom_start = 9) \n", "\n", "for _, row in data.iterrows():\n", " color = colourgrad(0, 500, min(row[\"PM25_Concentration\"], 500))\n", " folium.CircleMarker([\n", " row[\"Latitude\"],row[\"Longitude\"]],\n", " color=color, radius=9, fill_opacity=1, fill=True, fill_color=color,\n", " popup=row[\"station_id\"] + \":\" + str(row[\"PM25_Concentration\"])).add_to(map_hooray)\n", "\n", "map_hooray" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.merge(bj_aq[bj_aq[\"time\"]==\"2017-01-01 20:00:00\"], stations, on=\"station_id\")\n", "map_hooray = folium.Map(\n", " location=[39.929, 116.8],\n", " tiles = \"Stamen Terrain\",\n", " zoom_start = 9) \n", "\n", "for _, row in data.iterrows():\n", " color = colourgrad(0, 500, min(row[\"PM25_Concentration\"], 500))\n", " folium.CircleMarker([\n", " row[\"Latitude\"],row[\"Longitude\"]],\n", " color=color, radius=9, fill_opacity=1, fill=True, fill_color=color,\n", " popup=row[\"station_id\"] + \":\" + str(row[\"PM25_Concentration\"])).add_to(map_hooray)\n", "\n", "map_hooray" ] } ], "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 }