Skip to content

Commit

Permalink
Fix guessing agency timezone but disable it.
Browse files Browse the repository at this point in the history
Guessing timezone causes validation errors at the moment.
See #28.
  • Loading branch information
mehdisadeghi committed Mar 2, 2018
1 parent c2ebb81 commit 6666f25
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
62 changes: 34 additions & 28 deletions osmtogtfs/osm/builders/agency_builder.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
"""Functionality to build a list of agencies."""
import logging
import hashlib

from timezonefinder import TimezoneFinder

from osmtogtfs.osm.models import Agency


def build_agencies(relations):
TZ_FINDER = TimezoneFinder()


def build_agencies(relations, nodes, ways):
extracted = []
for rel in relations.values():
agency = extract_agency(rel)
agency = build_agency(rel, nodes)
if agency and agency.agency_id not in extracted:
extracted.append(agency.agency_id)
yield agency
yield agency#._replace(agency_timezone=_guess_timezone(rel, nodes, ways))


def extract_agency(relation):
def build_agency(relation, nodes):
"""Extract agency information."""
# TODO: find out the operator for routes without operator tag.
# See: http://wiki.openstreetmap.org/wiki/Key:operator
Expand All @@ -30,27 +36,27 @@ def extract_agency(relation):

agency_id = int(hashlib.sha256(op.encode('utf8')).hexdigest(), 16) % 10**8

return Agency(agency_id, '', op, '')#self._guess_timezone(relation)}


# def _guess_timezone(self, relation):
# """Guess timezone of the relation by looking at it's nodes."""
# lon, lat = self._get_first_coordinate(relation)
# timezone = self.tzfinder.timezone_at(lng=lon, lat=lat)
# if not timezone:
# logging.debug('No timezone found for (%s, %s)', lon, lat)
# return timezone

# def _get_first_coordinate(self, relation):
# for member in relation.members:
# if member.ref in self.nodes:
# return self.nodes[member.ref].lon, self.nodes[member.ref].lat
# elif member.ref in self.ways:
# return self._get_first_way_coordinate(member.ref)
# logging.debug('No node found for relation %s', relation.id)
# return 0, 0

# def _get_first_way_coordinate(self, way_id):
# if way_id in self.ways and self.ways[way_id]:
# # Pick the first node
# return self.ways[way_id][0][0], self.ways[way_id][0][1]
return Agency(agency_id, '', op, '')


def _guess_timezone(relation, nodes, ways):
"""Guess timezone of the relation by looking at it's nodes."""
lon, lat = _get_first_coordinate(relation, nodes, ways)
timezone = TZ_FINDER.timezone_at(lng=lon, lat=lat)
if not timezone:
logging.debug('No timezone found for (%s, %s)', lon, lat)
return timezone

def _get_first_coordinate(relation, nodes, ways):
for member_id, member_role in relation.member_info:
if member_id in nodes:
return nodes[member_id].lon, nodes[member_id].lat
elif member_id in ways:
return _get_first_way_coordinate(member_id, ways)
logging.debug('No node found for relation %s', relation.id)
return 0, 0

def _get_first_way_coordinate(way_id, ways):
if way_id in ways:
# Pick the first node
return ways[way_id].points[0]
2 changes: 1 addition & 1 deletion osmtogtfs/osm/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, filename):

@property
def agencies(self):
return build_agencies(self.rh.relations)
return build_agencies(self.rh.relations, self.nh.nodes, self.wh.ways)

@property
def routes(self):
Expand Down

0 comments on commit 6666f25

Please sign in to comment.