You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Snapping and vertex tool stop working when layers are removed and added again back to the project. Sometimes this happens after the first time and sometimes after numerous times. Only way to recover from this is to restart QGIS.
Steps to reproduce the issue
It was incredibly difficult to reproduce the bug reliably but finally after hours of trying I managed to do that with a little bit of scripting. I prepared a snapping.zip that includes snapping.gpkg with two layers and python script snapping.py with this content:
frompathlibimportPathfromtypingimportcastfromqgis.coreimport (
Qgis,
QgsPointXY,
QgsProject,
QgsSnappingConfig,
QgsSnappingUtils,
QgsVectorLayer,
)
fromqgis.guiimportQgisInterfacefromqgis.PyQt.QtCoreimportQCoreApplicationfromqgis.utilsimportifaceasutils_ifaceGPKG_PATH=Path(__file__).parent/"snapping.gpkg"LAYER_NAMES= ["area1", "area2"]
SHAPES= ["Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))", "Polygon ((2 0, 3 0, 3 1, 2 1, 2 0))"]
iface=cast(QgisInterface, utils_iface)
defset_snapping_mode_to_all_layers():
snapping_config=QgsSnappingConfig(QgsProject.instance().snappingConfig())
snapping_config.setMode(Qgis.SnappingMode.AllLayers)
snapping_config.setEnabled(True)
QgsProject.instance().setSnappingConfig(snapping_config)
defcheck_if_location_snapping_is_working_for_layers() ->bool:
forlayerinQgsProject.instance().mapLayers().values():
works=_check_if_location_snapping_is_working_for_layer(layer)
ifnotworks:
returnFalsereturnTruedefsnapping_still_works() ->bool:
""" For users to be able to observe the bug, the snapping should not be working initially """ifnotcheck_if_location_snapping_is_working_for_layers():
layer=next(
layer.name()
forlayerinQgsProject.instance().mapLayers().values()
iflayer.isEditable()
)
print(f"Snapping is not working for {layer}")
returnFalseforiinrange(100):
print(f"Sub-iteration {i}")
_add_layers()
ifnotcheck_if_location_snapping_is_working_for_layers():
returnTrue_remove_layers()
_process_events()
returnTruedef_check_if_location_snapping_is_working_for_layer(layer: QgsVectorLayer) ->bool:
layer.startEditing()
iface.setActiveLayer(layer)
geom=next(iter(layer.getFeatures())).geometry()
vertex=next(iter(geom.vertices()))
utils: QgsSnappingUtils=iface.mapCanvas().snappingUtils()
utils.setMapSettings(iface.mapCanvas().snappingUtils().mapSettings())
utils.setCurrentLayer(layer)
point=QgsPointXY(vertex.x(), vertex.y())
match=utils.snapToMap(point, relaxed=False)
ifnotmatch.isValid():
# Leave the layer in edit mode to make it easy to observe manuallyreturnFalselayer.rollBack()
returnTruedef_add_layers():
forlayer_nameinLAYER_NAMES:
layer=QgsVectorLayer(
f"{GPKG_PATH!s}|layername={layer_name}", layer_name, "ogr"
)
QgsProject.instance().addMapLayer(layer)
def_remove_layers():
QgsProject.instance().removeAllMapLayers()
def_process_events():
QCoreApplication.processEvents()
deftest_reproduce_snapping_bug():
set_snapping_mode_to_all_layers()
foriinrange(20):
print(f"Iteration {i}")
snapping_working=snapping_still_works()
ifnotsnapping_working:
return_process_events()
print("Could not reproduce the bug...")
The text was updated successfully, but these errors were encountered:
Joonalai
added
the
Bug
Either a bug report, or a bug fix. Let's hope for the latter!
label
Nov 20, 2024
Joonalai
changed the title
Snapping stops working when layers are removed and added to the project multiple times
Vertex tool and snapping stop working when layers are removed and added to the project multiple times
Nov 20, 2024
What is the bug or the crash?
Snapping and vertex tool stop working when layers are removed and added again back to the project. Sometimes this happens after the first time and sometimes after numerous times. Only way to recover from this is to restart QGIS.
Steps to reproduce the issue
It was incredibly difficult to reproduce the bug reliably but finally after hours of trying I managed to do that with a little bit of scripting. I prepared a snapping.zip that includes snapping.gpkg with two layers and python script snapping.py with this content:
test_reproduce_snapping_bug()
in python consoleI sincerely ask not to put a "Python Console" label in this issue since this is merely a way to reproduce the bug reliably.
Versions
3.13.0-CAPI-1.19.0 (Running)
And also with 3.41.0-Master
Supported QGIS version
New profile
Additional context
I tested with QGIS debugging and it seems that the bug lies in this line:
QGIS/src/core/qgspointlocator.cpp
Line 1338 in 458185a
QGIS/src/core/qgspointlocator.cpp
Line 112 in 458185a
The text was updated successfully, but these errors were encountered: