示例#1
0
文件: views.py 项目: wlach/routez
def stoptimes_in_range(request, location):
    distance = float(request.GET.get('distance', 500.0))
    if distance > 1000.0:
        return HttpResponseNotFound(simplejson.dumps(
                { 'errors': ["Distance of more than 1000 meters not allowed"] }), 
                                    mimetype="application/json")

    starttime = get_starttime(request.GET.get('time'))
    latlng = geocoder.geocode(str(location))

    if not latlng:
        return HttpResponseNotFound(simplejson.dumps(
                { 'errors': ["Location not found"] }), 
                                    mimetype="application/json")

    import routez.travel
    graph = routez.travel.graph
    tstops = graph.find_tripstops_in_range(latlng[0], latlng[1], TripStop.GTFS,
                                           distance)

    # filter twice, once to get all the stops we're interested in, then
    # remove any duplicate trips at stops that are further away
    stoplist = []
    distance_to_stop_hash = {}
    for ts in tstops:
        distance_to_stop = latlng_dist(latlng[0], latlng[1], ts.lat, ts.lng)
        distance_to_stop_hash[ts.id] = distance_to_stop
        route_ids = get_route_ids_for_stop(graph, int(ts.id), starttime)
        stoplist.append([ ts.id, distance_to_stop, route_ids ])
    stoplist.sort(lambda x, y: cmp(x[1], y[1]))

    stopsjson = []
    thophash = set()
    for stop in stoplist:
        routedicts = []
        for route_id in stop[2]:
            thops = find_triphops_for_stop(graph, stop[0], route_id, starttime, 3)

            if len(thops):
                # if ANY trip ids are duplicated between stops, omit (we don't
                # want to return ANY duplicate information, even if it means
                # we omit some information which is slightly different)
                unique_tripstop = True
                for thop in thops:
                    if thop.trip_id in thophash:
                        unique_tripstop = False
                    else:
                        thophash.add(thop.trip_id)
                if unique_tripstop:
                    times = []
                    trips = []
                    for thop in thops:
                        times.append(thop.start_time)
                        if thop.headsign_id >= 0:
                            headsign = StopHeadsign.objects.filter(
                                id=thop.headsign_id)[0].headsign
                        else:
                            headsign = Trip.objects.filter(
                                id=thop.trip_id)[0].headsign

                        trips.append({ "headsign": headsign, "time": thop.start_time })
                    route = Route.objects.filter(id=route_id)[0]
                    routedict = { 
                        "short_name": route.short_name,
                        "long_name": route.long_name,
                        "trips": trips,
                        "type": route.type,
                        "times": times }
                    routedicts.append(routedict)
        if len(routedicts) > 0:
            routedicts.sort(lambda x,y: x['times'][0]-y['times'][0])

            dbstop = Stop.objects.filter(id=stop[0])[0]

            stopsjson.append({ 
                    "name": dbstop.name,
                    "code": dbstop.stop_code,
                    "lat": dbstop.lat,
                    "lng": dbstop.lng,
                    "distance": distance_to_stop_hash[stop[0]],
                    "routes": routedicts })
            
    return HttpResponse(simplejson.dumps({ 'location': { 'lat': latlng[0], 
                                                         'lng': latlng[1] },
                                           'stops': stopsjson }), 
                        mimetype="application/json")
示例#2
0
文件: views.py 项目: wlach/routez
def routeplan(request):
    start = request.GET["start"]
    end = request.GET["end"]
    time_str = request.GET.get("time", "")

    errors = []

    start_latlng = geocoder.geocode(str(start))
    end_latlng = geocoder.geocode(str(end))

    if not start_latlng:
        errors.append("start_latlng_decode")
    if not end_latlng:
        errors.append("end_latlng_decode")

    if len(errors):
        return HttpResponseNotFound(simplejson.dumps({"errors": errors}), mimetype="application/json")

    import parsedatetime.parsedatetime as pdt

    calendar = pdt.Calendar()
    start_time = calendar.parse(time_str)[0]
    daysecs = time.mktime((start_time[0], start_time[1], start_time[2], 0, 0, 0, 0, 0, -1))
    now = datetime.datetime.fromtimestamp(time.mktime(start_time))
    today_secs = (now.hour * 60 * 60) + (now.minute * 60) + (now.second)

    import routez.travel

    graph = routez.travel.graph
    trippath = graph.find_path(
        time.mktime(start_time), False, start_latlng[0], start_latlng[1], end_latlng[0], end_latlng[1]
    )
    if not trippath:
        return HttpResponseNotFound(simplejson.dumps({"errors": ["find_path"]}), mimetype="application/json")

    actions_desc = []
    route_shortnames = []
    last_action = None
    walking_time = 0
    for action in trippath.get_actions():
        # order is always: get off (if applicable), board (if applicable),
        # then move
        if last_action and last_action.route_id != action.route_id:
            if last_action.route_id != -1:
                stop = Stop.objects.filter(id=last_action.dest_id)[0]
                action_time = human_time(last_action.end_time)
                actions_desc.append(
                    {"type": "alight", "lat": stop.lat, "lng": stop.lng, "stopname": stop.name, "time": action_time}
                )

        if not last_action or last_action.route_id != action.route_id:
            if action.route_id >= 0:
                action_time = human_time(action.start_time)
                route = Route.objects.filter(id=action.route_id)[0]
                stop = Stop.objects.filter(id=action.src_id)[0]
                actions_desc.append(
                    {
                        "type": "board",
                        "lat": stop.lat,
                        "lng": stop.lng,
                        "stopname": stop.name,
                        "time": action_time,
                        "route_id": action.route_id,
                        "route_type": route.type,
                        "route_shortname": route.short_name,
                        "route_longname": route.long_name,
                    }
                )

        ts = graph.get_tripstop(action.src_id)
        if action.route_id == -1:
            walking_time += action.end_time - action.start_time
        action_time = human_time(action.start_time)
        actions_desc.append(
            {
                "type": "pass",
                "id": action.src_id,
                "lat": ts.lat,
                "lng": ts.lng,
                "time": action_time,
                "dest_id": action.dest_id,
            }
        )
        last_action = action

    # if we had a path at all, append the last getting off action here
    if last_action:
        action_time = human_time(last_action.end_time)
        actions_desc.append({"type": "arrive", "lat": end_latlng[0], "lng": end_latlng[1], "time": action_time})

    trip_plan = {
        "start": {"lat": start_latlng[0], "lng": start_latlng[1]},
        "end": {"lat": end_latlng[0], "lng": end_latlng[1]},
        "actions": actions_desc,
        "departure_time": human_time(daysecs + today_secs),
        "walking_time": walking_time,
    }

    return HttpResponse(simplejson.dumps(trip_plan), mimetype="application/json")