コード例 #1
0
ファイル: web_services.py プロジェクト: Yanhan0507/Apt
    def get(self):
        stream_obj = Stream()
        print "mViewNearbyImages:: starts~"

        loc_str = self.request.get("loc_str")
        loc = loc_str.split('_')
        lat = float(loc[0])
        lon = float(loc[1])

        print "mViewNearbyImages:: get location", lat, lon
        images = Image.query().fetch()

        sorted_imgs = sorted(images, key=lambda image: image.date, reverse=True)

        max_idx = len(sorted_imgs)

        print "mViewNearbyImages:: len(sorted_imgs)=", max_idx

        if max_idx >= NUM_IMG_PER_PAGE:
            max_idx = NUM_IMG_PER_PAGE

        img_url_lst = list()
        stream_id_lst = list()
        distance_lst = list()

        R = 6373.0
        r_m_lat = radians(lat)
        r_m_lon = radians(lon)

        for idx in xrange(0, max_idx):
            image = sorted_imgs[idx]
            img_url_lst.append(SERVICE_URL+"/view_photo/"+str(image.blob_key))
            stream_id_lst.append(stream_obj.get_stream_id_by_img_id(image.img_id))

            img_lat = radians(image.location.lat)
            img_lon = radians(image.location.lon)
            dlat = r_m_lat - img_lat
            dlon = r_m_lon - img_lon
            a = (sin(dlat / 2)) ** 2 + cos(r_m_lat) * cos(img_lat) * (sin(dlon / 2)) ** 2
            c = 2 * atan2(sqrt(a), sqrt(1 - a))
            distance = R * c

            distance_lst.append(distance)

        print "mViewNearbyImages:: ldistance_lst=", str(distance_lst)

        self.respond(img_url_lst=img_url_lst, stream_id_lst=stream_id_lst, distance_lst=distance_lst, status="success")