1

I have a vector polygon and vector polyline layer. I want to enable the snapping for both the layer with tolerance value? How can i do that using python programming. I have tried this code but getting the following error:

snapper = QgsMapCanvasSnapper(canvas) 
snapper.setSnapSettingsForLayer(aLayer,True,SnapToVertexAndSegment,pixels,0.0001‌​,True) 

Traceback (most recent call last): File "", line 1, in AttributeError: 'QgsMapCanvasSnapper' object has no attribute 'setSnapSettingsForLayer'

Can anyone help me in setting the snapping options to the currentLayer using python programming?

2 Answers 2

2

You must define it first:

result3 = QgsVectorLayer("LineString", "ligne", "memory") 
#it's to create your layer

ligneid=result3.id()
# it allows you to have the idvalue for your layer

QgsProject.instance().setSnapSettingsForLayer(ligneid,True,2,1,1000,True)
# it defines the snapping options ligneid : the id of your layer, True : to enable the layer snapping, 2 : options (0: on vertex, 1 on segment, 2: vertex+segment), 1: pixel (0: type of unit on map), 1000 : tolerance, true : avoidIntersection)
0
0

I'm using QGIS 3.22.16. I created a new QgsSnappingConfig() and used setIndividualLayerSettings to add each Layer to my selection. Then I assign the config to the project instance.

snapping_config = QgsSnappingConfig() 
snapping_config.setEnabled(True)
snapping_config.setMode(QgsSnappingConfig.AdvancedConfiguration)
snapping_config.setIndividualLayerSettings(self.gebauedelayer, QgsSnappingConfig.IndividualLayerSettings(True, QgsSnappingConfig.VertexFlag, 10, QgsTolerance.Pixels))
snapping_config.setIndividualLayerSettings(self.flurlayer, QgsSnappingConfig.IndividualLayerSettings(True, QgsSnappingConfig.VertexFlag, 10, QgsTolerance.Pixels))
QgsProject.instance().setSnappingConfig(snapping_config)`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.