(Python) Geofencing by Thomas Roccia

Created the Sunday 19 March 2023. Updated 1 year ago.

Description:

This code snippet demonstrates a basic geofencing concept using Python. It checks if a target location (given by latitude and longitude coordinates) is within a specified radius (in kilometers) from a central point.

Code

            from geopy import Point
from geopy.distance import great_circle

def is_within_geofence(target_lat, target_lon, center_lat, center_lon, radius_km):
    center_point = Point(center_lat, center_lon)
    target_point = Point(target_lat, target_lon)

    distance = great_circle(center_point, target_point).km

    return distance <= radius_km

# Example usage
target_latitude = 40.7128
target_longitude = -74.0060

center_latitude = 40.730610
center_longitude = -73.935242

radius = 5  # 5 km

if is_within_geofence(target_latitude, target_longitude, center_latitude, center_longitude, radius):
    print("Target is within the geofence.")
else:
    print("Target is outside the geofence.")