File size: 4,548 Bytes
f3e5a17 5a8534e f3e5a17 cb325dd 588f1c0 cb325dd 588f1c0 cb325dd 588f1c0 cb325dd 588f1c0 cb325dd 588f1c0 f3e5a17 588f1c0 f3e5a17 588f1c0 f3e5a17 588f1c0 f3e5a17 588f1c0 f3e5a17 588f1c0 f3e5a17 588f1c0 f3e5a17 588f1c0 f3e5a17 588f1c0 f3e5a17 |
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 pandas as pd
import streamlit as st
from utils.utils_functions import calculate_distances
# Streamlit UI
st.title("Multi Points Distance Calculator")
st.write(
"""This app allows you to calculate the distance between multiple points in 2 datasets.
Please choose a file containing the latitude and longitude columns for each dataset.
"""
)
dataset1_sample_file_path = "samples/Dataset1.xlsx"
dataset2_sample_file_path = "samples/Dataset2.xlsx"
download_col1, download_col2 = st.columns(2)
with download_col1:
st.download_button(
label="Dataset1 Sample File",
data=open(dataset1_sample_file_path, "rb").read(),
file_name="Dataset1.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
with download_col2:
st.download_button(
label="Dataset2 Sample File",
data=open(dataset2_sample_file_path, "rb").read(),
file_name="Dataset2.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
upload_data1_col, upload_data2_col = st.columns(2)
with upload_data1_col:
# Upload Dataset 1
st.subheader("Upload Dataset 1 (Reference Points)")
file1 = st.file_uploader("Upload first dataset (Excel)", type=["xlsx"], key="file1")
with upload_data2_col:
# Upload Dataset 2
st.subheader("Upload Dataset 2 (Comparison Points)")
file2 = st.file_uploader(
"Upload second dataset (Excel)", type=["xlsx"], key="file2"
)
if file1 and file2:
param_col1, param_col2 = st.columns(2)
try:
# Read the datasets
df1 = pd.read_excel(file1)
df2 = pd.read_excel(file2)
with param_col1:
st.subheader("Select Columns for Dataset 1")
code_col1 = st.selectbox("Select 'CODE' Column", df1.columns, key="code1")
lat_col1 = st.selectbox("Select 'Latitude' Column", df1.columns, key="lat1")
long_col1 = st.selectbox(
"Select 'Longitude' Column", df1.columns, key="long1"
)
with param_col2:
st.subheader("Select Columns for Dataset 2")
code_col2 = st.selectbox("Select 'CODE' Column", df2.columns, key="code2")
lat_col2 = st.selectbox("Select 'Latitude' Column", df2.columns, key="lat2")
long_col2 = st.selectbox(
"Select 'Longitude' Column", df2.columns, key="long2"
)
min_distance = st.number_input(
"Minimum Distance (km)", min_value=0.0, value=5.0
)
# Calculate distances when button is clicked
if st.button("Calculate Distances"):
df_distances, df_closest, df_closest_min_distance = calculate_distances(
df1,
df2,
code_col1,
lat_col1,
long_col1,
code_col2,
lat_col2,
long_col2,
min_distance,
)
# # Display all distances
# st.subheader("All Distances")
# st.dataframe(df_distances)
# Display closest points
st.subheader("Closest Matches")
st.dataframe(df_closest)
st.subheader("Closest Matches below Min Distance")
st.dataframe(df_closest_min_distance)
# Downloadable All distances CSV
st.download_button(
label="Download All Distances as CSV",
data=df_distances.to_csv(index=False),
file_name="all_distances.csv",
mime="text/csv",
on_click="ignore",
type="primary",
)
# Downloadable Closest matches CSV
st.download_button(
label="Download Closest Matches as CSV",
data=df_closest.to_csv(index=False),
file_name="closest_matches.csv",
mime="text/csv",
on_click="ignore",
type="primary",
)
# Downloadable Closest matches below Min Distance CSV
st.download_button(
label=f"Download Closest Matches below {min_distance}km as CSV",
data=df_closest_min_distance.to_csv(index=False),
file_name=f"closest_matches_{min_distance}km.csv",
mime="text/csv",
on_click="ignore",
type="primary",
)
except Exception as e:
st.error(f"Error processing files: {e}")
|