+ if details.geometry_output & ntyp.GeometryFormat.GEOJSON:
+ def _add_geometry(sql: SaSelect, column: SaColumn) -> SaSelect:
+ return sql.add_columns(sa.func.ST_AsGeoJSON(
+ sa.case((sa.func.ST_NPoints(column) > 5000,
+ sa.func.ST_SimplifyPreserveTopology(column, 0.0001)),
+ else_=column), 7).label('geometry_geojson'))
+ else:
+ def _add_geometry(sql: SaSelect, column: SaColumn) -> SaSelect:
+ return sql.add_columns(sa.func.ST_GeometryType(column).label('geometry_type'))
+
+ row_func: RowFunc[nres.DetailedResult]
+ row, row_func = await find_in_all_tables(conn, place, _add_geometry)
+
+ if row is None:
+ return None
+
+ result = row_func(row, nres.DetailedResult)
+ assert result is not None
+
+ # add missing details
+ assert result is not None
+ if 'type' in result.geometry:
+ result.geometry['type'] = GEOMETRY_TYPE_MAP.get(result.geometry['type'],
+ result.geometry['type'])
+ indexed_date = getattr(row, 'indexed_date', None)
+ if indexed_date is not None:
+ result.indexed_date = indexed_date.replace(tzinfo=dt.timezone.utc)
+
+ await nres.add_result_details(conn, [result], details)
+
+ return result
+
+
+async def get_simple_place(conn: SearchConnection, place: ntyp.PlaceRef,
+ details: ntyp.LookupDetails) -> Optional[nres.SearchResult]:
+ """ Retrieve a place as a simple search result from the database.
+ """
+ log().function('get_simple_place', place=place, details=details)
+
+ def _add_geometry(sql: SaSelect, col: SaColumn) -> SaSelect:
+ if not details.geometry_output:
+ return sql
+
+ out = []
+
+ if details.geometry_simplification > 0.0:
+ col = sa.func.ST_SimplifyPreserveTopology(col, details.geometry_simplification)
+
+ if details.geometry_output & ntyp.GeometryFormat.GEOJSON:
+ out.append(sa.func.ST_AsGeoJSON(col, 7).label('geometry_geojson'))
+ if details.geometry_output & ntyp.GeometryFormat.TEXT:
+ out.append(sa.func.ST_AsText(col).label('geometry_text'))
+ if details.geometry_output & ntyp.GeometryFormat.KML:
+ out.append(sa.func.ST_AsKML(col, 7).label('geometry_kml'))
+ if details.geometry_output & ntyp.GeometryFormat.SVG:
+ out.append(sa.func.ST_AsSVG(col, 0, 7).label('geometry_svg'))
+
+ return sql.add_columns(*out)
+
+
+ row_func: RowFunc[nres.SearchResult]
+ row, row_func = await find_in_all_tables(conn, place, _add_geometry)
+
+ if row is None:
+ return None
+
+ result = row_func(row, nres.SearchResult)
+ assert result is not None
+
+ # add missing details
+ assert result is not None
+ if hasattr(row, 'bbox'):
+ result.bbox = ntyp.Bbox.from_wkb(row.bbox)
+
+ await nres.add_result_details(conn, [result], details)
+
+ return result