How To Check If A Coordinate Pair (lat,lon) Exists In A Coordinate Grid?
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 be
Solution 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])
Solution 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))
.
Solution 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')
Post a Comment for "How To Check If A Coordinate Pair (lat,lon) Exists In A Coordinate Grid?"