safety_map / app.py
dltmdgus's picture
Create app.py
a5b25f1 verified
raw
history blame
2.2 kB
import streamlit as st
from geopy.geocoders import Nominatim
import pandas as pd
import folium
from folium.plugins import HeatMap
from geopy.distance import geodesic
import numpy as np
import warnings
# κ²½κ³  λ©”μ‹œμ§€λ₯Ό λ¬΄μ‹œν•˜κΈ°
warnings.filterwarnings("ignore", category=UserWarning, module="geopy")
# Geolocator μ„€μ •
geolocator = Nominatim(user_agent="hwaseong_locator")
# μœ„λ„μ™€ 경도λ₯Ό μ£Όμ†Œλ‘œ λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜
def get_location_name(lat, lon):
location = geolocator.reverse((lat, lon), language='ko')
return location.address if location else "μ£Όμ†Œλ₯Ό 찾을 수 μ—†μŒ"
# μœ„ν—˜λ„ λΆ„λ₯˜ ν•¨μˆ˜
def classify_risk(score):
if score >= 2:
return "맀우 μ•ˆμ „"
elif 1 <= score < 2:
return "μ•ˆμ „"
elif 0 <= score < 1:
return "보톡"
elif -1 < score < 0:
return "μœ„ν—˜"
else:
return "맀우 μœ„ν—˜"
# ν™”μ„±μ‹œ 쀑심 μ’Œν‘œ
hwaseong_center = [37.198, 127.034]
# μ‚¬μš©μž μž…λ ₯을 λ°›κΈ°
st.title("ν™”μ„±μ‹œ μœ„μΉ˜ μœ„ν—˜λ„ 뢄석기")
lat_input = st.number_input("μœ„λ„ (Latitude)", value=37.198, format="%.6f")
lon_input = st.number_input("경도 (Longitude)", value=127.034, format="%.6f")
# μ‹€μ œ μ£Όμ†Œ 확인
location_name = get_location_name(lat_input, lon_input)
st.write(f"μœ„μΉ˜: {location_name}")
# μ˜ˆμ‹œ 데이터 (κ°„λ‹¨νžˆ μ •ν•΄λ‘” μ˜ˆμ‹œμž…λ‹ˆλ‹€. μ‹€μ œλ‘œλŠ” 이 데이터λ₯Ό 기반으둜 κ³„μ‚°ν•©λ‹ˆλ‹€.)
sample_scores = {
(37.198, 127.034): 3, # μ˜ˆμ‹œ μ•ˆμ „ μ§€μ—­
(37.198, 127.035): -2, # μ˜ˆμ‹œ μœ„ν—˜ μ§€μ—­
}
# ν•΄λ‹Ή μœ„μΉ˜μ— λŒ€ν•œ 점수 μ°ΎκΈ°
def get_score_for_location(lat, lon):
closest_location = min(sample_scores.keys(), key=lambda x: geodesic((lat, lon), x).meters)
return sample_scores[closest_location]
score = get_score_for_location(lat_input, lon_input)
risk_category = classify_risk(score)
st.write(f"μœ„ν—˜λ„: {risk_category}")
# 지도 ν‘œμ‹œ
m = folium.Map(location=[lat_input, lon_input], zoom_start=16)
folium.Marker([lat_input, lon_input], popup=location_name).add_to(m)
# κ²°κ³Όλ₯Ό HTML둜 μ €μž₯
m.save("location_map.html")
# λ‹€μš΄λ‘œλ“œ 링크
st.download_button("μœ„μΉ˜ 지도 λ‹€μš΄λ‘œλ“œ", "location_map.html")