-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocoding_app.py
More file actions
63 lines (51 loc) · 2.79 KB
/
Copy pathgeocoding_app.py
File metadata and controls
63 lines (51 loc) · 2.79 KB
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
import pandas as pd
import streamlit as st
from crown_dependencies import geocode_crown_dependencies as gcd
st.title('Postcode lookup')
#st.write("If entering multiple postcodes, add one per line")
if 'input' not in st.session_state:
st.session_state.input = ''
text_input = st.text_area(label="Enter postcodes", placeholder="Add one postcode per line")
text_split = text_input.splitlines()
query_list = []
for i in text_split:
query_list.append(i.upper().strip())
crown_dep_pcds = ('JE', 'GY', 'IM')
# strip whitespace and force uppercase
#text_input = text_input.upper().replace(" ", "")
if st.button(label="Geocode", type="primary"):
if len(query_list) > 0:
with st.spinner('Loading data ...'):
# if PCD starts with GY, JE, IM
# load alt sources
# display message about accuracy
df = pd.read_feather('./data/onspd_nov2023.feather')
df = df[['pcds', 'lat', 'long']]
# results = df[df.pcds.isin(query_list)]
results = pd.DataFrame(query_list).rename(columns={0: 'pcds'}).merge(df, how='left') # this is slower but preserves the input order
# process crown dependencies
df_crown_deps = results[results.pcds.str.startswith(crown_dep_pcds)]
results_no_cd = results[~results.pcds.str.startswith(crown_dep_pcds)]
df_crown_deps_gcd = gcd(list(df_crown_deps.pcds.values))
df_crown_deps_gcd = df_crown_deps_gcd.reset_index().rename(columns={'index':'pcds'})
results = pd.concat([results_no_cd, df_crown_deps_gcd])
if len(results) < 2:
results_title = "# Search result"
else:
results_title = "# Search results"
else:
st.write("No query entered")
if len(results)> 0:
st.markdown(results_title)
matched = len(results[~results.lat.isna()])
st.write(f"**{matched}** matches from **{len(query_list)}** inputs ({round(matched/len(query_list) * 100)}\%)")
st.dataframe(results, hide_index=True, use_container_width=True)
st.write("_Select cells to copy and paste or click the download icon to download as CSV_")
if results.pcds.str.startswith(crown_dep_pcds).any():
st.write("Note: `GY`, `JE` and `IM` postcode locations are only approximate, accurate to the district level")
else:
st.markdown(results_title)
st.write("No match found")
st.markdown("""---""")
st.write("Data source: Office for National Statistics licensed under the [Open Government Licence v.3.0](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/)")
st.write("Code available [on GitHub](https://github.com/ioalexei/postcode_lookup_streamlit). Something broken? [Report it here](https://github.com/ioalexei/postcode_lookup_streamlit/issues).")