Skip to content

Commit

Permalink
Add feature field to SnapResult (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
plimkilde authored Dec 19, 2024
1 parent 42fe891 commit 26db481
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 4 additions & 3 deletions rivertopo/snapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
))

Expand Down
15 changes: 9 additions & 6 deletions tests/test_snapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ 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],
[5.5, 2.5],
])

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])

0 comments on commit 26db481

Please sign in to comment.