spaghetti.element_as_gdf

spaghetti.element_as_gdf(net, vertices=False, arcs=False, pp_name=None, snapped=False, routes=None, id_col='id', geom_col=None)[source]

Return a geopandas.GeoDataFrame of network elements. This can be (a) the vertices of a network; (b) the arcs of a network; (c) both the vertices and arcs of the network; (d) the raw point pattern associated with the network; (e) the snapped point pattern of (d); or (f) the shortest path routes between point observations.

Parameters:
netspaghetti.Network

A spaghetti network object.

verticesbool

Extract the network vertices (True). Default is False.

arcsbool

Extract the network arcs (True). Default is False.

pp_namestr

Name of the network.PointPattern to extract. Default is None.

snappedbool

If extracting a network.PointPattern, set to True for snapped point locations along the network. Default is False.

routesdict

See paths from spaghetti.Network.shortest_paths. Default is None.

id_colstr

geopandas.GeoDataFrame column name for IDs. Default is "id". When extracting routes this creates an (origin, destination) tuple.

geom_colstr

Deprecated and will be removed in the minor release. geopandas.GeoDataFrame column name for IDs. Default is "id". When extracting routes this creates an (origin, destination) tuple.

Returns:
pointsgeopandas.GeoDataFrame

Network point elements (either vertices or network.PointPattern points) as a geopandas.GeoDataFrame of shapely.geometry.Point objects with an "id" column and "geometry"" column. If the network object has a network_component_vertices attribute, then component labels are also added in a column.

linesgeopandas.GeoDataFrame

Network arc elements as a geopandas.GeoDataFrame of shapely.geometry.LineString objects with an "id" column and "geometry" column. If the network object has a network_component_labels attribute, then component labels are also added in a column.

pathsgeopandas.GeoDataFrame

Shortest path routes along network arc elements as a geopandas.GeoDataFrame of shapely.geometry.LineString objects with an "id" (see spaghetti.Network.shortest_paths()) column and "geometry" column.

Raises:
KeyError

In order to extract a network.PointPattern it must already be a part of the network object. This exception is raised when a network.PointPattern is being extracted that does not exist within the network object.

Notes

When both network vertices and arcs are desired, the variable declaration must be in the order: <vertices>, <arcs>. This function requires geopandas.

Examples

Instantiate a network object.

>>> import spaghetti
>>> from libpysal import examples
>>> ntw = spaghetti.Network(examples.get_path("streets.shp"))

Extract the network elements (vertices and arcs) as geopandas.GeoDataFrame objects.

>>> vertices_df, arcs_df = spaghetti.element_as_gdf(
...     ntw, vertices=True, arcs=True
... )

Examine the first vertex. It is a member of the component labeled 0.

>>> vertices_df.loc[0]
id                                            0
geometry      POINT (728368.04762 877125.89535)
comp_label                                    0
Name: 0, dtype: object

Calculate the total length of the network.

>>> arcs_df.geometry.length.sum()
104414.09200823458