def almanacs_kml(self): json = request.params.get('extent') # We need to make sure we only select almanacs with pages here, query = meta.Session.query(Almanac).join(Almanac.pages).distinct() # ... and eager-load the pages since the ktml template uses them. query = query.options(eagerload(Almanac.pages)) # Tried also with contains_eager, not sure what the difference is # but I only get a fraction of the expected almanacs: #query = meta.Session.query(Almanac).join(Almanac.pages).distinct() #query = query.options(contains_eager(Almanac.pages)) # Also tried using a single, second query for the pages. # ... NOPE, requires sqlalchemy > 0.6.0 which blows up on us, # maybe not compatible w/ pylons 0.9.7? #query = meta.Session.query(Almanac).join(Almanac.pages).distinct() #query = query.options(subqueryload(Almanac.pages)) # Tried also without the explicit join().distinct(), this gives # back all almanac whether they have any pages or not: #query = meta.Session.query(Almanac).options(eagerload(Almanac.pages)) if json is not None: shape = simplejson.loads(json) # Stupid asShape returns an Adapter instead of a Geometry. We round # trip it through wkb to get the correct type. bbox = wkb.loads(asShape(shape).to_wkb()) query = query.filter(func.st_intersects(Almanac.location, func.st_transform('SRID=%s;%s' % ('4326', b2a_hex(bbox.to_wkb())), storage_SRID))) c.almanacs = query.order_by(Almanac.modified.desc()).limit(200).all() response.content_type = 'application/vnd.google-earth.kml+xml kml' return render('/almanac/kml.mako')
def _nearby_almanacs(self, bounds): return meta.Session.query(Almanac).join(Almanac.pages).distinct().filter(func.st_intersects(Almanac.location, func.st_transform('SRID=%s;%s' % ('4326', b2a_hex(bounds.to_wkb())), storage_SRID))).limit(10).all()