将纬度/经度转换为XY [英] Convert Lat/long to XY
问题描述
我想将经/纬度转换为XY坐标。我捡起了这个方程,但没有得到想要的输出:
I want to convert lat/long to XY coordinates. I picked up this equation but can't get the desired output:
x = r λ cos(φ0)
y = r φ
这两个点的度量是:
point1 = (-37.8206195, 144.9837765)
point2 = (-37.8193712, 144.9837765)
尝试:
import math
avg = (-37.8206195 + -37.8193712)/2
rad_avg = math.pi / 180
point1 = (-37.8206195, 144.9837765)
point2 = (-37.8193712, 144.9837765)
dist = rad_avg * math.cos(avg)
print(dist)
出局:
0.01732592680044846
输出应该在1.6亿左右
The output should be around 160m
推荐答案
首先 math.cos 期望以弧度表示的角度参数。要从度数转换为弧度,您需要执行以下操作:
First of all math.cos expects angle argument in radians. To convert from degrees to radians you need to do:
rad_avg = avg * math.pi / 180
甚至:
math.radians(<angle_in_degrees>)
基本上,这意味着您正在使用<$ c映射180º $ c> pi 并取您的角度部分。
Basically it means you're mapping 180º with pi
and taking the portion for your angle.
我假设您要通过首先转换来计算两个点之间的距离到 xy坐标(根据您的参考)。
I assume then that you want to compute distance between both points by converting it first to "xy" coordinates (according to your reference).
首先需要在同一坐标系中获得两个点。如链接所示,对于小区域,可以通过以下方式估算它们:
You need to get first both points in the same coordinate system. As the link states, for small areas, they can be estimated by:
- x = rλcos(φ0)
- y = rφ
所以您需要这样做:
import math
point1 = (-37.8206195, 144.9837765) # Lat/Long (lambda/phi)
point2 = (-37.8193712, 144.9837765) # Lat/Long (lambda/phi)
r = 6371000 # meters
phi_0 = point1[1]
cos_phi_0 = math.cos(math.radians(phi_0))
def to_xy(point, r, cos_phi_0):
lam = point[0]
phi = point[1]
return (r * math.radians(lam) * cos_phi_0, r * math.radians(phi))
point1_xy = to_xy(point1, r, cos_phi_0)
point2_xy = to_xy(point2, r, cos_phi_0)
最后,要计算笛卡尔坐标的距离,您需要使用皮塔哥拉斯定理 d = sqrt(delta_x ^ 2 + delta_y ^ 2)
Finally, to compute distance in cartesian coordinates you need to use the Pitagoras Theorem d = sqrt(delta_x^2 + delta_y^2)
在您的示例中:
dist = math.sqrt((point1_xy[0] - point2_xy[0])**2 + (point1_xy[1] - point2_xy[1])**2)
其中结果: 113.67954606562853
。
此外,还有一种捷径可以使距离公式正确:
Plus, there's a shortcut to get it right to the distance formula:
-
d = r * sqrt(x²+y²)
其中,x =(λ2-λ1)*数学。 cos(φ0)
和y =(φ2-φ1)
d = r * sqrt(x² + y²)
wherex = (λ2 - λ1) * math.cos(φ0)
andy = (φ2 - φ1)
这篇关于将纬度/经度转换为XY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!