KeshavRa commited on
Commit
1f2416b
·
verified ·
1 Parent(s): b078700

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -2
app.py CHANGED
@@ -1,6 +1,51 @@
1
  import streamlit as st
2
  import json
3
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def validate_zipcode(zipcode):
6
  try:
@@ -90,8 +135,26 @@ else:
90
  # filter domestic violence
91
  if data['Domestic Violence'] == 'No':
92
  shelters = shelters[(shelters['Domestic Violence'] == "No")]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  st.table(shelters)
94
- print(type(shelters))
95
 
96
  # Display the current shelter information
97
  shelter = shelters_def[st.session_state.shelter_index]
@@ -111,4 +174,4 @@ else:
111
  if st.button("Next"):
112
  if st.session_state.shelter_index < len(shelters_def) - 1:
113
  st.session_state.shelter_index += 1
114
- st.experimental_rerun()
 
1
  import streamlit as st
2
  import json
3
  import pandas as pd
4
+ import requests
5
+
6
+ def get_coordinates(address: str, api_key: str) -> list:
7
+ """
8
+ Get the coordinates (latitude and longitude) of an address using the OpenCage Data Geocoding API.
9
+
10
+ Parameters:
11
+ address (str): The address to geocode.
12
+ api_key (str): Your OpenCage Data API key.
13
+
14
+ Returns:
15
+ list: A list containing the latitude and longitude of the address.
16
+ """
17
+ # Encode the address for the URL
18
+ encoded_address = requests.utils.quote(address)
19
+
20
+ # Construct the API URL
21
+ url = f"https://api.opencagedata.com/geocode/v1/json?q={encoded_address}&key={api_key}"
22
+
23
+ # Make the API request
24
+ response = requests.get(url)
25
+
26
+ if response.status_code == 200:
27
+ # Parse the JSON response
28
+ data = response.json()
29
+
30
+ # Check if there are any results
31
+ if data['results']:
32
+ # Extract the latitude and longitude from the first result
33
+ latitude = data['results'][0]['geometry']['lat']
34
+ longitude = data['results'][0]['geometry']['lng']
35
+ return [latitude, longitude]
36
+ else:
37
+ raise ValueError("No results found for the given address.")
38
+ else:
39
+ raise Exception(f"API request failed with status code {response.status_code}")
40
+
41
+ def haversine(lat1, lon1, lat2, lon2):
42
+ R = 6371 # Earth radius in kilometers. Use 3956 for miles.
43
+ dlat = math.radians(lat2 - lat1)
44
+ dlon = math.radians(lon2 - lon1)
45
+ a = math.sin(dlat / 2) ** 2 + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon / 2) ** 2
46
+ c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
47
+ distance = R * c
48
+ return distance
49
 
50
  def validate_zipcode(zipcode):
51
  try:
 
135
  # filter domestic violence
136
  if data['Domestic Violence'] == 'No':
137
  shelters = shelters[(shelters['Domestic Violence'] == "No")]
138
+
139
+ # calculate distances between zipcodes
140
+ api_key = os.environ['OpenCage_API_KEY']
141
+
142
+ shelters_coordinates = shelters.apply(lambda row: get_coordinates(row['Zip Code'], api_key), axis=1).tolist()
143
+ user_coordinates = get_coordinates(data['Zip Code'])
144
+
145
+ distances = []
146
+ for coordinates in shelters_coordinates:
147
+ distances.append(haversine(coordinates[0], coordinates[1], user_coordinates[0], user_coordinates[1]))
148
+
149
+ shelters['zipcode_score'] = distances
150
+
151
+ # urgency
152
+
153
+ # duration
154
+
155
+ # services
156
+
157
  st.table(shelters)
 
158
 
159
  # Display the current shelter information
160
  shelter = shelters_def[st.session_state.shelter_index]
 
174
  if st.button("Next"):
175
  if st.session_state.shelter_index < len(shelters_def) - 1:
176
  st.session_state.shelter_index += 1
177
+ st.experimental_rerun()