2017-11-11 240 views
0

我基本上有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,只需添加到不同的數組,但這將花費很多時間

回答

1

您需要使用boolean indexing.執行此操作無論何時創建布爾數組的形狀與感興趣數組的形狀相同,您可以通過使用布爾數組索引來獲取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] 
+1

是的,它們的大小相同。謝謝你的回答,代碼很好 – Barkz