From 26db481339655a4462e9c24c1e267488d4522367 Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen <34237736+plimkilde@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:07:16 +0100 Subject: [PATCH] Add feature field to SnapResult (#28) --- rivertopo/snapping.py | 7 ++++--- tests/test_snapping.py | 15 +++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/rivertopo/snapping.py b/rivertopo/snapping.py index 148cb06..91231cd 100644 --- a/rivertopo/snapping.py +++ b/rivertopo/snapping.py @@ -6,9 +6,9 @@ ogr.UseExceptions() -SnapResult = namedtuple('SnapResult', ['segment', 'param', 'offset']) +SnapResult = namedtuple('SnapResult', ['feature', 'segment', 'chainage', 'offset']) -def snap_points(points, linestring): +def snap_points(points, feature_id, linestring): """ For an array of points, find their nearest locations on a given linestring geometry. @@ -55,8 +55,9 @@ def snap_points(points, linestring): offset = point_dists[closest_segment_index] * np.sign(np.cross(vector_rejections[closest_segment_index], linestring_vectors[closest_segment_index])) snap_results.append(SnapResult( + feature=feature_id, segment=closest_segment_index, - param=linestring_vector_projparams[closest_segment_index], + chainage=linestring_vector_projparams[closest_segment_index], offset=offset, )) diff --git a/tests/test_snapping.py b/tests/test_snapping.py index a85616e..e90969a 100644 --- a/tests/test_snapping.py +++ b/tests/test_snapping.py @@ -12,6 +12,8 @@ def test_snap_points(): linestring.AddPoint(6.0, 3.0) linestring.AddPoint(9.0, 7.0) + feature_id = 42 + points = np.array([ [1.0, 1.0], [4.5, 3.5], @@ -19,13 +21,14 @@ def test_snap_points(): ]) expected_results = [ - SnapResult(segment=0, param=0.28, offset=-0.2), - SnapResult(segment=1, param=0.25, offset=-0.5), - SnapResult(segment=1, param=0.75, offset=0.5), + SnapResult(feature=42, segment=0, chainage=0.28, offset=-0.2), + SnapResult(feature=42, segment=1, chainage=0.25, offset=-0.5), + SnapResult(feature=42, segment=1, chainage=0.75, offset=0.5), ] - snap_results = snap_points(points, linestring) + snap_results = snap_points(points, feature_id, linestring) + assert [actual.feature for actual in snap_results] == [expected.feature for expected in expected_results] assert [actual.segment for actual in snap_results] == [expected.segment for expected in expected_results] - assert np.allclose([actual.param for actual in snap_results], [expected.param for expected in expected_results]) - assert np.allclose([actual.offset for actual in snap_results], [expected.offset for expected in expected_results]) + assert np.allclose([actual.chainage for actual in snap_results], [expected.chainage for expected in expected_results]) + assert np.allclose([actual.chainage for actual in snap_results], [expected.chainage for expected in expected_results])