Skip to content

Commit

Permalink
Merge pull request #34 from plimkilde/fix-numpy-warning
Browse files Browse the repository at this point in the history
Fix deprecation warning from np.cross
  • Loading branch information
plimkilde authored Jan 6, 2025
2 parents d171bf0 + b8aacc2 commit ef0a2bc
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion rivertopo/snapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

SnapResult = namedtuple('SnapResult', ['feature', 'segment', 'param', 'offset'])

def cross2d(x, y):
"""
Scalar-value cross product of 2D vectors.
This used to be possible with np.cross(), but this has been deprecated with
NumPy 2.0+.
"""
return x[..., 0] * y[..., 1] - x[..., 1] * y[..., 0]

def snap_points(points, feature_id, linestring):
"""
For an array of points, find their nearest locations on a given linestring
Expand Down Expand Up @@ -52,7 +61,7 @@ def snap_points(points, feature_id, linestring):
closest_segment_index = np.argmin(point_dists)

# Horizontal signed offset (negative left, positive right, as seen in the direction of the line segment)
offset = point_dists[closest_segment_index] * np.sign(np.cross(vector_rejections[closest_segment_index], linestring_vectors[closest_segment_index]))
offset = point_dists[closest_segment_index] * np.sign(cross2d(vector_rejections[closest_segment_index], linestring_vectors[closest_segment_index]))

snap_results.append(SnapResult(
feature=feature_id,
Expand Down

0 comments on commit ef0a2bc

Please sign in to comment.