How to find user location within 500 meters of given lat and long in python

I want to find the user's location within 500 meters of the given lat and long in Python.

Given lat and long = 19.114315.72.911174

And I want to check if the new lat and long is in the range of 500 meters from the given lat and long.

new lat and long = 19.112398,72.912743

I use this formula in python ..

math.acos(math.sin(19.114315) * math.sin(19.112398) + math.cos(19.114315) * math.cos(19.112398) * math.cos(72.912743 - (72.911174))) * 6371 <= 0.500 

But this does not give me the expected results. Am I missing something? please, help..

+4
source share
3 answers

( ) . , , 500 , , , (, - ).

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

def haversine(lat1, long1, lat2, long2, EARTH_RADIUS_KM=6372.8):

    # get distance between the points
    phi_Lat = radians(lat2 - lat1)
    phi_Long = radians(long2 - long1)

    lat1 = radians(lat1)
    lat2 = radians(lat2)

    a = sin(phi_Lat/2)**2 + \
        cos(lat1) * cos(lat2) * \
        sin(phi_Long/2)**2

    c = 2 * asin(sqrt(a))
    return EARTH_RADIUS_KM * c

, :

points_1 = (19.114315,72.911174)
points_2 = (19.112398,72.912743)
threshold_km = 0.5


distance_km = haversine(points_1[0], points_1[1], points_2[0], points_2[1])
if distance_km < threshold_km:
    print('within range')
else:
    print('outside range')
+1

: . , , .

math.cos(longRad - (longRad)) == math.cos(0) == 1

im , ..

import math

def inRangeRad(latRad, longRad):
    sinLatSqrd = math.sin(latRad) * math.sin(latRad)
    cosLatSqrd = math.cos(latRad) * math.cos(latRad)
    inner = sinLatSqrd +  cosLatSqrd * math.cos(longRad - (longRad))
    return math.acos( inner ) * 6371 <= 0.500

def inRangeDeg(latDeg, longDeg):
    latRad = 0.0174532925 * latDeg
    longRad = 0.0174532925 * longDeg
    return inRangeRad(latRad, longRad)

print "test"
print "19.114315, 72.911174"
print inRangeDeg(19.114315, 72.911174)
0

! cos sin , GPS-, !

https://en.wikipedia.org/wiki/Geodesy#Geodetic_problems

In the case of plane geometry (valid for small areas on the Earth’s surface), the solutions of both problems are reduced to simple trigonometry. On the sphere, the solution is much more complicated, for example, in the inverse problem, the azimuths will differ between the two ends of the point connecting a large circle, an arc, i.e. geodesic.

Check out GeoPy for these calculations; you really don't want to implement this yourself.

0
source

Source: https://habr.com/ru/post/1619822/


All Articles