def get(self): login_user = get_login_user() # bbox is in GeoJson notation [minlon,minlat,maxlon,maxlat] bbox = self.request.get('bbox',"0,0,0,0").split(',') minlat = float(bbox[1]) minlon = float(bbox[0]) maxlat = float(bbox[3]) maxlon = float(bbox[2]) geojson = {"type": "FeatureCollection"} geojson['features'] = [] box = geo.geotypes.Box(maxlat,maxlon,minlat,minlon) for geoix in GeoIndex.bounding_box_fetch(GeoIndex.all(), box, max_results=111): try: con = geocode_contact(geoix.contact_ref, include_attic=False, login_user=login_user) if con: # geoconding successful geojson['features'].extend(con) else: nongeo.append(encode_contact(contact_ref, login_user, include_attic=False)) except db.ReferencePropertyResolveError: logging.critical("AttributeError while encoding") geojson["bbox"] = bbox logging.debug("Return %s objects" % (len(geojson['features']))) # encode and return # self.response.headers['Content-Type'] = "application/json" self.response.headers['Content-Type'] = "text/plain" self.response.out.write(json.dumps(geojson))
def get(self): login_user = get_login_user() query = self.request.get('query',"") include_attic = True if self.request.get('attic',None) else False # data structures for data transport to client nongeo = [] geojson = [] geojson = {"type": "FeatureCollection"} geojson['features'] = [] minlat = 0.0 maxlat = 0.0 minlon = 0.0 maxlon = 0.0 if query: cis = lookup_contacts(query, include_attic) # Save the query result in memcache together with the information about # which portion of it we are displaying (the first result_size datasets as # it is a fresh query!) if login_user: if not memcache.set('query', {'query': query, 'offset': 0, 'results': cis}, time=5000, namespace=str(login_user.key())): logging.error("memcache failed") # fetch a number of data from the results for contact in db.get(cis[0:settings.RESULT_SIZE]): if not contact: # may happen if index is not up to date logging.warning("Query returned invalid contact reference") continue try: con = geocode_contact(contact, include_attic=include_attic, login_user=login_user) if con: # geoconding successful geojson['features'].extend(con) else: nongeo.append(encode_contact(contact_ref, login_user, include_attic=False)) except db.ReferencePropertyResolveError: logging.critical("AttributeError while encoding") # calculate bounding box (viewport) for feature in geojson['features']: coords = feature['geometry']['coordinates'] if feature['id'] == 'display': # initialize to first point if minlon == 0.0: minlon = coords[0] maxlon = coords[0] if coords[0] > maxlon: maxlon = coords[0] if coords[0] < minlon: minlon = coords[0] if minlat == 0.0: minlat = coords[1] maxlat = coords[1] if coords[1] > maxlat: maxlat = coords[1] if coords[1] < minlat: minlat = coords[1] geojson["bbox"] = [minlon,minlat,maxlon,maxlat] # encode and return # self.response.headers['Content-Type'] = "application/json" self.response.headers['Content-Type'] = "text/plain" self.response.out.write(json.dumps(geojson, indent= 2 if settings.DEBUG else 0))