File size: 3,994 Bytes
ac64203
 
 
95d668a
ac64203
 
 
 
 
 
 
 
 
 
 
 
 
fcdd232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac64203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95d668a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac64203
 
 
 
 
 
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
import pandas as pd
import streamlit as st
from lat_lon_parser import parse, to_str_deg_min_sec
from st_aggrid import AgGrid


class DataFrames:
    Dframe = pd.DataFrame()


st.title("GPS Coordinate Converter")
st.write(
    """This program allows you to convert coordinates from degree-minute-second (13°15'6.20"N) to decimal (13.2517222222222) and vice versa.
Please choose a file containing the latitude and longitude columns.
                    """
)

decimal_to_degrees_sample_file_path = "samples/Decimal_to_DMS.xlsx"
degrees_to_decimal_sample_file_path = "samples/DMS_to_Decimal.xlsx"

col1, col2, col3 = st.columns(3)

with col1:
    st.download_button(
        label="Download Decimal_to_DMS Sample File",
        data=open(decimal_to_degrees_sample_file_path, "rb").read(),
        file_name="Decimal_to_DMS.xlsx",
        mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )
with col2:
    st.download_button(
        label="Download DMS_to_Decimal Sample File",
        data=open(degrees_to_decimal_sample_file_path, "rb").read(),
        file_name="DMS_to_Decimal.xlsx",
        mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )

uploaded_file = st.file_uploader("Choose a file", type=["xlsx"])
col1_list = []
if uploaded_file is not None:
    DataFrames.Dframe = pd.read_excel(uploaded_file, keep_default_na=False)
    col1_list = DataFrames.Dframe.columns.tolist()

    latitude_dd = st.selectbox("Choose Latitude Column", options=col1_list)
    longitude_dd = st.selectbox("Choose Longitude Column", options=col1_list)

    conversion_choice = st.selectbox(
        "Choose Conversion Type", options=["Decimal", "DegMinSec"]
    )

    if st.button("CONVERT", type="primary"):

        try:
            # if not latitude_dd or not longitude_dd:
            #     st.error("Please choose the latitude and longitude columns")

            # if DataFrames.Dframe.empty:
            #     st.error("Please choose a file first")

            df = DataFrames.Dframe.copy()

            df["converted_latitude"] = df[latitude_dd]
            df["converted_longitude"] = df[longitude_dd]

            if conversion_choice == "Decimal":
                df["converted_longitude"] = df["converted_longitude"].str.replace(
                    "O", "W"
                )
                df["converted_latitude"] = df["converted_latitude"].apply(parse)
                df["converted_longitude"] = df["converted_longitude"].apply(parse)
            else:
                df["converted_latitude"] = df["converted_latitude"].apply(
                    to_str_deg_min_sec
                )
                df["converted_longitude"] = df["converted_longitude"].apply(
                    to_str_deg_min_sec
                )
                df["converted_latitude"] = df["converted_latitude"].apply(
                    lambda x: x.replace("-", "") + "S" if "-" in x else x + "N"
                )
                df["converted_longitude"] = df["converted_longitude"].apply(
                    lambda x: x.replace("-", "") + "W" if "-" in x else x + "E"
                )

            DataFrames.Dframe = df
            st.success("Coordinates converted Sucessfully")

            @st.fragment
            def table_data():
                if DataFrames.Dframe is not None:
                    AgGrid(
                        DataFrames.Dframe,
                        fit_columns_on_grid_load=True,
                        theme="streamlit",
                        enable_enterprise_modules=True,
                        filter=True,
                        # columns_auto_size_mode=ColumnsAutoSizeMode.FIT_CONTENTS,
                    )

            table_data()

        except Exception as e:
            st.error(
                f"An error occurred. Make sure the file contains the latitude and longitude columns. Error: {e}"
            )
else:
    st.info("Please choose a file containing the latitude and longitude columns")