XClose

An introduction to research programming with Python

Home
Menu

Extended exercise: the biggest earthquake in the UK this century

USGS earthquake catalog

GeoJSON is a JSON-based file format for sharing geographic data. One example dataset available in the GeoJSON format is the USGS earthquake catalog. A web service application programming interface (API) is provided for programatically accessing events in the earthquake catalog. Specifically the query method allows querying the catalog for events with the query parameters passed as key=value pairs.

We can use the requests Python library to simplify constructing the appropriate query string to add to the URL and to deal with sending the HTTP request.

In [1]:
import requests

We first define a variable URL for the earthquake catalog web service API.

In [2]:
earthquake_catalog_api_url = "http://earthquake.usgs.gov/fdsnws/event/1/query"

We now need to define the parameters of our query. We want to get the data in GeoJSON format for all events in the earthquake catalog with date on or after 1st January 2000 and with location within a bounding box covering the UK. We will filter the events to only request those with magnitude greater or equal to 1 to avoid downloading responses for more frequent small magnitude events. Finally we want the results to be returned in order of ascending time.

In [3]:
query_parameters = {
    "format": "geojson",
    "starttime": "2000-01-01",
    "maxlatitude": "60.830",
    "minlatitude": "49.877",
    "maxlongitude": "1.767",
    "minlongitude": "-8.182",
    "minmagnitude": "1",
    "orderby": "time-asc"
}

We now can execute the API query using the requests.get function. This takes as arguments the URL to make the request from and, optionally, a dictionary argument params containing the parameters to send in the query string for the request. A requests.Response object is returned, which represents the server's response to the HTTP request made.

In [4]:
quakes_response = requests.get(earthquake_catalog_api_url, params=query_parameters)

The response object has various attributes and methods. A useful attribute to check is the ok attribute which will be False if the status code for the response to the request corresponds to a client or server error and True otherwise.

In [5]:
quakes_response.ok
Out[5]:
True

We can also check specifically that the status code corresponds to the expected 200 OK using the status_code attribute

In [6]:
quakes_response.status_code == 200
Out[6]:
True

The actual content of the response can be accessed in various formats. The content attribute gives the content of the response as bytes. As here we expect the response content to be Unicode-encoded text, the text attribute is more relevant as it gives the response content as a Python string. We can display the first 100 characters of the response content as follows

In [7]:
print(quakes_response.text[:100])
{"type":"FeatureCollection","metadata":{"generated":1666873178000,"url":"https://earthquake.usgs.gov

Task

The task for this exercie is to determine the location of the largest magnitude earthquake in the UK this century, using the data from the USGS earthquake catalog.

You will need to:

  • Query the USGS earthquake catalog for the relevant event data (see above).
  • Parse the data as JSON.
  • Understand how the data is structured into dictionaries and lists
    • Where is the magnitude?
    • Where is the place description or coordinates?
  • Program a search through all the quakes to find the biggest quake.
  • Find the place of the biggest quake.
  • Form a URL for a map tile centered at that latitude and longitude (look back at the introductory example).
  • Display that map tile image.