question 305 HTTP & API Clients · medium
Flatten earthquakes for a spreadsheet
A dashboard wants the USGS GeoJSON as flat rows. Write quake_rows(geojson): one dictionary per earthquake (skip other event types), sorted by time, with the time as UTC text and the coordinates split into named fields.
Function
quake_rows(geojson)geojsona parsed USGS response:
{"features": [...]}; properties.time is milliseconds since 1970-01-01 UTCRow
{"id", "time", "mag", "place", "longitude", "latitude", "depth_km"}, with time like "2025-01-01T04:39:18Z" (whole seconds, UTC)Returnsa new list of rows, oldest first;
[] when there are no earthquakesExamples
callquake_rows({'type': 'FeatureCollection', 'metadata': {'status': 200, 'count': 1}, 'features': [{'type': 'Feature', 'properties': {'mag': 5.5, 'place': '153 km S of Isangel, Vanuatu', 'time': 1735706358208, 'type': 'earthquake', 'title': 'M 5.5 - 153 km S of Isangel, Vanuatu'}, 'geometry': {'type': 'Point', 'coordinates': [169.3819, -20.9273, 22]}, 'id': 'us6000pgsd'}]})returns[{'id': 'us6000pgsd', 'time': '2025-01-01T04:39:18Z', 'mag': 5.5, 'place': '153 km S of Isangel, Vanuatu', 'longitude': 169.3819, 'latitude': -20.9273, 'depth_km': 22}]
callquake_rows({'type': 'FeatureCollection', 'metadata': {'status': 200, 'count': 2}, 'features': [{'type': 'Feature', 'properties': {'mag': 6.1, 'place': 'South Sandwich Islands region', 'time': 1735739304752, 'type': 'earthquake', 'title': 'M 6.1 - South Sandwich Islands region'}, 'geometry': {'type': 'Point', 'coordinates': [-26.7059, -56.4095, 70]}, 'id': 'us6000pgv4'}, {'type': 'Feature', 'properties': {'mag': 4.7, 'place': '67 km WNW of Rabaul, Papua New Guinea', 'time': 1735696019538, 'type': 'earthquake', 'title': 'M 4.7 - 67 km WNW of Rabaul, Papua New Guinea'}, 'geometry': {'type': 'Point', 'coordinates': [151.6536, -3.8682, 10]}, 'id': 'us6000pjqz'}]})returns[{'id': 'us6000pjqz', 'time': '2025-01-01T01:46:59Z', 'mag': 4.7, 'place': '67 km WNW of Rabaul, Papua New Guinea', 'longitude': 151.6536, 'latitude': -3.8682, 'depth_km': 10}, {'id': 'us6000pgv4', 'time': '2025-01-01T13:48:24Z', 'mag': 6.1, 'place': 'South Sandwich Islands region', 'longitude': -26.7059, 'latitude': -56.4095, 'depth_km': 70}]
callquake_rows({'type': 'FeatureCollection', 'metadata': {'status': 200, 'count': 0}, 'features': []})returns[]