如何从 python 中的 2 个 numpy 数组中提取纬度/长坐标



我基本上有2个数组,一个包含Lattitude值,一个包含经度。我想要的是提取满足一定要求的那些。

xLong = np.extract(abs(Long-requirement)<0.005,Long)
xLat = np.extract(abs(Lat-requirement)<0.005,Lat)

Lat 和 Long 是 numpy 数组。

但是,我只想获得那些纬度/经度都满足要求的坐标,我不确定该怎么做.

如果可能的话,我需要使用 numpy 函数,因为我也在寻找优化。我知道我可以使用 for 遍历所有内容并添加到不同的数组中,但这需要很多时间

您需要使用布尔索引来执行此操作。 每当创建与感兴趣数组形状相同的布尔数组时,都可以通过使用布尔数组进行索引来获取True值。 我在下面假设LongLat的大小相同;如果不是,代码将引发异常。

# start building the boolean array.  long_ok and lat_ok will be the same
# shape as xLong and xLat.
long_ok = np.abs(Long - requirement) < 0.005
lat_ok = np.abs(Lat - requirement) < 0.005
# both_ok is still a boolean array which is True only where 
# long and lat are both in the region of interest
both_ok = long_ok & lat_ok
# now actually index into the original arrays.
long_final = Long[both_ok]
lat_final = Lat[both_ok]

最新更新