Skip to content

Commit

Permalink
fixed some functions in StandardTripsCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
ialokim authored and grote committed Mar 1, 2018
1 parent f72ab11 commit bc7aaa8
Showing 1 changed file with 57 additions and 16 deletions.
73 changes: 57 additions & 16 deletions osm2gtfs/creators/trips_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def _prepare_trips(self, feed, schedule, itinerary):
if input_fr == itinerary.fr and input_to == itinerary.to:
trip_services = trip["services"]
for service in trip_services:
services.append(service)
if service not in services:
services.append(service)

if not services:
print(" Warning: From and to values didn't match with schedule.")
Expand Down Expand Up @@ -124,14 +125,32 @@ def _verify_data(self, schedule, line, itinerary):
str(line.route_id) + ")")
print(" " + itinerary.osm_url)
print(" " + line.osm_url)
return True
return False

# Check if time information in schedule can be found for
# the itinerary
if itinerary.route_id not in schedule['lines']:
print(" Warning: Route not found in schedule.")
print("Warning: Route not found in schedule.")
return False

# Check if from and to tags are valid and correspond to
# the actual name of the first and last stop of the itinerary.
for trip in schedule['lines'][itinerary.route_id]:
if (trip["from"] == itinerary.fr and trip["to"] == itinerary.to):
trip_stations = trip["stations"]
if trip_stations[0] != itinerary.fr:
print("Warning: The first station in the stations list of trip (" +
str(itinerary.route_id) + ") doesn't match first station of itinerary.")
print(" " + itinerary.osm_url)
print(" Please review the schedule file.")
return False
elif trip_stations[-1] != itinerary.to:
print("Warning: The last station in the stations list of trip (" +
str(itinerary.route_id) + ") doesn't match last station of itinerary.")
print(" " + itinerary.osm_url)
print(" Please review the schedule file.")
return False

return True

def _add_shape_to_feed(self, feed, shape_id, itinerary):
Expand Down Expand Up @@ -165,16 +184,16 @@ def _add_itinerary_trips(self, feed, itinerary, line, trip_builder,
gtfs_trip = route.AddTrip(feed, headsign=itinerary.to,
service_period=trip_builder['service_period'])
trips_count += 1
search_idx = 0

# Go through all stops of an itinerary
for itinerary_stop_id in itinerary.get_stops():
for itinerary_stop_idx, itinerary_stop_id in enumerate(itinerary.get_stops()):

# Load full stop object
try:
itinerary_stop = trip_builder[
'all_stops']['regular'][itinerary_stop_id]
except ValueError:

sys.stderr.write(
"Itinerary (" + itinerary.route_url + ") misses a stop: \n")
sys.stderr.write(
Expand All @@ -187,20 +206,40 @@ def _add_itinerary_trips(self, feed, itinerary, line, trip_builder,
except ValueError:
print("Warning: Stop in itinerary was not found in GTFS.")
print(" " + itinerary_stop.osm_url)
continue

# Make sure we compare same unicode encoding
if type(itinerary_stop.name) is str:
itinerary_stop.name = itinerary_stop.name.decode('utf-8')

time = "-"
schedule_stop_idx = -1
# Check if we have specific time information for this stop.
try:
time = trip[trip_builder['stops'].index(itinerary_stop.name)]
schedule_stop_idx = trip_builder['stops'].index(itinerary_stop.name, search_idx)
except ValueError:
pass

# Validate time information
if time != "-":
if itinerary_stop.get_parent_station() is not None:
# If stop name not found, check for the parent_station name, too.
itinerary_station = trip_builder[
'all_stops']['stations'][str(itinerary_stop.get_parent_station())]
if type(itinerary_station.name) is str:
itinerary_station.name = itinerary_station.name.decode('utf-8')
try:
schedule_stop_idx = trip_builder[
'stops'].index(itinerary_station.name, search_idx)
except ValueError:
pass

# Make sure the last stop of itinerary will keep being the last stop in GTFS
last_stop_schedule = schedule_stop_idx == len(trip_builder['stops']) - 1
last_stop_itinerary = itinerary_stop_idx == len(itinerary.get_stops()) - 1
if last_stop_schedule != last_stop_itinerary:
schedule_stop_idx = -1

if schedule_stop_idx != -1:
time = trip[schedule_stop_idx]
search_idx = schedule_stop_idx + 1

# Validate time information
try:
time_at_stop = str(
datetime.strptime(time, "%H:%M").time())
Expand All @@ -216,10 +255,10 @@ def _add_itinerary_trips(self, feed, itinerary, line, trip_builder,
else:
try:
gtfs_trip.AddStopTime(gtfs_stop)
except ValueError:
print("Warning: Could not add first a stop to trip.")
except transitfeed.problems.OtherProblem:
print("Warning: Could not add first stop to trip without time information.")
print(" " + itinerary_stop.name +
" - " + itinerary_stop.osm_id)
" - " + itinerary_stop.osm_url)
break

# Add reference to shape
Expand Down Expand Up @@ -296,13 +335,14 @@ def _load_itinerary_schedule(self, schedule, itinerary, service):
:return times: List of strings
"""
times = None
times = []
for trip in schedule['lines'][itinerary.route_id]:
trip_services = trip["services"]
if (trip["from"] == itinerary.fr and
trip["to"] == itinerary.to and
service in trip_services):
times = trip["times"]
for time in trip["times"]:
times.append(time)
if times is None:
print("Warning: Couldn't load times from schedule for route")
return times
Expand All @@ -322,4 +362,5 @@ def _load_scheduled_stops(self, schedule, itinerary, service):
"to"] == itinerary.to and service in trip_services):
for stop in trip["stations"]:
stops.append(stop)
break
return stops

0 comments on commit bc7aaa8

Please sign in to comment.