import streamlit as st import requests import pandas as pd from datetime import datetime # Define the base URL for the USGS Earthquake API BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query" # Function to get earthquake data def get_earthquake_data(starttime, endtime, minmagnitude, latitude, longitude, maxradiuskm): params = { "format": "geojson", "starttime": starttime, "endtime": endtime, "minmagnitude": minmagnitude, "latitude": latitude, "longitude": longitude, "maxradiuskm": maxradiuskm, } response = requests.get(BASE_URL, params=params) if response.status_code == 200: data = response.json() return data["features"] else: st.error("Error fetching data from the API.") return [] # Function to get latitude and longitude from a place name using Nominatim def get_lat_lon(place_name): url = f"https://nominatim.openstreetmap.org/search?q={place_name}&format=json&limit=1" headers = { 'User-Agent': 'YourAppName/1.0 (your_email@example.com)' } response = requests.get(url, headers=headers) if response.status_code == 200: results = response.json() if results: latitude = float(results[0]["lat"]) longitude = float(results[0]["lon"]) return latitude, longitude else: st.error("No results found for the specified place.") return None, None else: st.error(f"Error fetching coordinates for the place. HTTP status code: {response.status_code}") return None, None # Streamlit app def main(): st.title("Earthquake Data Lookup") # Input field for the place name place_name = st.sidebar.text_input("Enter City or Country Name", "San Francisco") if place_name: latitude, longitude = get_lat_lon(place_name) if latitude is not None and longitude is not None: st.sidebar.write(f"Coordinates for {place_name}: ({latitude}, {longitude})") starttime = st.sidebar.date_input("Start Date", datetime(2024, 1, 1)) endtime = st.sidebar.date_input("End Date", datetime.now()) minmagnitude = st.sidebar.number_input("Minimum Magnitude", min_value=0.0, max_value=10.0, value=5.0, step=0.1) maxradiuskm = st.sidebar.number_input("Radius (km)", min_value=0.0, max_value=20001.6, value=500.0, step=1.0) if st.sidebar.button("Fetch Data"): with st.spinner("Fetching data..."): features = get_earthquake_data(starttime, endtime, minmagnitude, latitude, longitude, maxradiuskm) if features: records = [ { "Time": feature["properties"]["time"], "Magnitude": feature["properties"]["mag"], "Place": feature["properties"]["place"], "Latitude": feature["geometry"]["coordinates"][1], "Longitude": feature["geometry"]["coordinates"][0], "Depth (km)": feature["geometry"]["coordinates"][2], } for feature in features ] df = pd.DataFrame(records) df["Time"] = pd.to_datetime(df["Time"], unit='ms') st.dataframe(df) st.map(df.rename(columns={"Latitude": "lat", "Longitude": "lon"})) else: st.info("No data found for the given parameters.") else: st.error("Could not fetch coordinates. Please check the place name.") if __name__ == "__main__": main()