I've got a bug/problem in my PyQt QGIS based app.
CODE SNIPPET DOWN BELOW
A little context first:
The app enables users to load polygon data into different map layers which I display on the QGIS map. The map layers are pre-defined, and each layer has its own set of rules. For example, a polygon in my 'staging' layer has different rules than a polygon in my 'site' layer.
Once a layer is displayed on the map, the user can perform different actions on the polygons, one of the most common ones is modifying the polygon boundaries. I have set this up via a r-click interaction on the mapview, where a context menu appears, and the user chooses the option to modify a polygon.
The problem (in words):
Occasionally (like ~10-20% of the time), when a user tries to modify a polygon boundary, the vertices of the selected polygon don't 'light up' and offer the ability to move them. Upon further investigation of this issue, I found that when this issue occurs, polygons which have been clicked previously (in order to edit them), appear to be involved.
Take this example, say I successfully edit polygonA. Now I go to edit polygonB, when I trigger the modification, the verticies of polygonB do not light up (which would indicate that I'm able to modify polygonB) yet when I mouse hover over polygonA which was previously interacted with (but not selected for modification), the vertices of polygonB now light up - but only while the mouse is over polyA (the poly not selected for modification).
Here are some screenshots for reference (in each screenshot, redX = mouse location. Also, in the post_click picture, the black dashed styling indicates the tmp_layer (see code snippet below))
post_click -- Right Poly clicked, black dashed outline is 'tmp_layer'
post_click_desired_behavior -- when mouse is hovered over the poly we want to modify, it lights up indicating we can edit the vertices.
post_click_notdesired_behavior_0 -- despite clicking the Right poly, and then hovering over it with mouse, our vertices do not light up
post_click_notdesired_behavior_1 -- yet oddly, when we hover over a previously modified poly (Left Poly), the vertices of Right Poly light up
post_click_notdesired_behavior_2 -- similar as notdesired_behavior_1
Here is the code which gets triggered when a user initiates the polygon modification:
def modify_polygon(self, parent_feature: QgsFeature):
# create tmp layer and go into draw mode for that layer
tmp_layer = QgsVectorLayer("Polygon?crs=epsg:4326", "tmp_layer", "memory")
mySymbol = QgsFillSymbol.createSimple({
'color': hex_to_rgba("#FF0000", 50), # red with alpha for transparency
'outline_color': hex_to_rgba("#000000"), # black
'outline_width': '0.65',
'style': 'solid',
'outline_style': 'dash'
})
tmp_layer.renderer().setSymbol(mySymbol)
tmp_layer.startEditing()
tmp_feature = QgsFeature()
tmp_feature.setGeometry(parent_feature.geometry())
tmp_layer.dataProvider().addFeatures([tmp_feature])
tmp_layer.commitChanges()
tmp_layer.updateExtents()
# add temp layer to map, on top
root = QgsProject.instance().layerTreeRoot()
root.insertLayer(0, tmp_layer)
self.reorganize_layers_by_priority()
# add layer to the map, on top- then go into edit mode
iface.layerTreeView().setCurrentLayer(tmp_layer)
iface.actionVertexTool().trigger()
tmp_layer.startEditing()
# activate draw tool
self.edit_dialog = DrawDialog_EditPolygon(self,
tmp_layer,
self.done_with_polygon_edit,
self.check_polygon_edit,
self.edit_site_cancel,
parent_feature=parent_feature
)
self.edit_dialog.show()
There is much more code at play, but this is at least where I think the problem might be stemming from...
I have tried many different debugging approaches to try and verify that the correct feature data is going into tmp_layer, and I don't see any sign that tmp_layer is being populated with data from the wrong polygon. When this issue does occur, the actionVertexTool is not highlighting the correct feature (and there is only 1 feature going into tmp_layer, never 2).