Spaces:
Configuration error
Configuration error
Download app.py from Bora9/GroundwaterExplorer: direct link, hf CLI and curl.
- Browser
- Download file 8.95 kB
-
https://huggingface.co/spaces/Bora9/GroundwaterExplorer/resolve/main/app.py
- Command line
-
hf download hf://spaces/Bora9/GroundwaterExplorer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Bora9/GroundwaterExplorer/resolve/main/app.py
8.95 kB
| import streamlit as st | |
| import folium | |
| from streamlit_folium import st_folium | |
| import requests | |
| import numpy as np | |
| import geopandas as gpd | |
| import json | |
| import math | |
| st.set_page_config( | |
| layout="wide", | |
| page_title="TR Arazi & Jeoloji Analiz Portalı", | |
| page_icon="🗺️" | |
| ) | |
| # STİL AYARLARI | |
| st.markdown(""" | |
| <style> | |
| .metric-card { | |
| background-color: #f8f9fa; | |
| padding: 15px; | |
| border-radius: 10px; | |
| border-left: 5px solid #2e7d32; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| st.title("🗺️ Türkiye Arazi, Topoğrafya ve Jeoloji Analiz Portalı") | |
| st.caption("Parsel/Konum verileriyle uydu, DEM (yükseklik), eğim, iklim ve litoloji analizi türetme aracı[cite: 2].") | |
| # --------------------------------------------------------- | |
| # YARDIMCI FONKSİYONLAR & API VERİ ÇEKİMİ | |
| # --------------------------------------------------------- | |
| def fetch_dem_and_slope(lat, lon, step_meters=100): | |
| delta_deg = step_meters / 111000.0 | |
| lats = [lat, lat + delta_deg, lat - delta_deg, lat, lat] | |
| lons = [lon, lon, lon, lon + delta_deg, lon - delta_deg] | |
| url = f"https://api.open-meteo.com/v1/elevation?latitude={','.join(map(str, lats))}&longitude={','.join(map(str, lons))}" | |
| try: | |
| res = requests.get(url, timeout=10).json() | |
| elevations = res.get("elevation", [0]*5) | |
| z_center, z_north, z_south, z_east, z_west = elevations | |
| dz_dx = (z_east - z_west) / (2 * step_meters) | |
| dz_dy = (z_north - z_south) / (2 * step_meters) | |
| slope_rad = math.atan(math.sqrt(dz_dx**2 + dz_dy**2)) | |
| slope_pct = math.tan(slope_rad) * 100 | |
| slope_deg = math.degrees(slope_rad) | |
| return { | |
| "center_elevation": z_center, | |
| "slope_pct": round(slope_pct, 2), | |
| "slope_deg": round(slope_deg, 1), | |
| "min_elevation": min(elevations), | |
| "max_elevation": max(elevations) | |
| } | |
| except Exception: | |
| return {"center_elevation": 0, "slope_pct": 0, "slope_deg": 0, "min_elevation": 0, "max_elevation": 0} | |
| def fetch_weather(lat, lon): | |
| url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t=temperature_2m,relative_humidity_2m,precipitation" | |
| try: | |
| res = requests.get(url, timeout=10).json() | |
| curr = res.get("current", {}) | |
| return { | |
| "temp": curr.get("temperature_2m", "N/A"), | |
| "humidity": curr.get("relative_humidity_2m", "N/A"), | |
| "precip": curr.get("precipitation", 0) | |
| } | |
| except Exception: | |
| return {"temp": "N/A", "humidity": "N/A", "precip": 0} | |
| def fetch_lithology(lat, lon): | |
| url = f"https://macrostrat.org/api/v2/geologic_units/map?lat={lat}&lng={lon}" | |
| try: | |
| res = requests.get(url, timeout=10).json() | |
| data = res.get("success", {}).get("data", []) | |
| if data: | |
| unit = data[0] | |
| return { | |
| "lithology": unit.get("lith", "Alüvyon / Karışık Sedimanter"), | |
| "formation": unit.get("strat_name", "Yerel Birim"), | |
| "age": unit.get("t_interval", {}).get("name", "Kuvaterner / Neojen"), | |
| "desc": unit.get("desc", "Tanımlanmamış litolojik formasyon.") | |
| } | |
| except Exception: | |
| pass | |
| return { | |
| "lithology": "Sedimanter / Alüvyon (Genel Katman)", | |
| "formation": "Detaylı harita yüklenmedi", | |
| "age": "Kuvaterner", | |
| "desc": "Genel Türkiye jeoloji haritası verisi varsayıldı." | |
| } | |
| # --------------------------------------------------------- | |
| # SOL PANEL - GİRDİ FORMU | |
| # --------------------------------------------------------- | |
| with st.sidebar: | |
| st.header("📍 Konum ve Parsel Girdisi") | |
| input_type = st.radio("Veri Giriş Yöntemi:", ["Koordinat (WGS84)", "GeoJSON / KML Dosyası Yükle"]) | |
| lat = 39.9334 | |
| lon = 32.8597 | |
| uploaded_gdf = None | |
| if input_type == "Koordinat (WGS84)": | |
| lat = st.number_input("Enlem (Latitude)", value=39.9334, format="%.6f") | |
| lon = st.number_input("Boylam (Longitude)", value=32.8597, format="%.6f") | |
| else: | |
| uploaded_file = st.file_uploader("Parsel Dosyası (.geojson / .kml)", type=["geojson", "json", "kml"]) | |
| if uploaded_file is not None: | |
| try: | |
| uploaded_gdf = gpd.read_file(uploaded_file) | |
| centroid = uploaded_gdf.geometry.centroid.iloc[0] | |
| lat, lon = centroid.y, centroid.x | |
| st.success("Parsel sınırları başarıyla yüklendi!") | |
| except Exception as e: | |
| st.error(f"Dosya okuma hatası: {e}") | |
| st.markdown("---") | |
| analysis_radius = st.slider("Analiz Yarıçapı (Metre)", 50, 1000, 200, step=50) | |
| # --------------------------------------------------------- | |
| # HESAPLAMALAR VE VERİ ÇEKME | |
| # --------------------------------------------------------- | |
| with st.spinner("Jeoloji ve topoğrafya verileri çekiliyor..."): | |
| dem_data = fetch_dem_and_slope(lat, lon, step_meters=analysis_radius//2) | |
| weather_data = fetch_weather(lat, lon) | |
| litho_data = fetch_lithology(lat, lon) | |
| # --------------------------------------------------------- | |
| # ÖZET METRİK PANOSU | |
| # --------------------------------------------------------- | |
| c1, c2, c3, c4, c5 = st.columns(5) | |
| c1.metric("Rakım (DEM)", f"{dem_data['center_elevation']} m") | |
| c2.metric("Ortalama Eğim", f"%{dem_data['slope_pct']} ({dem_data['slope_deg']}°)") | |
| c3.metric("Baskın Litoloji", litho_data['lithology']) | |
| c4.metric("Jeolojik Dönem", litho_data['age']) | |
| c5.metric("Anlık Sıcaklık", f"{weather_data['temp']} °C") | |
| st.markdown("---") | |
| # --------------------------------------------------------- | |
| # HARİTA VE DETAYLI ANALİZ | |
| # --------------------------------------------------------- | |
| col_map, col_info = st.columns([7, 5]) | |
| with col_map: | |
| st.subheader("🛰️ İnteraktif Arazi & Uydu Katmanı") | |
| m = folium.Map(location=[lat, lon], zoom_start=16, max_zoom=20) | |
| folium.TileLayer( | |
| tiles='https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', | |
| attr='Esri World Imagery', | |
| name='Esri Uydu Görüntüsü', | |
| overlay=False | |
| ).add_to(m) | |
| folium.TileLayer( | |
| tiles='https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', | |
| attr='OpenTopoMap', | |
| name='Topoğrafik Harita', | |
| overlay=False | |
| ).add_to(m) | |
| if uploaded_gdf is not None: | |
| folium.GeoJson( | |
| uploaded_gdf, | |
| name="Parsel Sınırı", | |
| style_function=lambda x: {'fillColor': '#ff7800', 'color': '#ff0000', 'weight': 3, 'fillOpacity': 0.3} | |
| ).add_to(m) | |
| else: | |
| folium.Circle( | |
| radius=analysis_radius, | |
| location=[lat, lon], | |
| color="crimson", | |
| fill=True, | |
| fill_opacity=0.25, | |
| popup=f"Analiz Tampon Bölgesi ({analysis_radius}m)" | |
| ).add_to(m) | |
| folium.Marker( | |
| [lat, lon], | |
| popup=f"<b>Parsel Odak Noktası</b><br>Yükseklik: {dem_data['center_elevation']}m<br>Eğim: %{dem_data['slope_pct']}", | |
| icon=folium.Icon(color="red", icon="info-sign") | |
| ).add_to(m) | |
| folium.LayerControl().add_to(m) | |
| st_folium(m, width="100%", height=480) | |
| with col_info: | |
| st.subheader("📊 Arazi & Hidrojeoloji Raporu") | |
| with st.expander("🪨 Litoloji ve Kayaç Yapısı", expanded=True): | |
| st.write(f"**Formasyon Adı:** {litho_data['formation']}") | |
| st.write(f"**Kayaç Türü:** {litho_data['lithology']}") | |
| st.write(f"**Jeolojik Yaş:** {litho_data['age']}") | |
| st.caption(f"Açıklama: {litho_data['desc']}") | |
| with st.expander("📐 Topoğrafya ve Eğim Analizi", expanded=True): | |
| st.write(f"**Maksimum Yükseklik:** {dem_data['max_elevation']} m") | |
| st.write(f"**Minimum Yükseklik:** {dem_data['min_elevation']} m") | |
| st.write(f"**Kot Farkı:** {round(dem_data['max_elevation'] - dem_data['min_elevation'], 2)} m") | |
| slope = dem_data['slope_pct'] | |
| if slope < 5: | |
| st.success("🟢 **Düz / Çok Hafif Eğim (%0-5):** Yapılaşma ve tarım için çok elverişli.") | |
| elif slope < 15: | |
| st.info("🟡 **Orta Eğim (%5-15):** Standart hafriyat ve drenaj tedbirleri gerektirir.") | |
| else: | |
| st.warning("🔴 **Yüksek Eğim (>%15):** Heyelan/kütle hareketi riski kontrol edilmeli.") | |
| with st.expander("💧 Tahmini Hidrojeolojik Değerlendirme"): | |
| st.write("**Drenaj Durumu:** Yüzey akış potansiyeli eğimle orantılıdır.") | |
| st.write(f"**Nem Oranı:** %{weather_data['humidity']}") | |
| st.write(f"**Son Yağış Durumu:** {weather_data['precip']} mm") | |
| st.markdown("---") | |
| st.caption("ℹ️ *Not: TKGM doğrudan parsel sorgu API'sini üçüncü taraf erişimine kapatmıştır.*") |