|
import pandas as pd |
|
import streamlit as st |
|
|
|
from utils.utils_functions import calculate_distances |
|
|
|
|
|
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: |
|
|
|
st.subheader("Upload Dataset 1 (Reference Points)") |
|
file1 = st.file_uploader("Upload first dataset (Excel)", type=["xlsx"], key="file1") |
|
|
|
with upload_data2_col: |
|
|
|
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: |
|
|
|
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 |
|
) |
|
|
|
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, |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
st.subheader("Closest Matches") |
|
st.dataframe(df_closest) |
|
st.subheader("Closest Matches below Min Distance") |
|
st.dataframe(df_closest_min_distance) |
|
|
|
|
|
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", |
|
) |
|
|
|
|
|
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", |
|
) |
|
|
|
|
|
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}") |
|
|