我为不同的节点设置了一组lat / lon坐标 由于我必须在模拟器中表示这些位置,我必须将它们转换为笛卡尔坐标 为此,我使用python包pyproj。
lat1 = 50.0
lon1 = 10.0
lat2 = 50.5
lon2 = 10.5
x1, y1, z1 = wgs84_to_utm32(lat1, lon1, 0.0)
x2, y2, z2 = wgs84_to_utm32(lat2, lon2, 0.0)
print(distance_haversine(lat1, lon1, lat2, lon2))
print(distance_cartesian(x1, y1, x2, y2))
输出是:
66012.5130481
102485.874713
相差超过36公里。
所以我的问题是,如何转换纬度/经度坐标以保持距离。我不在乎最小的错误。
编辑:
#
# Convert WGS84 (Geographic) coordinates to UTM32 (Germany)
#
def wgs84_to_utm(lat, lon, alt):
wgs84 = Proj(init='epsg:4326')
utm_de = Proj(init='epsg:32632') # utm germany = 32
return transform(wgs84, utm_de, lat, lon, alt)
EDIT2:
好的,要知道,我知道我想把球体上的距离与平面上的距离进行比较。
但是由于我在模拟WLAN节点,因此这些节点之间的距离至关重要。但我没有其他信息,只有他们的纬度/经度坐标。
我如何在平面上表示这些纬度/经度坐标,以便保留距离?
EDIT3:
def distance_haversine(lat1, lon1, lat2, lon2):
# approximate radius of earth in km
R = 6373.0 * 1000
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
return distance
def distance_cartesian(x1, y1, x2, y2):
dx = x1 - x2
dy = y1 - y2
return sqrt(dx * dx + dy * dy)
答案 0 :(得分:1)
转换为utm时出现错误。请尝试使用utm模块
import utm
lat1 = 50.0
lon1 = 10.0
lat2 = 50.5
lon2 = 10.5
x1, y1, z1, u = utm.from_latlon(lat1, lon1)
x2, y2, z2, u = utm.from_latlon(lat2, lon2)
print(distance_haversine(lat1, lon1, lat2, lon2))
print(distance_cartesian(x1, y1, x2, y2))
输出:
66012.51304805529
66047.84250743943