Skip to content

Commit 2308b52

Browse files
Swap out spatialhash query for get_faces_containing_point
1 parent 6baf346 commit 2308b52

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

parcels/uxgrid.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,34 +68,39 @@ def get_axis_dim(self, axis: _UXGRID_AXES) -> int:
6868
return self.uxgrid.n_face
6969

7070
def search(self, z, y, x, ei=None):
71-
tol = 1e-10
71+
tol = 1e-6
7272

7373
def try_face(fid):
74-
bcoords, err = self.uxgrid._get_barycentric_coordinates(y, x, fid)
74+
bcoords, err = self._get_barycentric_coordinates(y, x, fid)
7575
if (bcoords >= 0).all() and (bcoords <= 1).all() and err < tol:
76-
return bcoords, fid
77-
return None, None
76+
return bcoords
77+
return None
7878

7979
zi, zeta = _search_1d_array(self.z.values, z)
8080

8181
if ei is not None:
8282
_, fi = self.unravel_index(ei)
83-
bcoords, fi_new = try_face(fi)
83+
bcoords = try_face(fi)
8484
if bcoords is not None:
85-
return bcoords, self.ravel_index(zi, fi_new)
85+
return bcoords, self.ravel_index(zi, fi)
8686
# Try neighbors of current face
8787
for neighbor in self.uxgrid.face_face_connectivity[fi, :]:
8888
if neighbor == -1:
8989
continue
90-
bcoords, fi_new = try_face(neighbor)
90+
bcoords = try_face(neighbor)
9191
if bcoords is not None:
92-
return bcoords, self.ravel_index(zi, fi_new)
92+
return bcoords, self.ravel_index(zi, neighbor)
9393

94-
# Global fallback using spatial hash
95-
fi, bcoords = self.uxgrid.get_spatial_hash().query([[x, y]])
94+
# Global fallback as last ditch effort
95+
face_ids = self.uxgrid.get_faces_containing_point([x, y], return_counts=False)[0]
96+
fi = face_ids[0] if len(face_ids) > 0 else -1
9697
if fi == -1:
9798
raise FieldOutOfBoundError(z, y, x)
98-
return {"Z": (zi, zeta), "FACE": (fi[0], bcoords[0])}
99+
bcoords = try_face(fi)
100+
if bcoords is None:
101+
raise FieldOutOfBoundError(z, y, x)
102+
103+
return {"Z": (zi, zeta), "FACE": (fi, bcoords)}
99104

100105
def _get_barycentric_coordinates(self, y, x, fi):
101106
"""Checks if a point is inside a given face id on a UxGrid."""
@@ -104,8 +109,8 @@ def _get_barycentric_coordinates(self, y, x, fi):
104109
node_ids = self.uxgrid.face_node_connectivity[fi, 0:n_nodes]
105110
nodes = np.column_stack(
106111
(
107-
np.deg2rad(self.uxgrid.grid.node_lon[node_ids].to_numpy()),
108-
np.deg2rad(self.uxgrid.grid.node_lat[node_ids].to_numpy()),
112+
np.deg2rad(self.uxgrid.node_lon[node_ids].to_numpy()),
113+
np.deg2rad(self.uxgrid.node_lat[node_ids].to_numpy()),
109114
)
110115
)
111116

0 commit comments

Comments
 (0)