- inner = sa.select(t,
- t.c.linegeo.ST_Distance(wkt).label('distance'),
- _locate_interpolation(t, wkt))\
- .where(t.c.linegeo.ST_DWithin(wkt, 0.001))\
- .where(t.c.parent_place_id == parent_place_id)\
- .order_by('distance')\
- .limit(1)\
- .subquery()
-
- sql = sa.select(inner.c.place_id,
- inner.c.parent_place_id,
- sa.literal(parent_type).label('osm_type'),
- sa.literal(parent_id).label('osm_id'),
- _interpolated_housenumber(inner),
- _interpolated_position(inner),
- inner.c.postcode,
- inner.c.distance)
-
- if self.details.geometry_output:
- sub = sql.subquery()
- sql = self._add_geometry_columns(sql, sub.c.centroid)
+ def _base_query() -> SaSelect:
+ inner = sa.select(t,
+ t.c.linegeo.ST_Distance(WKT_PARAM).label('distance'),
+ _locate_interpolation(t))\
+ .where(t.c.linegeo.within_distance(WKT_PARAM, 0.001))\
+ .where(t.c.parent_place_id == parent_place_id)\
+ .order_by('distance')\
+ .limit(1)\
+ .subquery('tiger')
+
+ return sa.select(inner.c.place_id,
+ inner.c.parent_place_id,
+ _interpolated_housenumber(inner),
+ _interpolated_position(inner),
+ inner.c.postcode,
+ inner.c.distance)
+
+ sql: SaLambdaSelect
+ if self.has_geometries():
+ sub = _base_query().subquery('geom')
+ sql = self._add_geometry_columns(sa.select(sub), sub.c.centroid)
+ else:
+ sql = sa.lambda_stmt(_base_query)