|
import requests |
|
import folium |
|
import pandas as pd |
|
from datetime import datetime, timedelta |
|
import time |
|
import os |
|
import json |
|
|
|
|
|
AFAD_API_URL = "https://deprem.afad.gov.tr/apiv2/event/filter" |
|
CACHE_DIR = os.path.join(os.path.dirname(__file__), "cache") |
|
|
|
def get_afad_data(hours=12, use_cache=True, cache_max_age_minutes=15): |
|
"""AFAD API'den son depremleri çek""" |
|
|
|
os.makedirs(CACHE_DIR, exist_ok=True) |
|
cache_file = os.path.join(CACHE_DIR, f"afad_data_{hours}h.json") |
|
|
|
|
|
if use_cache and os.path.exists(cache_file): |
|
file_age_minutes = (time.time() - os.path.getmtime(cache_file)) / 60 |
|
if file_age_minutes < cache_max_age_minutes: |
|
try: |
|
with open(cache_file, 'r', encoding='utf-8') as f: |
|
print(f"Loading cached data (age: {file_age_minutes:.1f} minutes)") |
|
return json.load(f) |
|
except Exception as e: |
|
print(f"Cache loading error: {e}") |
|
|
|
|
|
end_time = datetime.now() |
|
start_time = end_time - timedelta(hours=hours) |
|
|
|
|
|
params = { |
|
"start": start_time.strftime("%Y-%m-%d %H:%M:%S"), |
|
"end": end_time.strftime("%Y-%m-%d %H:%M:%S"), |
|
"minMag": 1.0, |
|
"orderby": "timedesc" |
|
} |
|
|
|
try: |
|
|
|
print(f"Fetching earthquake data from AFAD API for the last {hours} hours...") |
|
response = requests.get(AFAD_API_URL, params=params, timeout=10) |
|
response.raise_for_status() |
|
|
|
|
|
data = response.json() |
|
|
|
|
|
with open(cache_file, 'w', encoding='utf-8') as f: |
|
json.dump(data, f, ensure_ascii=False) |
|
|
|
return data |
|
except Exception as e: |
|
print(f"AFAD API error: {e}") |
|
|
|
|
|
if os.path.exists(cache_file): |
|
try: |
|
with open(cache_file, 'r', encoding='utf-8') as f: |
|
print(f"Using cached data due to API error") |
|
return json.load(f) |
|
except Exception as cache_error: |
|
print(f"Cache error: {cache_error}") |
|
|
|
|
|
return {"result": []} |
|
|
|
def create_quake_map(quake_data): |
|
"""Deprem verilerini haritada göster""" |
|
|
|
if not quake_data or "result" not in quake_data or not quake_data["result"]: |
|
|
|
m = folium.Map(location=[39.0, 35.0], zoom_start=5) |
|
folium.Marker( |
|
[39.0, 35.0], |
|
popup="Deprem verisi bulunamadı", |
|
icon=folium.Icon(color="gray") |
|
).add_to(m) |
|
return m |
|
|
|
|
|
quakes = [] |
|
for quake in quake_data["result"]: |
|
try: |
|
quakes.append({ |
|
"id": quake.get("eventID", ""), |
|
"time": quake.get("date", ""), |
|
"latitude": float(quake.get("latitude", 0)), |
|
"longitude": float(quake.get("longitude", 0)), |
|
"depth": float(quake.get("depth", 0)), |
|
"magnitude": float(quake.get("magnitude", 0)), |
|
"location": quake.get("location", ""), |
|
"type": quake.get("type", "") |
|
}) |
|
except (ValueError, TypeError) as e: |
|
print(f"Data conversion error: {e}") |
|
continue |
|
|
|
df = pd.DataFrame(quakes) |
|
|
|
if df.empty: |
|
|
|
m = folium.Map(location=[39.0, 35.0], zoom_start=5) |
|
folium.Marker( |
|
[39.0, 35.0], |
|
popup="Deprem verisi bulunamadı", |
|
icon=folium.Icon(color="gray") |
|
).add_to(m) |
|
return m |
|
|
|
|
|
center_lat = df["latitude"].mean() |
|
center_lon = df["longitude"].mean() |
|
|
|
|
|
m = folium.Map(location=[center_lat, center_lon], zoom_start=6) |
|
|
|
|
|
for _, row in df.iterrows(): |
|
|
|
if row["magnitude"] >= 5.0: |
|
color = "red" |
|
elif row["magnitude"] >= 4.0: |
|
color = "orange" |
|
elif row["magnitude"] >= 3.0: |
|
color = "yellow" |
|
else: |
|
color = "green" |
|
|
|
|
|
radius = row["magnitude"] * 5000 |
|
|
|
|
|
popup_html = f""" |
|
<div style="font-family: Arial; width: 200px;"> |
|
<h4>Deprem Bilgisi</h4> |
|
<b>Tarih:</b> {row["time"]}<br> |
|
<b>Büyüklük:</b> {row["magnitude"]}<br> |
|
<b>Derinlik:</b> {row["depth"]} km<br> |
|
<b>Konum:</b> {row["location"]}<br> |
|
</div> |
|
""" |
|
|
|
|
|
folium.Circle( |
|
location=[row["latitude"], row["longitude"]], |
|
radius=radius, |
|
color=color, |
|
fill=True, |
|
fill_color=color, |
|
fill_opacity=0.4, |
|
popup=folium.Popup(popup_html, max_width=300) |
|
).add_to(m) |
|
|
|
|
|
folium.CircleMarker( |
|
location=[row["latitude"], row["longitude"]], |
|
radius=5, |
|
color=color, |
|
fill=True, |
|
fill_color="white", |
|
fill_opacity=0.7, |
|
popup=folium.Popup(popup_html, max_width=300) |
|
).add_to(m) |
|
|
|
|
|
legend_html = """ |
|
<div style="position: fixed; bottom: 50px; left: 50px; z-index: 1000; background-color: white; padding: 10px; border: 2px solid grey; border-radius: 5px;"> |
|
<p><b>Deprem Büyüklüğü</b></p> |
|
<p><i class="fa fa-circle" style="color: red;"></i> ≥ 5.0</p> |
|
<p><i class="fa fa-circle" style="color: orange;"></i> 4.0 - 4.9</p> |
|
<p><i class="fa fa-circle" style="color: yellow;"></i> 3.0 - 3.9</p> |
|
<p><i class="fa fa-circle" style="color: green;"></i> < 3.0</p> |
|
</div> |
|
""" |
|
m.get_root().html.add_child(folium.Element(legend_html)) |
|
|
|
return m |
|
|
|
def get_latest_quakes(hours=12): |
|
"""Son depremleri çek ve haritada göster""" |
|
|
|
quake_data = get_afad_data(hours) |
|
|
|
|
|
quake_map = create_quake_map(quake_data) |
|
|
|
|
|
timestamp = datetime.now().strftime("%d.%m.%Y %H:%M:%S") |
|
quake_map.get_root().html.add_child(folium.Element( |
|
f'<div style="position: fixed; top: 10px; right: 10px; z-index: 1000; background-color: white; padding: 5px; border-radius: 5px;">' |
|
f'Son Güncelleme: {timestamp}</div>' |
|
)) |
|
|
|
|
|
return quake_map._repr_html_() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
quake_data = get_afad_data(hours=24) |
|
print(f"Fetched {len(quake_data.get('result', []))} earthquakes") |
|
|
|
|
|
quake_map = create_quake_map(quake_data) |
|
|
|
|
|
output_file = "son_depremler.html" |
|
quake_map.save(output_file) |
|
print(f"Map saved to {output_file}") |
|
|