(
    gdf_network,
    approach="primal",
    length="mm_len",
    multigraph=True,
    directed=False,
    angles=True,
    angle="angle",
)
return net
Convert LineString GeoDataFrame to networkx.MultiGraph or other Graph as per
    specification.

    Preserves columns as edge or node attributes (depending on the ``approach``).
    Index is not preserved.

    See the User Guide page :doc:`../../user_guide/graph/convert` for details.

    Parameters
    ----------
    gdf_network : GeoDataFrame
        GeoDataFrame containing objects to convert
    approach : str, default 'primal'
        Allowed options are ``'primal'`` or ``'dual'``. Primal graph represents
        endpoints as nodes and LineStrings as edges, dual graph represents
        LineStrings as nodes and their topological relation as edges. In such a
        case, it can encode an angle between LineStrings as an edge attribute.
    length : str, default 'mm_len'
        name of attribute of segment length (geographical) which will be saved to graph
    multigraph : bool, default True
        create ``MultiGraph`` of ``Graph`` (potentially directed). ``MutliGraph``
        allows multiple
        edges between any pair of nodes, which is a common case in street networks.
    directed : bool, default False
        create directed graph (``DiGraph`` or ``MultiDiGraph``). Directionality follows
        the order of LineString coordinates.
    angles : bool, default True
        capture angles between LineStrings as an attribute of a dual graph. Ignored if
        ``approach="primal"``.
    angle : str, default 'angle'
        name of attribute of angle between LineStrings which will be saved to graph.
        Ignored if ``approach="primal"``.

    Returns
    -------
    networkx.Graph,
    networkx.MultiGraph,
    networkx.DiGraph,
    networkx.MultiDiGraph
        Graph as per specification

    See also
    --------
    nx_to_gdf

    Examples
    --------
    >>> import geopandas as gpd
    >>> df = gpd.read_file(momepy.datasets.get_path('bubenec'), layer='streets')
    >>> df.head(5)
                                                geometry
    0  LINESTRING (1603585.640 6464428.774, 1603413.2...
    1  LINESTRING (1603268.502 6464060.781, 1603296.8...
    2  LINESTRING (1603607.303 6464181.853, 1603592.8...
    3  LINESTRING (1603678.970 6464477.215, 1603675.6...
    4  LINESTRING (1603537.194 6464558.112, 1603557.6...

    Primal graph:

    >>> G = momepy.gdf_to_nx(df)
    >>> G
    <networkx.classes.multigraph.MultiGraph object at 0x7f8cf90fad50>

    >>> G_directed = momepy.gdf_to_nx(df, directed=True)
    >>> G_directed
    <networkx.classes.multidigraph.MultiDiGraph object at 0x7f8cf90f56d0>

    >>> G_digraph = momepy.gdf_to_nx(df, multigraph=False, directed=True)
    >>> G_digraph
    <networkx.classes.digraph.DiGraph object at 0x7f8cf9150c10>

    >>> G_graph = momepy.gdf_to_nx(df, multigraph=False, directed=False)
    >>> G_graph
    <networkx.classes.graph.Graph object at 0x7f8cf90facd0>

    Dual graph:

    >>> G_dual = momepy.gdf_to_nx(df, approach="dual")
    >>> G_dual
    <networkx.classes.multigraph.MultiGraph object at 0x7f8cf9150fd0>
Convert LineString GeoDataFrame to networkx.MultiGraph or other Graph as per
    specification.
Convert LineString GeoDataFrame to networkx . MultiGraph or other Graph as per specification .
def gdf_to_nx(
    gdf_network,
    approach="primal",
    length="mm_len",
    multigraph=True,
    directed=False,
    angles=True,
    angle="angle",
):
    """
    Convert LineString GeoDataFrame to networkx.MultiGraph or other Graph as per
    specification.

    Preserves columns as edge or node attributes (depending on the ``approach``).
    Index is not preserved.

    See the User Guide page :doc:`../../user_guide/graph/convert` for details.

    Parameters
    ----------
    gdf_network : GeoDataFrame
        GeoDataFrame containing objects to convert
    approach : str, default 'primal'
        Allowed options are ``'primal'`` or ``'dual'``. Primal graph represents
        endpoints as nodes and LineStrings as edges, dual graph represents
        LineStrings as nodes and their topological relation as edges. In such a
        case, it can encode an angle between LineStrings as an edge attribute.
    length : str, default 'mm_len'
        name of attribute of segment length (geographical) which will be saved to graph
    multigraph : bool, default True
        create ``MultiGraph`` of ``Graph`` (potentially directed). ``MutliGraph``
        allows multiple
        edges between any pair of nodes, which is a common case in street networks.
    directed : bool, default False
        create directed graph (``DiGraph`` or ``MultiDiGraph``). Directionality follows
        the order of LineString coordinates.
    angles : bool, default True
        capture angles between LineStrings as an attribute of a dual graph. Ignored if
        ``approach="primal"``.
    angle : str, default 'angle'
        name of attribute of angle between LineStrings which will be saved to graph.
        Ignored if ``approach="primal"``.

    Returns
    -------
    networkx.Graph,
    networkx.MultiGraph,
    networkx.DiGraph,
    networkx.MultiDiGraph
        Graph as per specification

    See also
    --------
    nx_to_gdf

    Examples
    --------
    >>> import geopandas as gpd
    >>> df = gpd.read_file(momepy.datasets.get_path('bubenec'), layer='streets')
    >>> df.head(5)
                                                geometry
    0  LINESTRING (1603585.640 6464428.774, 1603413.2...
    1  LINESTRING (1603268.502 6464060.781, 1603296.8...
    2  LINESTRING (1603607.303 6464181.853, 1603592.8...
    3  LINESTRING (1603678.970 6464477.215, 1603675.6...
    4  LINESTRING (1603537.194 6464558.112, 1603557.6...

    Primal graph:

    >>> G = momepy.gdf_to_nx(df)
    >>> G
    <networkx.classes.multigraph.MultiGraph object at 0x7f8cf90fad50>

    >>> G_directed = momepy.gdf_to_nx(df, directed=True)
    >>> G_directed
    <networkx.classes.multidigraph.MultiDiGraph object at 0x7f8cf90f56d0>

    >>> G_digraph = momepy.gdf_to_nx(df, multigraph=False, directed=True)
    >>> G_digraph
    <networkx.classes.digraph.DiGraph object at 0x7f8cf9150c10>

    >>> G_graph = momepy.gdf_to_nx(df, multigraph=False, directed=False)
    >>> G_graph
    <networkx.classes.graph.Graph object at 0x7f8cf90facd0>

    Dual graph:

    >>> G_dual = momepy.gdf_to_nx(df, approach="dual")
    >>> G_dual
    <networkx.classes.multigraph.MultiGraph object at 0x7f8cf9150fd0>


    """
    gdf_network = gdf_network.copy()
    if "key" in gdf_network.columns:
        gdf_network.rename(columns={"key": "__key"}, inplace=True)

    if multigraph and directed:
        net = nx.MultiDiGraph()
    elif multigraph and not directed:
        net = nx.MultiGraph()
    elif not multigraph and directed:
        net = nx.DiGraph()
    else:
        net = nx.Graph()

    net.graph["crs"] = gdf_network.crs
    gdf_network[length] = gdf_network.geometry.length
    fields = list(gdf_network.columns)

    if approach == "primal":
        _generate_primal(net, gdf_network, fields, multigraph)

    elif approach == "dual":
        if directed:
            raise ValueError("Directed graphs are not supported in dual approach.")

        _generate_dual(
            net, gdf_network, fields, angles=angles, multigraph=multigraph, angle=angle
        )

    else:
        raise ValueError(
            f"Approach {approach} is not supported. Use 'primal' or 'dual'."
        )

    return net
def gdf_to_nx ( gdf_network , approach = "primal" , length = "mm_len" , multigraph = True , directed = False , angles = True , angle = "angle" , ) : gdf_network = gdf_network . copy ( ) if "key" in gdf_network . columns : gdf_network . rename ( columns = { "key" : "__key" } , inplace = True ) if multigraph and directed : net = nx . MultiDiGraph ( ) elif multigraph and not directed : net = nx . MultiGraph ( ) elif not multigraph and directed : net = nx . DiGraph ( ) else : net = nx . Graph ( ) net . graph [ "crs" ] = gdf_network . crs gdf_network [ length ] = gdf_network . geometry . length fields = list ( gdf_network . columns ) if approach == "primal" : _generate_primal ( net , gdf_network , fields , multigraph ) elif approach == "dual" : if directed : raise ValueError ( "Directed graphs are not supported in dual approach." ) _generate_dual ( net , gdf_network , fields , angles = angles , multigraph = multigraph , angle = angle ) else : raise ValueError ( f"Approach {approach} is not supported. Use 'primal' or 'dual'." ) return net