Python 沿直线生成lat long
我需要找到更靠近其他坐标的坐标(lat/long),这两个坐标之间的距离占一定百分比 例如:Python 沿直线生成lat long,python,python-2.7,coordinates,Python,Python 2.7,Coordinates,我需要找到更靠近其他坐标的坐标(lat/long),这两个坐标之间的距离占一定百分比 例如: location1 = (32.7991094663, -117.234719251) location2 = (32.7094778234, -117.136413578) # find a new location closer by 60% from location1 than location2 find_closer_location(location1, location2, 60)
location1 = (32.7991094663, -117.234719251)
location2 = (32.7094778234, -117.136413578)
# find a new location closer by 60% from location1 than location2
find_closer_location(location1, location2, 60)
为了计算距离,我使用下面的算法
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6371 * c
return km
计算它:
def moveMe(t1,t2,perc):
x1, y1= t1 # location1 = (32.7991094663, -117.234719251)
x2, y2 = t2 # location2 = (32.7094778234, -117.136413578)
dx = (x2-x1) / 100.0 * perc
dy = (y2-y1) / 100.0 * perc
return (x1+dx,y1+dy)
location1 = (32.7991094663, -117.234719251)
location2 = (32.7094778234, -117.136413578)
print(moveMe(location1,location2,10))
输出:
(32.808072630590004, -117.2445498183)
这将在
perc
100)t1
和t2
之间的直线上插入一个新点。如果你想从这条线中获得分数,你就必须更具创造性。谢谢大家的努力
最后,我想我有了一个解决办法
from math import radians, cos, sin, asin, sqrt, atan2, degrees, log, tan, pi
R = 6378.1 #Radius of the Earth in km
def get_distance(lon1, lat1, lon2, lat2):
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = R * c
return km
def calculateBearing(lat1, lng1, lat2, lng2):
'''calculates the azimuth in degrees from start point to end point'''
startLat = radians(lat1)
startLong = radians(lng1)
endLat = radians(lat2)
endLong = radians(lng2)
dLong = endLong - startLong
dPhi = log(tan(endLat/2.0+pi/4.0)/tan(startLat/2.0+pi/4.0))
if abs(dLong) > pi:
if dLong > 0.0:
dLong = -(2.0 * pi - dLong)
else:
dLong = (2.0 * pi + dLong)
bearing = (degrees(atan2(dLong, dPhi)) + 360.0) % 360.0;
return bearing
def getDestinationLatLong(lat, lng, azimuth, d):
'''returns the lat an long of destination point
given the start lat, long, aziuth, and distance'''
brng = radians(azimuth) #Bearing is degrees converted to radians.
lat1 = radians(lat) #Current dd lat point converted to radians
lon1 = radians(lng) #Current dd long point converted to radians
lat2 = asin(sin(lat1) * cos(d/R) + cos(lat1)* sin(d/R)* cos(brng))
lon2 = lon1 + atan2(sin(brng) * sin(d/R)* cos(lat1), cos(d/R)- sin(lat1)* sin(lat2))
#convert back to degrees
lat2 = degrees(lat2)
lon2 = degrees(lon2)
return[lat2, lon2]
location1 = (32.7991094663, -117.234719251)
location2 = (32.7094778234, -117.136413578)
percentage = 60
distance = get_distance(location1[1], location1[0], location2[1], location2[0])
print distance
new_distance = distance - (distance * percentage / 100)
print new_distance
azimuth = calculateBearing(location1[0], location1[1], location2[0], location2[1])
coords = getDestinationLatLong(location1[0], location1[1], azimuth, new_distance)
print coords
参考资料:什么百分比?Ken Syme,如果我理解正确,两个坐标之间的距离百分比。你得了2分。如果你想把其中一个点移动到离另一个点近60%的地方,你能指定要移动的点吗?地球不是平的!用
(10,10)
和(15,15)
的方式有20%没有(9,9)
。。你也不应该鼓励低质量的问题。@sayse修复了它-为什么你认为它是一个低质量的问题?如果你把贴在这里的每一个问题都记下来,去掉那些可以通过思考、谷歌搜索或其他研究来解决的问题,那么几乎没有一个问题会被解决。@KenSyme-嗯,他没有定义距离是在地面上测量的。你认为他想把“在地面上行驶”的距离减半吗?@PatrickArtner-因为op没有用他们自己的任何努力来解决他们的问题。