Skip to content

Commit

Permalink
Move basic configuration into separate json file
Browse files Browse the repository at this point in the history
Moved basic configuration into separate json file

Closes #3
  • Loading branch information
Felix authored and grote committed Sep 19, 2016
1 parent 71f44c8 commit 2bb8c03
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 34 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ This data is stored in python objects and cached on disk for efficient re-use.
The osm2gtfs script then uses the OpenStreetMap data and local schedule information
to create a GTFS file using Google's transitfeed library and validates it after creation.

Use
------------

python osm2gtfs.py -c fenix.json.example

Requirements
------------

Expand Down
25 changes: 25 additions & 0 deletions fenix.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"query": {
"bbox": {
"n": "-27.2155",
"s": "-27.9410",
"e": "-48.2711",
"w": "-49.0155"
}
},
"agency": {
"agency_id": "BR-Floripa",
"agency_name": "Consórcio Fênix",
"agency_url": "http://www.consorciofenix.com.br/",
"agency_timezone": "America/Sao_Paulo",
"agency_lang": "pt",
"agency_phone": "+55 (48) 3025-6868"
},
"feed_info": {
"publisher_name": "Torsten Grote",
"publisher_url": "https://transportr.grobox.de",
"version": "0.1",
"start_date": "20160901",
"end_date": "20170831"
}
}
70 changes: 40 additions & 30 deletions osm2gtfs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python
# coding=utf-8

import transitfeed
Expand All @@ -9,19 +9,15 @@
from datetime import datetime
from osmhelper.osm_routes import Route, RouteMaster

FLORIANOPOLIS = {"e": "-48.2711", "n": "-27.2155", "s": "-27.9410", "w": "-49.0155"}

DEBUG_ROUTE = "104"

START_DATE = "20160901"
END_DATE = "20170831"

WEEKDAY = "Dias Úteis"
SATURDAY = "Sábado"
SUNDAY = "Domingo"

# Handle arguments
parser = argparse.ArgumentParser(prog='osm2gtfs', description='Create GTFS from OpenStreetMap data.')

parser.add_argument('--config', '-c', metavar='FILE', type=argparse.FileType('r'), help='Configuration json file', required=True)
group = parser.add_mutually_exclusive_group()
group.add_argument('--refresh-route', metavar='ROUTE', type=int, help='Refresh OSM data for ROUTE')
group.add_argument('--refresh-all-routes', action="store_true", help='Refresh OSM data for all routes')
Expand All @@ -31,18 +27,32 @@


def main():

# Load config json file
try:
config = json.load(args.config)
except ValueError, e:
print('Failed to load config json file:')
print(e)
sys.exit(0)

# Initialize information from config file
bbox = config['query']['bbox']
start_date = config['feed_info']['start_date']
end_date = config['feed_info']['end_date']

# --refresh-route
if args.refresh_route is not None:
osmhelper.refresh_route(args.refresh_route, "bus", FLORIANOPOLIS)
osmhelper.refresh_route(args.refresh_route, "bus", bbox)
sys.exit(0)
elif args.refresh_all_routes:
osmhelper.get_routes("bus", FLORIANOPOLIS, refresh=True)
osmhelper.get_routes("bus", bbox, refresh=True)
sys.exit(0)
elif args.refresh_all_stops:
osmhelper.get_stops(osmhelper.get_routes("bus", FLORIANOPOLIS), refresh=True)
osmhelper.get_stops(osmhelper.get_routes("bus", bbox), refresh=True)
sys.exit(0)
elif args.refresh_all:
osmhelper.refresh_data("bus", FLORIANOPOLIS)
osmhelper.refresh_data("bus", bbox)
sys.exit(0)

# Get Fenix data from JSON file
Expand All @@ -53,7 +63,7 @@ def main():
linhas = json_data[0]['data']

# Get OSM routes and check data
routes = osmhelper.get_routes("bus", FLORIANOPOLIS)
routes = osmhelper.get_routes("bus", bbox)

blacklist = ['10200', '12400', '328', '466', '665']
# Try to find OSM routes in Fenix data
Expand Down Expand Up @@ -82,28 +92,28 @@ def main():

schedule = transitfeed.Schedule()
agency = schedule.AddAgency(
name="Consórcio Fênix",
url="http://www.consorciofenix.com.br/",
timezone="America/Sao_Paulo",
agency_id="BR-Floripa"
name=config['agency']['agency_name'],
url=config['agency']['agency_url'],
timezone=config['agency']['agency_timezone'],
agency_id=config['agency']['agency_id']
)

service_weekday = schedule.NewDefaultServicePeriod()
service_weekday.SetStartDate(START_DATE)
service_weekday.SetEndDate(END_DATE)
service_weekday.SetStartDate(start_date)
service_weekday.SetEndDate(end_date)
service_weekday.SetWeekdayService(True)
service_weekday.SetWeekendService(False)

service_saturday = schedule.NewDefaultServicePeriod()
service_saturday.SetStartDate(START_DATE)
service_saturday.SetEndDate(END_DATE)
service_saturday.SetStartDate(start_date)
service_saturday.SetEndDate(end_date)
service_saturday.SetWeekdayService(False)
service_saturday.SetWeekendService(False)
service_saturday.SetDayOfWeekHasService(5, True)

service_sunday = schedule.NewDefaultServicePeriod()
service_sunday.SetStartDate(START_DATE)
service_sunday.SetEndDate(END_DATE)
service_sunday.SetStartDate(start_date)
service_sunday.SetEndDate(end_date)
service_sunday.SetWeekdayService(False)
service_sunday.SetWeekendService(False)
service_sunday.SetDayOfWeekHasService(6, True)
Expand Down Expand Up @@ -148,14 +158,14 @@ def main():
add_trips(schedule, line, service_weekday, route, weekday, WEEKDAY)
add_trips(schedule, line, service_saturday, route, saturday, SATURDAY)
add_trips(schedule, line, service_sunday, route, sunday, SUNDAY)

feed_info = transitfeed.FeedInfo()
feed_info.feed_publisher_name = "Torsten Grote"
feed_info.feed_publisher_url = "https://transportr.grobox.de"
feed_info.feed_lang = "pt"
feed_info.feed_start_date = START_DATE
feed_info.feed_end_date = END_DATE
feed_info.feed_version = "0.1"
feed_info.feed_publisher_name = config['feed_info']['publisher_name']
feed_info.feed_publisher_url = config['feed_info']['publisher_url']
feed_info.feed_lang = config['agency']['agency_lang']
feed_info.feed_start_date = start_date
feed_info.feed_end_date = end_date
feed_info.feed_version = config['feed_info']['version']
schedule.AddFeedInfoObject(feed_info)

schedule.Validate(transitfeed.ProblemReporter())
Expand Down Expand Up @@ -207,7 +217,7 @@ def add_trips(schedule, line, service, route, horarios, day):
for time_group in horarios[key]:
for time_point in time_group:
# parse first departure time
start_time = datetime.strptime(time_point[0], "%H:%M")
start_time = datetime.strptime(time_point[0], "%H:%M")
start_time = str(start_time.time())

# calculate last arrival time for GTFS
Expand Down
8 changes: 4 additions & 4 deletions osmhelper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python
# coding=utf-8

import overpy
Expand Down Expand Up @@ -66,7 +66,7 @@ def get_routes(route_type, bbox, refresh=False):
<bbox-query e="%s" n="%s" s="%s" w="%s"/>
</query>
<print/>
""" % (route_type, bbox["e"], bbox["n"], bbox["s"], bbox["w"]))
""" % (route_type, str(bbox["e"]), str(bbox["n"]), str(bbox["s"]), str(bbox["w"])))

route_masters = get_route_masters(route_type, bbox, refresh)

Expand Down Expand Up @@ -105,7 +105,7 @@ def get_route_masters(route_type, bbox, refresh=False):
</query>
<recurse type="relation-backwards"/>
<print/>
""" % (route_type, bbox["e"], bbox["n"], bbox["s"], bbox["w"]))
""" % (route_type, str(bbox["e"]), str(bbox["n"]), str(bbox["s"]), str(bbox["w"])))

for rel in result.get_relations():

Expand Down Expand Up @@ -286,7 +286,7 @@ def refresh_route(route_ref, route_type, bbox):
<bbox-query e="%s" n="%s" s="%s" w="%s"/>
</query>
<print/>
""" % (route_type, str(route_ref), bbox["e"], bbox["n"], bbox["s"], bbox["w"]))
""" % (route_type, str(route_ref), str(bbox["e"]), str(bbox["n"]), str(bbox["s"]), str(bbox["w"])))

if route_ref in route_masters:
# TODO also refresh route master
Expand Down

0 comments on commit 2bb8c03

Please sign in to comment.