我想找出一个未知的目标位置(纬度和经度坐标)。有4个已知点(建筑物内经纬度坐标对的建筑物对),每个点到目标位置的距离以米为单位。如何在Python中计算目标位置的坐标?
我测试了以下代码(怀疑“ initial_location”参数的含义是什么,可以是任何站点吗?)。因此,我在ArcGIS中验证了结果,并在每个测站周围创建了一个缓冲区(使用算法的距离作为半径)并与缓冲区结果相交,这似乎不合逻辑。
from scipy.optimize import minimize
import math
def mse(x, locations, distances):
mse = 0.0
for location, distance in zip(locations, distances):
distance_calculated = great_circle_distance(x[0], x[1], location[0], location[1])
mse += math.pow(distance_calculated - distance, 2.0)
return mse / len(locations)
EARTH_CIRCUMFERENCE = 6378137 # earth circumference in meters
def great_circle_distance(lat1,lon1,lat2,lon2):
dLat = math.radians(lat2 - lat1)
dLon = math.radians(lon2 - lon1)
a = (math.sin(dLat / 2) * math.sin(dLat / 2) +
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
math.sin(dLon / 2) * math.sin(dLon / 2))
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = EARTH_CIRCUMFERENCE * c
return d
initial_location = (41.828286,1.759349) # (lat, long)
locations = [(41.828286,1.759349),(41.827988,1.759572),(41.827891,1.75934),(41.828189,1.759117)] # [ (lat1, long1), ... ]
distances = [10,10,10,10] # [ distance1, ... ]
result = minimize(
mse, # The error function
initial_location, # The initial guess
args=(locations, distances), # Additional parameters for mse
method='L-BFGS-B', # The optimisation algorithm
options={
'ftol':1e-5, # Tolerance
'maxiter': 1e+7 # Maximum iterations
})
location = result.x
print("Location",location)