snkit package¶
snkit - a spatial networks toolkit
-
class
snkit.
Network
(nodes=None, edges=None)[source]¶ Bases:
object
A Network is composed of nodes (points in space) and edges (lines)
nodes : geopandas.geodataframe.GeoDataFrame, optional edges : geopandas.geodataframe.GeoDataFrame, optional
nodes : geopandas.geodataframe.GeoDataFrame edges : geopandas.geodataframe.GeoDataFrame
-
set_crs
(crs=None, epsg=None, allow_override=False)[source]¶ Set the coordinate reference system (CRS) of the network nodes and edges.
- crs : pyproj.CRS, optional if epsg is specified
- The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string.
- epsg : int, optional if crs is specified
- EPSG code specifying output projection.
- allow_override : bool, default False
- If the nodes or edges GeoDataFrame already has a CRS, allow to replace the existing CRS, even when both are not equal.
-
to_crs
(crs=None, epsg=None)[source]¶ Transform network nodes and edges geometries to a new coordinate reference system (CRS).
- crs : pyproj.CRS, optional if epsg is specified
- The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string.
- epsg : int, optional if crs is specified
- EPSG code specifying output projection.
-
to_file
(filename, nodes_layer='nodes', edges_layer='edges', **kwargs)[source]¶ Write nodes and edges to a geographic data file with layers.
Any additional keyword arguments are passed through to geopandas.GeoDataFrame.to_file.
- filename : str
- Path to geographic data file with layers
- nodes_layer : str, optional, default ‘nodes’
- Layer name for nodes.
- edges_layer : str, optional, default ‘edges’
- Layer name for edges.
-
Submodules¶
snkit.network module¶
Network representation and utilities
-
class
snkit.network.
Network
(nodes=None, edges=None)[source]¶ Bases:
object
A Network is composed of nodes (points in space) and edges (lines)
nodes : geopandas.geodataframe.GeoDataFrame, optional edges : geopandas.geodataframe.GeoDataFrame, optional
nodes : geopandas.geodataframe.GeoDataFrame edges : geopandas.geodataframe.GeoDataFrame
-
set_crs
(crs=None, epsg=None, allow_override=False)[source]¶ Set the coordinate reference system (CRS) of the network nodes and edges.
- crs : pyproj.CRS, optional if epsg is specified
- The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string.
- epsg : int, optional if crs is specified
- EPSG code specifying output projection.
- allow_override : bool, default False
- If the nodes or edges GeoDataFrame already has a CRS, allow to replace the existing CRS, even when both are not equal.
-
to_crs
(crs=None, epsg=None)[source]¶ Transform network nodes and edges geometries to a new coordinate reference system (CRS).
- crs : pyproj.CRS, optional if epsg is specified
- The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string.
- epsg : int, optional if crs is specified
- EPSG code specifying output projection.
-
to_file
(filename, nodes_layer='nodes', edges_layer='edges', **kwargs)[source]¶ Write nodes and edges to a geographic data file with layers.
Any additional keyword arguments are passed through to geopandas.GeoDataFrame.to_file.
- filename : str
- Path to geographic data file with layers
- nodes_layer : str, optional, default ‘nodes’
- Layer name for nodes.
- edges_layer : str, optional, default ‘edges’
- Layer name for edges.
-
-
snkit.network.
add_component_ids
(network: snkit.network.Network, id_col: str = 'component_id') → snkit.network.Network[source]¶ Add column of connected component IDs to network edges and nodes
-
snkit.network.
add_ids
(network, id_col='id', edge_prefix='edge', node_prefix='node')[source]¶ Add or replace an id column with ascending ids
-
snkit.network.
concat_dedup
(dfs)[source]¶ Concatenate a list of GeoDataFrames, dropping duplicate geometries - note: repeatedly drops indexes for deduplication to work
-
snkit.network.
d_within
(geom, gdf, distance)[source]¶ Find the subset of a GeoDataFrame within some distance of a shapely geometry
-
snkit.network.
drop_duplicate_geometries
(gdf, keep='first')[source]¶ Drop duplicate geometries from a dataframe
-
snkit.network.
edges_intersecting
(line, edges, tolerance=1e-09)[source]¶ Find edges intersecting line
-
snkit.network.
edges_intersecting_points
(line, edges, tolerance=1e-09)[source]¶ Return intersection points of intersecting edges
-
snkit.network.
get_connected_components
(network)[source]¶ Get connected components within network and id to each individual graph
-
snkit.network.
intersection_endpoints
(geom, output=None)[source]¶ Return the points from an intersection geometry
It extracts the starting and ending points of intersection geometries recursively and appends them to output. This doesn’t handle polygons or collections of polygons.
-
snkit.network.
intersects
(geom, gdf, tolerance=1e-09)[source]¶ Find the subset of a GeoDataFrame intersecting with a shapely geometry
-
snkit.network.
link_nodes_to_edges_within
(network, distance, condition=None, tolerance=1e-09)[source]¶ Link nodes to all edges within some distance
-
snkit.network.
link_nodes_to_nearest_edge
(network, condition=None)[source]¶ Link nodes to all edges within some distance
-
snkit.network.
matching_gdf_from_geoms
(gdf, geoms)[source]¶ Create a geometry-only GeoDataFrame with column name to match an existing GeoDataFrame
-
snkit.network.
merge_edges
(network, id_col='id', by=None)[source]¶ Merge edges that share a node with a connectivity degree of 2
network : snkit.network.Network id_col : string by : List[string], optional
list of columns to use when merging an edge path - will not merge if edges have different values.
-
snkit.network.
nearest
(geom, gdf)[source]¶ Find the element of a GeoDataFrame nearest a shapely geometry
-
snkit.network.
nearest_vertex_idx_on_line
(point, line)[source]¶ Return the index of nearest vertex to a point on a line
-
snkit.network.
nodes_intersecting
(line, nodes, tolerance=1e-09)[source]¶ Find nodes intersecting line
-
snkit.network.
read_file
(filename, nodes_layer='nodes', edges_layer='edges', **kwargs)[source]¶ Read a geographic data file with layers containing nodes and edges.
Any additional keyword arguments are passed through to geopandas.read_file.
- filename : str
- Path to geographic data file with layers
- nodes_layer : str, optional, default ‘nodes’
- Layer name for nodes, or None if nodes should not be read.
- edges_layer : str, optional, default ‘edges’
- Layer name for edges, or None if edges should not be read.
-
snkit.network.
round_geometries
(network, precision=3)[source]¶ Round coordinates of all node points and vertices of edge linestrings to some precision
-
snkit.network.
snap_line
(line, points, tolerance=1e-09)[source]¶ Snap a line to points within tolerance, inserting vertices as necessary
-
snkit.network.
split_edge_at_points
(edge, points, tolerance=1e-09)[source]¶ Split edge at point/multipoint
-
snkit.network.
split_edges_at_intersections
(network, tolerance=1e-09)[source]¶ Split network edges where they intersect line geometries
-
snkit.network.
split_edges_at_nodes
(network: snkit.network.Network, tolerance: float = 1e-09, chunk_size: Optional[int, None] = None)[source]¶ Split network edges where they intersect node geometries.
N.B. Can operate in parallel if SNKIT_PROCESSES is in the environment and a positive integer.
- Args:
network: Network object to split edges for. tolerance: Proximity within which nodes are said to intersect an edge. chunk_size: When splitting in parallel, set the number of edges per
unit of work.- Returns:
- Network with edges split at nodes (within proximity tolerance).
-
snkit.network.
split_line
(line, points, tolerance=1e-09)[source]¶ Split line at point or multipoint, within some tolerance