def get(self):
        bq = BqQuery(PROJECT_ID, DATASET_ID, TABLE_ID)
        self.response.headers['Content-Type'] = 'application/json'
        res = bq.query(u'''SELECT
    address,
    AVG(lat) as lat,
    AVG(lng) as lng,
    AVG(
        SQRT(
            POW(RADIANS(ABS(lat - lat_origin)) *
            6335439.32708317 / POW(SQRT(1 - 0.00669438002301188 *
            POW(SIN(RADIANS((lat + lat_origin)/2)), 2)), 3), 2)
            +
            POW(RADIANS(ABS(lng - lng_origin)) * 6378137.000 /
            SQRT(1 - 0.00669438002301188 *
            POW(SIN(RADIANS((lat + lat_origin)/2)),2)) *
             COS(RADIANS((lat + lat_origin)/2)), 2)
        )
    ) AS distance
FROM places.places
group by address;''')
        r = BqQueryResult(res)
        for item in r.items():
            ps = PlaceSummary\
                .query(PlaceSummary.address == item['address'])\
                .get()
            if ps is None:
                ps = PlaceSummary.create_instance(**item)
            else:
                ps.update(**item)
            ps.put()
        self.response.write(json.dumps(r.as_dict()))
    def post(self):

        lat = self.request.get('lat')
        lng = self.request.get('lng')

        place = Place.create_instance(lat=lat, lng=lng)
        future = place.put_async(use_memcache=True)
        geocoder = GeoCoder(lat, lng)
        result = GeoCodingResult(geocoder, json.loads(self.request.get('json')))

        res = result.as_dict()

        res['origin'] = future.get_result()
        coded = GeoCodedPlace.create_instance(**res)

        future = coded.put_async(use_memcache=True)

        bq = BqQuery(PROJECT_ID, DATASET_ID, TABLE_ID)
        bq.insert([result.as_bq()])

        future.get_result()
        self.response.write('')
    def post(self):
        lat = self.request.get('lat')
        lng = self.request.get('lng')

        place = Place.create_instance(lat=lat, lng=lng)
        future = place.put_async(use_memcache=True)
        geocoder = GeoCoder(lat, lng)
        result = GeoCodingResult(geocoder)

        res = result.as_dict()
        res['origin'] = future.get_result()
        coded = GeoCodedPlace.create_instance(**res)

        future = coded.put_async(use_memcache=True)
        context = {
            'place': place,
            'coded': coded
        }

        bq = BqQuery(PROJECT_ID, DATASET_ID, TABLE_ID)
        bq.insert([result.as_bq()])

        self.render_response('templates/location.html', context)
        future.get_result()