Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions utils/geofencing.py
Original file line number Diff line number Diff line change
@@ -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