3

I'm using QGIS 3.14 Pi and here's my scenario. I have a layer of points and each points contain a numerical value code, let's say it's 123. There may be multiple points with the same code value. In another layer, I have polygons, each with a unique code. What I want to do is select (click on) a point and have the polygon in another layer with the matching code value select also. The points may or may not be inside the polygon.

Many years ago in MapInfo I had an MBX that would do this, so I imagine there's a QGIS plugin that would do the same, I just don't know what to call it.

2

I don't know there is a plugin for that, but you can use the following script. Change the layer names and run the script in QGIS Python Editor.

point_lyr = QgsProject.instance().mapLayersByName("POINT_LAYER_NAME")[0]
polygon_lyr = QgsProject.instance().mapLayersByName("POLYGON_LAYER_NAME")[0]

def select_related_polygon(selectedIds, deselected, clearAndSelect):
    if selectedIds:
        ID = selectedIds[0] # selected point id
        selected_point = point_lyr.getFeature(ID)
        
        expression = "code = " + str(selected_point["code"])
        polygon_lyr.selectByExpression(expression)

point_lyr.selectionChanged.connect(select_related_polygon)

The script works for just one selected point or the first point in selectedIds list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.