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

时间:2022-04-27 16:58:17

I basically have 2 arrays, one containing Lattitude values, one Longitude. What I want is to extract those that meet a certain requirement .

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

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. 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

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

1 个解决方案

#1


1  

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. I assume below that Long and Lat are the same size; if they're not the code will throw an exception.

您需要使用布尔索引来执行此操作。每当您创建一个与您感兴趣的数组相同的布尔数组时,您可以通过使用布尔数组进行索引来获得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]

#1


1  

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. I assume below that Long and Lat are the same size; if they're not the code will throw an exception.

您需要使用布尔索引来执行此操作。每当您创建一个与您感兴趣的数组相同的布尔数组时,您可以通过使用布尔数组进行索引来获得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]