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.GeoDataFrameof 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.PointPatternto extract. Default isNone.- snappedbool
If extracting a
network.PointPattern, set toTruefor snapped point locations along the network. Default isFalse.- routes
dict See
pathsfromspaghetti.Network.shortest_paths. Default isNone.- id_col
str geopandas.GeoDataFramecolumn 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.GeoDataFramecolumn 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.PointPatternpoints) as ageopandas.GeoDataFrameofshapely.geometry.Pointobjects with an"id"column and"geometry""column. If the network object has anetwork_component_verticesattribute, then component labels are also added in a column.- lines
geopandas.GeoDataFrame Network arc elements as a
geopandas.GeoDataFrameofshapely.geometry.LineStringobjects with an"id"column and"geometry"column. If the network object has anetwork_component_labelsattribute, then component labels are also added in a column.- paths
geopandas.GeoDataFrame Shortest path routes along network arc elements as a
geopandas.GeoDataFrameofshapely.geometry.LineStringobjects with an"id"(seespaghetti.Network.shortest_paths()) column and"geometry"column.
- points
- Raises:
KeyErrorIn order to extract a
network.PointPatternit must already be a part of the network object. This exception is raised when anetwork.PointPatternis 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.GeoDataFrameobjects.>>> 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)