diff --git a/api/v1/node.py b/api/v1/node.py index 2738658..a28feaf 100644 --- a/api/v1/node.py +++ b/api/v1/node.py @@ -4,7 +4,7 @@ from fastapi import APIRouter, HTTPException from pytz import timezone -from shapely import Point +from shapely import get_coordinates from tzfpy import get_tz from middlewares.cache_middleware import configure_cache @@ -17,8 +17,8 @@ photo_id_re = re.compile(r'view/(?P\S+)\.') -def _get_timezone(position: Point) -> tuple[str | None, str | None]: - timezone_name: str | None = get_tz(position.x, position.y) +def _get_timezone(x: float, y: float) -> tuple[str | None, str | None]: + timezone_name: str | None = get_tz(x, y) timezone_offset = None if timezone_name: @@ -78,9 +78,11 @@ async def get_node(node_id: int): if aed is None: raise HTTPException(404, f'Node {node_id} not found') + x, y = get_coordinates(aed.position)[0] + photo_dict = await _get_image_data(aed.tags) - timezone_name, timezone_offset = _get_timezone(aed.position) + timezone_name, timezone_offset = _get_timezone(x, y) timezone_dict = { '@timezone_name': timezone_name, '@timezone_offset': timezone_offset, @@ -97,8 +99,8 @@ async def get_node(node_id: int): **timezone_dict, 'type': 'node', 'id': aed.id, - 'lat': aed.position.y, - 'lon': aed.position.x, + 'lat': y, + 'lon': x, 'tags': aed.tags, 'version': aed.version, } diff --git a/api/v1/tile.py b/api/v1/tile.py index 3ed8cfc..66d7c87 100644 --- a/api/v1/tile.py +++ b/api/v1/tile.py @@ -66,10 +66,6 @@ async def get_tile( def _mvt_encode(bbox: BBox, layers: Sequence[dict]) -> bytes: with start_span(description='Transforming MVT geometry'): - bbox_coords = np.asarray((get_coordinates(bbox.p1)[0], get_coordinates(bbox.p2)[0])) - bbox_coords = np.asarray(MVT_TRANSFORMER.transform(bbox_coords[:, 0], bbox_coords[:, 1])).T - span = bbox_coords[1] - bbox_coords[0] - coords_range = [] coords = [] @@ -80,17 +76,22 @@ def _mvt_encode(bbox: BBox, layers: Sequence[dict]) -> bytes: coords_range.append((coords_len, coords_len + len(feature_coords))) coords.extend(feature_coords) - coords = np.asarray(coords) - coords = np.asarray(MVT_TRANSFORMER.transform(coords[:, 0], coords[:, 1])).T - coords = np.rint((coords - bbox_coords[0]) / span * MVT_EXTENT).astype(int) - - i = 0 - for layer in layers: - for feature in layer['features']: - feature_coords_range = coords_range[i] - feature_coords = coords[feature_coords_range[0] : feature_coords_range[1]] - feature['geometry'] = set_coordinates(feature['geometry'], feature_coords) - i += 1 + if coords: + bbox_coords = np.asarray((get_coordinates(bbox.p1)[0], get_coordinates(bbox.p2)[0])) + bbox_coords = np.asarray(MVT_TRANSFORMER.transform(bbox_coords[:, 0], bbox_coords[:, 1])).T + span = bbox_coords[1] - bbox_coords[0] + + coords = np.asarray(coords) + coords = np.asarray(MVT_TRANSFORMER.transform(coords[:, 0], coords[:, 1])).T + coords = np.rint((coords - bbox_coords[0]) / span * MVT_EXTENT).astype(int) + + i = 0 + for layer in layers: + for feature in layer['features']: + feature_coords_range = coords_range[i] + feature_coords = coords[feature_coords_range[0] : feature_coords_range[1]] + feature['geometry'] = set_coordinates(feature['geometry'], feature_coords) + i += 1 with start_span(description='Encoding MVT'): return mvt.encode( diff --git a/states/aed_state.py b/states/aed_state.py index 7c5c780..6313061 100644 --- a/states/aed_state.py +++ b/states/aed_state.py @@ -8,7 +8,7 @@ from cachetools import TTLCache from pymongo import DeleteOne, ReplaceOne, UpdateOne from sentry_sdk import start_span, start_transaction, trace -from shapely import Point, points +from shapely import Point, get_coordinates, points from shapely.geometry import mapping from shapely.geometry.base import BaseGeometry from sklearn.cluster import Birch @@ -239,9 +239,11 @@ async def get_aed_by_id(aed_id: int) -> AED | None: async def get_all_aeds(filter: dict | None = None) -> Sequence[AED]: cursor = AED_COLLECTION.find(filter, projection={'_id': False}) docs = await cursor.to_list(None) + if not docs: + return () + coords = tuple(doc['position']['coordinates'] for doc in docs) positions = points(coords) - result = [None] * len(docs) for i, doc, position in zip(range(len(docs)), docs, positions, strict=True): @@ -262,7 +264,7 @@ async def get_aeds_within_geom(cls, geometry: BaseGeometry, group_eps: float | N if len(aeds) <= 1 or group_eps is None: return aeds - positions = tuple((aed.position.x, aed.position.y) for aed in aeds) + positions = tuple(get_coordinates(aed.position)[0] for aed in aeds) # deterministic sampling max_fit_samples = 7000