19

Lets say I have 2 kind of coordinate, first called center_point and second called test_point. I want to know if test_point coordinate is inside near or not to center_point coordinate by applying radius threshold. If I write it, its like:

center_point = [{'lat': -7.7940023, 'lng': 110.3656535}]
test_point = [{'lat': -7.79457, 'lng': 110.36563}]

radius = 5 # in kilometer

How to check if the test_point inside or outside the radius from center_point in Python? how I perform this kind task in Python?

Result expected will say that test_point inside or outside the radius from center_point coordinate.

ytomo
  • 809
  • 1
  • 7
  • 23
  • 1
    Calculate distance using haversine formula http://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points and see if it is less than r – plasmon360 Mar 09 '17 at 03:59
  • hi @user1753919 thanks, it works. – ytomo Mar 09 '17 at 04:52

3 Answers3

37

from recommendation of @user1753919 in his/her comment, I got the answer here: Haversine Formula in Python (Bearing and Distance between two GPS points)

final code:

from math import radians, cos, sin, asin, sqrt

def haversine(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles
    return c * r

center_point = [{'lat': -7.7940023, 'lng': 110.3656535}]
test_point = [{'lat': -7.79457, 'lng': 110.36563}]

lat1 = center_point[0]['lat']
lon1 = center_point[0]['lng']
lat2 = test_point[0]['lat']
lon2 = test_point[0]['lng']

radius = 1.00 # in kilometer

a = haversine(lon1, lat1, lon2, lat2)

print('Distance (km) : ', a)
if a <= radius:
    print('Inside the area')
else:
    print('Outside the area')

Thanks

Community
  • 1
  • 1
ytomo
  • 809
  • 1
  • 7
  • 23
20

GeoPy can handle it gracefully:

from geopy import distance

center_point = [{'lat': -7.7940023, 'lng': 110.3656535}]
test_point = [{'lat': -7.79457, 'lng': 110.36563}]
radius = 5 # in kilometer

center_point_tuple = tuple(center_point[0].values()) # (-7.7940023, 110.3656535)
test_point_tuple = tuple(test_point[0].values()) # (-7.79457, 110.36563)

dis = distance.distance(center_point_tuple, test_point_tuple).km
print("Distance: {}".format(dis)) # Distance: 0.0628380925748918

if dis <= radius:
    print("{} point is inside the {} km radius from {} coordinate".format(test_point_tuple, radius, center_point_tuple))
else:
    print("{} point is outside the {} km radius from {} coordinate".format(test_point_tuple, radius, center_point_tuple))

or if you need to know the great circle distance:

dis = distance.great_circle(center_point_tuple, test_point_tuple).km
print("Distance: {}".format(dis)) # Distance: 0.0631785164583489
Igor-S
  • 655
  • 8
  • 10
  • 1
    That's the format that was in the question. It's also more explicit where is latitude and where is longitude. – Igor-S Jan 10 '22 at 18:53
  • Oh yes. Sorry. You're right. That makes sense. I wanted to update the previous comment but I can't do that. So I'm gonna delete it – Devenom Jan 11 '22 at 14:29
2
from math import sqrt
a = center_point[0]['lat'] - test_point[0]['lat']
b = center_point[0]['lng'] - test_point[0]['lng']
c = sqrt(a * a  +  b * b)
if (c < radius):
        print("inside")
else:
        print("outside")
chrki
  • 6,143
  • 6
  • 35
  • 55
Egor
  • 764
  • 2
  • 6
  • 11