{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# USGS Earthquakes with the Mapboxgl-Jupyter Python Library\n", "https://github.com/mapbox/mapboxgl-jupyter" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Python 3.5+ only!\n", "import asyncio\n", "from aiohttp import ClientSession\n", "import json, geojson, os, time\n", "import pandas as pd\n", "from datetime import datetime, timedelta\n", "from mapboxgl.viz import *\n", "from mapboxgl.utils import *" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Get Data from the USGS API\n", "data = []\n", "\n", "async def fetch(url, headers, params, session):\n", " async with session.get(url, headers=headers, params=params) as resp:\n", " tempdata = await resp.json()\n", " data.append(tempdata)\n", " return tempdata\n", " \n", "async def bound_fetch(sem, url, headers, params, session):\n", " # Getter function with semaphore.\n", " async with sem:\n", " await fetch(url, headers, params, session)\n", "\n", "async def get_quakes(param_list, headers):\n", " # Store tasks to run\n", " tasks = []\n", " \n", " # create instance of Semaphore\n", " sem = asyncio.Semaphore(1000)\n", " \n", " # Generate URL from parameters\n", " endpoint = '/query'\n", " url = '{base_url}{endpoint}'.format(base_url=base_url, endpoint=endpoint)\n", " \n", " async with ClientSession() as session:\n", " for i in range(len(param_list)):\n", " task = asyncio.ensure_future(bound_fetch(sem, url, headers, param_list[i], session))\n", " tasks.append(task)\n", " responses = await asyncio.gather(*tasks)\n", " return responses\n", " \n", "def create_params(starttime, endtime, minmagnitude):\n", " return {\n", " 'format': 'geojson', \n", " 'starttime': starttime,\n", " 'endtime': endtime,\n", " 'minmagnitude': minmagnitude\n", " }" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Default parameters \n", "base_url = 'https://earthquake.usgs.gov/fdsnws/event/1'\n", "HEADERS = {\n", " 'user-agent': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) '\n", " 'AppleWebKit/537.36 (KHTML, like Gecko) '\n", " 'Chrome/45.0.2454.101 Safari/537.36'),\n", "}\n", "\n", "# Make a list of data to get in a date range\n", "api_args = []\n", "startdate = '2012-01-01'\n", "date = datetime.strptime(startdate, \"%Y-%m-%d\")\n", "for i in range(1000):\n", " low = datetime.strftime(date + timedelta(days=i*10), \"%Y-%m-%d\")\n", " high = datetime.strftime(date + timedelta(days=(i+1)*10), \"%Y-%m-%d\")\n", " api_args.append(create_params(low, high, 2))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#Run api queries for all queries generated\n", "loop = asyncio.get_event_loop()\n", "future = asyncio.ensure_future(get_quakes(api_args, HEADERS))\n", "temp = loop.run_until_complete(future)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#Collect results into a Pandas dataframe\n", "\n", "keep_props = ['felt', 'mag', 'magType', 'place', 'time', 'tsunami', 'longitude', 'latitude']\n", "df = pd.DataFrame(columns=keep_props, index=[0])\n", "features = []\n", "\n", "for fc in data:\n", " for f in fc['features']:\n", " feature = {}\n", " for k in f['properties']:\n", " if k in keep_props:\n", " feature[k] = f['properties'][k]\n", " feature['longitude'] = f['geometry']['coordinates'][0]\n", " feature['latitude'] = f['geometry']['coordinates'][1]\n", " feature['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(feature['time']/1000))\n", " features.append(feature)\n", " \n", "df = pd.DataFrame.from_dict(features) " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(198772, 9)\n" ] }, { "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", "
feltlatitudelongitudemagmagTypeplacetimetimestamptsunami
0NaN-17.861-178.6395.0mbFiji region13296943882302012-02-19 15:33:080
\n", "
" ], "text/plain": [ " felt latitude longitude mag magType place time \\\n", "0 NaN -17.861 -178.639 5.0 mb Fiji region 1329694388230 \n", "\n", " timestamp tsunami \n", "0 2012-02-19 15:33:08 0 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(df.shape)\n", "df.head(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Set your Mapbox access token.\n", "Set a MAPBOX_ACCESS_TOKEN environment variable, or copy your token to use this notebook.\n", "\n", "If you do not have a Mapbox access token, sign up for an account at https://www.mapbox.com/\n", "\n", "If you already have an account, you can grab your token at https://www.mapbox.com/account/" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "token = os.getenv('MAPBOX_ACCESS_TOKEN')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'feature_count': 198772, 'filename': 'points.geojson', 'type': 'file'}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Generate a geojson file from the dataframe \n", "\n", "df_to_geojson(df, filename='points.geojson', precision=4, lon='longitude', lat='latitude',\n", " properties=['mag','timestamp'])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Create a graduated circle visualization using quantile breaks and a custom color palette\n", "measure = 'mag'\n", "\n", "color_palette = ['#66C3F9','#88A8E4','#9C8DC9','#A474A8','#A25E87','#974B66','#853D49','#6F3230']\n", "color_breaks = [round(df[measure].quantile(q=x*0.1), 2) for x in range(3,11)]\n", "color_stops = create_color_stops(color_breaks, colors=color_palette)\n", "\n", "radius_breaks = [round(df[measure].quantile(q=x*0.1), 2) for x in range(3,11)]\n", "radius_stops = create_radius_stops(radius_breaks, 1, 10)\n", "\n", "\n", "viz = GraduatedCircleViz('https://dl.dropbox.com/s/h4xjjlc9ggnr88b/earthquake-points-180223.geojson', #'points.geojson',\n", " color_property = measure,\n", " color_stops = color_stops,\n", " radius_property = measure,\n", " radius_stops = radius_stops,\n", " opacity=0.75,\n", " below_layer='waterway-label',\n", " zoom=2,\n", " center= [-95, 37],\n", " access_token=token)\n", "viz.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Conda Py3", "language": "python", "name": "myenv" }, "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }