Earthquake
Start of a project to bring in information from a website with an Application Programming Interface (API). We are going to use a modified version of "Project: Fetching Current Weather Data" from "Automate the boring stuff with Python" by Al Sweigart
Instead of weather data were are collecting a month's worth of earthquake data.
What is going on below?
We import libraries to deal with json, request from the server and the pandas library.
import json, requests
import pandas as pd
from pandas import json_normalize
In this section we creating a string made up of the URL.
Requesting the information from the site with the URL we created and pass back the information. Data comes from the US Geological survey https://www.usgs.gov/about/about-us/who-we-are and one of their earthquake feeds.
Requesting the information from the site with the URL we created and pass back the information.
Then print out what was returned.
url='https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson'
response= requests.get(url)
response.raise_for_status()
Now we need to load the data which is in JSON form into a Python version.
The data coming from the website has many levels we need to convert the lists into a form we can use ie. normalise/normalize it https://pandas.pydata.org/pandas-docs/version/1.2.0/reference/api/pandas.json_normalize.html
quakeData = json.loads(response.text)
df = json_normalize(quakeData['features'])
Plots some maps
To do the map plotting we need to add four new modules
plotly https://plotly.com/python/ geopandas https://geopandas.org/en/stable/getting_started/introduction.html shapely https://isbe.bwk.tue.nl/education/Python/04_02_Shapely.html folium mapclassify https://towardsdatascience.com/data-101s-spatial-visualizations-and-analysis-in-python-with-folium-39730da2adf
!pip install plotly
!pip install geopandas
!pip install shapely
!pip install folium mapclassify
What is happening here
Quick overview but would benefit from greater detail
- Importing the ability to great points and also the new modified form of DataFrame GeoDataFrame with a DataFrame with the added ability to define shapes
- The returned data has the co-ordinates as a list of three numbers. For ease the co-ordinates are converted from the list individually and the longitude and latitude are given their own columns in the DataFrame.
- Now we can use the longitude and latitude to makes a plot on points.
from shapely.geometry import Point
from geopandas import GeoDataFrame
longitude_df=[]
latitude_df=[]
#dfg=df.get('geometry.coordinates')
df['lon'] = pd.DataFrame(df['geometry.coordinates'].tolist())[0]
df['lat'] = pd.DataFrame(df['geometry.coordinates'].tolist())[1]
geometry = [Point(xy) for xy in zip(df.lon, df.lat)]
df = df.drop(['lon', 'lat'], axis=1)
gdf = GeoDataFrame(df, crs="EPSG:4326", geometry=geometry)
gdf.plot("properties.mag", legend=True)
<AxesSubplot:>
The code can be found at https://github.com/scottturneruon/earthquake.git
gdf.explore("properties.mag", legend=True)
In [7]:gdf.explore("properties.mag", legend=True)
No comments:
Post a Comment