Skip to content

Commit

Permalink
Improved handling of human readable times, refs #11192
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertHilbrich committed Feb 21, 2024
1 parent d62f011 commit 859a86e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
25 changes: 15 additions & 10 deletions tools/import/gtfs/gtfs2osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
import sumolib # noqa
from sumolib.xml import parse_fast_nested # noqa
from sumolib.miscutils import benchmark # noqa
from sumolib.miscutils import benchmark, parseTime, humanReadableTime # noqa

# ----------------------- gtfs, osm and sumo modes ----------------------------
OSM2SUMO_MODES = {
Expand Down Expand Up @@ -650,6 +650,9 @@ def write_gtfs_osm_outputs(options, map_routes, map_stops, missing_stops, missin
if options.verbose:
print("Generates stops and routes output")

# determine if we need to format times (depart, duration, until) to be human readable or whole seconds
ft = humanReadableTime if "hrtime" in options and options.hrtime else lambda x: int(x)

with sumolib.openz(options.additional_output, mode='w') as output_file:
sumolib.xml.writeHeader(output_file, root="additional")
for stop, value in map_stops.items():
Expand Down Expand Up @@ -718,10 +721,10 @@ def write_gtfs_osm_outputs(options, map_routes, map_stops, missing_stops, missin
seqs[stopSeq] = row.trip_id
veh_attr = (row.trip_id, day,
main_shape, row.route_id, seqs[stopSeq],
row.arrival_fixed.days + day,
str(row.arrival_fixed).split(' ')[2],
ft(parseTime(str(row.arrival_fixed.days + day) +
":" + str(row.arrival_fixed).split(' ')[2])),
min(stop_index), max(stop_index), pt_type, pt_color)
output_file.write(u' <vehicle id="%s.%s" route="%s" line="%s_%s" depart="%s:%s" departEdge="%s" arrivalEdge="%s" type="%s"%s>\n' % veh_attr) # noqa
output_file.write(u' <vehicle id="%s.%s" route="%s" line="%s_%s" depart="%s" departEdge="%s" arrivalEdge="%s" type="%s"%s>\n' % veh_attr) # noqa
output_file.write(u' <param key="gtfs.route_name" value=%s/>\n' %
sumolib.xml.quoteattr(str(row.route_short_name), True))
if row.trip_headsign:
Expand All @@ -736,13 +739,15 @@ def write_gtfs_osm_outputs(options, map_routes, map_stops, missing_stops, missin
if stop_index >= check_seq:
check_seq = stop_index
# TODO check stop position if we are on the same edge as before
stop_attr = (stop.stop_item_id, stop.arrival_fixed.days + day,
str(stop.arrival_fixed).split(' ')[2],
options.duration, stop.departure_fixed.days + day,
str(stop.departure_fixed).split(' ')[
2], stop.stop_sequence, stop_list.stop_sequence.max(),
stop_attr = (stop.stop_item_id,
ft(parseTime(str(stop.arrival_fixed.days + day) +
":" + str(stop.arrival_fixed).split(' ')[2])),
ft(options.duration),
ft(parseTime(str(stop.departure_fixed.days + day) +
":" + str(stop.departure_fixed).split(' ')[2])),
stop.stop_sequence, stop_list.stop_sequence.max(),
sumolib.xml.quoteattr(stop.stop_name, True))
output_file.write(u' <stop busStop="%s" arrival="%s:%s" duration="%s" until="%s:%s"/><!--stopSequence="%s/%s" %s-->\n' % stop_attr) # noqa
output_file.write(u' <stop busStop="%s" arrival="%s" duration="%s" until="%s"/><!--stopSequence="%s/%s" %s-->\n' % stop_attr) # noqa
elif stop_index < check_seq:
# stop not downstream
sequence_errors.append((stop.stop_item_id, sumolib.xml.quoteattr(stop.stop_name, True),
Expand Down
3 changes: 2 additions & 1 deletion tools/import/gtfs/gtfs2pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ def main(options):
stops = map_stops(options, net, routes, aout, edgeMap, fixedStops)
aout.write(u'</additional>\n')
with sumolib.openz(options.route_output, mode='w') as rout:
ft = humanReadableTime if options.hrtime else lambda x: x
sumolib.xml.writeHeader(rout, os.path.basename(__file__), "routes", options=options)
for vehID, edges in routes.items():
if edges:
Expand All @@ -460,7 +461,7 @@ def main(options):
if offset is None:
offset = stop[1]
rout.write(u' <stop busStop="%s" duration="%s" until="%s"/> <!-- %s -->\n' %
(stop[0], options.duration, stop[1] - offset, stop[2]))
(stop[0], ft(options.duration), ft(stop[1] - offset), stop[2]))
rout.write(u' </route>\n')
else:
print("Warning! Empty route", vehID, file=sys.stderr)
Expand Down

0 comments on commit 859a86e

Please sign in to comment.