Skip to content

Commit

Permalink
More direct shapely operations, Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaczero committed Feb 20, 2024
1 parent 7c22b43 commit 1b95988
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
14 changes: 8 additions & 6 deletions api/v1/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,8 +17,8 @@
photo_id_re = re.compile(r'view/(?P<id>\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:
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
Expand Down
31 changes: 16 additions & 15 deletions api/v1/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions states/aed_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit 1b95988

Please sign in to comment.