如何将纬度/经纬度坐标转换为公里,以创建1公里的栅格

2024-04-24 11:26:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含十进制纬度/经度坐标的数据帧。在

我的目标是在1km²的矩形网格上聚合数据。为此,我根据Convert latitude, longitude to distance from equator in kilometers and round to nearest kilometer中描述的方法将坐标转换为km

该方法包括计算从参考点到点(lat=0,lon)和(lat,lon=0)的距离。在

但它不起作用,因为它似乎依赖于参考点。在

通过将我的参考点取为(lon_ref=mean(lon),lat_ref=mean(lat)),我最终聚集在彼此相距120公里的相同瓷砖点上。在

这是我正在使用的代码:

# get the coordinates of my reference point

lat_ref, lon_ref = data["lat"].mean() , data["lon"].mean()

# the distance function

from pyproj import Geod 
wgs84_geod = Geod(ellps='WGS84')

format = lambda x: wgs84_geod.inv(lon_ref,lat_ref,0,x)[2]/1000 #km 

format = lambda x: wgs84_geod.inv(lon_ref,lat_ref,x,0)[2]/1000 #km 

# Apply the function on my dataframe

data["londist"]=data['lon'].map(format)

data["latdist"]=data['lat'].map(format)

# round to the nearest km

step=1 # 1km

to_bin = lambda x: np.round(x / step) * step

data["latbin"] = data['latdist'].map(to_bin)

data["lonbin"] = data['londist'].map(to_bin)

这对某些人有用,但对其他人不行

示例:

^{2}$

使用上述代码计算距离和圆角:

point1 (latbin = 259 , lonbin=5205)

point2(latbin = 259 , lonbin=5205)

这两点将合并在一起 不过,两点之间的距离是85公里!在

dist=wgs84_geod.inv(4.29949,46.9574,3.18153,46.9972)[2]/1000 

我怎样才能解决这个问题? 如果我的数据帧中有1000万lat/lon,有没有其他有效的方法来进行聚合?在


Tags: theto数据方法refformatmapdata
1条回答
网友
1楼 · 发布于 2024-04-24 11:26:59

看起来您给format变量赋值了两次,第二次赋值删除了第一个。你的意思是要吃format_lon和{}之类的东西吗?在

请注意,一旦您修复了这个问题,结果仍将取决于参考点-这是不可避免的,当您将球坐标投影到平面地图。但结果应该是合理的。在

相关问题 更多 >