Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to last Transitfeed #122

Merged
merged 3 commits into from
Feb 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion osm2gtfs/creators/accra/trips_creator_accra.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def add_trips_to_feed(self, feed, data):
ROUTE_FREQUENCY = DEFAULT_ROUTE_FREQUENCY
trip_gtfs.AddFrequency(
"05:00:00", "22:00:00", ROUTE_FREQUENCY * 60)

if 'travel_time' in a_route.tags:
try:
TRAVEL_TIME = int(a_route.tags['travel_time'])
Expand All @@ -82,6 +81,8 @@ def add_trips_to_feed(self, feed, data):
print("travel_time not a number for route " + str(
a_route.osm_id))
TRAVEL_TIME = DEFAULT_TRAVEL_TIME
else:
TRAVEL_TIME = DEFAULT_TRAVEL_TIME

for index_stop, a_stop in enumerate(a_route.stops):
stop_id = a_stop.split('/')[-1]
Expand Down
9 changes: 1 addition & 8 deletions osm2gtfs/creators/feed_info_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ def __repr__(self):
return rep

def add_feed_info_to_feed(self, feed):
feed_info = self.prepare_feed_info()
feed.AddFeedInfoObject(feed_info)

# Missing feed_info workaround
# https://github.com/google/transitfeed/issues/395
# noinspection PyProtectedMember
# pylint: disable=protected-access
feed.AddTableColumns('feed_info', feed_info._ColumnNames())
feed.AddFeedInfoObject(self.prepare_feed_info())

def prepare_feed_info(self):
"""
Expand Down
22 changes: 0 additions & 22 deletions osm2gtfs/osm2gtfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,8 @@ def main():
# Write GTFS
feed.WriteGoogleTransitFeed(config.output)

# Add feed_info.txt to GTFS
add_feed_info(feed, config.output)

sys.exit()


# noinspection PyProtectedMember
def add_feed_info(feed, output_file):
"""
Add feed_info.txt file to GTFS
Workaround for https://github.com/google/transitfeed/issues/395
"""
if 'feed_info' not in feed._table_columns: # pylint: disable=protected-access
return

with transitfeed.zipfile.ZipFile(output_file, 'a') as archive:
feed_info_string = transitfeed.StringIO.StringIO()
writer = transitfeed.util.CsvUnicodeWriter(feed_info_string)
columns = feed.GetTableColumns('feed_info')
writer.writerow(columns)
writer.writerow([transitfeed.util.EncodeUnicode(feed.feed_info[c]) for c in columns])
# pylint: disable=protected-access
feed._WriteArchiveString(archive, 'feed_info.txt', feed_info_string)


if __name__ == "__main__":
main()
Binary file modified osm2gtfs/tests/fixtures/accra/accra_tests.zip.ref
Binary file not shown.
114 changes: 111 additions & 3 deletions osm2gtfs/tests/tests_accra.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,110 @@ def is_identical_gtfs(gtfs1, gtfs2):
return True


def check_osm_route_stop_times(gtfs1, gtfs2, osm_relation_id):
osm_relation_id = str(osm_relation_id)
zf1 = zipfile.ZipFile(gtfs1)
zf2 = zipfile.ZipFile(gtfs2)
# Grabbing the trip_ids from both gtfs
trips_id1 = []
with zf1.open("trips.txt") as trip_file:
reader = csv.DictReader(trip_file)
for row in reader:
if row["route_id"] == osm_relation_id:
trips_id1.append(row['trip_id'])
trips_id2 = []
with zf2.open("trips.txt") as trip_file:
reader = csv.DictReader(trip_file)
for row in reader:
if row["route_id"] == osm_relation_id:
trips_id2.append(row['trip_id'])
if trips_id1 != trips_id2:
print("Error on count of trips found ({:d} <> {:d})".format(
len(trips_id1),
len(trips_id2)
))
return False
# Grabbing simplified stop_times for found trips
stop_times1 = []
stop_times2 = []
with zf1.open("stop_times.txt") as st_file:
reader = csv.DictReader(st_file)
for row in reader:
if row["trip_id"] in trips_id1:
st = {
"trip_id": row['trip_id'],
"stop_id": row['stop_id'],
"stop_sequence": row['stop_sequence'],
"arrival_time": row['arrival_time'],
"departure_time": row['departure_time'],
}
stop_times1.append(st)
with zf2.open("stop_times.txt") as st_file:
reader = csv.DictReader(st_file)
for row in reader:
if row["trip_id"] in trips_id2:
st = {
"trip_id": row['trip_id'],
"stop_id": row['stop_id'],
"stop_sequence": row['stop_sequence'],
"arrival_time": row['arrival_time'],
"departure_time": row['departure_time'],
}
stop_times2.append(st)
if len(stop_times1) != len(stop_times2):
print("Error on count of stop_times found ({:d} <> {:d})".format(
len(stop_times1),
len(stop_times2)
))
return False
# Checking for the first error in stop_times with an explicit message
for st1 in stop_times1:
trip_point_found = False
for st2 in stop_times2:
if st1["trip_id"] == st2["trip_id"] \
and st1["stop_sequence"] == st2["stop_sequence"]:
trip_point_found = True
if st1["stop_id"] != st2["stop_id"]:
print(
"stop_id different for trip_id={} and stop_sequence={} ({} <> {})".format(
st1["trip_id"],
st1["stop_sequence"],
st1["stop_id"],
st2["stop_id"]
)
)
return False
elif st1["arrival_time"] != st2["arrival_time"] \
or st1["departure_time"] != st2["departure_time"]:
print(
"Stop times are different for trip_id={} and stop_sequence={}".format(
st1["trip_id"],
st1["stop_sequence"]
)
)
for st in [st1, st2]:
print(
"\t arrival_time={} departure_time={}".format(
st["arrival_time"],
st["departure_time"],
)
)
return False
else:
# trip_point found with no difference, break for st2 loop
# to continue for the next st1
break
if not trip_point_found:
print(
"No corresponing stop_time for trip_id={} and stop_sequence={}".format(
st1["trip_id"],
st1["stop_sequence"]
)
)
return False
return True


def get_gtfs_infos(gtfs):
gtfs_infos = {}
gtfs_infos["stop_points_count"] = 0
Expand Down Expand Up @@ -146,17 +250,21 @@ def test_gtfs_from_cache(self):
feed.WriteGoogleTransitFeed(self.config.output)
gtfs_expected_result = os.path.join(self.fixture_folder, "accra_tests.zip.ref")
gtfs_generated_result = os.path.join(self.data_dir, "accra_tests.zip")
self.assertTrue(is_valid_gtfs(gtfs_generated_result), 'The generated GTFS is not valid')
self.assertTrue(is_valid_gtfs(gtfs_expected_result), 'The expected GTFS is not valid')
self.assertTrue(is_valid_gtfs(gtfs_generated_result), "The generated GTFS is not valid")
self.assertTrue(is_valid_gtfs(gtfs_expected_result), "The expected GTFS is not valid")
self.assertTrue(is_identical_gtfs(gtfs_expected_result, gtfs_generated_result),
'The generated GTFS is different from the expected one')
"The generated GTFS is different from the expected one")
gtfs_infos = get_gtfs_infos(gtfs_generated_result)
self.assertEqual(gtfs_infos["stop_points_count"], 2529,
"Wrong stop_points count in the generated GTFS")
self.assertEqual(gtfs_infos["stop_areas_count"], 1656,
"Wrong stop_areas count in the generated GTFS")
self.assertEqual(gtfs_infos["routes_count"], 277,
"Wrong routes count in the generated GTFS")
self.assertTrue(
check_osm_route_stop_times(gtfs_expected_result, gtfs_generated_result, 7551952),
"Error found on stop_times of osm relation 7551952"
)


def load_tests(loader, tests, pattern):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
keywords='openstreetmap gtfs schedule public-transportation python',
author='Various collaborators: https://github.com/grote/osm2gtfs/graphs/contributors',

install_requires=['attrs>=17.1.0', 'overpy>=0.4', 'transitfeed<1.2.16', 'mock', 'webcolors'],
install_requires=['attrs>=17.1.0', 'overpy>=0.4', 'transitfeed>=1.2.16', 'mock', 'webcolors'],
packages=find_packages(),
include_package_data=True,
entry_points={
Expand Down