Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,96 +1,72 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import pandas as pd
|
3 |
-
import
|
4 |
from streamlit_folium import st_folium
|
5 |
-
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
@st.
|
12 |
def load_farm_data():
|
13 |
-
return pd.read_csv(
|
14 |
-
|
15 |
-
farm_data = load_farm_data()
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
<script>
|
21 |
-
navigator.geolocation.getCurrentPosition(
|
22 |
-
(position) => {
|
23 |
-
const latitude = position.coords.latitude;
|
24 |
-
const longitude = position.coords.longitude;
|
25 |
-
document.getElementById("user_lat").value = latitude;
|
26 |
-
document.getElementById("user_lon").value = longitude;
|
27 |
-
document.getElementById("submit_button").click();
|
28 |
-
}
|
29 |
-
);
|
30 |
-
</script>
|
31 |
-
<input type="hidden" id="user_lat" name="latitude">
|
32 |
-
<input type="hidden" id="user_lon" name="longitude">
|
33 |
-
<form action="#" method="get">
|
34 |
-
<input type="submit" id="submit_button" style="display:none">
|
35 |
-
</form>
|
36 |
-
""",
|
37 |
-
unsafe_allow_html=True,
|
38 |
-
)
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
user_lat = query_params.get("latitude", [None])[0]
|
43 |
-
user_lon = query_params.get("longitude", [None])[0]
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
user_lon = float(user_lon)
|
48 |
-
st.success(f"مختصات شما: {user_lat}, {user_lon}")
|
49 |
|
50 |
-
#
|
|
|
51 |
farm_name = st.sidebar.text_input("نام مزرعه را وارد کنید:")
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
# مختصات مزرعه
|
64 |
-
farm_lat = farm.iloc[0]["latitude"]
|
65 |
-
farm_lon = farm.iloc[0]["longitude"]
|
66 |
|
|
|
67 |
if user_lat and user_lon:
|
68 |
-
# محاسبه فاصله بین کاربر و مزرعه
|
69 |
user_location = (user_lat, user_lon)
|
70 |
farm_location = (farm_lat, farm_lon)
|
71 |
-
distance =
|
72 |
-
st.write(f"فاصله
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
m = folium.Map(location=map_center, zoom_start=13, tiles="CartoDB Satellite")
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
|
86 |
-
|
|
|
87 |
folium.Marker(
|
88 |
location=[user_lat, user_lon],
|
89 |
popup="موقعیت شما",
|
90 |
icon=folium.Icon(color="blue"),
|
91 |
).add_to(m)
|
92 |
|
93 |
-
#
|
94 |
folium.PolyLine(
|
95 |
locations=[(user_lat, user_lon), (farm_lat, farm_lon)],
|
96 |
color="red",
|
@@ -98,9 +74,9 @@ if farm_name:
|
|
98 |
opacity=1,
|
99 |
).add_to(m)
|
100 |
|
101 |
-
|
102 |
-
|
103 |
else:
|
104 |
st.error("مزرعهای با این نام یافت نشد.")
|
105 |
else:
|
106 |
-
st.sidebar.write("
|
|
|
1 |
import streamlit as st
|
2 |
+
import ee
|
3 |
import pandas as pd
|
4 |
+
import geopy.distance
|
5 |
from streamlit_folium import st_folium
|
6 |
+
import folium
|
7 |
|
8 |
+
# Initialize Google Earth Engine
|
9 |
+
service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
|
10 |
+
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
|
11 |
+
ee.Initialize(credentials)
|
12 |
|
13 |
+
# Load the CSV file
|
14 |
+
@st.cache
|
15 |
def load_farm_data():
|
16 |
+
return pd.read_csv('Farm_Details_Export.csv') # Replace with your CSV file path
|
|
|
|
|
17 |
|
18 |
+
# Define a function to calculate route distance
|
19 |
+
def calculate_route(user_location, farm_location):
|
20 |
+
return geopy.distance.distance(user_location, farm_location).km
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# Streamlit app
|
23 |
+
st.title("مسیریابی مزارع نیشکر دهخدا")
|
|
|
|
|
24 |
|
25 |
+
# Load farm data
|
26 |
+
farm_data = load_farm_data()
|
|
|
|
|
27 |
|
28 |
+
# Sidebar for user inputs
|
29 |
+
st.sidebar.title("جستجوی مزرعه")
|
30 |
farm_name = st.sidebar.text_input("نام مزرعه را وارد کنید:")
|
31 |
|
32 |
+
# User location input
|
33 |
+
user_lat = st.sidebar.number_input("عرض جغرافیایی شما:")
|
34 |
+
user_lon = st.sidebar.number_input("طول جغرافیایی شما:")
|
35 |
|
36 |
+
if farm_name:
|
37 |
+
# Find the farm in the dataset
|
38 |
+
farm_row = farm_data[farm_data['FarmName'] == farm_name]
|
39 |
+
if not farm_row.empty:
|
40 |
+
farm_lat = farm_row.iloc[0]['Latitude']
|
41 |
+
farm_lon = farm_row.iloc[0]['Longitude']
|
|
|
|
|
|
|
42 |
|
43 |
+
# Calculate route distance
|
44 |
if user_lat and user_lon:
|
|
|
45 |
user_location = (user_lat, user_lon)
|
46 |
farm_location = (farm_lat, farm_lon)
|
47 |
+
distance = calculate_route(user_location, farm_location)
|
48 |
+
st.sidebar.write(f"فاصله تا مزرعه: {distance:.2f} کیلومتر")
|
49 |
|
50 |
+
# Display the map
|
51 |
+
st.write(f"نمایش مزرعه {farm_name} روی نقشه:")
|
52 |
+
m = folium.Map(location=[farm_lat, farm_lon], zoom_start=15, tiles="CartoDB Satellite")
|
|
|
53 |
|
54 |
+
# Add marker for the farm
|
55 |
+
folium.Marker(
|
56 |
+
location=[farm_lat, farm_lon],
|
57 |
+
popup=f"مزرعه: {farm_name}",
|
58 |
+
icon=folium.Icon(color="green"),
|
59 |
+
).add_to(m)
|
60 |
|
61 |
+
# Add marker for user location (if provided)
|
62 |
+
if user_lat and user_lon:
|
63 |
folium.Marker(
|
64 |
location=[user_lat, user_lon],
|
65 |
popup="موقعیت شما",
|
66 |
icon=folium.Icon(color="blue"),
|
67 |
).add_to(m)
|
68 |
|
69 |
+
# Draw line between user and farm
|
70 |
folium.PolyLine(
|
71 |
locations=[(user_lat, user_lon), (farm_lat, farm_lon)],
|
72 |
color="red",
|
|
|
74 |
opacity=1,
|
75 |
).add_to(m)
|
76 |
|
77 |
+
# Render map
|
78 |
+
st_folium(m, width=800, height=600)
|
79 |
else:
|
80 |
st.error("مزرعهای با این نام یافت نشد.")
|
81 |
else:
|
82 |
+
st.sidebar.write("نام مزرعه را وارد کنید تا روی نقشه نمایش داده شود.")
|