team-9/backend/geo_access.py

60 lines
1.8 KiB
Python
Raw Normal View History

2025-08-02 01:52:25 +02:00
from math import radians, sin, cos, sqrt, atan2
from shapely import Polygon
# class PolygonChecker:
# """
# A class to check if a point is inside a polygon defined by its vertices.
# The polygon is defined by a list of points (latitude, longitude).
#
# """
# def __init__(self, *points):
# self.coords = points[:] if points else []
# self.polygon = Polygon(self.coords)
#
# def is_inside(self, point) -> bool:
# return self.polygon.contains(point)
class CircleChecker:
"""
A class to check if a point is inside a circle defined by its center and radius.
The circle is defined by a center point (latitude, longitude) and a radius in meters
"""
def __init__(self, center, radius_meters):
self.center = center
self.radius_meters = radius_meters
def is_inside(self, point) -> bool:
return is_inside_circle(self.center, point, self.radius_meters)
def is_inside_circle(center, point, radius_meters):
R = 6371000 # Earth radius in meters
lat1, lon1 = center
lat2, lon2 = point
dlat = radians(lat2 - lat1)
dlon = radians(lon2 - lon1)
a = sin(dlat / 2) ** 2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon / 2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
return distance <= radius_meters
# Default CircleChecker with a radius of 1000 meters
lido_circle_checker = CircleChecker((46.6770371, 11.1860135), 160) # Center
# Default PolygonChecker with a predefined polygon
# polygon_coords = [
# (46.677168, 11.185606),
# (46.676910, 11.185692),
# (46.676673, 11.186094),
# (46.676691, 11.186630),
# (46.676673, 11.187094),
# (46.677309, 11.187242),
# (46.677280, 11.185606)
# ]
#
# lido_polygon_checker = PolygonChecker(polygon_coords)