ai4er-cdt / geograph

GeoGraph provides a tool for analysing habitat fragmentation and related problems in landscape ecology. GeoGraph builds a geospatially referenced graph from land cover or field survey data and enables graph-based landscape ecology analysis as well as interactive visualizations.
https://geograph.readthedocs.io
MIT License
39 stars 10 forks source link

Make graph components accessible #27

Closed rdnfn closed 3 years ago

rdnfn commented 3 years ago

Description: for the UI it would be great to have the following method/function, either as part of GeoGraph or as a standalone function:

def get_graph_components(graph: nx.Graph()) -> gpd.GeoDataFrame():
    """Return a GeoDataFrame with graph components.

    This method takes an nx.Graph and determines the individual disconnected graph
    components that make up the graph. Each row of the returned GeoDataFrame 
    corresponds to a graph component, with entries in column 'geometry' being the union
    of all individual polygons making up a component.

    This method allows for the UI to visualise components and output their number as 
    a metric.

    More info on the definition of graph components can be found here:
    https://en.wikipedia.org/wiki/Component_(graph_theory)

    Args:
        graph (nx.Graph): nx.Graph of a GeoGraph
    """

Using the returned GeoDataFrame, the UI can then color code the components and output their number as a metric.

rdnfn commented 3 years ago

@herbiebradley: do you know if NetworkX already has some component functionality built-in?

herbiebradley commented 3 years ago

This should be fairly easy to do using https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.connected_components.html

That will return a list of node indices which make up the components, then dataframes have plenty of ways of selecting only a limited number of rows, so it should be easy to just slice the main graph dataframe and return only the relevant rows.

rdnfn commented 3 years ago

Brilliant, that looks very promising!

herbiebradley commented 3 years ago

Closed by #31