将地理位置转换为x y坐标以从中心点获得相对位置?

问题描述

我正在尝试从中心的一个“纬度”坐标到所有其他坐标的相对位置。

一个例子,假设我们有3个城市,阿姆斯特丹,伦敦和苏黎世。

阿姆斯特丹应该是我在2D平面上具有(0,0)xy坐标的中心点。现在,借助罗盘方位角,我正在尝试计算阿姆斯特丹从伦敦(西)和苏黎世(东南)的相对方向。一旦获取了方向并计算了阿姆斯特丹和伦敦之间以及阿姆斯特丹和苏黎世之间的距离,我便希望将其绘制为XY坐标。

我不确定在此方面我缺少什么。我在概念上错了吗?

当前输出

Current Output:

预期输出

Expected Output:

代码示例:

import math

def calc_compass_bearing(pointA,pointB):

    lat1 = math.radians(pointA[0])
    lat2 = math.radians(pointB[0])

    diffLong = math.radians(pointB[1] - pointA[1])

    x = math.sin(diffLong) * math.cos(lat2)
    y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1)
                                           * math.cos(lat2) * math.cos(diffLong))

    initial_bearing = math.atan2(x,y)

    initial_bearing = math.degrees(initial_bearing)
    compass_bearing = (initial_bearing + 360) % 360

    return compass_bearing


def calc_dist(lat1,lon1,lat2,lon2):

    R = 6372.8  # earth radius in kms

    dLat = math.radians(lat2 - lat1)
    dLon = math.radians(lon2 - lon1)
    lat1 = math.radians(lat1)
    lat2 = math.radians(lat2)

    a = math.sin(dLat / 2)**2 + math.cos(lat1) * \
        math.cos(lat2) * math.sin(dLon / 2)**2
    c = 2 * math.asin(math.sqrt(a))

    return R * c


locs = {'london': (51.509865,-0.118092),'zurich': (47.36667,8.55),'amsterdam': (52.377956,4.897070)}


# center point here is amsterdam
cp = 'amsterdam'
cp_geo_cords = locs[cp]
cp_xy_cords = (0,0)


plot_input = {}
for i in locs.keys():
    rp_geo_cords = locs[i]

    if i == cp:
        plot_input[i] = cp_xy_cords
    else:
        bearing = calc_compass_bearing(cp_geo_cords,rp_geo_cords)
        dist = calc_dist(
            cp_geo_cords[0],cp_geo_cords[1],rp_geo_cords[0],rp_geo_cords[1])

        bearing_rad = bearing * (math.pi / 180)  # convert bearing to radians
        delta_x = dist * math.sin(bearing_rad)
        delta_y = dist * math.cos(bearing_rad)
        tree_x = cp_xy_cords[0] + delta_x
        tree_y = cp_xy_cords[1] + delta_y

        plot_input[i] = (tree_x,tree_y)


#plotting using pyvis
from pyvis.network import Network
net = Network(height='800px',width='1200px')

for node in plot_input.keys():
    net.add_node(node,color='red',size=15,x=plot_input[node][0],y=plot_input[node][0],physics=False)

net.show("output.html")

解决方法

我发现了错误。绘图时,我也在y参数中使用了x针线虫。

for node in plot_input.keys():
    net.add_node(node,color='red',size=15,x=plot_input[node][0],y=plot_input[node][0],physics=False)

代码在解决此问题后效果很好!只需反转Y轴即可。