momepy.gdf_to_nx#

momepy.gdf_to_nx(gdf_network, approach='primal', length='mm_len', multigraph=True, directed=False, angles=True, angle='angle', oneway_column=None)[source]#

Convert a LineString GeoDataFrame to a networkx.MultiGraph or other Graph as per specification. Columns are preserved as edge or node attributes (depending on the approach). Index is not preserved.

See the User Guide page Converting from GeoDataFrame to Graph and back for details.

Parameters
gdf_networkGeoDataFrame

A GeoDataFrame containing objects to convert.

approachstr, default ‘primal’

Allowed options are 'primal' or 'dual'. Primal graphs represent endpoints as nodes and LineStrings as edges. Dual graphs represent LineStrings as nodes and their topological relation as edges. In such a case, it can encode an angle between LineStrings as an edge attribute.

lengthstr, default ‘mm_len’

The attribute name of segment length (geographical) which will be saved to the graph.

multigraphbool, default True

Create a MultiGraph of Graph (potentially directed). MutliGraph allows multiple edges between any pair of nodes, which is a common case in street networks.

directedbool, default False

Create a directed graph (DiGraph or MultiDiGraph). Directionality follows the order of LineString coordinates.

anglesbool, default True

Capture the angles between LineStrings as an attribute of a dual graph. Ignored if approach='primal'.

anglestr, default ‘angle’

The attribute name of the angle between LineStrings which will be saved to the graph. Ignored if approach='primal'.

oneway_columnstr, default None

Create an additional edge for each LineString which allows bidirectional path traversal by specifying the boolean column in the GeoDataFrame. Note, that the reverse conversion nx_to_gdf(gdf_to_nx(gdf, directed=True, oneway_column="oneway")) will contain additional duplicated geometries.

Returns
netnetworkx.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>