Anne314159 commited on
Commit
39e53bb
·
verified ·
1 Parent(s): de811ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -139,42 +139,46 @@ def page_article_to_social_post():
139
  st.success('Generated Content:')
140
  st.write(post_content)
141
 
142
- # Corrected Function to get a route from OpenRouteService
143
- def get_route(start_coord, end_coord, api_key):
 
 
 
 
 
144
  api_key = os.getenv('OPENSTREET_API')
145
- url = "https://api.openrouteservice.org/v2/directions/driving-car"
 
 
 
146
  headers = {
147
- 'Authorization': api_key,
148
- 'Content-Type': 'application/json'
149
- }
150
- body = {
151
- "coordinates": [start_coord, end_coord]
152
  }
153
- response = requests.post(url, headers=headers, data=json.dumps(body))
 
 
 
154
  if response.status_code == 200:
155
  route = response.json()['features'][0]['geometry']['coordinates']
156
  return route
157
  else:
158
- st.error("Failed to get route data")
159
  return None
160
 
161
  def page_vacation():
162
  st.title("Route Finder with OpenRouteService")
163
 
164
- # Input for API Key
165
- api_key = st.text_input("Enter your OpenRouteService API Key", type="password")
166
-
167
  # Inputs for start and end coordinates
168
  st.header("Start Point")
169
- start_lat = st.number_input("Start Latitude", value=37.76)
170
- start_lon = st.number_input("Start Longitude", value=-122.4)
171
 
172
  st.header("End Point")
173
- end_lat = st.number_input("End Latitude", value=37.77)
174
- end_lon = st.number_input("End Longitude", value=-122.41)
175
 
176
- if st.button("Find Route") and api_key:
177
- route = get_route([start_lon, start_lat], [end_lon, end_lat], api_key)
178
  if route:
179
  # Convert the route to a DataFrame
180
  route_df = pd.DataFrame(route, columns=['lon', 'lat'])
@@ -183,20 +187,20 @@ def page_vacation():
183
  st.pydeck_chart(pdk.Deck(
184
  map_style='mapbox://styles/mapbox/light-v9',
185
  initial_view_state=pdk.ViewState(
186
- latitude=37.76,
187
- longitude=-122.4,
188
  zoom=11,
189
  pitch=50,
190
  ),
191
  layers=[
192
  pdk.Layer(
193
- 'LineLayer',
194
- data=route_df,
195
- get_source_position='[lon, lat]',
196
- get_target_position='[lon, lat]',
197
- get_color='[255, 0, 0, 160]',
198
- get_width=5,
199
- pickable=True
200
  ),
201
  ],
202
  ))
 
139
  st.success('Generated Content:')
140
  st.write(post_content)
141
 
142
+
143
+ import os
144
+ import requests
145
+ import streamlit as st
146
+ import pandas as pd
147
+
148
+ def get_route(start_coord, end_coord):
149
  api_key = os.getenv('OPENSTREET_API')
150
+ if not api_key:
151
+ st.error("API key not found. Please set the ORS_API_KEY environment variable.")
152
+ return
153
+
154
  headers = {
155
+ 'Accept': 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8',
 
 
 
 
156
  }
157
+ url = f"https://api.openrouteservice.org/v2/directions/driving-car?api_key={api_key}&start={start_coord[0]},{start_coord[1]}&end={end_coord[0]},{end_coord[1]}"
158
+
159
+ response = requests.get(url, headers=headers)
160
+
161
  if response.status_code == 200:
162
  route = response.json()['features'][0]['geometry']['coordinates']
163
  return route
164
  else:
165
+ st.error(f"Failed to get route data. Status code: {response.status_code}")
166
  return None
167
 
168
  def page_vacation():
169
  st.title("Route Finder with OpenRouteService")
170
 
 
 
 
171
  # Inputs for start and end coordinates
172
  st.header("Start Point")
173
+ start_lat = st.number_input("Start Latitude", value=49.41461)
174
+ start_lon = st.number_input("Start Longitude", value=8.681495)
175
 
176
  st.header("End Point")
177
+ end_lat = st.number_input("End Latitude", value=49.420318)
178
+ end_lon = st.number_input("End Longitude", value=8.687872)
179
 
180
+ if st.button("Find Route"):
181
+ route = get_route([start_lon, start_lat], [end_lon, end_lat])
182
  if route:
183
  # Convert the route to a DataFrame
184
  route_df = pd.DataFrame(route, columns=['lon', 'lat'])
 
187
  st.pydeck_chart(pdk.Deck(
188
  map_style='mapbox://styles/mapbox/light-v9',
189
  initial_view_state=pdk.ViewState(
190
+ latitude=49.41461,
191
+ longitude=8.681495,
192
  zoom=11,
193
  pitch=50,
194
  ),
195
  layers=[
196
  pdk.Layer(
197
+ 'LineLayer',
198
+ data=route_df,
199
+ get_source_position='[lon, lat]',
200
+ get_target_position='[lon, lat]',
201
+ get_color='[255, 0, 0, 160]',
202
+ get_width=5,
203
+ pickable=True
204
  ),
205
  ],
206
  ))