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