如何在 python 中找到距给定纬度和经度 500 米以内的用户位置
how to find user location within 500 meters from given lat and long in python
我想在 Python 中找到距给定经纬度 500 米以内的用户位置。
给定纬度和经度 = 19.114315,72.911174
而且我想检查新的纬度和经度是否在给定的纬度和经度的 500 米范围内..
新经纬度 = 19.112398,72.912743
我在 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
但它没有给我预期的结果..我是不是错过了什么?
请帮助..
提示:编写可重复使用的代码,并清理你的数学。
看来你有一个错误,就在该公式的末尾..
math.cos(longRad - (longRad))
== math.cos(0) == 1
我不擅长地理方面的东西,所以我不会纠正它..
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)
要非常小心!您不能只使用 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 to both problems reduce to simple trigonometry.
On the sphere, the solution is significantly more complex, e.g., in
the inverse problem the azimuths will differ between the two end
points of the connecting great circle, arc, i.e. the geodesic.
查看 GeoPy 这些计算,您真的不想自己实现它。
您可以使用 Haversine 公式获得两点之间的大圆距离(沿球体)。将地球视为远距离的球体存在一些问题,但对于 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')
我想在 Python 中找到距给定经纬度 500 米以内的用户位置。
给定纬度和经度 = 19.114315,72.911174
而且我想检查新的纬度和经度是否在给定的纬度和经度的 500 米范围内..
新经纬度 = 19.112398,72.912743
我在 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
但它没有给我预期的结果..我是不是错过了什么? 请帮助..
提示:编写可重复使用的代码,并清理你的数学。 看来你有一个错误,就在该公式的末尾..
math.cos(longRad - (longRad))
== math.cos(0) == 1
我不擅长地理方面的东西,所以我不会纠正它..
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)
要非常小心!您不能只使用 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 to both problems reduce to simple trigonometry. On the sphere, the solution is significantly more complex, e.g., in the inverse problem the azimuths will differ between the two end points of the connecting great circle, arc, i.e. the geodesic.
查看 GeoPy 这些计算,您真的不想自己实现它。
您可以使用 Haversine 公式获得两点之间的大圆距离(沿球体)。将地球视为远距离的球体存在一些问题,但对于 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')