问题
I have an algorithm that computes shapes using geographic coordinates when certain conditions are satisfied. The algorithm outputs a latitude list and a longitude list like seen below. So lat[0] and lon[0] would represent a coordinate pair. I want to create a boolean array in the shape of a latitude and longitude map where the indices would be true if a corresponding coordinate point exists in the algorithm output. I have the original latitude and longitude information from the netcdf file, but how do I make a 2d array of coordinate points that can be compared against the algorithm output, and then use the matching indices to make this boolean array?
I've tried combining the latitude and longitude into a single array. len(lat) = 81, len(lon) = 480
and I need an array of (81,480)
. I figure I would have to use an numpy where function to determine where the coordinate pairs match.
lat_alg = [-47.25 -47.25 -47.25 -48. -48. -48. -48. -48. -48. -48.
-48. -48.75 -48.75 -48.75 -48.75 -48.75 -48.75 -49.5 -49.5 -49.5
-49.5 -50.25 -50.25 -50.25]
lon_alg = [225.75 226.5 227.25 226.5 227.25 228. 228.75 229.5 230.25 231.
231.75 228. 228.75 229.5 230.25 231. 231.75 229.5 230.25 231.
231.75 230.25 231. 231.75]
The boolean array I create is ...
ar_tracker = np.zeros((len(lat),len(lon)))
and I want the output to be 1 where the coordinates match.
回答1:
Since you have real values you cannot check with ==
. So we have to use windows confined by inequalities. As a result we get the indices of the elements within the choosen window (11 and 12 in this example)
# 1. build numpy arrays
lat = np.array([-47.25, -47.25, -47.25, -48., -48., -48., -48., -48., -48., -48., -48., -48.75, -48.75, -48.75, -48.75, -48.75, -48.75, -49.5, -49.5, -49.5, -49.5, -50.25, -50.25, -50.25])
lon= np.array([225.75, 226.5, 227.25, 226.5, 227.25, 228., 228.75, 229.5, 230.25, 231., 231.75, 228., 228.75, 229.5, 230.25, 231., 231.75, 229.5, 230.25, 231., 231.75, 230.25, 231., 231.75])
# 2. pick the values in the desired window for each data series, set to zeros the others
La = np.where( (lat> -49.0) & (lat<-48), lat, 0*lat)
Lo = np.where( (lon>226) & (lon<229), lon, 0*lon)
#3. give the indices where both series are non-zero
ind = np.argwhere(np.abs(Lo*La)>0.0001)
ind
array([[11],
[12]], dtype=int64)
Or If you prefere an array with booleans:
(np.abs(Lo*La)>0.0001).astype(int)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0])
回答2:
Try this:
import numpy as np
lat_alg = np.array([-47.25, -47.25, -47.25, -48., -48., -48., -48., -48., -48.,
-48., -48., -48.75, -48.75, -48.75, -48.75, -48.75, -48.75,
-49.5, -49.5, -49.5, -49.5, -50.25, -50.25, -50.25])
lon_alg = np.array([225.75, 226.5, 227.25, 226.5, 227.25, 228., 228.75, 229.5,
230.25, 231., 231.75, 228., 228.75, 229.5, 230.25, 231.,
231.75, 229.5, 230.25, 231., 231.75, 230.25, 231., 231.75])
desired_position = np.array([-47.5, 228])
tolerance = 1
lat = np.abs(lat_alg - desired_position[0]) <= tolerance
lon = np.abs(lon_alg - desired_position[1]) <= tolerance
desired_area = np.outer(lat, lon).astype(int)
If you want to compare exact matches instead of coordinate windows, try using numpy.isclose to avoid float-point discrepancies:
import numpy as np
lat_alg = np.array([-47.25, -47.25, -47.25, -48., -48., -48., -48., -48., -48.,
-48., -48., -48.75, -48.75, -48.75, -48.75, -48.75, -48.75,
-49.5, -49.5, -49.5, -49.5, -50.25, -50.25, -50.25])
lon_alg = np.array([225.75, 226.5, 227.25, 226.5, 227.25, 228., 228.75, 229.5,
230.25, 231., 231.75, 228., 228.75, 229.5, 230.25, 231.,
231.75, 229.5, 230.25, 231., 231.75, 230.25, 231., 231.75])
desired_position = np.array([-47.5, 228])
lat = np.isclose(lat_alg - desired_position[0], 0)
lon = np.isclose(lon_alg - desired_position[1], 0)
exact_matches = np.outer(lat, lon).astype(int)
Both desired_area
and exact_matches
are 2d arrays with shape (len(lat), len(lon))
.
回答3:
In order to be totally sure about if the pair exists, I suggest you build a list of tuples where each tuple contains a pair (lat,lon). For example:
def Mesh(X,Y):
A=[]
for x,y in zip(X,Y):
A.append((x,y))
return A
Coord=Mesh(lat_alg,lon_alg)
Then, if you know your grid resolution, you can easily check a pair as follows:
coord=(-49.5,230.25)
if coord in Coord:
print('True')
else:
print('False')
来源:https://stackoverflow.com/questions/57294654/how-to-check-if-a-coordinate-pair-lat-lon-exists-in-a-coordinate-grid