Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,29 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# Function to get latitude and longitude from a place name using Nominatim
|
5 |
def get_lat_lon(place_name):
|
@@ -23,18 +47,45 @@ def get_lat_lon(place_name):
|
|
23 |
|
24 |
# Streamlit app
|
25 |
def main():
|
26 |
-
st.title("
|
27 |
|
28 |
# Input field for the place name
|
29 |
-
place_name = st.text_input("Enter City Name", "San Francisco")
|
30 |
|
31 |
if place_name:
|
32 |
latitude, longitude = get_lat_lon(place_name)
|
33 |
|
34 |
if latitude is not None and longitude is not None:
|
35 |
-
st.write(f"Coordinates for {place_name}:")
|
36 |
-
|
37 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
if __name__ == "__main__":
|
40 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
+
import pandas as pd
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
# Define the base URL for the USGS Earthquake API
|
7 |
+
BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query"
|
8 |
+
|
9 |
+
# Function to get earthquake data
|
10 |
+
def get_earthquake_data(starttime, endtime, minmagnitude, latitude, longitude, maxradiuskm):
|
11 |
+
params = {
|
12 |
+
"format": "geojson",
|
13 |
+
"starttime": starttime,
|
14 |
+
"endtime": endtime,
|
15 |
+
"minmagnitude": minmagnitude,
|
16 |
+
"latitude": latitude,
|
17 |
+
"longitude": longitude,
|
18 |
+
"maxradiuskm": maxradiuskm,
|
19 |
+
}
|
20 |
+
response = requests.get(BASE_URL, params=params)
|
21 |
+
if response.status_code == 200:
|
22 |
+
data = response.json()
|
23 |
+
return data["features"]
|
24 |
+
else:
|
25 |
+
st.error("Error fetching data from the API.")
|
26 |
+
return []
|
27 |
|
28 |
# Function to get latitude and longitude from a place name using Nominatim
|
29 |
def get_lat_lon(place_name):
|
|
|
47 |
|
48 |
# Streamlit app
|
49 |
def main():
|
50 |
+
st.title("Earthquake Data Lookup")
|
51 |
|
52 |
# Input field for the place name
|
53 |
+
place_name = st.sidebar.text_input("Enter City or Country Name", "San Francisco")
|
54 |
|
55 |
if place_name:
|
56 |
latitude, longitude = get_lat_lon(place_name)
|
57 |
|
58 |
if latitude is not None and longitude is not None:
|
59 |
+
st.sidebar.write(f"Coordinates for {place_name}: ({latitude}, {longitude})")
|
60 |
+
|
61 |
+
starttime = st.sidebar.date_input("Start Date", datetime(2024, 1, 1))
|
62 |
+
endtime = st.sidebar.date_input("End Date", datetime.now())
|
63 |
+
minmagnitude = st.sidebar.number_input("Minimum Magnitude", min_value=0.0, max_value=10.0, value=5.0, step=0.1)
|
64 |
+
maxradiuskm = st.sidebar.number_input("Radius (km)", min_value=0.0, max_value=20001.6, value=500.0, step=1.0)
|
65 |
+
|
66 |
+
if st.sidebar.button("Fetch Data"):
|
67 |
+
with st.spinner("Fetching data..."):
|
68 |
+
features = get_earthquake_data(starttime, endtime, minmagnitude, latitude, longitude, maxradiuskm)
|
69 |
+
if features:
|
70 |
+
records = [
|
71 |
+
{
|
72 |
+
"Time": feature["properties"]["time"],
|
73 |
+
"Magnitude": feature["properties"]["mag"],
|
74 |
+
"Place": feature["properties"]["place"],
|
75 |
+
"Latitude": feature["geometry"]["coordinates"][1],
|
76 |
+
"Longitude": feature["geometry"]["coordinates"][0],
|
77 |
+
"Depth (km)": feature["geometry"]["coordinates"][2],
|
78 |
+
}
|
79 |
+
for feature in features
|
80 |
+
]
|
81 |
+
df = pd.DataFrame(records)
|
82 |
+
df["Time"] = pd.to_datetime(df["Time"], unit='ms')
|
83 |
+
st.dataframe(df)
|
84 |
+
st.map(df.rename(columns={"Latitude": "lat", "Longitude": "lon"}))
|
85 |
+
else:
|
86 |
+
st.info("No data found for the given parameters.")
|
87 |
+
else:
|
88 |
+
st.error("Could not fetch coordinates. Please check the place name.")
|
89 |
|
90 |
if __name__ == "__main__":
|
91 |
main()
|