I have 2 traffic networks (A and B) with links relating to each other. Each link has an id. E.g. link 1 of network A can have a relation to 3 links of network B. Link 2 of network A might also have a relation to 1 of those 3 links of network B, and also to another one etc. I'm depicting these relations in GeoJSONs with features in the form of:

  "features": [{
  "type": "Feature",
  "properties": {
    "id": 12345,
    "name": "Street with a certain name",
    "relations": [
  "geometry": {
    "type": "LineString",
    "coordinates": [
      [-74.06, 40.60],
      [-74.06, 40.60],
      [-74.06, 40.61]

So in a different layer/GeoJSON of network B there will be linestrings with the IDs 123, 456, 789 that relate to the Linestring above of network A and vice versa.

Another possibility, instead of storing relations within the JSON, would be to have the relations in a separate (csv) table, with each row depicting a relation, like:


I would like to be able to visualize these relations, e.g. when selecting the link of network A above, I would like the 3 related links of network B to be 'highlighted' in some way, or at least be filtered automatically.

Is there a way to easily and efficiently achieve this in QGIS, maybe via a Python script (Networks could contain multiple thousand links)? This is supposed to be part of a larger Python plugin. My preferred way to achieve this would either be highlighting the relating links while at the same time keeping a view of both whole networks, either somehow automatically when this 'mode' is enabled, or at least by activating some script after selecting the link, like 'Visualize Relations'.

  • Maybe this answer gives you a hint. When a point is selected, the related lines are highlighted Dec 22 '20 at 14:34
  • Welcome to GIS SE! As a new user be sure to take the Tour to learn about our focussed Q&A format. We're a little different from other sites. For questions that involve code, we ask that you show us where you are stuck with your own code by including a code snippet in your question. Dec 22 '20 at 14:41
  • Thanks! flashFeatures seems to be very helpful.
    – nico
    Dec 22 '20 at 16:39

I assume you are loading these 2 layers to QGIS and creating a QGIS relation among them.

So, there are at least 2 plugins that help you select child features when you select a parent feature. These plugins are Actions for relations and Select by relationship. Follow the links to see how they work.

Besides these two plugins (none of them actually highlighted child features as I wanted on QGIS 3.16.1), you also have the following code snippet (adapted from the latter plugin I mentioned) to test parent-child visualization:

parent = iface.activeLayer()  # Get parent layer from the ToC 

def selectChildren(fids, foo, bar):
    relations = QgsProject.instance().relationManager().relationsByName('my_rel')
    rel = relations[0]  # Assuming we only have 1 relation named 'my_rel'
    referencingLayer = rel.referencingLayer()
    referencedLayer = rel.referencedLayer()

    request = QgsFeatureRequest().setFilterFids(fids)
    fit = referencedLayer.getFeatures(request)
    childIds = []
    for f in fit:
        it = rel.getRelatedFeatures(f)
        childIds.extend([i.id() for i in it])

    iface.mapCanvas().flashFeatureIds(referencingLayer, childIds)


The result looks like this:

enter image description here

Note that the code snippet relies on the explicit QGIS relation set between both layers, instead of discovering spatial relations on the fly.

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.