Skip to content

Commit

Permalink
fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hbruch committed Oct 23, 2023
1 parent 1820649 commit d651c99
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/services/stops.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _find_stops_around_transformed(self, stopsDataFrame, transformedLine, distan

def _convert_to_dataframe(self, stops):
return gpd.GeoDataFrame([[stop.name, stop.lon, stop.lat,
stop.id, Point(self.projection(stop.lon, stop.lat))] for stop in stops], columns = ['stop_name','x','y','id','geometry'])
stop.id, Point(self.projection(stop.lon, stop.lat))] for stop in stops], columns = ['stop_name','x','y','id','geometry'], crs=self.internal_projection)

def _sort_by_distance(self, stops, transformedLine):
stops['distance']=stops.apply(lambda row: transformedLine.project(row['geometry']), axis=1)
Expand Down
32 changes: 22 additions & 10 deletions app/services/trips.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from app.services.gtfs_constants import *
from app.services.routing import RoutingService, RoutingException
from app.services.stops import is_carpooling_stop
from app.utils.utils import assert_folder_exists, is_older_than_days, yesterday
from app.utils.utils import assert_folder_exists, is_older_than_days, yesterday, geodesic_distance_in_m
from shapely.geometry import Point, LineString, box
from geojson_pydantic.geometries import LineString as GeoJSONLineString
from datetime import datetime, timedelta

import numpy as np
import os
import json
import logging
Expand Down Expand Up @@ -99,6 +99,10 @@ def put_carpool(self, carpool: Carpool):
if existing_carpool and existing_carpool.lastUpdated == carpool.lastUpdated:
enhanced_carpool = existing_carpool
else:
if len(carpool.stops) < 2 or self.distance_in_m(carpool) < 1000:
logger.warning("Failed to add carpool %s:%s to TripStore, distance too low", carpool.agency, carpool.id)
self.handle_failed_carpool_enhancement(carpool)
return
enhanced_carpool = self.transformer.enhance_carpool(carpool)
# TODO should only store enhanced_carpool, if it has 2 or more stops
assert_folder_exists(f'data/enhanced/{carpool.agency}/')
Expand All @@ -109,14 +113,22 @@ def put_carpool(self, carpool: Carpool):
return self._load_as_trip(enhanced_carpool)
except RoutingException as err:
logger.warning("Failed to add carpool %s:%s to TripStore due to RoutingException %s", carpool.agency, carpool.id, getattr(err, 'message', repr(err)))
assert_folder_exists(f'data/failed/{carpool.agency}/')
with open(f'data/failed/{carpool.agency}/{carpool.id}.json', 'w', encoding='utf-8') as f:
f.write(carpool.json())
self.handle_failed_carpool_enhancement(carpool)
except Exception as err:
logger.error("Failed to add carpool %s:%s to TripStore.", carpool.agency, carpool.id, exc_info=True)
assert_folder_exists(f'data/failed/{carpool.agency}/')
with open(f'data/failed/{carpool.agency}/{carpool.id}.json', 'w', encoding='utf-8') as f:
f.write(carpool.json())
self.handle_failed_carpool_enhancement(carpool)

def handle_failed_carpool_enhancement(sellf, carpool: Carpool):
assert_folder_exists(f'data/failed/{carpool.agency}/')
with open(f'data/failed/{carpool.agency}/{carpool.id}.json', 'w', encoding='utf-8') as f:
f.write(carpool.json())

def distance_in_m(self, carpool):
if len(carpool.stops) < 2:
return 0
s1 = carpool.stops[0]
s2 = carpool.stops[-1]
return geodesic_distance_in_m((s1.lon, s1.lat),(s2.lon, s2.lat))

def recently_added_trips(self):
return list(self.recent_trips.values())
Expand Down Expand Up @@ -229,8 +241,8 @@ def enhance_carpool(self, carpool):
if len(virtual_stops) > MAX_STOPS_PER_TRIP:
# in case we found more than MAX_STOPS_PER_TRIP, we retain first and last
# half of MAX_STOPS_PER_TRIP
virtual_stops = virtual_stops[:int(MAX_STOPS_PER_TRIP/2)]+virtual_stops[int(-MAX_STOPS_PER_TRIP/2):]

virtual_stops = virtual_stops.iloc[np.r_[0:int(MAX_STOPS_PER_TRIP/2), int(MAX_STOPS_PER_TRIP/2):]]
trip_id = f"{carpool.agency}:{carpool.id}"
stop_times = self._stops_and_stop_times(carpool.departureTime, trip_id, virtual_stops)

Expand Down
9 changes: 8 additions & 1 deletion app/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
from datetime import datetime, date, timedelta
from pyproj import Geod

def assert_folder_exists(foldername):
if not os.path.isdir(foldername):
Expand Down Expand Up @@ -30,4 +31,10 @@ def yesterday():
return date_days_ago(1)

def date_days_ago(number_of_days):
return date.today() - timedelta(days=number_of_days)
return date.today() - timedelta(days=number_of_days)

def geodesic_distance_in_m(coord1, coord2):
geod = Geod(ellps="WGS84")
lons = [coord1[0], coord2[0]]
lats = [coord1[1], coord2[1]]
return geod.line_lengths(lons, lats)[0]

0 comments on commit d651c99

Please sign in to comment.