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

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 Converting from GeoDataFrame to Graph and back for details.


GeoDataFrame containing objects to convert

approachstr, 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.

lengthstr, default ‘mm_len’

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

multigraphbool, 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.

directedbool, default False

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

anglesbool, default True

capture angles between LineStrings as an attribute of a dual graph. Ignored if approach="primal".

anglestr, default ‘angle’

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


Graph as per specification

See also



>>> import geopandas as gpd
>>> df = gpd.read_file(momepy.datasets.get_path('bubenec'), layer='streets')
>>> df.head(5)
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>