I'm working with code from the Disconnected Islands plugin in QGIS x64 and Python. Normally, the plugin ends by writing the FID of features to a CSV and their corresponding network group ID.

Instead, I would like to be able to find all the network groups with only one feature (= lines with no connections). I'm not sure how to proceed.

Preferably, I would like to be able to do what ArcGIS's Find Disconnected Features in Geometric Network tool does, but I need to use QGIS.

# get the network
layer = iface.activeLayer()
G = nx.Graph()

# construct graph
for feat in layer.getFeatures():
    line = feat.geometry().asPolyline()
    for i in range(len(line)-1):
        G.add_edges_from([((line[i][0], line[i][1]), (line[i+1][0], line[i+1][1]), 
                          {'fid': feat.id()})])

# evaluate on connected components
connected_components = list(nx.connected_component_subgraphs(G))

# gather edges and components to which they belong
fid_comp = {}
for i, graph in enumerate(connected_components):
   for edge in graph.edges_iter(data=True):
       fid_comp[edge[2].get('fid', None)] = i

1 Answer 1


I have not tested the plugin code but, assuming you end up with a dictionary containing feature id as key and component id as value, something like:

fid_comp = {1: 0, 2: 0, 3: 1, 4: 1, 5: 1, 6: 2, 7: 3, 8: 3, 9: 4, 10: 5}

The problem becomes one of simply extracting the feature ids that have a unique value in the dictionary (i.e. they don't share their component id value with any other feature).

Try something like this:

countMap = {}
for v in fid_comp.values():
    countMap[v] = countMap.get(v,0) + 1
isolated = [k for k, v in fid_comp.items() if countMap[v] == 1]

This should select all your disconnected features, which you could then save as a new layer if you wish.

Your Answer

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

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