Created
September 10, 2020 16:19
-
-
Save simonw/10c65f6391fb56ea0aeb0b4b021226a4 to your computer and use it in GitHub Desktop.
Earthquakes demo for PyRVA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"url = \"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import httpx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"geojson = httpx.get(url).json()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'type': 'Feature',\n", | |
" 'properties': {'mag': 2.07,\n", | |
" 'place': '24km ENE of Hydesville, CA',\n", | |
" 'time': 1599691903260,\n", | |
" 'updated': 1599692000016,\n", | |
" 'tz': None,\n", | |
" 'url': 'https://earthquake.usgs.gov/earthquakes/eventpage/nc73455616',\n", | |
" 'detail': 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc73455616.geojson',\n", | |
" 'felt': None,\n", | |
" 'cdi': None,\n", | |
" 'mmi': None,\n", | |
" 'alert': None,\n", | |
" 'status': 'automatic',\n", | |
" 'tsunami': 0,\n", | |
" 'sig': 66,\n", | |
" 'net': 'nc',\n", | |
" 'code': '73455616',\n", | |
" 'ids': ',nc73455616,',\n", | |
" 'sources': ',nc,',\n", | |
" 'types': ',nearby-cities,origin,phase-data,',\n", | |
" 'nst': 10,\n", | |
" 'dmin': 0.1186,\n", | |
" 'rms': 0.04,\n", | |
" 'gap': 106,\n", | |
" 'magType': 'md',\n", | |
" 'type': 'earthquake',\n", | |
" 'title': 'M 2.1 - 24km ENE of Hydesville, CA'},\n", | |
" 'geometry': {'type': 'Point',\n", | |
" 'coordinates': [-123.8499985, 40.6601677, 22.67]},\n", | |
" 'id': 'nc73455616'}" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"geojson['features'][0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def feature(f):\n", | |
" d = f[\"properties\"]\n", | |
" d[\"latitude\"] = f[\"geometry\"][\"coordinates\"][1]\n", | |
" d[\"longitude\"] = f[\"geometry\"][\"coordinates\"][0]\n", | |
" d['id'] = f['id']\n", | |
" return d" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'mag': 2.07,\n", | |
" 'place': '24km ENE of Hydesville, CA',\n", | |
" 'time': 1599691903260,\n", | |
" 'updated': 1599692000016,\n", | |
" 'tz': None,\n", | |
" 'url': 'https://earthquake.usgs.gov/earthquakes/eventpage/nc73455616',\n", | |
" 'detail': 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc73455616.geojson',\n", | |
" 'felt': None,\n", | |
" 'cdi': None,\n", | |
" 'mmi': None,\n", | |
" 'alert': None,\n", | |
" 'status': 'automatic',\n", | |
" 'tsunami': 0,\n", | |
" 'sig': 66,\n", | |
" 'net': 'nc',\n", | |
" 'code': '73455616',\n", | |
" 'ids': ',nc73455616,',\n", | |
" 'sources': ',nc,',\n", | |
" 'types': ',nearby-cities,origin,phase-data,',\n", | |
" 'nst': 10,\n", | |
" 'dmin': 0.1186,\n", | |
" 'rms': 0.04,\n", | |
" 'gap': 106,\n", | |
" 'magType': 'md',\n", | |
" 'type': 'earthquake',\n", | |
" 'title': 'M 2.1 - 24km ENE of Hydesville, CA',\n", | |
" 'latitude': 40.6601677,\n", | |
" 'longitude': -123.8499985,\n", | |
" 'id': 'nc73455616'}" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"feature(geojson[\"features\"][0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import sqlite_utils" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"db = sqlite_utils.Database(\"/tmp/earthquakes.db\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<Table quakes (mag, place, time, updated, tz, url, detail, felt, cdi, mmi, alert, status, tsunami, sig, net, code, ids, sources, types, nst, dmin, rms, gap, magType, type, title, latitude, longitude, id)>" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"db[\"quakes\"].insert_all([feature(d) for d in geojson['features']], pk=\"ids\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CREATE TABLE [quakes] (\n", | |
" [mag] FLOAT,\n", | |
" [place] TEXT,\n", | |
" [time] INTEGER,\n", | |
" [updated] INTEGER,\n", | |
" [tz] TEXT,\n", | |
" [url] TEXT,\n", | |
" [detail] TEXT,\n", | |
" [felt] TEXT,\n", | |
" [cdi] TEXT,\n", | |
" [mmi] TEXT,\n", | |
" [alert] TEXT,\n", | |
" [status] TEXT,\n", | |
" [tsunami] INTEGER,\n", | |
" [sig] INTEGER,\n", | |
" [net] TEXT,\n", | |
" [code] TEXT,\n", | |
" [ids] TEXT PRIMARY KEY,\n", | |
" [sources] TEXT,\n", | |
" [types] TEXT,\n", | |
" [nst] INTEGER,\n", | |
" [dmin] FLOAT,\n", | |
" [rms] FLOAT,\n", | |
" [gap] FLOAT,\n", | |
" [magType] TEXT,\n", | |
" [type] TEXT,\n", | |
" [title] TEXT,\n", | |
" [latitude] FLOAT,\n", | |
" [longitude] FLOAT,\n", | |
" [id] TEXT\n", | |
")\n" | |
] | |
} | |
], | |
"source": [ | |
"print(db[\"quakes\"].schema)" | |
] | |
}, | |
{ | |
"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.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment