如何在Python中从2个numpy数组中提取纬度/经度坐标?
问题描述:
我基本上有2个数组,其中一个包含Lattitude值,一个Longitude。我想要的是提取符合特定要求的那些。如何在Python中从2个numpy数组中提取纬度/经度坐标?
xLong = np.extract(abs(Long-requirement)<0.005,Long)
xLat = np.extract(abs(Lat-requirement)<0.005,Lat)
Lat和Long是numpy数组。
但是,我只想得到那些经纬度都符合要求的坐标,我不知道该怎么做。
如果可能,我需要使用numpy函数,因为我也在寻找优化。我知道我可以遍历所有使用的for,只需添加到不同的数组,但这将花费很多时间
答
您需要使用boolean indexing.执行此操作无论何时创建布尔数组的形状与感兴趣数组的形状相同,您可以通过使用布尔数组索引来获取True
值。我在下面假设Long
和Lat
是相同的大小;如果他们不是这个代码会抛出一个异常。
# 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]
是的,它们的大小相同。谢谢你的回答,代码很好 – Barkz