Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecation warning from np.cross #34

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading