def from_handmade(cls, start, middlePoints, destination): """Convert hand-made route data to a way """ if start and destination: # route points & message points are generated at once # * empty string as message => no message point, just route point routePoints = [(start[0], start[1], None)] messagePoints = [] mLength = 0 # in meters lastLat, lastLon = start[0], start[1] for point in middlePoints: lat, lon, elevation, message = point mLength += geo.distance(lastLat, lastLon, lat, lon) * 1000 routePoints.append((lat, lon, elevation)) if message != "": # is it a message point ? point = TurnByTurnPoint(lat, lon, elevation, message) point.distanceFromStart = mLength messagePoints.append(point) lastLat, lastLon = lat, lon routePoints.append((destination[0], destination[1], None)) way = cls(routePoints) way.add_message_points(messagePoints) # huge guestimation (avg speed 60 km/h = 16.7 m/s) seconds = mLength / 16.7 way.duration = seconds way._setLength(mLength) # done, return the result return way else: return None
def from_handmade(cls, start, middlePoints, destination): """Convert hand-made route data to a way """ if start and destination: # route points & message points are generated at once # * empty string as message => no message point, just route point routePoints = [(start[0], start[1], None)] messagePoints = [] mLength = 0 # in meters lastLat, lastLon = start[0], start[1] for point in middlePoints: lat, lon, elevation, message = point mLength += geo.distance(lastLat, lastLon, lat, lon) * 1000 routePoints.append((lat, lon, elevation)) if message != "": # is it a message point ? point = TurnByTurnPoint(lat, lon, elevation, message) point.distanceFromStart = mLength messagePoints.append(point) lastLat, lastLon = lat, lon routePoints.append((destination[0], destination[1], None)) way = cls(routePoints) way.add_message_points(messagePoints) # huge guestimation (avg speed 60 km/h = 16.7 m/s) seconds = mLength / 16.7 way.duration = seconds way._setLength(mLength) # done, return the result return way else: return None
def from_google_directions_result(cls, gResult): """Convert Google Directions result to a way object""" leg = gResult['routes'][0]['legs'][0] steps = leg['steps'] points = decode_polyline(gResult['routes'][0]['overview_polyline']['points']) # length of the route can computed from its metadata if 'distance' in leg: # this field might not be present mLength = leg['distance']['value'] else: mLength = None # the route also contains the expected duration in seconds if 'duration' in leg: # this field might not be present sDuration = leg['duration']['value'] else: sDuration = None way = cls(points) way._setLength(mLength) way.duration = sDuration messagePoints = [] mDistanceFromStart = 0 for step in steps: # TODO: abbreviation filtering message = step['html_instructions'] # as you can see, for some reason, # the coordinates in Google Directions steps are reversed: lat = step['start_location']['lat'] lon = step['start_location']['lng'] #TODO: end location ? point = TurnByTurnPoint(lat, lon, message=message) point.distanceFromStart = mDistanceFromStart # store point to temporary list messagePoints.append(point) # update distance for next point mDistanceFromLast = step["distance"]['value'] mDistanceFromStart = mDistanceFromStart + mDistanceFromLast way.add_message_points(messagePoints) return way
def from_google_directions_result(cls, gResult): """Convert Google Directions result to a way object""" leg = gResult['routes'][0]['legs'][0] steps = leg['steps'] points = decode_polyline(gResult['routes'][0]['overview_polyline']['points']) # length of the route can computed from its metadata if 'distance' in leg: # this field might not be present mLength = leg['distance']['value'] else: mLength = None # the route also contains the expected duration in seconds if 'duration' in leg: # this field might not be present sDuration = leg['duration']['value'] else: sDuration = None way = cls(points) way._setLength(mLength) way.duration = sDuration messagePoints = [] mDistanceFromStart = 0 for step in steps: # TODO: abbreviation filtering message = step['html_instructions'] # as you can see, for some reason, # the coordinates in Google Directions steps are reversed: lat = step['start_location']['lat'] lon = step['start_location']['lng'] #TODO: end location ? point = TurnByTurnPoint(lat, lon, message=message) point.distanceFromStart = mDistanceFromStart # store point to temporary list messagePoints.append(point) # update distance for next point mDistanceFromLast = step["distance"]['value'] mDistanceFromStart = mDistanceFromStart + mDistanceFromLast way.add_message_points(messagePoints) return way