- # The inner SQL brings results in the right order, so that
- # later only a minimum of results needs to be checked with ST_Contains.
- inner = sa.select(t, sa.literal(0.0).label('distance'))\
- .where(t.c.rank_search.between(5, self.max_rank))\
- .where(t.c.rank_address.between(5, 25))\
- .where(t.c.geometry.ST_GeometryType().in_(('ST_Polygon', 'ST_MultiPolygon')))\
- .where(t.c.geometry.intersects(wkt))\
- .where(t.c.name != None)\
- .where(t.c.indexed_status == 0)\
- .where(t.c.linked_place_id == None)\
- .where(t.c.type != 'postcode')\
- .order_by(sa.desc(t.c.rank_search))\
- .limit(50)\
- .subquery('area')
+ def _base_query() -> SaSelect:
+ # The inner SQL brings results in the right order, so that
+ # later only a minimum of results needs to be checked with ST_Contains.
+ inner = sa.select(t, sa.literal(0.0).label('distance'))\
+ .where(t.c.rank_search.between(5, MAX_RANK_PARAM))\
+ .where(t.c.geometry.intersects(WKT_PARAM))\
+ .where(sa.func.PlacexGeometryReverseLookuppolygon())\
+ .order_by(sa.desc(t.c.rank_search))\
+ .limit(50)\
+ .subquery('area')