question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ops.snap not functioning as expected.

See original GitHub issue

I believe I found a bug in ops.snap; I’ve made a post on StackExchange GIS but no one has really responded. I will try to replicate that post here.

Expected behavior and actual behavior.

ops.snap does not snap two geometries when using a tolerance that is greater than their distance apart. Specifically, the endpoints of two polylines are 0.0006 apart, but ops.snap with a tolerance of 0.001 does not snap the geometries together. A tolerance of 0.003 does snap them together, however.

Steps to reproduce the problem.

I have two polylines whose endpoints are near to each other, but do not overlap. I’ve put them into this shapefile. I want to use shapely’s ops.snap() to snap their endpoints, but it’s not working as expected.

from shapely import ops
import geopandas as gpd

wontsnap = gpd.read_file(path_to_wontsnap)
g1 = wontsnap.geometry.values[0]
g2 = wontsnap.geometry.values[1]

If I check the distance between the geometries, I get

g1.distance(g2)
Out[661]: 0.0006421029901831172

This agrees (roughly) with the measure tool in QGIS:

image

Now if I try to snap the two geometries with a tolerance that is greater than the distance between their endpoints:

snapped = ops.snap(g1, g2, 0.001)

The snapped object contains only the g1 geometry. I.e., they don’t snap.

If I increase the tolerance by 10x:

snapped = ops.snap(g1, g2, 0.01) The snapped object includes both geometries, snapped correctly.

If I trim the g1 and g2 geometries to the last and first 10 points, respectively, snapping works as expected.

I have tried various tolerances between 0.001 and 0.01–the geometries will snap at 0.003 but not 0.002. g1.is_valid and g2.is_valid both return True.

Operating system

Windows 10

Shapely version and provenance

1.6.3 via conda install shapely

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sgilliescommented, Apr 9, 2021

@erihe251 yes, I think maximum is more true than mimimum. It’s curious that JTS, GEOS, and PostGIS avoid using either minimum or maximum in their documentation of this feature. Maybe maximum isn’t exactly correct.

I too am surprised that your point doesn’t snap to (1, 0).

1reaction
erihe251commented, Apr 8, 2021

I agree that the behaviour is not what I would expect from reading the documentation.

from shapely.geometry import Point as Point
from shapely.geometry import LineString as LineString
from shapely.ops import snap as snap

point = Point(0.1, 0)
line = LineString([(0, 0), (1, 0), (2, 0)])
for tol in [0, 0.2, 1, 1.5, 2.1]:
    print(snap(point, line, tol))



gives

POINT (0.1 0)
POINT (0 0)
POINT (0 0)
POINT (2 0)
POINT (2 0)

Shouldn’t it snap to (1,0) at some point?

In the documentation the tolerance is also described as: The tolerance argument specifies the minimum distance between vertices for them to be snapped. Shouldn’t it be maximum distance?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ops.snap not functioning as expected. · Issue #646 · shapely ...
I want to use shapely's ops.snap() to snap their endpoints, but it's not working as expected. from shapely import ops import geopandas as ......
Read more >
Why is Shapely's snapping (GEO snaps) not working as ...
The shapely.ops.snap function snaps to the vertices of geometries only. See the illustration below. On the left, the red vertex is within ...
Read more >
Osnap (Object Snap) does not work in AutoCAD
Issue: When trying to use object snaps to create or edit objects in AutoCAD drawings, the expected snap point markers are not showing....
Read more >
Snapcraft development tips: how to troubleshoot snaps with ...
Inspect service status & manually run services. With snaps that contain services, your most likely point of friction will be after the build ......
Read more >
Error In File Operations Snap - SnapLogic Community
You may have expected to copy the source file into the directory specified in the Target property, but the Snap does not work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found