Spaces:
Sleeping
Sleeping
File size: 4,399 Bytes
bbe788d 940f5b5 897ea2d bbe788d 0697975 bbe788d c9c2156 bbe788d 643a822 d5bb172 643a822 b5290a2 c9c2156 bbe788d b5290a2 c9c2156 b5290a2 9c795d0 80eb9a3 b5290a2 0f74558 80eb9a3 bbe788d b5290a2 643a822 bbe788d b3053e9 bbe788d fff8af2 d669f57 fff8af2 efca6f0 fd31ef9 efca6f0 897ea2d fd31ef9 897ea2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import streamlit as st
import pandas as pd
import numpy as np
import openpyxl
from sklearn.neighbors import KNeighborsRegressor
from geopy.distance import geodesic
# Set wide mode
st.set_page_config(layout="wide")
# Set dark theme
st.markdown(
"""
<style>
body {
color: white;
background-color: #1e1e1e;
}
.st-df-header, .st-df-body, .st-df-caption {
color: #f8f9fa; /* Bootstrap table header text color */
}
.st-eb {
background-color: #343a40; /* Streamlit exception box background color */
}
</style>
""",
unsafe_allow_html=True
)
# Create a DataFrame with sample data
data = pd.read_excel('ven_ter_fim_PEDÓ.xlsx')
# Function to calculate distance in meters between two coordinates
def calculate_distance(lat1, lon1, lat2, lon2):
coords_1 = (lat1, lon1)
coords_2 = (lat2, lon2)
return geodesic(coords_1, coords_2).meters
# Find the maximum distance between coordinates
max_distance = 0
for index, row in data.iterrows():
distance = calculate_distance(row['latitude'], row['longitude'], data['latitude'].mean(), data['longitude'].mean())
if distance > max_distance:
max_distance = distance
# Calculate a zoom level based on the maximum distance
zoom_level = round(15 - np.log10(max_distance))
# Create a sidebar for controls
with st.sidebar:
# Display a title
st.title('avalia.se')
# Dropdown to select specific coordinates
selected_coords = st.selectbox('Selecione Coordenadas', ['Random', 'Custom'])
if selected_coords == 'Custom':
custom_lat = st.number_input('Enter Latitude', value=-29.45086)
custom_lon = st.number_input('Enter Longitude', value=-51.9847)
radius_visible = True # Show radius slider for custom coordinates
else:
custom_lat, custom_lon = data['latitude'].mean(), data['longitude'].mean()
radius_visible = False # Hide radius slider for random coordinates
# Slider for setting the zoom level
zoom_level = st.slider('Nível de zoom', min_value=1, max_value=15, value=zoom_level)
# Conditionally render the radius slider
if radius_visible:
radius_in_meters = st.slider('Selecione raio (em metros)', min_value=100, max_value=5000, value=1000)
if selected_coords == 'Custom' and radius_visible:
# Filter data based on the radius and drop NaN values
filtered_data = data[data.apply(lambda x: calculate_distance(x['latitude'], x['longitude'], custom_lat, custom_lon), axis=1) <= radius_in_meters]
filtered_data = filtered_data.dropna()
# Apply KNN and get predicted Vunit values
predicted_vunit = knn_predict(filtered_data, 'Vunit', knn_features)
# Add predicted Vunit values to filtered_data
filtered_data['Predicted_Vunit'] = predicted_vunit
# Add a custom CSS class to the map container
st.markdown(f"""<style>
.map {{
width: 100%;
height: 100vh;
}}
</style>""", unsafe_allow_html=True)
# Wrap the map in a container with the custom CSS class
with st.container():
st.map(filtered_data, zoom=zoom_level, use_container_width=True)
# Function to apply KNN and return Vunit values
def knn_predict(df, target_column, features_columns, k=5):
# Separate features and target variable
X = df[features_columns]
y = df[target_column]
# Create KNN regressor
knn = KNeighborsRegressor(n_neighbors=k)
# Fit the model
knn.fit(X, y)
# Use the model to predict Vunit for the filtered_data
predictions = knn.predict(filtered_data[features_columns])
return predictions
# Features columns for KNN
knn_features = ['latitude', 'longitude', 'Area'] # Add other relevant features
# Check if KNN should be applied
if selected_coords == 'Custom' and radius_visible:
# Apply KNN and get predicted Vunit values
predicted_vunit = knn_predict(data, 'Vunit', knn_features)
# Add predicted Vunit values to filtered_data
filtered_data['Predicted_Vunit'] = predicted_vunit
# Display the map and filtered_data
with st.container():
if selected_coords == 'Custom':
st.map(filtered_data, zoom=zoom_level, use_container_width=True)
# Display the predicted Vunit values if applicable
if 'Predicted_Vunit' in filtered_data.columns:
st.write("Predicted Vunit Values:")
st.write(filtered_data[['latitude', 'longitude', 'Vunit', 'Predicted_Vunit']]) |