{ "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", " | felt | \n", "latitude | \n", "longitude | \n", "mag | \n", "magType | \n", "place | \n", "time | \n", "timestamp | \n", "tsunami | \n", "
---|---|---|---|---|---|---|---|---|---|
0 | \n", "NaN | \n", "-17.861 | \n", "-178.639 | \n", "5.0 | \n", "mb | \n", "Fiji region | \n", "1329694388230 | \n", "2012-02-19 15:33:08 | \n", "0 | \n", "