From 82edb2fe5d00b3c98a6cde985f7fec71f784a150 Mon Sep 17 00:00:00 2001 From: mudrcidullah Date: Sun, 15 Feb 2026 12:51:12 +0300 Subject: [PATCH] feat: add geofencing utility for distance-based attendance validation --- utils/geofencing.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 utils/geofencing.py diff --git a/utils/geofencing.py b/utils/geofencing.py new file mode 100644 index 0000000..00a7ba5 --- /dev/null +++ b/utils/geofencing.py @@ -0,0 +1,35 @@ +import math + +def is_within_boundary(student_lat, student_lon, school_lat, school_lon, max_distance_km): + """ + Determine whether a student is within the allowed geographical boundary. + + Parameters: + student_lat (float): Student's current latitude + student_lon (float): Student's current longitude + school_lat (float): School's latitude + school_lon (float): School's longitude + max_distance_km (float): Maximum allowed distance in kilometers + + Returns: + bool: True if student is within the allowed boundary, False otherwise + """ + + EARTH_RADIUS_KM = 6371 + + delta_lat = math.radians(school_lat - student_lat) + delta_lon = math.radians(school_lon - student_lon) + + student_lat_rad = math.radians(student_lat) + school_lat_rad = math.radians(school_lat) + + a = (math.sin(delta_lat / 2) ** 2 + + math.cos(student_lat_rad) * + math.cos(school_lat_rad) * + math.sin(delta_lon / 2) ** 2) + + c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) + + distance = EARTH_RADIUS_KM * c + + return distance <= max_distance_km \ No newline at end of file