I'm working with code from the Disconnected Islands plugin in QGIS x64 and Python 3. Normally, the plugin uses networkx to create a geometric network and ends by writing the FID of features with no connections to a CSV and their corresponding network group ID.
I'm not familiar with network theory/geometric networks, so I'm not sure if this is possible: is there a way to return the fid of segments with only one connection? If so, which part of the code should I change?
# 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