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:
- net
spaghetti.Network
A spaghetti network object.
- verticesbool
Extract the network vertices (
True
). Default isFalse
.- arcsbool
Extract the network arcs (
True
). Default isFalse
.- pp_name
str
Name of the
network.PointPattern
to extract. Default isNone
.- snappedbool
If extracting a
network.PointPattern
, set toTrue
for snapped point locations along the network. Default isFalse
.- routes
dict
See
paths
fromspaghetti.Network.shortest_paths
. Default isNone
.- id_col
str
geopandas.GeoDataFrame
column name for IDs. Default is"id"
. When extracting routes this creates an (origin, destination) tuple.- geom_col
str
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.
- net
- Returns:
- points
geopandas.GeoDataFrame
Network point elements (either vertices or
network.PointPattern
points) as ageopandas.GeoDataFrame
ofshapely.geometry.Point
objects with an"id"
column and"geometry""
column. If the network object has anetwork_component_vertices
attribute, then component labels are also added in a column.- lines
geopandas.GeoDataFrame
Network arc elements as a
geopandas.GeoDataFrame
ofshapely.geometry.LineString
objects with an"id"
column and"geometry"
column. If the network object has anetwork_component_labels
attribute, then component labels are also added in a column.- paths
geopandas.GeoDataFrame
Shortest path routes along network arc elements as a
geopandas.GeoDataFrame
ofshapely.geometry.LineString
objects with an"id"
(seespaghetti.Network.shortest_paths()
) column and"geometry"
column.
- points
- Raises:
KeyError
In order to extract a
network.PointPattern
it must already be a part of the network object. This exception is raised when anetwork.PointPattern
is being extracted that does not exist within the network object.
See also
Notes
When both network vertices and arcs are desired, the variable declaration must be in the order: <vertices>, <arcs>.
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() np.float64(104414.09200823458)