views.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:bikewatch 作者: codeformunich 项目源码 文件源码
def probability(request,ltlat, ltlong, rblat, rblong ,poslat, poslong, dayindex, hourindex):

    #dayindex starts with 1 for sunday, see https://docs.djangoproject.com/en/dev/ref/models/querysets/#week-day

    loc = Point(float(poslong),float(poslat),srid=4326 ) #fixme: check that srid=4326 is right
    print(loc)
    result = Bikes.objects.filter(timestamp__week_day=dayindex).filter(timestamp__hour=hourindex).filter(bikes__gt=0)\
        .extra({'date_found' : "date(timestamp)"}).values('date_found')\
        .annotate(min_distance=Min(Distance('place_coords', loc))).order_by('min_distance')
    result_count = len(result)


    result_ranges = {}
    percentages = [0.25,0.50,0.75,0.90]
    p_ind = 0
    for i in range(result_count): # this finds the minimum distance for which the percentages from the list are fullfiled for bike availability
        percentage_sum = (i+1)/result_count
        while p_ind < len(percentages):
            if percentages[p_ind] <= percentage_sum:
                result_ranges[str(percentages[p_ind])]=result[i]["min_distance"]
                p_ind+=1
            else:
                break
    return HttpResponse(json.dumps(result_ranges), content_type='application/json')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号