[英]How to extract lat / long coordinates from 2 numpy arrays in python?
I basically have 2 arrays, one containing Lattitude values, one Longitude. 我基本上有2个数组,一个包含纬度值,一个经度。 What I want is to extract those that meet a certain requirement .
我要提取的是那些符合一定条件的东西。
xLong = np.extract(abs(Long-requirement)<0.005,Long)
xLat = np.extract(abs(Lat-requirement)<0.005,Lat)
Lat and Long are numpy arrays. Lat和Long是numpy数组。
However, I only want to get those coordinates that both lat/long meet the requirement and I'm not sure how to do it . 但是,我只想获取经纬度都满足要求的那些坐标,而且我不确定如何做到。
If it's possible, I need to use numpy functions since I'm looking for optimization as well. 如果可能的话,因为我也在寻找优化,所以我需要使用numpy函数。 I know that I can iterate through all using a for and just add to different array but that would take a lot of time
我知道我可以使用for进行遍历并添加到其他数组中,但这会花费很多时间
You need to do this with boolean indexing. 您需要使用布尔索引进行此操作。 Whenever you create an boolean array the same shape as your array of interest, you can get just the
True
values out by indexing with the boolean array. 只要创建与目标数组形状相同的布尔数组,就可以通过使用布尔数组建立索引来获取
True
值。 I assume below that Long
and Lat
are the same size; 在下面,我假设
Long
和Lat
大小相同; if they're not the code will throw an exception. 如果不是,则代码将引发异常。
# 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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.